ISSDK  1.8
IoT Sensing Software Development Kit
mma845x_drv.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file mma845x_drv.c
11  * @brief The mma845x_drv.c file implements the mma845x functional interface.
12  */
13 
14 //-----------------------------------------------------------------------
15 // ISSDK Includes
16 //-----------------------------------------------------------------------
17 #include "mma845x_drv.h"
18 
19 //-----------------------------------------------------------------------
20 // Functions
21 //-----------------------------------------------------------------------
23  mma845x_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
24 {
26  uint8_t reg;
27 
28  /*! Check the input parameters. */
29  if ((pSensorHandle == NULL) || (pBus == NULL))
30  {
32  }
33 
34  pSensorHandle->deviceInfo.deviceInstance = index;
35  pSensorHandle->deviceInfo.functionParam = NULL;
36  pSensorHandle->deviceInfo.idleFunction = NULL;
37 
38  /*! Read and store the device's WHO_AM_I.*/
39  status = Register_I2C_Read(pBus, &pSensorHandle->deviceInfo, sAddress, MMA845x_WHO_AM_I, 1, &reg);
40  if ((ARM_DRIVER_OK != status) || (whoAmi != reg))
41  {
42  pSensorHandle->isInitialized = false;
43  return SENSOR_ERROR_INIT;
44  }
45 
46  /*! Initialize the sensor handle. */
47  pSensorHandle->pCommDrv = pBus;
48  pSensorHandle->slaveAddress = sAddress;
49  pSensorHandle->isInitialized = true;
50  return SENSOR_ERROR_NONE;
51 }
52 
54  registeridlefunction_t idleTask,
55  void *userParam)
56 {
57  pSensorHandle->deviceInfo.functionParam = userParam;
58  pSensorHandle->deviceInfo.idleFunction = idleTask;
59 }
60 
62 {
64 
65  /*! Validate for the correct handle and register write list.*/
66  if ((pSensorHandle == NULL) || (pRegWriteList == NULL))
67  {
69  }
70 
71  /*! Check whether sensor handle is initialized before applying configuration.*/
72  if (pSensorHandle->isInitialized != true)
73  {
74  return SENSOR_ERROR_INIT;
75  }
76 
77  /* Put the device into standby mode so that configuration can be applied. */
78  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
80  if (ARM_DRIVER_OK != status)
81  {
82  return SENSOR_ERROR_WRITE;
83  }
84 
85  /*! Apply the Sensor Configuration based on the Register Write List */
86  status = Sensor_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
87  pRegWriteList);
88  if (ARM_DRIVER_OK != status)
89  {
90  return SENSOR_ERROR_WRITE;
91  }
92 
93  /*! Put the device into active mode and ready for reading data.*/
94  status = Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
96  if (ARM_DRIVER_OK != status)
97  {
98  return SENSOR_ERROR_WRITE;
99  }
100 
101  return SENSOR_ERROR_NONE;
102 }
103 
105  const registerreadlist_t *pReadList,
106  uint8_t *pBuffer)
107 {
108  int32_t status;
109 
110  /*! Validate for the correct handle and register read list.*/
111  if ((pSensorHandle == NULL) || (pReadList == NULL) || (pBuffer == NULL))
112  {
114  }
115 
116  /*! Check whether sensor handle is initialized before reading sensor data.*/
117  if (pSensorHandle->isInitialized != true)
118  {
119  return SENSOR_ERROR_INIT;
120  }
121 
122  /*! Parse through the read list and read the data one by one. */
123  status = Sensor_I2C_Read(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
124  pReadList, pBuffer);
125  if (ARM_DRIVER_OK != status)
126  {
127  return SENSOR_ERROR_READ;
128  }
129 
130  return SENSOR_ERROR_NONE;
131 }
132 
134 {
135  if (pSensorHandle == NULL)
136  {
138  }
139 
140  /*! Check whether sensor handle is initialized before triggering sensor reset.*/
141  if (pSensorHandle->isInitialized != true)
142  {
143  return SENSOR_ERROR_INIT;
144  }
145 
146  /*! Trigger sensor device reset.*/
147  Register_I2C_Write(pSensorHandle->pCommDrv, &pSensorHandle->deviceInfo, pSensorHandle->slaveAddress,
149 
150  /*! De-initialize sensor handle. */
151  pSensorHandle->isInitialized = false;
152  return SENSOR_ERROR_NONE;
153 }
ARM_DRIVER_I2C * pCommDrv
Definition: mma845x_drv.h:32
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.
This structure defines the Write command List.
Definition: sensor_drv.h:68
int32_t status
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.
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.
int32_t MMA845x_I2C_Deinit(mma845x_i2c_sensorhandle_t *pSensorHandle)
The interface function to De Initialize sensor..
Definition: mma845x_drv.c:133
int32_t MMA845x_I2C_Configure(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mma845x_drv.c:61
int32_t MMA845x_I2C_Initialize(mma845x_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: mma845x_drv.c:22
void MMA845x_I2C_SetIdleTask(mma845x_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mma845x_drv.c:53
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
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:71
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
#define MMA845x_CTRL_REG1_MODE_ACTIVE
Definition: mma845x.h:1600
#define MMA845x_CTRL_REG2_RST_ENABLED
Definition: mma845x.h:1678
registerDeviceInfo_t deviceInfo
Definition: mma845x_drv.h:31
#define MMA845x_CTRL_REG2_RST_MASK
Definition: mma845x.h:1657
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define MMA845x_CTRL_REG1_MODE_STANDBY
Definition: mma845x.h:1599
This defines the sensor specific information.
Definition: mma845x_drv.h:29
#define MMA845x_CTRL_REG1_MODE_MASK
Definition: mma845x.h:1580
The mma845x_drv.h file describes the MMA845x driver interface and structures.
registeridlefunction_t idleFunction
Definition: sensor_drv.h:104
int32_t MMA845x_I2C_ReadData(mma845x_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma845x_drv.c:104