1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file  isl29023_drv.c
10  * @brief The isl29023_drv.c file implements the isl29023 functional interface.
11  */
12 
13 //-----------------------------------------------------------------------
14 // ISSDK Includes
15 //-----------------------------------------------------------------------
16 #include "isl29023_drv.h"
17 
18 //-----------------------------------------------------------------------
19 // Functions
20 //-----------------------------------------------------------------------
ISL29023_I2C_Initialize(isl29023_i2c_sensorhandle_t * pSensorHandle,ARM_DRIVER_I2C * pBus,uint8_t index,uint16_t sAddress,uint8_t test)21 int32_t ISL29023_I2C_Initialize(
22     isl29023_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t test)
23 {
24     int32_t status;
25     uint8_t reg;
26 
27     /*! Check the input parameters. */
28     if ((pSensorHandle == NULL) || (pBus == NULL))
29     {
30         return SENSOR_ERROR_INVALID_PARAM;
31     }
32 
33     pSensorHandle->deviceInfo.deviceInstance = index;
34     pSensorHandle->deviceInfo.functionParam = NULL;
35     pSensorHandle->deviceInfo.idleFunction = NULL;
36 
37     /*!  Read and store the device's test.*/
38     status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, ISL29023_TEST, 1, &reg);
39     if ((ARM_DRIVER_OK != status) || (test != reg))
40     {
41         pSensorHandle->isInitialized = false;
42         return SENSOR_ERROR_INIT;
43     }
44 
45     /*! Initialize the sensor handle. */
46     pSensorHandle->pCommDrv = pBus;
47     pSensorHandle->slaveAddress = sAddress;
48     pSensorHandle->isInitialized = true;
49     return SENSOR_ERROR_NONE;
50 }
51 
ISL29023_I2C_SetIdleTask(isl29023_i2c_sensorhandle_t * pSensorHandle,registeridlefunction_t idleTask,void * userParam)52 void ISL29023_I2C_SetIdleTask(isl29023_i2c_sensorhandle_t *pSensorHandle,
53                              registeridlefunction_t idleTask,
54                              void *userParam)
55 {
56     pSensorHandle->deviceInfo.functionParam = userParam;
57     pSensorHandle->deviceInfo.idleFunction = idleTask;
58 }
59 
ISL29023_I2C_Configure(isl29023_i2c_sensorhandle_t * pSensorHandle,const registerwritelist_t * pRegWriteList)60 int32_t ISL29023_I2C_Configure(isl29023_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
61 {
62     int32_t status;
63 
64     /*! Validate for the correct handle and register write list.*/
65     if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
66     {
67         return SENSOR_ERROR_INVALID_PARAM;
68     }
69 
70     /*! Check whether sensor handle is initialized before applying configuration.*/
71     if (pSensorHandle->isInitialized != true)
72     {
73         return SENSOR_ERROR_INIT;
74     }
75 
76     /*! Apply the Sensor Configuration based on the Register Write List */
77     status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
78                               pRegWriteList);
79     if (ARM_DRIVER_OK != status)
80     {
81         return SENSOR_ERROR_WRITE;
82     }
83 
84     return SENSOR_ERROR_NONE;
85 }
86 
ISL29023_I2C_ReadData(isl29023_i2c_sensorhandle_t * pSensorHandle,const registerreadlist_t * pReadList,uint8_t * pBuffer)87 int32_t ISL29023_I2C_ReadData(isl29023_i2c_sensorhandle_t *pSensorHandle,
88                              const registerreadlist_t *pReadList,
89                              uint8_t *pBuffer)
90 {
91     int32_t status;
92 
93     /*! Validate for the correct handle and register read list.*/
94     if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
95     {
96         return SENSOR_ERROR_INVALID_PARAM;
97     }
98 
99     /*! Check whether sensor handle is initialized before reading sensor data.*/
100     if (pSensorHandle->isInitialized != true)
101     {
102         return SENSOR_ERROR_INIT;
103     }
104 
105     /*! Parse through the read list and read the data one by one. */
106     status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
107                              pReadList, pBuffer);
108     if (ARM_DRIVER_OK != status)
109     {
110         return SENSOR_ERROR_READ;
111     }
112 
113     return SENSOR_ERROR_NONE;
114 }
115 
ISL29023_I2C_DeInit(isl29023_i2c_sensorhandle_t * pSensorHandle)116 int32_t ISL29023_I2C_DeInit(isl29023_i2c_sensorhandle_t *pSensorHandle)
117 {
118     int32_t status;
119 
120     if (pSensorHandle == NULL)
121     {
122         return SENSOR_ERROR_INVALID_PARAM;
123     }
124 
125     /*! Check whether sensor handle is initialized before triggering sensor reset.*/
126     if (pSensorHandle->isInitialized != true)
127     {
128         return SENSOR_ERROR_INIT;
129     }
130 
131     /*! Trigger sensor device reset.*/
132     status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
133                                 ISL29023_CMD_I, ISL29023_CMD_I_OP_POWER_DOWN, ISL29023_CMD_I_OP_MASK, false);
134     if (ARM_DRIVER_OK != status)
135     {
136         return SENSOR_ERROR_WRITE;
137     }
138     else
139     {
140         /*! De-initialize sensor handle. */
141         pSensorHandle->isInitialized = false;
142     }
143 
144     return SENSOR_ERROR_NONE;
145 }
146