1 /*
2 * Copyright 2020 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8
9 /**
10 * @file dbap_drv.c
11 * @brief The dbap_drv.c file implements the dbap functional interface.
12 */
13
14 //-----------------------------------------------------------------------
15 // ISSDK Includes
16 //-----------------------------------------------------------------------
17 #include "gpio_driver.h"
18 #include "dbap_drv.h"
19 #include "systick_utils.h"
20
DBAP_I2C_Initialize(dbap_i2c_sensorhandle_t * pSensorHandle,ARM_DRIVER_I2C * pBus,uint8_t index,uint16_t sAddress,uint8_t whoAmi)21 int32_t DBAP_I2C_Initialize(
22 dbap_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
23 {
24 int32_t status;
25 uint8_t reg = 1;
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 WHO_AM_I.*/
38 status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, FXPS7250_WHO_AM_I, 1, ®);
39 if ((ARM_DRIVER_OK != status) || (whoAmi != 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
50 return SENSOR_ERROR_NONE;
51 }
52
DBAP_I2C_SetIdleTask(dbap_i2c_sensorhandle_t * pSensorHandle,registeridlefunction_t idleTask,void * userParam)53 void DBAP_I2C_SetIdleTask(dbap_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
54 {
55 pSensorHandle->deviceInfo.functionParam = userParam;
56 pSensorHandle->deviceInfo.idleFunction = idleTask;
57 }
58
DBAP_I2C_Configure(dbap_i2c_sensorhandle_t * pSensorHandle,const registerwritelist_t * pRegWriteList)59 int32_t DBAP_I2C_Configure(dbap_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
60 {
61 int32_t status;
62
63 /*! Validate for the correct handle and register write list.*/
64 if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
65 {
66 return SENSOR_ERROR_INVALID_PARAM;
67 }
68
69 /*! Check whether sensor handle is initialized before applying configuration.*/
70 if (pSensorHandle->isInitialized != true)
71 {
72 return SENSOR_ERROR_INIT;
73 }
74
75 /*! Apply the Sensor Configuration based on the Register Write List */
76 status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
77 pRegWriteList);
78 if (ARM_DRIVER_OK != status)
79 {
80 return SENSOR_ERROR_WRITE;
81 }
82
83 return SENSOR_ERROR_NONE;
84 }
85
DBAP_I2C_ReadData(dbap_i2c_sensorhandle_t * pSensorHandle,const registerreadlist_t * pReadList,uint8_t * pBuffer)86 int32_t DBAP_I2C_ReadData(dbap_i2c_sensorhandle_t *pSensorHandle,
87 const registerreadlist_t *pReadList,
88 uint8_t *pBuffer)
89 {
90 int32_t status;
91
92 /*! Validate for the correct handle and register read list.*/
93 if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
94 {
95 return SENSOR_ERROR_INVALID_PARAM;
96 }
97
98 /*! Check whether sensor handle is initialized before reading sensor data.*/
99 if (pSensorHandle->isInitialized != true)
100 {
101 return SENSOR_ERROR_INIT;
102 }
103
104 /*! Parse through the read list and read the data one by one. */
105 status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
106 pReadList, pBuffer);
107 if (ARM_DRIVER_OK != status)
108 {
109 return SENSOR_ERROR_READ;
110 }
111
112 return SENSOR_ERROR_NONE;
113 }
114
115