ISSDK  1.7
IoT Sensing Software Development Kit
sensor_io_spi.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_spi.c
37  * @brief The sensor_io_spi.c file contains definitions for low-level interface functions
38  * for reading and writing data from/to sensor using CMSIS APIs.
39  */
40 
41 #include "Driver_SPI.h"
42 #include "sensor_drv.h"
43 #include "systick_utils.h"
44 #include "sensor_io_spi.h"
45 
46 /*******************************************************************************
47  * Functions
48  ******************************************************************************/
49 /*! The interface function to write register data to a sensor. */
50 int32_t Sensor_SPI_BlockWrite(ARM_DRIVER_SPI *pCommDrv,
51  registerDeviceInfo_t *devInfo,
52  void *pWriteParams,
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_SPI_BlockWrite(pCommDrv, devInfo, pWriteParams, 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_SPI_Read(pCommDrv, devInfo, pWriteParams, 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_SPI_Write(ARM_DRIVER_SPI *pCommDrv,
98  registerDeviceInfo_t *devInfo,
99  void *pWriteParams,
100  const registerwritelist_t *pRegWriteList)
101 {
102  int32_t status;
103  bool endDataMarkAwaited;
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  endDataMarkAwaited = (pCmd + 1)->writeTo != 0xFFFF;
117  /*! Set the register based on the values in the register value pair.*/
118  status = Register_SPI_Write(pCommDrv, devInfo, pWriteParams, pCmd->writeTo, pCmd->value, pCmd->mask);
119  if (ARM_DRIVER_OK != status)
120  {
121  return SENSOR_ERROR_WRITE;
122  }
123  ++pCmd;
124  } while (endDataMarkAwaited);
125 
126  return SENSOR_ERROR_NONE;
127 }
128 
129 /*! The interface function to read register data from a sensor. */
130 int32_t Sensor_SPI_Read(ARM_DRIVER_SPI *pCommDrv,
131  registerDeviceInfo_t *devInfo,
132  void *pReadParams,
133  const registerreadlist_t *pReadList,
134  uint8_t *pOutBuffer)
135 {
136  int32_t status;
137  uint8_t *pBuf;
138 
139  /*! Validate for the correct handle.*/
140  if (pCommDrv == NULL || pReadList == NULL || pOutBuffer == NULL)
141  {
143  }
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_SPI_Read(pCommDrv, devInfo, pReadParams, pCmd->readFrom, pCmd->numBytes, pBuf);
151  if (ARM_DRIVER_OK != status)
152  {
153  return SENSOR_ERROR_READ;
154  }
155  pBuf += pCmd->numBytes;
156  }
157 
158  return SENSOR_ERROR_NONE;
159 }
The sensor_drv.h file contains sensor state and error definitions.
int32_t Register_SPI_BlockWrite(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pWriteParams, uint8_t offset, const uint8_t *pBuffer, uint8_t bytesToWrite)
The interface function to block write to a sensor register.
int32_t status
void BOARD_DELAY_ms(uint32_t delay_ms)
Function to insert delays.
int32_t Sensor_SPI_BlockWrite(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pWriteParams, const registercommandlist_t *pCommandList, uint8_t error_mask)
Write commands to a sensor.
Definition: sensor_io_spi.c:50
The sensor_io_spi.h file declares low-level interface functions for reading and writing sensor regist...
int32_t Sensor_SPI_Write(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pWriteParams, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_spi.c:97
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_SPI_Read(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pReadParams, uint8_t offset, uint8_t length, uint8_t *pOutBuffer)
The interface function to read a sensor register.
ARM Systick Utilities.
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
int32_t Sensor_SPI_Read(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pReadParams, const registerreadlist_t *pReadList, uint8_t *pOutBuffer)
Read register data from a sensor.
int32_t Register_SPI_Write(ARM_DRIVER_SPI *pCommDrv, registerDeviceInfo_t *devInfo, void *pWriteParams, uint8_t offset, uint8_t value, uint8_t mask)
The interface function to write a sensor register.