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