ISF  2.1
Intelligent Sensing Framework for Kinetis with Processor Expert
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
fsl_mma865x_i2c_3D_accel.c
Go to the documentation of this file.
1 /**
2  ** @file fsl_mma865x_i2c_3D_accel.c
3  ** @version 01.00
4  ** @brief
5  **
6  */
7 /**
8  ** @addtogroup fsl_mma865x_i2c_3D_accel_module fsl_mma865x_i2c_3D_accel module documentation
9  ** @{
10  */
11 
12 /* MODULE fsl_mma865x_i2c_3D_accel. */
13 
14 #include <isf.h>
15 #include <isf_types.h>
16 #include <lwmem.h>
17 #include <lwsem.h>
18 #include <lwevent.h>
19 #include <isf_sm_api.h>
20 #include <isf_dsa_adapter.h>
21 #include <isf_bm.h>
22 #include <isf_sensor_types.h>
23 #include <isf_fifo.h>
25 #include <isf_comm.h>
26 #include <isf_util.h>
27 #include <isf_sensors.h>
28 #include "mma865x.h"
29 #include "fsl_i2c_master_driver.h"
31 
32 
33 /*! @brief User specified settings that may change */
34 static mma865x_Specific_Settings_t mma865x_settings;
35 
36 
37 /*! @brief The constant array, fsl_mma865x_period, maps the available sample rates into the compatible sampling periods. */
38 static const isf_duration_t fsl_mma865x_period[] =
39 {
40  1250, // 800 Hz
41  2500, // 400 Hz
42  5000, // 200 Hz
43  10000, // 100 Hz
44  20000, // 50 Hz
45  80000, // 12.5 Hz
46  160000, // 6.25 Hz
47  640000 // 1.56 Hz
48 };
49 
50 /*
51  * @brief This table contains the Accel conversion factors based on the full-scale range selected.
52  */
53 const struct
54 {
55  float floatFactor;
56  float floatOffset;
60 {
61  { 0.000061, 0.0, 8, 0},
62  { 0.000122, 0.0, 16, 0},
63  { 0.000244, 0.0, 32, 0}
64 };
65 
68 
69 // Define the MMA865x functional interface status return type.
71 
72 /*
73  * Local function prototypes.
74  */
75 void mma865x_Reset(int32_t *status, void* pSensorHandle);
76 void mma865x_CheckId(int32_t *status, void* pSensorHandle);
77 void mma865x_SetMode(int32_t *status, void* pSensorHandle, int32_t Mode);
78 void mma865x_ValidateConfig(int32_t *status, void* pSensorHandle, void* pSettings);
81 isf_dsa_status_t mma865x_GetData(isf_SensorHandle_t *pSensorHandle, void* pBuffer);
82 
83 // Converter routines for the supported data types
84 static isf_dsa_status_t float_accel3d_converter(mma865x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma865x_DataBuffer_t *nativeSample, void *vpConvertedSample );
85 static isf_dsa_status_t fixed_accel3d_converter(mma865x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma865x_DataBuffer_t *nativeSample, void *vpConvertedSample );
86 
87 /*
88  ** ===================================================================
89  ** Method : fsl_mma865x_i2c_3D_accel_Initialize (component ISFDSA)
90  ** @brief
91  **
92  ** Parameters :
93  ** NAME - DESCRIPTION
94  ** @param
95  ** isf_SensorHandle_t *pSensorHandle
96  ** @return
97  ** isf_dsa_status_t -
98  ** ===================================================================
99  */
101 {
102  if ( NULL == pSensorHandle){
103  return DSA_ERR_PARAM;
104  }
105 
106  int32_t status;
107 
108  // Check if the sensor is available, and that it has already been initialized.
109  if (pSensorHandle->adapterStatus > DSA_STATE_NOT_INITIALIZED) {
110  return DSA_ERR_INITIALIZE;
111  }
112 
113  // Allocate the adapter specific memory for the descriptor
114  pSensorHandle->pDeviceDescriptor = (DeviceDescriptor_t *)_lwmem_alloc_system_zero(sizeof(DeviceDescriptor_t));
115 
116  // Create and validate helper reference pointers.
117  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
118 
119  if (NULL == pDeviceDescriptor) {
120  return DSA_ERR_INITIALIZE;
121  }
122 
123  // Allocate the current sample data buffer and initialize it.
124  pDeviceDescriptor->pCurrentSample = _lwmem_alloc_system_zero(sizeof(mma865x_DataBuffer_t));
125  mma865x_DataBuffer_t *pCurrentSampleBuffer = (mma865x_DataBuffer_t *)pDeviceDescriptor->pCurrentSample;
126  pCurrentSampleBuffer->timeStamp = 0;
127  pCurrentSampleBuffer->accel[0] = 0;
128  pCurrentSampleBuffer->accel[1] = 0;
129  pCurrentSampleBuffer->accel[2] = 0;
130  pCurrentSampleBuffer->addr = ((i2c_device_t *)pSensorHandle->pSensorStaticConfig->commInfo)->address;
131 
132  dm_ChannelDescriptor_t *pChannelDescriptor = &pDeviceDescriptor->cDescriptor;
133 
134  // Initialize the channel
135  if(ISF_SUCCESS != dm_channel_init(pSensorHandle->pSensorStaticConfig->channelId, pChannelDescriptor)){
136  return DSA_ERR_INITIALIZE;
137  }
138  // Start the Bus
139  if(ISF_SUCCESS != dm_channel_start(pChannelDescriptor)){
140  return DSA_ERR_INITIALIZE;
141  }
142 
143  // Get the Channel state to check if it is ready to use
144  if (COMM_STATE_OK != dm_channel_get_state(pChannelDescriptor)) {
145  return DSA_ERR_INITIALIZE;
146  }
147 
148  // Open the device and get the device handler
149  if(ISF_SUCCESS != dm_device_open(pChannelDescriptor, (void *)pSensorHandle->pSensorStaticConfig->commInfo, &pDeviceDescriptor->deviceHandle)){
150  return DSA_ERR_INITIALIZE;
151  }
152 
153  // A very short delay is needed for device startup.
154  _time_delay_ticks(1);
155 
156  // Query the sensor to validate Device Messaging initialization and the sensor identity.
157  mma865x_CheckId(&status,pSensorHandle);
158  if (ISF_SUCCESS != status)
159  {
160  return DSA_ERR_INITIALIZE;
161  }
162 
163  // Set the adapter state to be initialized.
164  pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
165 
166  // Create a semaphore to synchronize the device descriptor across tasks
167  if (MQX_OK != _lwsem_create(&pDeviceDescriptor->deviceSemaphore, 1)){
168  return DSA_ERR_INITIALIZE;
169  }
170 
171  pDeviceDescriptor->skipFramecnt = 0;
172 
173  return ISF_SUCCESS;
174 }
175 
176 /*
177  ** ===================================================================
178  ** Method : fsl_mma865x_i2c_3D_accel_ValidateSettings (component ISFDSA)
179  ** @brief
180  **
181  ** Parameters :
182  ** NAME - DESCRIPTION
183  ** @param
184  ** isf_SensorHandle_t *pSensorHandle
185  ** @param
186  ** isf_SubscriptionSettings_t *pSettings
187  ** @return
188  ** isf_dsa_status_t -
189  ** ===================================================================
190  */
192 {
193  // Check the input arguments.
194  if ((NULL == pSensorHandle) || (NULL == pSensorSettings)) {
195  return DSA_ERR_PARAM;
196  }
197 
198  int32_t status;
199 
200  mma865x_ValidateConfig(&status,pSensorHandle,pSensorSettings);
201 
202  return status;
203 }
204 
205 /*
206  ** ===================================================================
207  ** Method : fsl_mma865x_i2c_3D_accel_Configure (component ISFDSA)
208  ** @brief
209  **
210  ** Parameters :
211  ** NAME - DESCRIPTION
212  ** @param
213  ** isf_SensorHandle_t *pSensorHandle
214  ** @param
215  ** isf_SubscriptionSettings_t *pConfigSettings
216  ** @return
217  ** isf_dsa_status_t -
218  ** ===================================================================
219  */
221 {
222  // Check pointers.
223  if((NULL == pSensorHandle) || (NULL == pSensorSettings) ){
224  return DSA_ERR_PARAM;
225  }
226 
227  int32_t status;
228 
229  int32_t retStat = DSA_ERR_CONFIGURE; // Return Status
230 
231  // Create helper reference pointers.
232  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*) pSensorHandle->pDeviceDescriptor;
233 
234  // Check the device descriptor pointer.
235  if (NULL == pDeviceDescriptor)
236  {
237  return retStat;
238  }
239 
240  // Lock the device descriptor.
241  _lwsem_wait_ticks(&pDeviceDescriptor->deviceSemaphore, 0);
242 
243  // Check the driver state.
244  if (DSA_STATE_INITIALIZED > pSensorHandle->adapterStatus){
245  goto unlockdescriptor;
246  }
247 
248  // User Notice: This is a required function call. If it is not here, there may be conflicts, data loss,
249  // or any number of undesirable things may occur.
250  if (ISF_SUCCESS != dm_channel_acquire_lock(&pDeviceDescriptor->cDescriptor, 0)) {
251  goto unlockdescriptor;
252  }
253 
254  // Set the sensor into STANDBY mode for configuration.
255  mma865x_SetMode(&status, pSensorHandle, SENSOR_STANDBY);
256  if (ISF_SUCCESS != status) {
257  goto unlockchannel;
258  }
259 
260  // Configure the sensor.
261  retStat = mma865x_SetConfig(pSensorHandle, pSensorSettings);
262  if (ISF_SUCCESS != retStat) {
263  goto unlockchannel;
264  }
265 
266  // Register the periodic callback function with the Bus Manager.
267  pDeviceDescriptor->token = bm_register_periodic_callback( pSensorSettings->nSamplePeriod, fsl_mma865x_i2c_3D_accel_PeriodicCallback , pSensorHandle);
268  if(BM_ERROR & pDeviceDescriptor->token){
269  goto unlockchannel;
270  }
271 
272  //Store the configured settings
273  pSensorHandle->controlData.sensorSettings = *pSensorSettings;
274 
276  retStat = ISF_SUCCESS;
277 
278 unlockchannel:
279  // Unlock the channel.
280  // The explicit lock must be released when the configuration is done allowing the bus to be used by others.
281  // User Notice: This is a required function call.
282  status = dm_channel_release_lock(&pDeviceDescriptor->cDescriptor);
283  if (ISF_SUCCESS != status)
284  {
285  retStat = status;
286  }
287 
288 unlockdescriptor:
289 
290  // Unlock the device descriptor.
291  _lwsem_post(&pDeviceDescriptor->deviceSemaphore);
292 
293  return retStat;
294 }
295 
296 /*
297  ** ===================================================================
298  ** Method : fsl_mma865x_i2c_3D_accel_StartData (component ISFDSA)
299  ** @brief
300  **
301  ** Parameters :
302  ** NAME - DESCRIPTION
303  ** @param
304  ** isf_SensorHandle_t *pSensorHandle
305  ** @return
306  ** isf_dsa_status_t -
307  ** ===================================================================
308  */
310 {
311  // Check pointers.
312  if(NULL == pSensorHandle){
313  return DSA_ERR_PARAM;
314  }
315 
316  int32_t status;
317  int32_t retStat = DSA_ERR_START_DATA;
318 
319  // Create helper reference pointers.
320  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
321 
322  // Check the device descriptor.
323  if (NULL == pDeviceDescriptor)
324  return DSA_ERR_PARAM;
325 
326  // Lock the device descriptor.
327  _lwsem_wait_ticks(&pDeviceDescriptor->deviceSemaphore, 0);
328 
329  // Check the driver state.
330  if (DSA_STATE_CONFIGURED_STOPPED > pSensorHandle->adapterStatus)
331  {
332  goto unlockdescriptor;
333  }
334 
335  // Set the sensor into ACTIVE mode for operation.
336  mma865x_SetMode(&status, pSensorHandle, SENSOR_ACTIVE);
337  if (ISF_SUCCESS != status) {
338  goto unlockdescriptor;
339  }
340 
341  // Start the Bus Manager sampling timer.
342  if (BM_ERROR & bm_start(FALSE, pDeviceDescriptor->token)) // Check for Bus Manager error and return on failure.
343  goto unlockdescriptor;
344 
345  // Set device to active state.
347  retStat = ISF_SUCCESS;
348 
349 unlockdescriptor:
350  // Unlock the device descriptor.
351  _lwsem_post(&pDeviceDescriptor->deviceSemaphore);
352 
353  return retStat;
354 }
355 
356 /*
357  ** ===================================================================
358  ** Method : fsl_mma865x_i2c_3D_accel_EndData (component ISFDSA)
359  ** @brief
360  **
361  ** Parameters :
362  ** NAME - DESCRIPTION
363  ** @param
364  ** isf_SensorHandle_t *pSensorHandle
365  ** @return
366  ** isf_dsa_status_t -
367  ** ===================================================================
368  */
370 {
371  // Check pointers.
372  if(NULL == pSensorHandle){
373  return DSA_ERR_PARAM;
374  }
375 
376  int32_t status;
377 
378  // Create helper reference pointers.
379  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
380 
381  // Set the default return status.
382  int32_t retStat = DSA_ERR_END_DATA;
383 
384  // Check the device descriptor.
385  if (NULL == pDeviceDescriptor)
386  return retStat;
387 
388  // Lock the device descriptor.
389  _lwsem_wait_ticks(&pDeviceDescriptor->deviceSemaphore, 0);
390 
391  // Check the driver state.
392  if (DSA_STATE_CONFIGURED_STOPPED >= pSensorHandle->adapterStatus)
393  goto unlockdescriptor;
394 
395  // Check that the adapter is not already started.
396  if (DSA_STATE_CONFIGURED_STARTED == pSensorHandle->adapterStatus) {
397  // If it is, then stop the Bus Manager (BM) sample timer.
398  isf_status_t bmStopStatus = bm_stop(pDeviceDescriptor->token);
399  if (BM_ERROR & bmStopStatus)
400  goto unlockdescriptor;
401  }
402 
403  // Set the sensor into STANDBY mode for configuration.
404  mma865x_SetMode(&status, pSensorHandle, SENSOR_STANDBY);
405  if (ISF_SUCCESS != status) {
406  goto unlockdescriptor;
407  }
408 
410  retStat = ISF_SUCCESS;
411 
412 
413 unlockdescriptor:
414  // Unlock the device descriptor.
415  _lwsem_post(&pDeviceDescriptor->deviceSemaphore);
416 
417  return retStat;
418 }
419 
420 /*
421  ** ===================================================================
422  ** Method : fsl_mma865x_i2c_3D_accel_Calibrate (component ISFDSA)
423  ** @brief
424  **
425  ** Parameters :
426  ** NAME - DESCRIPTION
427  ** @param
428  ** isf_SensorHandle_t *pSensorHandle
429  ** @return
430  ** isf_dsa_status_t -
431  ** ===================================================================
432  */
434 {
435  int32_t retStat = ISF_SUCCESS;
436 
437  return retStat;
438 }
439 
440 /*
441  ** ===================================================================
442  ** Method : fsl_mma865x_i2c_3D_accel_Shutdown (component ISFDSA)
443  ** @brief
444  **
445  ** Parameters :
446  ** NAME - DESCRIPTION
447  ** @param
448  ** isf_SensorHandle_t *pSensorHandle
449  ** @return
450  ** isf_dsa_status_t -
451  ** ===================================================================
452  */
454 {
455  if(NULL == pSensorHandle){
456  return DSA_ERR_PARAM;
457  }
458 
459  // Create helper reference pointers.
460  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
461 
462  // Remove the sensor adapter from the Bus Manager callbacks.
463  bm_unregister_callback(pDeviceDescriptor->token);
464  pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
465  return ISF_SUCCESS;
466 
467 }
468 
469 /*
470  ** ===================================================================
471  ** Method : fsl_mma865x_i2c_3D_accel_PeriodicCallback (component ISFDSA)
472  ** @brief
473  **
474  ** Parameters :
475  ** NAME - DESCRIPTION
476  ** @param
477  ** void* pSensorHandle -
478  ** @return
479  ** void -
480  ** ===================================================================
481  */
483 {
484  // Check pointers.
485  if(NULL == pSensorHandle){
486  return;
487  }
488 
489  // Create helper reference pointers.
490  volatile isf_SensorHandle_t *pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
492  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHdl->pDeviceDescriptor;
493  isf_status_t st;
494  int32 numBytes;
495 
496  // Check the device descriptor
497  if (NULL == pDeviceDescriptor){
498  return;
499  }
500 
501  // Create a helper pointer to the current sample buffer.
502  mma865x_DataBuffer_t *pCurrentSampleBuffer = (mma865x_DataBuffer_t *)pDeviceDescriptor->pCurrentSample;
503 
504  // Apply sample skip if necessary.
505  if (pDeviceDescriptor->skipFramecnt) {
506  // Not done skipping samples.
507  --pDeviceDescriptor->skipFramecnt;
508  return;
509  }
510 
511  // Get the time stamp
512  pCurrentSampleBuffer->timeStamp = isf_time_util_get_usec();
513 
514  // Lock the device descriptor.
515  if (MQX_OK !=_lwsem_wait_ticks(&pDeviceDescriptor->deviceSemaphore, 0))
516  {
517  goto unlockdescriptor;
518  }
519 
520 
521  // Check the driver state.
522  if (DSA_STATE_CONFIGURED_STARTED != pSensorHdl->adapterStatus)
523  {
524  goto unlockdescriptor;
525  }
526 
527  // Get sensor data.
528  mma865x_GetData(pSensorHandle, ((uint8*)&pCurrentSampleBuffer->accel[0])); // Returns samples in big endian.
529 
530  // Lock the fifo for update.
531  if(MQX_OK != isf_fifo_lock(pFifo)){
532  return;
533  }
534 
536 
537 
538  // write the new data
540  {
541  *pFifoEntry = *pCurrentSampleBuffer;
542  }
543  else
544  {
546  pSensorHdl,
549  pCurrentSampleBuffer,
550  pFifoEntry,
551  &numBytes);
552  }
553 
554  // Increment the fifo to the next sample entry.
555  st = isf_fifo_el_increment(pFifo);
556 
557  // Unlock the fifo.
558  isf_fifo_unlock(pFifo);
559 
560  if (st == ISF_FIFO_FULL)
561  {
562  // Notify the user using their registered event information
563  _lwevent_set(pSensorHdl->controlData.pEventGroup,(_mqx_uint)pSensorHdl->controlData.nEventFieldIndex);
564  }
565 
566 unlockdescriptor:
567  // Unlock the device descriptor.
568  _lwsem_post(&pDeviceDescriptor->deviceSemaphore);
569 
570 }
571 
572 /*!
573  * @brief This function coverts the raw sample data to the desired output type.
574  */
576  (
577  volatile isf_SensorHandle_t *pSensorHandle,
578  isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType,
579  void *pNativeSample,
580  void *pConvertedSample,
581  int32 *numBytes
582  )
583 {
586 
587  pConverter = NULL;
588 
589  switch (convertToType)
590  {
592  if (resultType == DSA_RESULT_TYPE_ENG_FLOAT)
593  pConverter = float_accel3d_converter;
594  else if (resultType == DSA_RESULT_TYPE_ENG_FIXED)
595  pConverter = fixed_accel3d_converter;
596  break;
597 
598  default:
600  }
601  if (pConverter == NULL)
603 
604  retStat = pConverter(
606  (mma865x_DataBuffer_t *)pNativeSample,
607  pConvertedSample
608  );
609 
610  return retStat;
611 }
612 
613 static isf_dsa_status_t float_accel3d_converter(mma865x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma865x_DataBuffer_t *nativeSample, void *vpConvertedSample )
614 {
615  isf_Acceleration3D_Float_t *convertedSample = (isf_Acceleration3D_Float_t *)vpConvertedSample;
616  convertedSample->timestamp = nativeSample->timeStamp;
617  // These factors and offsets really depends on the configured sensor Range in pSensorSpecificConfig
618  //convertedSample->accel[0] = nativeSample->accel[0] * MMA865x_ACCEL_FLOAT_CONVERSION_FACTOR + MMA865x_ACCEL_FLOAT_CONVERSION_OFFSET;
619  //convertedSample->accel[1] = nativeSample->accel[1] * MMA865x_ACCEL_FLOAT_CONVERSION_FACTOR + MMA865x_ACCEL_FLOAT_CONVERSION_OFFSET;
620  //convertedSample->accel[2] = nativeSample->accel[2] * MMA865x_ACCEL_FLOAT_CONVERSION_FACTOR + MMA865x_ACCEL_FLOAT_CONVERSION_OFFSET;
621 
622  // Convert the sample based on the full-scale range.
623  convertedSample->accel[0] = nativeSample->accel[0] * mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].floatFactor + mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].floatOffset;
624  convertedSample->accel[1] = nativeSample->accel[1] * mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].floatFactor + mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].floatOffset;
625  convertedSample->accel[2] = nativeSample->accel[2] * mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].floatFactor + mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].floatOffset;
626  return ISF_SUCCESS;
627 }
628 
629 static isf_dsa_status_t fixed_accel3d_converter(mma865x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma865x_DataBuffer_t *nativeSample, void *vpConvertedSample )
630 {
631  isf_Acceleration3D_EngFixed_t *convertedSample = (isf_Acceleration3D_EngFixed_t *)vpConvertedSample;
632  convertedSample->timestamp = nativeSample->timeStamp;
633  // These factors and offsets really depend on the configured sensor Range in pSensorSpecificConfig
634  //convertedSample->accel[0] = nativeSample->accel[0] * MMA865x_ACCEL_FIXED_CONVERSION_FACTOR + MMA865x_ACCEL_FIXED_CONVERSION_OFFSET;
635  //convertedSample->accel[1] = nativeSample->accel[1] * MMA865x_ACCEL_FIXED_CONVERSION_FACTOR + MMA865x_ACCEL_FIXED_CONVERSION_OFFSET;
636  //convertedSample->accel[2] = nativeSample->accel[2] * MMA865x_ACCEL_FIXED_CONVERSION_FACTOR + MMA865x_ACCEL_FIXED_CONVERSION_OFFSET;
637 
638  // Convert the sample based on the full-scale range.
639  convertedSample->accel[0] = (nativeSample->accel[0] * mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].fixedFactor + mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].fixedOffset)/4;
640  convertedSample->accel[1] = (nativeSample->accel[1] * mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].fixedFactor + mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].fixedOffset)/4;
641  convertedSample->accel[2] = (nativeSample->accel[2] * mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].fixedFactor + mma865xAccelConvTable[pSensorSpecificConfig->accelFullScaleRange].fixedOffset)/4;
642  return ISF_SUCCESS;
643 }
644 
645 /*!
646  * @brief This function resets the MMA865x sensor resulting in a default configuration for
647  * all registers.
648  */
649 void mma865x_Reset(int32_t* status, void* pSensorHandle)
650 {
651  uint8 buffer[1] = {0};
652 
653  // Create and validate helper reference pointers.
654  isf_SensorHandle_t* pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
655  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHdl->pDeviceDescriptor;
656  dm_DeviceDescriptor_t* pDeviceHandle = &pDeviceDescriptor->deviceHandle;
657 
658  // Reset the device.
659  // Put the device the STANDBY state which is required before any configuration registers are set.
660  buffer[0]= MMA865x_RST_MASK; // no shift
661  if (ISF_SUCCESS != dm_device_write(pDeviceHandle, MMA865x_CTRL_REG2, buffer, 1, 1)){
662  *status = DSA_ERR_INITIALIZE;
663  return;
664  }
665 
666  // Burn a write and ignore the return.
667  buffer[0] = 0;
668  ( void ) dm_device_write(pDeviceHandle, MMA865x_ASLP_COUNT, buffer, 1, 1);
669 
670  // Turn-on time = 1 second (MMA865x Data Sheet) for OSR = 2^7 (128).
671  _time_delay_ticks(1000/5);
672 
673  *status = ISF_SUCCESS;
674 }
675 
676 /*!
677  * @brief This function validates the MMA865x sensor is connected via the I2C bus.
678  */
679 void mma865x_CheckId(int32_t *status, void* pSensorHandle)
680 {
681  uint8 buffer[1] = {0};
682  int32 retStat = SENSOR_ERROR_CHECKID;
683 
684  // Create and validate helper reference pointers.
685  isf_SensorHandle_t* pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
686  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHdl->pDeviceDescriptor;
687  dm_DeviceDescriptor_t* pDeviceHandle = &pDeviceDescriptor->deviceHandle;
688 
689  if (ISF_SUCCESS != dm_device_read(pDeviceHandle, MMA865x_WHO_AM_I, &buffer[0], 1, 1)){
690  *status = retStat;
691  }
692  if ((MMA865x_WHOAMI_VALUE_8652 != buffer[0]) && (MMA865x_WHOAMI_VALUE_8653 != buffer[0])) {
693  *status = retStat;
694  }
695 
696  *status = ISF_SUCCESS;
697 }
698 
699 /*!
700  * @brief This function validates the configuration for the MMA865x sensor.
701  */
702 void mma865x_ValidateConfig(int32_t* status, void* pSensorHandle, void* pSettings)
703 {
704  isf_dsa_status_t st; // The status variable.
705  isf_SensorHandle_t *pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
706  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*) pSensorHdl->pDeviceDescriptor;
707  isf_dsa_SensorSettings_t *pConfig = (isf_dsa_SensorSettings_t*)pSettings;
708 
709  // Validate the configuration data using the global data
710  st = mma865x_GetConfig( pConfig, pDeviceDescriptor );
711 
712  // If the configuration data has been modified. convey this information to user Application.
713  if (st) {
714  *status = DSA_RET_SETTINGS_CHANGED;
715  return;
716  }
717 
718  *status = ISF_SUCCESS;
719 }
720 
721 /*!
722  * @brief This function sets the MMA865x part to the requested mode.
723  */
724 void mma865x_SetMode(int32_t* status, void* pSensorHandle, int32_t Mode)
725 {
726  isf_SensorHandle_t* pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
727  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHdl->pDeviceDescriptor;
728  dm_DeviceDescriptor_t* pDeviceHandle = &pDeviceDescriptor->deviceHandle;
729 
730  uint8 buffer[1] = {0x00};
731 
732  switch(Mode)
733  {
734  // This will put the sensor into the standby mode
735  case SENSOR_STANDBY:
736  case SENSOR_BOOT:
737  case SENSOR_READY:
738  {
739  // this sensor requires read-modify-write
740  if (ISF_SUCCESS != dm_device_read(pDeviceHandle, MMA865x_CTRL_REG1, buffer, 1, 1) ){
741  *status = SENSOR_ERROR_SETMODE;
742  return;
743  }
744  buffer[0] = (buffer[0] & ~MMA865x_ACTIVE_MASK);
745  if (ISF_SUCCESS != dm_device_write(pDeviceHandle, MMA865x_CTRL_REG1, buffer, 1, 1) ){
746  *status = SENSOR_ERROR_SETMODE;
747  return;
748  }
749  }
750  break;
751 
752  // This will put the sensor into the active state
753  case SENSOR_ACTIVE:
754  {
755  // this sensor requires read-modify-write
756  if (ISF_SUCCESS != dm_device_read(pDeviceHandle, MMA865x_CTRL_REG1, buffer, 1, 1) ){
757  *status = SENSOR_ERROR_SETMODE;
758  return;
759  }
760  buffer[0] = (buffer[0] | MMA865x_ACTIVE_MASK);
761  if (ISF_SUCCESS != dm_device_write(pDeviceHandle, MMA865x_CTRL_REG1, buffer, 1, 1) ){
762  *status = SENSOR_ERROR_SETMODE;
763  return;
764  }
765  // Read the value of the status register to wait for valid data
766  buffer[0] = 0;
767  do {
768  if (ISF_SUCCESS != dm_device_read(pDeviceHandle, MMA865x_STATUS_00, buffer, 1, 1) ){
769  *status = SENSOR_ERROR_SETMODE;
770  return;
771  }
772  _time_delay_ticks(1); // short delay to yield CPU/RTOS.
773  } while (0 == MMA865x_GET_FIELD(ZYXDR,buffer[0]) ); // Wait for Data Ready.
774  }
775  break;
776 
777  default:
778  *status = SENSOR_ERROR_SETMODE;
779  return;
780  }
781  // return success
782  *status = ISF_SUCCESS;
783 }
784 
785 /*!
786  * @brief This function retrieves the raw data from the MMA865x sensor.
787  */
788 #define BYTES_FOR_FULL_READ 7
789 
791 {
793 
794  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
795  dm_DeviceDescriptor_t* pDeviceHandle = &pDeviceDescriptor->deviceHandle;
796 
797  uint16* pSensorData = (uint16*)pBuffer;
798  uint8 registerData[BYTES_FOR_FULL_READ];
799 
800  // read the full width of the data for both full or short reads
802  { // return error SENSOR_ERROR_GETDATA
803  return retStat;
804  }
805 
806  // If there is any data, then format and copy it to the output buffer.
807  if ( MMA865x_GET_FIELD(ZYXDR, registerData[0] ) > 0 )
808  {
809  // collect the full 16-bit data for each of x,y,z
810  pSensorData[0] = ((int16)registerData[1])<<8 | registerData[2];
811  pSensorData[1] = ((int16)registerData[3])<<8 | registerData[4];
812  pSensorData[2] = ((int16)registerData[5])<<8 | registerData[6];
813  }
814  // return success
815  return ISF_SUCCESS;
816 }
817 
818 
819 /*!
820  * @brief This function sets the MMA865x sensor configuration.
821  */
823 {
824  int32_t retStat = DSA_ERR_CONFIGURE; // Return Status
825 
826  // Create helper reference pointers.
827  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*) pSensorHandle->pDeviceDescriptor;
829  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
830 
831  /* Write the sensor specific data based on the pre-computed register level pairs (Addr, Value) */
832  uint8 buffer[1];
833  for (uint32 reg=0; reg < pConfigSpecific->regCount; reg++)
834  {
835  buffer[0] = pConfigSpecific->regPairs[reg].regValue;
836  if (ISF_SUCCESS != dm_device_write(pDeviceHandle, pConfigSpecific->regPairs[reg].regAddr,&buffer[0],1,1))
837  {
838  return retStat;
839  }
840  }
841 
842  // Set up the Control register #1 functions: ODR override for SLEEP mode and the WAKE mode ODR.
843  buffer[0] = MMA865x_SET_FIELD(DR,mma865x_settings.sampleRateIndex);
844  if (ISF_SUCCESS != dm_device_write(&pDeviceDescriptor->deviceHandle, MMA865x_CTRL_REG1, buffer, 1, 1) ) return MMA865x_ERROR;
845 
846  return ISF_SUCCESS;
847 }
848 
849 /*
850  * Local Subroutines
851  */
852 
853 /*! @brief This function obtains the configuration register values for a given user configuration of the MMA865x.
854  * If the configuration has changed then it returns true and saves the new configuration.
855  *
856  * @param[out] pRequiredSettings update the data period in settings
857  * @param[out] pDeviceDes update the data rate in the generic sensor structure for this device
858  * @return ::mma865x_GetConfig() returns a value of type ::isf_dsa_status_t indicating the success or failure of the function
859  * call.
860  * @retval 0 is returned when the configuration is valid.
861  * @retval 1 is returned when the configuration is modified.
862  *
863  * @Constraints None
864  *
865  * @Reentrant Yes
866  * @Libs lib_fsl_mma865x_i2c_3D_accel.lib
867  */
869 {
870  // Validate the reporting rate.
871  uint8 retStat = 0; // no changes
872  uint8 periodFound = FALSE; // not found
873  uint8 dataRate;
874 
875  // get the number of array entries, which is not the max data rate by rather the size
876  // of the array (number or elements in the array), max index = size - 1
877  uint8 maxDataRateIndex = sizeof fsl_mma865x_period / sizeof fsl_mma865x_period[0];
878 
879  for (dataRate = 0; dataRate < maxDataRateIndex; dataRate++)
880  {
881  if (fsl_mma865x_period[dataRate] == pRequiredSettings->nSamplePeriod)
882  {
883  periodFound = TRUE;
884  break;
885  }
886  }
887 
888  // If a valid reporting rate has not been set, then default to the MMA865x hardware default of 800Hz.
889  if ( !periodFound )
890  {
892  pRequiredSettings->nSamplePeriod = FSL_MMA865x_ACCEL_800HZ;
893  mma865x_settings.sampleRateIndex = maxDataRateIndex - 1; // max range index
894  }
895  else
896  { // copy the rate index to local storage as needed
897  mma865x_settings.sampleRateIndex = dataRate;
898  }
899 
900  return retStat;
901 }
902 
903 /*!
904 ** @}
905 */
906 /* END fsl_mma865x_i2c_3D_accel. */
907 
908 
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_Initialize(isf_SensorHandle_t *pSensorHandle)
isf_status_t dm_channel_start(dm_ChannelDescriptor_t *apChannelDescriptor)
This function starts a channel.
void * pSensorSpecificSettings
This structure defines the dummy DSA data buffer format.
unsigned char uint8
This defines uint8 as unsigned char.
Definition: isf_types.h:18
#define TRUE
Definition: isf_types.h:52
fsl_i2c_master_driver.h defines structures and types for the i2c master driver.
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_EndData(isf_SensorHandle_t *pSensorHandle)
void mma865x_SetMode(int32_t *status, void *pSensorHandle, int32_t Mode)
This function sets the MMA865x part to the requested mode.
Standard fixed type for three axis accelerometers.
The fsl_mma865x_i2c_3D_accel.h file contains the definitions and functions supporting the MMA865x Sen...
void mma865x_Reset(int32_t *status, void *pSensorHandle)
This function resets the MMA865x sensor resulting in a default configuration for all registers...
uint32 isf_time_util_get_usec(void)
This API returns the time in microseconds.
Definition: isf_util.c:52
isf_status_t dm_device_open(dm_ChannelDescriptor_t *apChannelDescriptor, void *apDevice, dm_DeviceDescriptor_t *apDeviceDescriptor)
This function creates a device handle for a device at a specified channel address.
const void * commInfo
isf_dsa_ControlData_t controlData
isf_fifo_status_t isf_fifo_lock(isf_fifo_t *pFifo)
Lock a sample buffer for exclusive access.
Definition: isf_fifo.c:171
comm_State_t dm_channel_get_state(dm_ChannelDescriptor_t *apChannelDescriptor)
This function returns the channel state.
#define MMA865x_WHOAMI_VALUE_8653
Definition: mma865x.h:74
isf_fifo_status_t isf_fifo_unlock(isf_fifo_t *pFifo)
Release the exclusive access lock on a sample buffer.
Definition: isf_fifo.c:202
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_Calibrate(isf_SensorHandle_t *pSensorHandle, void *pCalData)
isf_fifo_status_t isf_fifo_el_increment(isf_fifo_t *pFifo)
Routine increments the insert pointer after direct access.
Definition: isf_fifo.c:240
isf_sensors.h contains the ISF Generic Sensor definitions and data structures required when a client ...
Information necessary to communicate with an I2C slave device.
#define FALSE
Definition: isf_types.h:56
float floatFactor
API definitions, types, and macros for the Intelligent Sensing Framework (ISF) Bus Manager (BM)...
dm_DeviceDescriptor_t deviceHandle
Definition: isf_sensors.h:52
This defines the DSA sensor device handle structure used to invoke the adapter access functions...
The mma865x.h contains the MMA865x Accelerometer register definitions, access macros, and device access functions.
uint8 sampleRateIndex
Holds the ODR index into the rate array fsl_mma865x_period */.
Define the sensor device descriptor.
Definition: isf_sensors.h:49
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_StartData(isf_SensorHandle_t *pSensorHandle)
float floatOffset
LWSEM_STRUCT deviceSemaphore
Definition: isf_sensors.h:54
unsigned long uint32
This defines uint32 as unsigned long.
Definition: isf_types.h:36
isf_dsa_status_t mma865x_GetConfig(isf_dsa_SensorSettings_t *pRequiredSettings, DeviceDescriptor_t *pDeviceDes)
This function obtains the configuration register values for a given user configuration of the MMA865x...
isf_SensorDataTypes_t mma865x_SupportedDataTypes[]
isf_SensorTypes_t
dm_ChannelDescriptor_t cDescriptor
Definition: isf_sensors.h:51
isf_status_t dm_device_write(dm_DeviceDescriptor_t *apDeviceDescriptor, int32 aOffset, uint8 *apWriteBuffer, uint32 aBuffsize, uint32 aNbyteWrite)
This function writes to a device.
isf_status_t bm_unregister_callback(bm_callback_token_t aToken)
This API unregisters one or more callbacks.
#define MMA865x_GET_FIELD(name, val)
Definition: mma865x.h:82
#define BYTES_FOR_FULL_READ
This function retrieves the raw data from the MMA865x sensor.
void mma865x_ValidateConfig(int32_t *status, void *pSensorHandle, void *pSettings)
This function validates the configuration for the MMA865x sensor.
#define MMA865x_SET_FIELD(name, val)
Definition: mma865x.h:81
The isf_types.h file contains the ISF data type definitions and some of the globally used macros...
isf_acceleration_g_fixed_32s1i16_t accel[3]
int32 isf_dsa_status_t
This is the Sensor Manager API return type definition.
isf_SensorTypes_t mma865x_SupportedSensorTypes[]
Supported sensor and data types for MMA865x.
isf_acceleration_g_float_t accel[3]
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_Convert(volatile isf_SensorHandle_t *pSensorHandle, isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType, void *pNativeSample, void *pConvertedSample, int32 *numBytes)
This function coverts the raw sample data to the desired output type.
isf_status_t dm_channel_acquire_lock(dm_ChannelDescriptor_t *apChannelDescriptor, isf_duration_t aTimeout)
This function locks the channel for exclusive access.
The isf_sensor_types.h contains the enumerated list of sensor types used by ISF.
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_Shutdown(isf_SensorHandle_t *pSensorHandle)
#define MMA865x_RST_MASK
Definition: mma865x.h:414
The isf_util.h file contains the utility method declarations and macros.
int32 fixedFactor
enum isf_dsa_result_enums isf_dsa_result_types_t
bm_callback_token_t bm_register_periodic_callback(isf_duration_t aPeriod, bm_callback_t *pCallback, void *pCbData)
This API schedules a callback at the specified period.
void mma865x_CheckId(int32_t *status, void *pSensorHandle)
This function validates the MMA865x sensor is connected via the I2C bus.
void fsl_mma865x_i2c_3D_accel_PeriodicCallback(void *pSensorHandle)
isf_dsa_result_types_t resultFormat
isf_SensorDataTypes_t
isf_status_t dm_channel_init(dm_ChannelId_t aChannelId, dm_ChannelDescriptor_t *apChannelDescriptor)
This function initializes a channel.
isf_dsa_status_t mma865x_GetData(isf_SensorHandle_t *pSensorHandle, void *pBuffer)
isf_status_t bm_stop(bm_callback_token_t aTokens)
This API stops one or more callback(s) by setting them to the inactive state.
const isf_SensorConfig_t * pSensorStaticConfig
isf_status_t bm_start(boolean aSync, bm_callback_token_t aTokens)
This API sets one or more callback(s) to the active state.
uint32 isf_duration_t
ISF time duration in microseconds.
Definition: isf.h:59
long int32
This defines int32 as long.
Definition: isf_types.h:32
isf_SensorDataTypes_t resultType
Main ISF header file. Contains code common to all ISF components.
#define MMA865x_ACTIVE_MASK
Definition: mma865x.h:372
LWEVENT_STRUCT * pEventGroup
short int16
This defines int16 as short.
Definition: isf_types.h:23
The isf_sm_api.h contains the collection of APIs for the Sensor Manager as well as related defines an...
int32 fixedOffset
isf_dsa_AdapterStatus_t adapterStatus
sys_channelId_t channelId
Standard float type for three axis accelerometers.
#define MMA865x_WHOAMI_VALUE_8652
Definition: mma865x.h:73
isf_status_t dm_device_read(dm_DeviceDescriptor_t *apDeviceDescriptor, int32 aOffset, uint8 *apReadBuffer, uint32 aBuffsize, uint32 aNbyteRead)
This function reads from a device.
unsigned short uint16
This defines uint16 as unsigned short.
Definition: isf_types.h:27
#define BM_ERROR
This value specifies a general Bus Manager error. If an error occurs in registering a callback...
Definition: isf_bm.h:57
isf_dsa_status_t mma865x_SetConfig(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pConfigSettings)
This function sets the MMA865x sensor configuration.
int32 isf_status_t
ISF return status type.
Definition: isf.h:51
This defines the DSA sensor configuration parameter structure configuring the sensor settings by a su...
isf_comm.h defines the common types for the Communications Service Family of the Intelligent Sensing ...
isf_dsa_SensorSettings_t sensorSettings
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_Configure(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
isf_dsa_status_t fsl_mma865x_i2c_3D_accel_ValidateSettings(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
This structure defines important settings.
void * isf_fifo_el_get_insert_pointer(isf_fifo_t *pFifo)
Routine returns the insert pointer for direct access.
Definition: isf_fifo.c:232
isf_status_t dm_channel_release_lock(dm_ChannelDescriptor_t *apChannelDescriptor)
This function releases exclusive channel access.
int32 mma865x_status_t
This structure defines a handle for the device.
Definition: isf_devmsg.h:61
const struct @21 mma865xAccelConvTable[]
The isf_accelerometer_types.h file contains the ISF data type definitions for use with the ISF generi...
This structure is a declaration of a channel descriptor type.
Definition: isf_devmsg.h:50
#define ISF_FIFO_FULL
Definition: isf_fifo.h:32
bm_callback_token_t token
Definition: isf_sensors.h:53