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