ISSDK  1.7
IoT Sensing Software Development Kit
sensor_io_i2c.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file sensor_io_i2c.c
37  * @brief The sensor_io_i2c.c file contains definitions for low-level interface functions
38  * for reading and writing data from/to sensor.
39  */
40 
41 #include "Driver_I2C.h"
42 #include "sensor_drv.h"
43 #include "systick_utils.h"
44 #include "sensor_io_i2c.h"
45 
46 /*******************************************************************************
47  * Code
48  ******************************************************************************/
49 /*! The interface function to block write commands to a sensor. */
50 int32_t Sensor_I2C_BlockWrite(ARM_DRIVER_I2C *pCommDrv,
51  registerDeviceInfo_t *devInfo,
52  uint16_t slaveAddress,
53  const registercommandlist_t *pCommandList,
54  uint8_t error_mask)
55 {
57  bool commandComplete;
58  uint8_t cocoBuffer[2] = {0};
59 
60  /*! Validate for the correct handle.*/
61  if ((pCommDrv == NULL) || (pCommandList == NULL))
62  {
64  }
65 
66  const registercommandlist_t *pCmd = pCommandList;
67  /*! Update register values based on register write list unless the next Cmd is the list terminator */
68  do
69  { /*! Write the command based on the values in the command and value pair.*/
70  status =
71  Register_I2C_BlockWrite(pCommDrv, devInfo, slaveAddress, pCmd->writeTo, pCmd->pWriteBuffer, pCmd->numBytes);
72  if (ARM_DRIVER_OK != status)
73  {
74  return SENSOR_ERROR_WRITE;
75  }
76 
77  do
78  { /*! Wait for Command Completion. */
79  BOARD_DELAY_ms(1);
80  status = Register_I2C_Read(pCommDrv, devInfo, slaveAddress, 0, sizeof(cocoBuffer), cocoBuffer);
81  if (ARM_DRIVER_OK != status)
82  {
83  return SENSOR_ERROR_READ;
84  }
85  commandComplete = cocoBuffer[1] & 0x80;
86  if (commandComplete && cocoBuffer[1] & error_mask)
87  {
88  return SENSOR_ERROR_WRITE;
89  }
90  } while (!commandComplete);
91  } while ((++pCmd)->writeTo != 0xFFFF);
92 
93  return SENSOR_ERROR_NONE;
94 }
95 
96 /*! The interface function to write register data to a sensor. */
97 int32_t Sensor_I2C_Write(ARM_DRIVER_I2C *pCommDrv,
98  registerDeviceInfo_t *devInfo,
99  uint16_t slaveAddress,
100  const registerwritelist_t *pRegWriteList)
101 {
102  int32_t status;
103  bool repeatedStart;
104 
105  /*! Validate for the correct handle.*/
106  if ((pCommDrv == NULL) || (pRegWriteList == NULL))
107  {
109  }
110 
111  const registerwritelist_t *pCmd = pRegWriteList;
112 
113  /*! Update register values based on register write list unless the next Cmd is the list terminator */
114  do
115  {
116  repeatedStart = (pCmd + 1)->writeTo != 0xFFFF;
117  /*! Set the register based on the values in the register value pair.*/
118  status =
119  Register_I2C_Write(pCommDrv, devInfo, slaveAddress, pCmd->writeTo, pCmd->value, pCmd->mask, repeatedStart);
120  if (ARM_DRIVER_OK != status)
121  {
122  return SENSOR_ERROR_WRITE;
123  }
124  ++pCmd;
125  } while (repeatedStart);
126 
127  return SENSOR_ERROR_NONE;
128 }
129 
130 /*! The interface function to read register data from a sensor. */
131 int32_t Sensor_I2C_Read(ARM_DRIVER_I2C *pCommDrv,
132  registerDeviceInfo_t *devInfo,
133  uint16_t slaveAddress,
134  const registerreadlist_t *pReadList,
135  uint8_t *pOutBuffer)
136 {
137  int32_t status;
138  uint8_t *pBuf;
139 
140  /*! Validate for the correct handle.*/
141  if (pCommDrv == NULL || pReadList == NULL || pOutBuffer == NULL)
142  {
144  }
145  const registerreadlist_t *pCmd = pReadList;
146 
147  /*! Traverse the read list and read the registers one by one unless the register read list numBytes is zero*/
148  for (pBuf = pOutBuffer; pCmd->numBytes != 0; pCmd++)
149  {
150  status = Register_I2C_Read(pCommDrv, devInfo, slaveAddress, pCmd->readFrom, pCmd->numBytes, pBuf);
151  if (ARM_DRIVER_OK != status)
152  {
153  return SENSOR_ERROR_READ;
154  }
155  pBuf += pCmd->numBytes;
156  }
157  return SENSOR_ERROR_NONE;
158 }
The sensor_drv.h file contains sensor state and error definitions.
int32_t Sensor_I2C_BlockWrite(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registercommandlist_t *pCommandList, uint8_t error_mask)
Write commands to a sensor.
Definition: sensor_io_i2c.c:50
int32_t status
void BOARD_DELAY_ms(uint32_t delay_ms)
Function to insert delays.
int32_t Sensor_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerreadlist_t *pReadList, uint8_t *pOutBuffer)
Read register data from a sensor.
int32_t Register_I2C_BlockWrite(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, const uint8_t *pBuffer, uint8_t bytesToWrite)
The interface function to write a sensor register.
const uint8_t * pWriteBuffer
Definition: sensor_drv.h:115
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
int32_t Register_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t length, uint8_t *pOutBuffer)
The interface function to read a sensor register.
ARM Systick Utilities.
int32_t Register_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t value, uint8_t mask, bool repeatedStart)
The interface function to write a sensor register.
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:128
This structure defines the Block command List.
Definition: sensor_drv.h:113
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...
int32_t Sensor_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_i2c.c:97