ISSDK  1.8
IoT Sensing Software Development Kit
isl29023_poll.c
Go to the documentation of this file.
1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file isl29023_poll.c
10  * @brief The isl29023_poll.c file implements the ISSDK ISL29023 sensor driver
11  * example demonstration in poll mode.
12  */
13 
14 /* SDK Includes */
15 #include "pin_mux.h"
16 #include "clock_config.h"
17 #include "board.h"
18 #include "fsl_debug_console.h"
19 #include "fsl_irqsteer.h"
20 
21 /* CMSIS Includes */
22 #include "Driver_I2C.h"
23 
24 /* ISSDK Includes */
25 #include "issdk_hal.h"
26 #include "isl29023_drv.h"
27 
28 /*******************************************************************************
29  * Macro Definitions
30  ******************************************************************************/
31 /*! @def ISL29023_DATA_SIZE
32  * @brief The size of the ISL29023 sensor data.
33  */
34 #define ISL29023_DATA_SIZE (2) /* 2 bytes of information */
35 
36 /*! @def ISL29023_INT_HT_DATA & ISL29023_INT_LT_DATA
37  * @brief ISL29023 Sensor High & Low treshold values. When the current light value
38  * is higher than HI-IRQ treshold value, or lower than LO-IRQ treshold value,
39  * the interrupt is generated. This means the new value is ready to sending out.
40  * The natural daylight value is between 20000-28000.
41  * @see For more info: https://www.intersil.com/content/dam/intersil/documents/isl2/isl29023.pdf
42  */
43 #define ISL29023_INT_HT_DATA (11000) /* HI-IRQ treshold value (relative to 16-bit res.)*/
44 #define ISL29023_INT_LT_DATA (5000) /* LO-IRQ treshold value (relative to 16-bit res.)*/
45 
46 /*******************************************************************************
47  * Constants
48  ******************************************************************************/
49 /*! Prepare the register write list to configure ISL29023 in 16-bits ALS continuous mode. */
51  /*! Configure CMD-I register to 1th integration cycles, and ALS cont. mode. */
54  /*! Configure CMD-II register to Range #1, and 16-bits ADC resolution. */
57  /*! Configure Low interrupt treshold registers. */
58  {ISL29023_INT_LT_LSB, (ISL29023_INT_LT_DATA & 0x00FF), 0xFF},
59  {ISL29023_INT_LT_MSB, ((ISL29023_INT_LT_DATA & 0xFF00)>>8), 0xFF},
60  /*! Configure High interrupt treshold registers. */
61  {ISL29023_INT_HT_LSB, (ISL29023_INT_HT_DATA & 0x00FF), 0xFF},
62  {ISL29023_INT_HT_MSB, ((ISL29023_INT_HT_DATA & 0xFF00)>>8), 0xFF},
64 
65 /*! Prepare the register read list to read ISL29023 IRQ flag bit. */
67 
68 /*! Prepare the register read list to read the raw ADC data from the ISL29023. */
71 
72 /*******************************************************************************
73  * Code
74  ******************************************************************************/
75 /*!
76  * @brief Main function
77  */
78 int main(void)
79 {
81  uint8_t irqStatus;
82  uint8_t data[ISL29023_DATA_SIZE];
84  sc_ipc_t ipc;
85  uint32_t freq;
86 
87  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER;
88  isl29023_i2c_sensorhandle_t ISL29023drv;
89 
90  /*! Initialize the MCU hardware. */
91  ipc = BOARD_InitRpc();
92  BOARD_InitMemory();
95 
97 
98  PRINTF("\r\n ------------------------------------------------------------------\r\n");
99  PRINTF("\r\n ISSDK ISL29023 sensor driver example demonstration with POLL mode\r\n");
100  PRINTF("\r\n ------------------------------------------------------------------\r\n");
101 
102  /* Power on LPI2C. */
103  if (sc_pm_set_resource_power_mode(ipc, SC_R_I2C_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
104  {
105  PRINTF("Error: Failed to power on LPI2C\r\n");
106  }
107 
108  /* Set LPI2C clock */
109  freq = CLOCK_SetIpFreq(kCLOCK_DMA_Lpi2c0, SC_24MHZ);
110  if (freq == 0)
111  {
112  PRINTF("Error: Failed to set LPI2C frequency\r\n");
113  }
114 
115  /* Power on IRQSTEER. */
116 #if (MIMX8QM_CM4_CORE0)
117  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_0, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
118  {
119  PRINTF("Error: Failed to power on IRQSTEER M4_0\r\n");
120  }
121 #elif (MIMX8QM_CM4_CORE1)
122  if (sc_pm_set_resource_power_mode(ipc, SC_R_IRQSTR_M4_1, SC_PM_PW_MODE_ON) != SC_ERR_NONE)
123  {
124  PRINTF("Error: Failed to power on IRQSTEER M4_1\r\n");
125  }
126 #endif
127 
128  /* Enable interrupt in irqsteer */
129  IRQSTEER_Init(IRQSTEER);
130  IRQSTEER_EnableInterrupt(IRQSTEER, DMA_I2C0_INT_IRQn);
131 
132  /*! Initialize the I2C driver. */
133  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
134  if (ARM_DRIVER_OK != status)
135  {
136  PRINTF("\r\n I2C Initialization Failed\r\n");
137  return -1;
138  }
139 
140  /*! Set the I2C Power mode. */
141  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
142  if (ARM_DRIVER_OK != status)
143  {
144  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
145  return -1;
146  }
147 
148  /*! Set the I2C bus speed. */
149  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
150  if (ARM_DRIVER_OK != status)
151  {
152  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
153  return -1;
154  }
155 
156  /*! Initialize the ISL29023 sensor driver. */
159  if (SENSOR_ERROR_NONE != status)
160  {
161  PRINTF("\r\n Sensor Initialization Failed\r\n");
162  return -1;
163  }
164  PRINTF("\r\n Successfully Initiliazed ISL29023 Sensor\r\n");
165  PRINTF("\r\n Device Address is 0x%x \r\n", ISL29023_I2C_ADDRESS);
166 
167  /*! Configure the ISL29023 sensor driver. */
168  status = ISL29023_I2C_Configure(&ISL29023drv, ISL29023_Config_Poll);
169  if (SENSOR_ERROR_NONE != status)
170  {
171  PRINTF("\r\n ISL29023 Sensor Configuration Failed, Err = %d\r\n", status);
172  return -1;
173  }
174  PRINTF("\r\n Successfully Applied ISL29023 Sensor Configuration\r\n");
175 
176  PRINTF("\r\n Reading ISL29023 ADC data\r\n");
177 
178  for (;;) /* Forever loop */
179  {
180  /*! Wait for clear IRQ flag bit. */
181  status = ISL29023_I2C_ReadData(&ISL29023drv, ISL29023_Status, &irqStatus);
182  if (0 == (irqStatus & ISL29023_CMD_I_FLAG_MASK))
183  {
184  continue;
185  }
186 
187  /*! Read the raw sensor data from the ISL29023. */
188  status = ISL29023_I2C_ReadData(&ISL29023drv, ISL29023_Output_Values, data);
189  if (ARM_DRIVER_OK != status)
190  {
191  PRINTF("\r\n Read Failed. \r\n");
192  return -1;
193  }
194 
195  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
196  rawData.light = ((uint16_t)data[1] << 8) | data[0];
197 
198  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
199  PRINTF("Light value: %d\r\n", rawData.light);
200  ASK_USER_TO_RESUME(20); /* Ask for user input after processing 20 samples. */
201  }
202 }
#define ISL29023_CMD_I_OP_MASK
Definition: isl29023.h:58
The isl29023_drv.h file describes the ISL29023 driver interface and structures.
#define ISL29023_CMD_I_FLAG_MASK
Definition: isl29023.h:55
This structure defines the isl29023 raw data buffer.
Definition: isl29023_drv.h:37
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define ISL29023_CMD_II_RANGE_1
Definition: isl29023.h:113
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
#define ISL29023_CMD_I_OP_ALS_CONT
Definition: isl29023.h:74
int32_t ISL29023_I2C_ReadData(isl29023_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: isl29023_drv.c:87
#define ISL29023_DATA_SIZE
The size of the ISL29023 sensor data.
Definition: isl29023_poll.c:34
int32_t ISL29023_I2C_Configure(isl29023_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: isl29023_drv.c:60
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
This defines the sensor specific information.
Definition: isl29023_drv.h:28
#define ISL29023_I2C_TEST_VALUE
Definition: isl29023.h:27
#define BOARD_BootClockRUN
Definition: clock_config.h:19
#define ISL29023_CMD_I_PRST_MASK
Definition: isl29023.h:52
uint8_t data[FXLS8962_DATA_SIZE]
int32_t ISL29023_I2C_Initialize(isl29023_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t test)
The interface function to initialize the sensor.
Definition: isl29023_drv.c:21
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define ISL29023_INT_LT_DATA
Definition: isl29023_poll.c:44
#define ISL29023_INT_HT_DATA
Definition: isl29023_poll.c:43
#define ISL29023_CMD_II_RANGE_MASK
Definition: isl29023.h:103
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
int main(void)
Main function.
Definition: isl29023_poll.c:78
uint16_t readFrom
Definition: sensor_drv.h:80
const registerreadlist_t ISL29023_Output_Values[]
Definition: isl29023_poll.c:69
This structure defines the Read command List.
Definition: sensor_drv.h:78
const registerreadlist_t ISL29023_Status[]
Definition: isl29023_poll.c:66
const registerwritelist_t ISL29023_Config_Poll[]
Definition: isl29023_poll.c:50
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define ISL29023_CMD_II_RES_MASK
Definition: isl29023.h:106
#define ISL29023_CMD_I_PRST_1
Definition: isl29023.h:65
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define ISL29023_CMD_II_RES_16
Definition: isl29023.h:117
#define ISL29023_I2C_ADDRESS
Definition: isl29023.h:26