ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
fsl_mma955x_i2cspi_pedo.c
Go to the documentation of this file.
1 /**
2  ** @file fsl_mma955x.c
3  ** @brief mma955x Sensor Adapter source file
4  ** @copyright Copyright (c) 2015, Freescale Semiconductor, Inc.
5  ** @version 01.00
6  ** @brief
7  **
8  */
9 
10 #include <isf.h>
11 #include <isf_types.h>
12 #include <isf_sm_api.h>
13 #include <isf_dsa_adapter.h>
14 #include <isf_bm.h>
15 #include <isf_sensor_types.h>
16 #include <isf_fifo.h>
17 #include <isf_pedometer_types.h>
18 #include <isf_comm.h>
19 #include <isf_util.h>
20 #include <isf_sensors.h>
21 #include "mma955x.h"
23 #include "Ac_Fixed_utils.h"
24 
25 extern void mma955xSetSamplingRate(uint32 index);
26 
27 /*! @brief Supported sensor and data types */
30 
31 /* Declare the local helper functions for mma955x */
32 void mma955x_ValidateConfig(int32_t *status, isf_SensorHandle_t* pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings);
33 int32 mma955x_GetData(int32_t *status, isf_SensorHandle_t* pSensorHandle, void* pBuffer);
36 int32 mma955x_read(dm_DeviceDescriptor_t *pDeviceHandle, int32 offset, int32 numBytes, uint8 *pBuffer);
37 int32 mma955x_send(dm_DeviceDescriptor_t* pDeviceHandle, const mma955x_CommandList_t *aCommandList);
39 
40 static isf_dsa_status_t float_mma955x_converter(mma955x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma955x_DataBuffer_t *nativeSample, void *vpConvertedSample );
41 static isf_dsa_status_t fixed_mma955x_converter(mma955x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma955x_DataBuffer_t *nativeSample, void *vpConvertedSample );
42 
43 
44 /*
45  ** ===================================================================
46  ** Method : fsl_mma955x_Initialize (component ISFDSA)
47  ** @brief
48  **
49  ** Parameters :
50  ** NAME - DESCRIPTION
51  ** @param
52  ** void* pSensorHandle -
53  ** @return
54  ** int32_t -
55  ** ===================================================================
56  */
58 {
59  int32 st = ISF_SUCCESS;
60 
61  if ( NULL == pSensorHandle){
62  return DSA_ERR_PARAM;
63  }
64 
65 
66  // Check if the sensor is available, and that it has already been initialized.
67  if (pSensorHandle->adapterStatus > DSA_STATE_NOT_INITIALIZED) {
68  return ISF_SUCCESS;
69  }
70 
71  // Allocate the adapter specific memory for the descriptor
72  pSensorHandle->pDeviceDescriptor = (mma955x_DeviceDescriptor_t *)OSA_MemAllocZero(sizeof(mma955x_DeviceDescriptor_t));
73 
74  // Create and validate helper reference pointers.
75  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
76 
77  if (NULL == pDeviceDescriptor) {
78  return DSA_ERR_INITIALIZE;
79  }
80 
81  // Allocate the current sample data buffer and initialize it.
82  pDeviceDescriptor->pCurrentSample = OSA_MemAllocZero(sizeof(mma955x_DataBuffer_t));
83 
84  dm_ChannelDescriptor_t *pChannelDescriptor = &pDeviceDescriptor->cDescriptor;
85 
86  // Initialize the channel
87  if(ISF_SUCCESS != dm_channel_init(pSensorHandle->pSensorStaticConfig->channelId, pChannelDescriptor)){
88  return DSA_ERR_INITIALIZE;
89  }
90  // Start the Bus
91  if(ISF_SUCCESS != dm_channel_start(pChannelDescriptor)){
92  return DSA_ERR_INITIALIZE;
93  }
94 
95  // Get the Channel state to check if it is ready to use
96  if (COMM_STATE_OK != dm_channel_get_state(pChannelDescriptor)) {
97  return DSA_ERR_INITIALIZE;
98  }
99 
100  // Open the device and get the device handler
101  if(ISF_SUCCESS != dm_device_open(pChannelDescriptor, (void *)pSensorHandle->pSensorStaticConfig->commInfo, &pDeviceDescriptor->deviceHandle)){
102  return DSA_ERR_INITIALIZE;
103  }
104 
105  // A very short delay is needed for device startup.
106  OSA_TimeDelay(100);
107  {
109 
110  if ( pConfigSpecific->initializeCommands.numCommands > 0)
111  {
112  st = mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->initializeCommands);
113  if (ISF_SUCCESS != st)
114  { // At times when the system comes up slow, this command fails and we need to try once again.
115  OSA_TimeDelay(100);
116  st = mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->initializeCommands);
117  }
118  }
119  /* Read and save the current configuration state for future use */
120  st = mma955x_GetConfig(pSensorHandle, &pDeviceDescriptor->localCfgBuffer);
121  }
122  // Set the adapter state to be initialized.
123  pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
124 
125  // Create a semaphore to synchronize the device descriptor across tasks
126  if (kStatus_OSA_Success != OSA_SemaCreate(&pDeviceDescriptor->deviceSemaphore, 1)){
127  return DSA_ERR_INITIALIZE;
128  }
129 
130  return st;
131 }
132 
133 /*
134  ** ===================================================================
135  ** Method : fsl_mma955x_ValidateSettings (component ISFDSA)
136  ** @brief
137  **
138  ** Parameters :
139  ** NAME - DESCRIPTION
140  ** @param
141  ** void* pSensorHandle -
142  ** @param
143  ** void* pSettings -
144  ** @return
145  ** int32_t -
146  ** ===================================================================
147  */
149 {
150  // Check the input arguments.
151  if ((NULL == pSensorHandle) || (NULL == pSubSettings)) {
152  return DSA_ERR_PARAM;
153  }
154 
155  int32_t status;
156  mma955x_ValidateConfig(&status,pSensorHandle,pSubSettings);
157 
158  return status;
159 }
160 
161 /*
162  ** ===================================================================
163  ** Method : fsl_mma955x_Configure (component ISFDSA)
164  ** @brief
165  **
166  ** Parameters :
167  ** NAME - DESCRIPTION
168  ** @param
169  ** void* pSensorHandle -
170  ** @param
171  ** void* pConfigSettings -
172  ** @return
173  ** int32_t -
174  ** ===================================================================
175  */
176 //#define DEBUG_READ_ALL_REGS
177 #ifdef DEBUG_READ_ALL_REGS
178 static uint8 debugRegs[18];
179 #endif
181 {
182  // Check pointers.
183  if((NULL == pSensorHandle) || (NULL == pSensorSettings) ){
184  return DSA_ERR_PARAM;
185  }
186 
187  int32_t retStat = DSA_ERR_CONFIGURE; // Return Status
188 
189  // Create helper reference pointers.
190  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
192 
193  // Check the device descriptor pointer.
194  if (NULL == pDeviceDescriptor)
195  {
196  return retStat;
197  }
198 
199  // Lock the device descriptor.
200  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
201 
202  // Check the driver state and if it has already been configured.
203  if (DSA_STATE_INITIALIZED > pSensorHandle->adapterStatus){
204  retStat = ISF_SUCCESS;
205  goto unlockdescriptor;
206  }
207 
208  mma955xSetSamplingRate(pSensorSettings->nSamplePeriod);
209 
210  if ( pConfigSpecific->configureCommands.numCommands > 0)
211  {
212  mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->configureCommands);
213  }
214 
215  if (pDeviceDescriptor->token)
216  {
217  bm_unregister_callback(pDeviceDescriptor->token);
218  }
219  // Register the periodic callback function with the Bus Manager.
220  pDeviceDescriptor->token = bm_register_periodic_callback( pSensorSettings->nSamplePeriod, fsl_mma955x_PeriodicCallback , (void *)pSensorHandle);
221  if(BM_ERROR & pDeviceDescriptor->token){
222  goto unlockdescriptor;
223  }
224 
225  //Store the configured settings
226  pSensorHandle->controlData.sensorSettings = *pSensorSettings;
227 
229  retStat = ISF_SUCCESS;
230 
231 unlockdescriptor:
232 
233  // Unlock the device descriptor.
234  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
235 
236  return retStat;
237 }
238 
239 /*
240  ** ===================================================================
241  ** Method : fsl_mma955x_StartData (component ISFDSA)
242  ** @brief
243  **
244  ** Parameters :
245  ** NAME - DESCRIPTION
246  ** @param
247  ** void* pSensorHandle -
248  ** @return
249  ** int32_t -
250  ** ===================================================================
251  */
253 {
254  // Check pointers.
255  if(NULL == pSensorHandle){
256  return DSA_ERR_PARAM;
257  }
258 
259  int32_t retStat = DSA_ERR_START_DATA;
260 
261  // Create helper reference pointers.
262  mma955x_DeviceDescriptor_t *pDeviceDescriptor = pSensorHandle->pDeviceDescriptor;
264 
265  // Check the device descriptor.
266  if (NULL == pDeviceDescriptor)
267  return DSA_ERR_PARAM;
268 
269  // Lock the device descriptor.
270  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
271 
272  // Check the driver state and if it data is already started.
273  if (DSA_STATE_CONFIGURED_STOPPED > pSensorHandle->adapterStatus)
274  {
275  retStat = ISF_SUCCESS;
276  goto unlockdescriptor;
277  }
278 
279  if ( pConfigSpecific->startDataCommands.numCommands > 0)
280  {
281  mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->startDataCommands);
282  }
283 
284  // Start the Bus Manager sampling timer.
285  if (BM_ERROR & bm_start(FALSE, pDeviceDescriptor->token)) // Check for Bus Manager error and return on failure.
286  goto unlockdescriptor;
287 
288  // Set device to active state.
290  retStat = ISF_SUCCESS;
291 
292 unlockdescriptor:
293  // Unlock the device descriptor.
294  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
295 
296 
297  return retStat;
298 }
299 
300 /*
301  ** ===================================================================
302  ** Method : fsl_mma955x_EndData (component ISFDSA)
303  ** @brief
304  **
305  ** Parameters :
306  ** NAME - DESCRIPTION
307  ** @param
308  ** void* pSensorHandle -
309  ** @return
310  ** int32_t -
311  ** ===================================================================
312  */
314 {
315  // Check pointers.
316  if(NULL == pSensorHandle){
317  return DSA_ERR_PARAM;
318  }
319 
320  // Create helper reference pointers.
321  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
323 
324  // Set the default return status.
325  int32_t retStat = DSA_ERR_END_DATA;
326 
327  // Check the device descriptor.
328  if (NULL == pDeviceDescriptor)
329  return retStat;
330 
331  // Lock the device descriptor.
332  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
333 
334  // Check the driver state if it has already been stopped.
335  if (DSA_STATE_CONFIGURED_STOPPED >= pSensorHandle->adapterStatus)
336  {
337  retStat = ISF_SUCCESS;
338  goto unlockdescriptor;
339  }
340 
341  // Check that the adapter is not already started.
342  if (DSA_STATE_CONFIGURED_STARTED == pSensorHandle->adapterStatus) {
343  // If it is, then stop the Bus Manager (BM) sample timer.
344  isf_status_t bmStopStatus = bm_stop(pDeviceDescriptor->token);
345  if (BM_ERROR & bmStopStatus)
346  {
347  retStat = ISF_SUCCESS;
348  goto unlockdescriptor;
349  }
350  }
351 
352  if ( pConfigSpecific->endDataCommands.numCommands > 0)
353  {
354  mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->endDataCommands);
355  }
357  retStat = ISF_SUCCESS;
358 
359 
360 unlockdescriptor:
361  // Unlock the device descriptor.
362  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
363 
364  return retStat;
365 }
366 
367 /*
368  ** ===================================================================
369  ** Method : fsl_mma955x_Calibrate (component ISFDSA)
370  ** @brief
371  **
372  ** Parameters :
373  ** NAME - DESCRIPTION
374  ** @param
375  ** void* pSensorHandle -
376  ** @return
377  ** int32_t -
378  ** ===================================================================
379  */
381 {
382  int32_t retStat = ISF_SUCCESS;
383 
384  return retStat;
385 }
386 
387 /*
388  ** ===================================================================
389  ** Method : fsl_mma955x_Shutdown (component ISFDSA)
390  ** @brief
391  **
392  ** Parameters :
393  ** NAME - DESCRIPTION
394  ** @param
395  ** void* pSensorHandle -
396  ** @return
397  ** int32_t -
398  ** ===================================================================
399  */
401 {
402  if(NULL == pSensorHandle){
403  return DSA_ERR_PARAM;
404  }
405 
406  // Create helper reference pointers.
407  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
409 
410  // Check the driver state if it has already been shutdown.
411  if (DSA_STATE_INITIALIZED == pSensorHandle->adapterStatus)
412  {
413  return ISF_SUCCESS;
414  }
415 
416  if ( pConfigSpecific->shutdownCommands.numCommands > 0)
417  {
418  mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->shutdownCommands);
419  }
420 
421  // Remove the sensor adapter from the Bus Manager callbacks.
422  bm_unregister_callback(pDeviceDescriptor->token);
423  pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
424  return ISF_SUCCESS;
425 }
426 
427 /*
428  ** ===================================================================
429  ** Method : fsl_mma955x_PeriodicCallback (component ISFDSA)
430  ** @brief
431  **
432  ** Parameters :
433  ** NAME - DESCRIPTION
434  ** @param
435  ** void* pSensorHandle -
436  ** @return
437  ** void -
438  ** ===================================================================
439  */
440 void fsl_mma955x_PeriodicCallback(void *pSensorHandle)
441 {
442  // Check pointers.
443  if(NULL == pSensorHandle){
444  return;
445  }
446 
447  int32_t status;
448  int32 numBytes;
449 
450  // Create helper reference pointers.
451  volatile isf_SensorHandle_t *pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
454  isf_status_t st;
455 
456  // Check the device descriptor
457  if (NULL == pDeviceDescriptor){
458  return;
459  }
460 
461  // Create a helper pointer to the current sample buffer.
462  mma955x_DataBuffer_t *pCurrentSampleBuffer = pDeviceDescriptor->pCurrentSample;
463 
464  // Lock the device descriptor.
465  if (kStatus_OSA_Success != OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER))
466  {
467  goto unlockdescriptor;
468  }
469 
470 
471  // Check the driver state.
472  if (DSA_STATE_CONFIGURED_STARTED != pSensorHdl->adapterStatus)
473  {
474  goto unlockdescriptor;
475  }
476 
477  // Get the time stamp
478  pCurrentSampleBuffer->timeStamp = isf_time_util_get_usec();
479  // Get sensor data.
480  mma955x_GetData(&status, pSensorHandle, ((uint8*)&pCurrentSampleBuffer->accel));
482  mma955x_GetPedometerData(pSensorHandle, &pCurrentSampleBuffer->pedometerData);
483 
484  // Lock the fifo for update.
485  if(kStatus_OSA_Success != isf_fifo_lock(pFifo)){
486  return;
487  }
488  void *pFifoEntry = isf_fifo_el_get_insert_pointer(pFifo);
489 
490  // write the new data
492  {
493  ((mma955x_PedometerNativeData_t*)pFifoEntry)->timeStamp = pCurrentSampleBuffer->timeStamp;
494  ((mma955x_PedometerNativeData_t*)pFifoEntry)->pedometerData = pCurrentSampleBuffer->pedometerData;
495  }
496  else
497  {
499  pSensorHdl,
502  pCurrentSampleBuffer,
503  pFifoEntry,
504  &numBytes);
505  }
506 
507  // Increment the fifo to the next sample entry.
508  st = isf_fifo_el_increment(pFifo);
509 
510  // Unlock the fifo.
511  isf_fifo_unlock(pFifo);
512 
513  if (st == ISF_FIFO_FULL)
514  {
515  // Notify the user using their registered event information
516  OSA_EventSet(pSensorHdl->controlData.pEventGroup,(uint32_t)pSensorHdl->controlData.nEventFieldIndex);
517  }
518 
519 unlockdescriptor:
520  // Unlock the device descriptor.
521  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
522 
523 }
524 
525 /*!
526  * @brief This function converts the raw sample data to the desired output type.
527  */
529  (
530  volatile isf_SensorHandle_t *pSensorHandle,
531  isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType,
532  void *pNativeSample,
533  void *pConvertedSample,
534  int32 *numBytes
535  )
536 {
539 
540  pConverter = NULL;
541 
542  switch (convertToType)
543  {
545  if (resultType == DSA_RESULT_TYPE_ENG_FLOAT)
546  pConverter = float_mma955x_converter;
547  else if (resultType == DSA_RESULT_TYPE_ENG_FIXED)
548  pConverter = fixed_mma955x_converter;
549  break;
550 
551  default:
553  }
554  if (pConverter == NULL)
556 
557  retStat = pConverter(
559  (mma955x_DataBuffer_t *)pNativeSample,
560  pConvertedSample
561  );
562 
563  return retStat;
564 }
565 
566 static isf_dsa_status_t float_mma955x_converter(mma955x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma955x_DataBuffer_t *nativeSample, void *vpConvertedSample )
567 {
568  isf_Acceleration3D_Float_t *convertedSample = (isf_Acceleration3D_Float_t *)vpConvertedSample;
569  convertedSample->timestamp = nativeSample->timeStamp;
570  // These factors and offsets really depend on the configured sensor Range in pSensorSpecificConfig
571  convertedSample->accel[0] = nativeSample->accel[0] * pSensorSpecificConfig->engFloatConversionFactor;
572  convertedSample->accel[1] = nativeSample->accel[1] * pSensorSpecificConfig->engFloatConversionFactor;
573  convertedSample->accel[2] = nativeSample->accel[2] * pSensorSpecificConfig->engFloatConversionFactor;
574 
575  return ISF_SUCCESS;
576 }
577 
578 static isf_dsa_status_t fixed_mma955x_converter(mma955x_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma955x_DataBuffer_t *nativeSample, void *vpConvertedSample )
579 {
580  isf_Acceleration3D_EngFixed_t *convertedSample = (isf_Acceleration3D_EngFixed_t *)vpConvertedSample;
581  isf_Acceleration3D_Float_t floatSample;
582  mma955x_Sensor_Specific_Settings_t *pSC = pSensorSpecificConfig; /* just a shorter name - shoud get optimized away */
583  isf_dsa_status_t st;
584 
585  st = float_mma955x_converter(pSC, nativeSample, &floatSample);
586 
587  convertedSample->timestamp = floatSample.timestamp;
588  // These factors and offsets really depend on the configured sensor Range in pSensorSpecificConfig
589  convertedSample->accel[0] = float32_To_AcFixed(floatSample.accel[0], pSC->engFixedTotalWidth, pSC->engFixedIntegerWidth, pSC->engFixedSignBit,&st);
590  convertedSample->accel[1] = float32_To_AcFixed(floatSample.accel[1], pSC->engFixedTotalWidth, pSC->engFixedIntegerWidth, pSC->engFixedSignBit,&st);
591  convertedSample->accel[2] = float32_To_AcFixed(floatSample.accel[2], pSC->engFixedTotalWidth, pSC->engFixedIntegerWidth, pSC->engFixedSignBit,&st);
592 
593  return ISF_SUCCESS;
594 }
595 
596 /*!
597  * @brief This function validates the configuration for the MAG3110 sensor.
598  */
599 void mma955x_ValidateConfig(int32_t *status, isf_SensorHandle_t* pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings)
600 {
601  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
602  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
604 
605  if ( pConfigSpecific->validateSettingsCommands.numCommands > 0)
606  {
607  *status = mma955x_send(pDeviceHandle, &pConfigSpecific->validateSettingsCommands);
608  }
609  else
610  {
611  *status = ISF_SUCCESS;
612  }
613 }
614 
615 /*!
616  * @brief This function retrieves the raw data from the MAG3110 sensor.
617  */
618 int32 mma955x_GetData(int32_t *status, isf_SensorHandle_t* pSensorHandle, void* pBuffer)
619 {
621  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
622  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
624 
625  // Get fsl_mma955x data.
626  retStat = mma955x_read
627  (
628  pDeviceHandle,
629  pConfigSpecific->sampleDataOffset,
630  pConfigSpecific->sampleDataSize,
631  pBuffer
632  );
633 
634  return retStat;
635 }
636 
638 {
640  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
641  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
644 
645  // Get Pedometer data.
646  if ( pConfigSpecific->getPedometerDataCommands.numCommands > 0)
647  {
648  mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->getPedometerDataCommands);
649  }
650 
651  retStat = mma955x_read
652  (
653  pDeviceHandle,
654  pConfigSpecific->commandReadOffset,
655  pConfigSpecific->commandReadSize,
656  (uint8*)&buffer
657  );
658 
659  //Swap bytes for 2 byte fields.
661  buffer.pedometerData.step_count = (buffer.pedometerData.step_count>>8) | (buffer.pedometerData.step_count<<8);
662  buffer.pedometerData.distance = (buffer.pedometerData.distance>>8) | (buffer.pedometerData.distance<<8);
663  buffer.pedometerData.speed = (buffer.pedometerData.speed>>8) | (buffer.pedometerData.speed<<8);
664  buffer.pedometerData.calories = (buffer.pedometerData.calories>>8) | (buffer.pedometerData.calories<<8);
665  buffer.pedometerData.sleep_count = (buffer.pedometerData.sleep_count>>8) | (buffer.pedometerData.sleep_count<<8);
666 
667  *pData = buffer.pedometerData;
668 
669  return retStat;
670 }
671 
673 {
675  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
676  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
679 
680  // Get fsl_mma955x data.
681  if ( pConfigSpecific->readConfigCommands.numCommands > 0)
682  {
683  mma955x_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->readConfigCommands);
684  }
685 
686  retStat = mma955x_read
687  (
688  pDeviceHandle,
689  pConfigSpecific->commandReadOffset,
690  pConfigSpecific->commandReadSize,
691  (uint8*)&buffer
692  );
693 
694  //Swap bytes for 2 byte fields.
695  buffer.configData.sleep_minimum = (buffer.configData.sleep_minimum>>8) | (buffer.configData.sleep_minimum<<8);
696  buffer.configData.sleep_maximum = (buffer.configData.sleep_maximum>>8) | (buffer.configData.sleep_maximum<<8);
699 
700  *pConfig = buffer.configData;
701 
702  return retStat;
703 }
704 
706 {
708  mma955x_DeviceDescriptor_t *pDeviceDescriptor = (mma955x_DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
709  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
711  uint8 buffer[32];
712  int i,j;
713 
714  //Swap bytes for 2 byte fields.
715  pConfig->sleep_minimum = (pConfig->sleep_minimum>>8) | (pConfig->sleep_minimum<<8);
716  pConfig->sleep_maximum = (pConfig->sleep_maximum>>8) | (pConfig->sleep_maximum<<8);
717  pConfig->sleep_count_threshold = (pConfig->sleep_count_threshold>>8) | (pConfig->sleep_count_threshold<<8);
718  pConfig->activity_count_threshold = (pConfig->activity_count_threshold>>8) | (pConfig->activity_count_threshold<<8);
719 
720  for(i=0; i<pConfigSpecific->writeConfigCommands.commands->nByteWrite; i++)
721  {
722  buffer[i] = pConfigSpecific->writeConfigCommands.commands->pWriteBuffer[i];
723  }
724 
725  for(j=0; j<pConfigSpecific->commandReadSize; i++,j++)
726  {
727  buffer[i] = ((uint8*)pConfig)[j];
728  }
729 
730  if (ISF_SUCCESS != dm_channel_acquire_lock(pDeviceHandle->pChannelDescriptor, 0)) {
731  return -1;
732  }
733 
734  retStat = dm_device_write
735  (
736  pDeviceHandle,
737  pConfigSpecific->writeConfigCommands.commands->offset,
738  buffer,
739  i,
740  i
741  );
742 
743  dm_channel_release_lock(pDeviceHandle->pChannelDescriptor);
744 
745  return retStat;
746 }
747 int32 mma955x_read(dm_DeviceDescriptor_t *pDeviceHandle, int32 offset, int32 numBytes, uint8 *pBuffer)
748 {
749  int32_t retStat;
750 
751  if (ISF_SUCCESS != dm_channel_acquire_lock(pDeviceHandle->pChannelDescriptor, 0)) {
752  return -1;
753  }
754  retStat = dm_device_read(pDeviceHandle, offset, pBuffer, numBytes, numBytes);
755 
757 
758  return retStat;
759 }
760 
761 int32 mma955x_send(dm_DeviceDescriptor_t* pDeviceHandle, const mma955x_CommandList_t *aCommandList)
762 {
763  int32_t retStat = 0;
764 
765  // User Notice: This is a required function call. If it is not here, there may be conflicts, data loss,
766  // or any number of undesirable things may occur.
767  if (ISF_SUCCESS != dm_channel_acquire_lock(pDeviceHandle->pChannelDescriptor, 0)) {
768  return -1;
769  }
770 
771  for (uint32 c=0; c < aCommandList->numCommands; c++)
772  {
773  retStat = dm_device_write
774  (
775  pDeviceHandle,
776  aCommandList->commands[c].offset,
777  aCommandList->commands[c].pWriteBuffer,
778  aCommandList->commands[c].nByteWrite,
779  aCommandList->commands[c].nByteWrite
780  );
781  if (retStat != ISF_SUCCESS)
782  {
783  return retStat;
784  }
785  retStat = mma955x_waitForCoCo(pDeviceHandle);
786  }
787 
789  return retStat;
790 }
791 
793 {
794  uint8 cmdComplete = 0;
795  int32_t retStat;
796  uint8 buffer[2] = { 0x00, 0xFF };
797 
798  // This call expects that the dm channel lock is already held
799 
800  while (!cmdComplete)
801  {
802  retStat = dm_device_read(pDeviceHandle, 0, buffer, 2, 2);
803  if (retStat != ISF_SUCCESS)
804  {
805  return retStat;
806  }
807  cmdComplete = buffer[1] & 0x80;
808 
809  if (!cmdComplete) OSA_TimeDelay(1);
810  }
811 
812  return buffer[1] & 0x7F;
813 }
814 
815 /*!
816 ** @}
817 */
818 
819 /* END fsl_mma955x. */
820 
isf_dsa_status_t fsl_mma955x_EndData(isf_SensorHandle_t *pSensorHandle)
isf_status_t dm_channel_start(dm_ChannelDescriptor_t *apChannelDescriptor)
This function starts a channel.
int32 mma955x_GetPedometerData(isf_SensorHandle_t *pSensorHandle, mma955x_PedometerData_t *pData)
void * pSensorSpecificSettings
unsigned char uint8
Definition: isf_types.h:76
Standard fixed type for three axis accelerometers.
isf_dsa_status_t fsl_mma955x_Configure(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
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.
isf_dsa_status_t fsl_mma955x_Calibrate(isf_SensorHandle_t *pSensorHandle, void *pCalData)
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.
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_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
dm_ChannelDescriptor_t * pChannelDescriptor
Definition: isf_devmsg.h:62
API definitions, types, and macros for the Intelligent Sensing Framework (ISF) Bus Manager (BM)...
This defines the DSA sensor device handle structure used to invoke the adapter access functions...
int32 mma955x_send(dm_DeviceDescriptor_t *pDeviceHandle, const mma955x_CommandList_t *aCommandList)
isf_dsa_status_t fsl_mma955x_Convert(volatile isf_SensorHandle_t *pSensorHandle, isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType, void *pNativeSample, void *pConvertedSample, int32 *numBytes)
This function converts the raw sample data to the desired output type.
int32 mma955x_GetData(int32_t *status, isf_SensorHandle_t *pSensorHandle, void *pBuffer)
This function retrieves the raw data from the MAG3110 sensor.
int32 mma955x_read(dm_DeviceDescriptor_t *pDeviceHandle, int32 offset, int32 numBytes, uint8 *pBuffer)
void fsl_mma955x_PeriodicCallback(void *pSensorHandle)
isf_SensorTypes_t
void mma955xSetSamplingRate(uint32 index)
Definition: MMA955x_1.c:221
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.
int32 mma955x_WriteConfig(isf_SensorHandle_t *pSensorHandle, mma955x_appConfigRegister_t *pConfig)
isf_dsa_status_t fsl_mma955x_Initialize(isf_SensorHandle_t *pSensorHandle)
The isf_types.h file contains the ISF data type definitions and some of the globally used macros...
isf_dsa_status_t fsl_mma955x_ValidateSettings(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings)
isf_acceleration_g_fixed_32s1i16_t accel[3]
isf_dsa_status_t fsl_mma955x_Shutdown(isf_SensorHandle_t *pSensorHandle)
int32 isf_dsa_status_t
This is the Sensor Manager API return type definition.
mma955x_PedometerData_t pedometerData
isf_acceleration_g_float_t accel[3]
This structure defines the Pedometer sample data buffer format.
uint8 *const pWriteBuffer
Definition: mma955x.h:61
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.
The isf_util.h file contains the utility method declarations and macros.
enum isf_dsa_result_enums isf_dsa_result_types_t
uint32 nByteWrite
Definition: mma955x.h:60
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.
uint16 pedometer_status_register
Definition: mma955x.h:19
mma955x_appConfigRegister_t localCfgBuffer
int32 mma955x_status_t
Definition: mma955x.h:15
dm_DeviceDescriptor_t deviceHandle
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.
uint32 float32_To_AcFixed(float fN, uint8 W, uint8 I, uint8 S, int32 *status)
This function converts an IEEE-754 32-bit floating point number into a fixed point integer format...
const mma955x_CommandList_t startDataCommands
Definition: mma955x.h:85
This structure defines the Pedometer Native data buffer format.
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.
uint8 mma955x_waitForCoCo(dm_DeviceDescriptor_t *pDeviceHandle)
mma955x_appConfigRegister_t configData
Definition: mma955x.h:54
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.
isf_SensorDataTypes_t resultType
Main ISF header file. Contains code common to all ISF components.
The isf_sm_api.h contains the collection of APIs for the Sensor Manager as well as related defines an...
isf_dsa_AdapterStatus_t adapterStatus
signed long int int32
Definition: isf_types.h:74
sys_channelId_t channelId
Standard float type for three axis accelerometers.
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_SensorDataTypes_t mma955x_SupportedDataTypes[]
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...
const mma955x_Command_t * commands
Definition: mma955x.h:67
isf_comm.h defines the common types for the Communications Service Family of the Intelligent Sensing ...
isf_dsa_SensorSettings_t sensorSettings
int32 mma955x_GetConfig(isf_SensorHandle_t *pSensorHandle, mma955x_appConfigRegister_t *pConfig)
The mma955x.h contains the mma955x Intelligent Sensor register definitions, access macros...
isf_dsa_status_t fsl_mma955x_StartData(isf_SensorHandle_t *pSensorHandle)
void * isf_fifo_el_get_insert_pointer(isf_fifo_t *pFifo)
Routine returns the insert pointer for direct access.
Definition: isf_fifo.c:229
mma955x_DataBuffer_t * pCurrentSample
isf_status_t dm_channel_release_lock(dm_ChannelDescriptor_t *apChannelDescriptor)
This function releases exclusive channel access.
isf_SensorTypes_t mma955x_SupportedSensorTypes[]
Supported sensor and data types.
unsigned long int uint32
Definition: isf_types.h:78
const float engFloatConversionFactor
Definition: mma955x.h:76
mma955x_PedometerData_t pedometerData
Definition: mma955x.h:48
This structure defines a handle for the device.
Definition: isf_devmsg.h:61
void mma955x_ValidateConfig(int32_t *status, isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings)
This function validates the configuration for the MAG3110 sensor.
const mma955x_CommandList_t initializeCommands
Definition: mma955x.h:83
This structure is a declaration of a channel descriptor type.
Definition: isf_devmsg.h:50
dm_ChannelDescriptor_t cDescriptor
#define ISF_FIFO_FULL
Definition: isf_fifo.h:33