ISSDK  1.7
IoT Sensing Software Development Kit
mma8491q_drv.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 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 mma8491q_drv.c
37  * @brief The mma8491q_drv.c file implements the MMA8491 sensor driver interfaces.
38  */
39 
40 //-----------------------------------------------------------------------
41 // ISSDK Includes
42 //-----------------------------------------------------------------------
43 #include "mma8491q_drv.h"
44 
45 //-----------------------------------------------------------------------
46 // Functions
47 //-----------------------------------------------------------------------
49  ARM_DRIVER_I2C *pBus,
50  uint8_t index,
51  uint16_t sAddress)
52 {
54  MMA8491Q_STATUS_t reg = {.w = MMA8491Q_STATUS_RESERVED_MASK}; /* Set the reserved bits to 1. */
55 
56  /*! Check the input parameters. */
57  if ((pSensorHandle == NULL) || (pBus == NULL))
58  {
60  }
61 
62  pSensorHandle->deviceInfo.deviceInstance = index;
63  pSensorHandle->deviceInfo.functionParam = NULL;
64  pSensorHandle->deviceInfo.idleFunction = NULL;
65 
66  /* Since this sensor does not have who am i register, we verify Status Register Reserved bits to validate
67  * communication. */
68  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, MMA8491Q_STATUS, 1, (uint8_t *)&reg);
69  if ((ARM_DRIVER_OK != status) || (MMA8491Q_STATUS_RESERVED_ZERO != reg.b.reserved))
70  {
71  pSensorHandle->isInitialized = false;
72  return SENSOR_ERROR_INIT;
73  }
74 
75  /*! Initialize the sensor handle. */
76  pSensorHandle->pCommDrv = pBus;
77  pSensorHandle->slaveAddress = sAddress;
78  pSensorHandle->isInitialized = true;
79  return SENSOR_ERROR_NONE;
80 }
81 
83  registeridlefunction_t idleTask,
84  void *userParam)
85 {
86  pSensorHandle->deviceInfo.functionParam = userParam;
87  pSensorHandle->deviceInfo.idleFunction = idleTask;
88 }
89 
91  const registerreadlist_t *pReadList,
92  uint8_t *pBuffer)
93 {
95 
96  /*! Validate for the correct handle and register read list.*/
97  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
98  {
100  }
101 
102  /*! Check whether sensor handle is initialized before reading sensor data.*/
103  if (pSensorHandle->isInitialized != true)
104  {
105  return SENSOR_ERROR_INIT;
106  }
107 
108  /*! Parse through the read list and read the data one by one. */
109  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
110  pReadList, pBuffer);
111  if (ARM_DRIVER_OK != status)
112  {
113  return SENSOR_ERROR_READ;
114  }
115 
116  return SENSOR_ERROR_NONE;
117 }
ARM_DRIVER_I2C * pCommDrv
Definition: mma8491q_drv.h:58
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
int32_t status
int32_t MMA8491Q_I2C_ReadData(mma8491q_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma8491q_drv.c:90
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.
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
registerDeviceInfo_t deviceInfo
Definition: mma8491q_drv.h:57
int32_t MMA8491Q_I2C_Initialize(mma8491q_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress)
The interface function to initialize the sensor.
Definition: mma8491q_drv.c:48
void MMA8491Q_I2C_SetIdleTask(mma8491q_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma8491q_drv.c:82
This defines the function pointers and sensor specific information.
Definition: mma8491q_drv.h:55
The mma8491q_drv.h file describes the MMA8491Q driver interface and structures.
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define MMA8491Q_STATUS_RESERVED_MASK
Definition: mma8491q.h:71
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.
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define MMA8491Q_STATUS_RESERVED_ZERO
Definition: mma8491q.h:90