ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
fsl_fxlc95000_i2cspi_pedo.c
Go to the documentation of this file.
1 /**
2  ** @file fsl_fxlc95000.c
3  ** @brief FXLC95000 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 "fxlc95000.h"
23 #include "Ac_Fixed_utils.h"
24 
25 
26 /*! @brief Supported sensor and data types */
29 
30 /* Declare the local helper functions for FXLC95000 */
31 void fxlc95000_ValidateConfig(int32_t *status, isf_SensorHandle_t* pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings);
32 int32 fxlc95000_GetData(int32_t *status, isf_SensorHandle_t* pSensorHandle, void* pBuffer);
35 int32 fxlc95000_read(dm_DeviceDescriptor_t *pDeviceHandle, int32 offset, int32 numBytes, uint8 *pBuffer);
36 int32 fxlc95000_send(dm_DeviceDescriptor_t* pDeviceHandle, const fxlc95000_CommandList_t *aCommandList);
38 
39 static isf_dsa_status_t float_fxlc95000_converter(fxlc95000_Sensor_Specific_Settings_t *pSensorSpecificConfig, fxlc95000_DataBuffer_t *nativeSample, void *vpConvertedSample );
40 static isf_dsa_status_t fixed_fxlc95000_converter(fxlc95000_Sensor_Specific_Settings_t *pSensorSpecificConfig, fxlc95000_DataBuffer_t *nativeSample, void *vpConvertedSample );
41 
42 
43 /*
44  ** ===================================================================
45  ** Method : fsl_fxlc95000_Initialize (component ISFDSA)
46  ** @brief
47  **
48  ** Parameters :
49  ** NAME - DESCRIPTION
50  ** @param
51  ** void* pSensorHandle -
52  ** @return
53  ** int32_t -
54  ** ===================================================================
55  */
57 {
58  int32 st = ISF_SUCCESS;
59 
60  if ( NULL == pSensorHandle){
61  return DSA_ERR_PARAM;
62  }
63 
64 
65  // Check if the sensor is available, and that it has already been initialized.
66  if (pSensorHandle->adapterStatus > DSA_STATE_NOT_INITIALIZED) {
67  return DSA_ERR_INITIALIZE;
68  }
69 
70  // Allocate the adapter specific memory for the descriptor
71  pSensorHandle->pDeviceDescriptor = (fxlc95000_DeviceDescriptor_t *)OSA_MemAllocZero(sizeof(fxlc95000_DeviceDescriptor_t));
72 
73  // Create and validate helper reference pointers.
75 
76  if (NULL == pDeviceDescriptor) {
77  return DSA_ERR_INITIALIZE;
78  }
79 
80  // Allocate the current sample data buffer and initialize it.
81  pDeviceDescriptor->pCurrentSample = OSA_MemAllocZero(sizeof(fxlc95000_DataBuffer_t));
82 
83  dm_ChannelDescriptor_t *pChannelDescriptor = &pDeviceDescriptor->cDescriptor;
84 
85  // Initialize the channel
86  if(ISF_SUCCESS != dm_channel_init(pSensorHandle->pSensorStaticConfig->channelId, pChannelDescriptor)){
87  return DSA_ERR_INITIALIZE;
88  }
89  // Start the Bus
90  if(ISF_SUCCESS != dm_channel_start(pChannelDescriptor)){
91  return DSA_ERR_INITIALIZE;
92  }
93 
94  // Get the Channel state to check if it is ready to use
95  if (COMM_STATE_OK != dm_channel_get_state(pChannelDescriptor)) {
96  return DSA_ERR_INITIALIZE;
97  }
98 
99  // Open the device and get the device handler
100  if(ISF_SUCCESS != dm_device_open(pChannelDescriptor, (void *)pSensorHandle->pSensorStaticConfig->commInfo, &pDeviceDescriptor->deviceHandle)){
101  return DSA_ERR_INITIALIZE;
102  }
103 
104  /* To enable Flash programming we skip initialization commands.
105  * Initialization will be done in Configure after flashing. */
106 
107  // Set the adapter state to be initialized.
108  pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
109 
110  // Create a semaphore to synchronize the device descriptor across tasks
111  if (kStatus_OSA_Success != OSA_SemaCreate(&pDeviceDescriptor->deviceSemaphore, 1)){
112  return DSA_ERR_INITIALIZE;
113  }
114 
115  return st;
116 }
117 
118 /*
119  ** ===================================================================
120  ** Method : fsl_fxlc95000_ValidateSettings (component ISFDSA)
121  ** @brief
122  **
123  ** Parameters :
124  ** NAME - DESCRIPTION
125  ** @param
126  ** void* pSensorHandle -
127  ** @param
128  ** void* pSettings -
129  ** @return
130  ** int32_t -
131  ** ===================================================================
132  */
134 {
135  // Check the input arguments.
136  if ((NULL == pSensorHandle) || (NULL == pSubSettings)) {
137  return DSA_ERR_PARAM;
138  }
139 
140  int32_t status;
141 
142  fxlc95000_ValidateConfig(&status,pSensorHandle,pSubSettings);
143 
144  return status;
145 }
146 
147 /*
148  ** ===================================================================
149  ** Method : fsl_fxlc95000_Configure (component ISFDSA)
150  ** @brief
151  **
152  ** Parameters :
153  ** NAME - DESCRIPTION
154  ** @param
155  ** void* pSensorHandle -
156  ** @param
157  ** void* pConfigSettings -
158  ** @return
159  ** int32_t -
160  ** ===================================================================
161  */
162 //#define DEBUG_READ_ALL_REGS
163 #ifdef DEBUG_READ_ALL_REGS
164 static uint8 debugRegs[18];
165 #endif
167 {
168  // Check pointers.
169  if((NULL == pSensorHandle) || (NULL == pSensorSettings) ){
170  return DSA_ERR_PARAM;
171  }
172 
173  int32_t retStat = DSA_ERR_CONFIGURE; // Return Status
174 
175  // Create helper reference pointers.
178 
179  // Check the device descriptor pointer.
180  if (NULL == pDeviceDescriptor)
181  {
182  return retStat;
183  }
184 
185  // Lock the device descriptor.
186  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
187 
188  // Check the driver state.
189  if (DSA_STATE_INITIALIZED > pSensorHandle->adapterStatus){
190  goto unlockdescriptor;
191  }
192 
193  // A very short delay is needed for device startup.
194  OSA_TimeDelay(100);
195  if ( pConfigSpecific->initializeCommands.numCommands > 0)
196  {
197  if (ISF_SUCCESS != fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->initializeCommands))
198  { // At times when the system comes up slow, this command fails and we need to try once again.
199  OSA_TimeDelay(100);
200  fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->initializeCommands);
201  }
202  }
203 
204  /* Read and save the current configuration state for future use */
205  fxlc95000_GetConfig(pSensorHandle, &pDeviceDescriptor->localCfgBuffer);
206  pDeviceDescriptor->localCfgBuffer.settings.nCurrentReportRate = isf_swap4byte(pSensorSettings->nSamplePeriod);
207  pDeviceDescriptor->localCfgBuffer.settings.nCurrentResolution = pConfigSpecific->resolution;
208  pDeviceDescriptor->localCfgBuffer.settings.nRange = pConfigSpecific->range;
209  pDeviceDescriptor->localCfgBuffer.settings.nMode = pConfigSpecific->mode;
210  fxlc95000_WriteConfig(pSensorHandle, &pDeviceDescriptor->localCfgBuffer);
211  OSA_TimeDelay(1);
212 
213  if ( pConfigSpecific->configureCommands.numCommands > 0)
214  {
215  fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->configureCommands);
216  }
217 
218  if (pDeviceDescriptor->token)
219  {
220  bm_unregister_callback(pDeviceDescriptor->token);
221  }
222  // Register the periodic callback function with the Bus Manager.
223  pDeviceDescriptor->token = bm_register_periodic_callback( pSensorSettings->nSamplePeriod, fsl_fxlc95000_PeriodicCallback , (void *)pSensorHandle);
224  if(BM_ERROR & pDeviceDescriptor->token)
225  {
226  goto unlockdescriptor;
227  }
228 
229  //Store the configured settings
230  pSensorHandle->controlData.sensorSettings = *pSensorSettings;
231 
233  retStat = ISF_SUCCESS;
234 
235 unlockdescriptor:
236 
237  // Unlock the device descriptor.
238  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
239 
240  return retStat;
241 }
242 
243 /*
244  ** ===================================================================
245  ** Method : fsl_fxlc95000_StartData (component ISFDSA)
246  ** @brief
247  **
248  ** Parameters :
249  ** NAME - DESCRIPTION
250  ** @param
251  ** void* pSensorHandle -
252  ** @return
253  ** int32_t -
254  ** ===================================================================
255  */
257 {
258  // Check pointers.
259  if(NULL == pSensorHandle){
260  return DSA_ERR_PARAM;
261  }
262 
263  int32_t retStat = DSA_ERR_START_DATA;
264 
265  // Create helper reference pointers.
266  fxlc95000_DeviceDescriptor_t *pDeviceDescriptor = pSensorHandle->pDeviceDescriptor;
268 
269  // Check the device descriptor.
270  if (NULL == pDeviceDescriptor)
271  return DSA_ERR_PARAM;
272 
273  // Lock the device descriptor.
274  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
275 
276  // Check the driver state.
277  if (DSA_STATE_CONFIGURED_STOPPED > pSensorHandle->adapterStatus)
278  {
279  goto unlockdescriptor;
280  }
281 
282  if ( pConfigSpecific->startDataCommands.numCommands > 0)
283  {
284  fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->startDataCommands);
285  }
286 
287  // Start the Bus Manager sampling timer.
288  if (BM_ERROR & bm_start(FALSE, pDeviceDescriptor->token)) // Check for Bus Manager error and return on failure.
289  goto unlockdescriptor;
290 
291  // Set device to active state.
293  retStat = ISF_SUCCESS;
294 
295 unlockdescriptor:
296  // Unlock the device descriptor.
297  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
298 
299  return retStat;
300 }
301 
302 /*
303  ** ===================================================================
304  ** Method : fsl_fxlc95000_EndData (component ISFDSA)
305  ** @brief
306  **
307  ** Parameters :
308  ** NAME - DESCRIPTION
309  ** @param
310  ** void* pSensorHandle -
311  ** @return
312  ** int32_t -
313  ** ===================================================================
314  */
316 {
317  // Check pointers.
318  if(NULL == pSensorHandle){
319  return DSA_ERR_PARAM;
320  }
321 
322  // Create helper reference pointers.
325 
326  // Set the default return status.
327  int32_t retStat = DSA_ERR_END_DATA;
328 
329  // Check the device descriptor.
330  if (NULL == pDeviceDescriptor)
331  return retStat;
332 
333  // Lock the device descriptor.
334  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
335 
336  // Check the driver state.
337  if (DSA_STATE_CONFIGURED_STOPPED >= pSensorHandle->adapterStatus)
338  goto unlockdescriptor;
339 
340  // Check that the adapter is not already started.
341  if (DSA_STATE_CONFIGURED_STARTED == pSensorHandle->adapterStatus)
342  {
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  goto unlockdescriptor;
347  }
348 
349  if ( pConfigSpecific->endDataCommands.numCommands > 0)
350  {
351  fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->endDataCommands);
352  }
354  retStat = ISF_SUCCESS;
355 
356 unlockdescriptor:
357  // Unlock the device descriptor.
358  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
359 
360  return retStat;
361 }
362 
363 /*
364  ** ===================================================================
365  ** Method : fsl_fxlc95000_Calibrate (component ISFDSA)
366  ** @brief
367  **
368  ** Parameters :
369  ** NAME - DESCRIPTION
370  ** @param
371  ** void* pSensorHandle -
372  ** @return
373  ** int32_t -
374  ** ===================================================================
375  */
377 {
378  int32_t retStat = ISF_SUCCESS;
379 
380  return retStat;
381 }
382 
383 /*
384  ** ===================================================================
385  ** Method : fsl_fxlc95000_Shutdown (component ISFDSA)
386  ** @brief
387  **
388  ** Parameters :
389  ** NAME - DESCRIPTION
390  ** @param
391  ** void* pSensorHandle -
392  ** @return
393  ** int32_t -
394  ** ===================================================================
395  */
397 {
398  if(NULL == pSensorHandle){
399  return DSA_ERR_PARAM;
400  }
401 
402  // Create helper reference pointers.
405 
406  if ( pConfigSpecific->shutdownCommands.numCommands > 0)
407  {
408  fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->shutdownCommands);
409  }
410 
411  // Remove the sensor adapter from the Bus Manager callbacks.
412  bm_unregister_callback(pDeviceDescriptor->token);
413  pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
414 
415  return ISF_SUCCESS;
416 }
417 
418 /*
419  ** ===================================================================
420  ** Method : fsl_fxlc95000_PeriodicCallback (component ISFDSA)
421  ** @brief
422  **
423  ** Parameters :
424  ** NAME - DESCRIPTION
425  ** @param
426  ** void* pSensorHandle -
427  ** @return
428  ** void -
429  ** ===================================================================
430  */
431 void fsl_fxlc95000_PeriodicCallback(void *pSensorHandle)
432 {
433  // Check pointers.
434  if(NULL == pSensorHandle)
435  {
436  return;
437  }
438 
439  int32_t status;
440  int32 numBytes;
441 
442  // Create helper reference pointers.
443  volatile isf_SensorHandle_t *pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
446  isf_status_t st;
447 
448  // Check the device descriptor
449  if (NULL == pDeviceDescriptor)
450  {
451  return;
452  }
453 
454  // Create a helper pointer to the current sample buffer.
455  fxlc95000_DataBuffer_t *pCurrentSampleBuffer = pDeviceDescriptor->pCurrentSample;
456 
457  // Lock the device descriptor.
458  if (kStatus_OSA_Success != OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER))
459  {
460  goto unlockdescriptor;
461  }
462 
463  // Check the driver state.
464  if (DSA_STATE_CONFIGURED_STARTED != pSensorHdl->adapterStatus)
465  {
466  goto unlockdescriptor;
467  }
468 
469  // Get sensor data.
470  fxlc95000_GetData(&status, pSensorHandle, ((uint8*)&pCurrentSampleBuffer->timeStamp));
471 
472  // Lock the fifo for update.
473  if(kStatus_OSA_Success != isf_fifo_lock(pFifo))
474  {
475  return;
476  }
477 
479 
480  // write the new data
482  {
483  *pFifoEntry = *pCurrentSampleBuffer;
484  }
485  else
486  {
488  pSensorHdl,
491  pCurrentSampleBuffer,
492  pFifoEntry,
493  &numBytes);
494  }
495 
496  // Increment the fifo to the next sample entry.
497  st = isf_fifo_el_increment(pFifo);
498 
499  // Unlock the fifo.
500  isf_fifo_unlock(pFifo);
501 
502  if (st == ISF_FIFO_FULL)
503  {
504  // Notify the user using their registered event information
505  OSA_EventSet(pSensorHdl->controlData.pEventGroup,(uint32_t)pSensorHdl->controlData.nEventFieldIndex);
506  }
507 
508 unlockdescriptor:
509  // Unlock the device descriptor.
510  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
511 }
512 
513 /*
514  ** ===================================================================
515  ** Method : fsl_fxlc95000_Flash (component ISFDSA)
516  ** @brief
517  **
518  ** Parameters :
519  ** NAME - DESCRIPTION
520  ** @param
521  ** void* pSensorHandle -
522  ** @param
523  ** void* pFlashData -
524  ** @param
525  ** uint8 numBytes -
526  ** @return
527  ** int32_t -
528  ** ===================================================================
529  */
530 isf_dsa_status_t fsl_fxlc95000_Flash(isf_SensorHandle_t *pSensorHandle, void *pFlashData, uint8 numBytes)
531 {
532  // Check the input arguments.
533  if ((NULL == pSensorHandle) || (NULL == pFlashData))
534  {
535  return DSA_ERR_PARAM;
536  }
537 
538  static bool flashingInProgress = FALSE;
539  int32_t status = ISF_SUCCESS;
542 
543  // Lock the device descriptor.
544  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
545 
546  /* 1-Byte Flash Command are start and stop markers */
547  if(numBytes == 1 && flashingInProgress == FALSE && *((uint8*)pFlashData) == 0x00)
548  { /* Host has sent stating Flash Bytes.*/
549  flashingInProgress = TRUE;
550  status = fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->flashPreprocessCommands);
551  }
552  else if(numBytes == 1 && flashingInProgress == TRUE && *((uint8*)pFlashData) == 0xFF)
553  { /* Host has sent stop flashing bytes, re-initialize Sensor. */
554  flashingInProgress = FALSE;
555  status = fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->flashPostprocessCommands);
556  }
557  else if(numBytes >= 8 && flashingInProgress == TRUE)
558  { /* Prepare CI_WRITE ROM Command. */
559  /* Check if the write command being attempted spans across a page boundary. */
560  #define CI_READ_WRITE_MAX_LEN 32
561  #define CI_READ_WRITE_HDR_LEN 6
562  #define CI_READ_WRITE_ADDR_LEN 2
563  uint8 bytesToPageBoundary = 0xFF - *((uint8*)pFlashData+3) + 1;
564  uint8 bytesAfterPageBoundary = numBytes - bytesToPageBoundary - 4; /* Excluding 4-byte address. */
565 
566  if(bytesToPageBoundary > 0 && bytesToPageBoundary < numBytes - 4)
567  {
568  /* Break it into 2 write commands. */
569  uint8 writeFlashCommand[CI_READ_WRITE_MAX_LEN] = { 0x0A, 0x00, 0x12, 0x34, 0x56, 0x78 };
570  uint8 writeFlashCommand_2[CI_READ_WRITE_MAX_LEN] = { 0x0A, 0x00, 0x12, 0x34, 0x56, 0x78 };
571 
572  writeFlashCommand[1] = (*((uint8*)pFlashData+1) == 0)?0x1F&(bytesToPageBoundary):0xC0|(bytesToPageBoundary);
573  for(int i=0; i<bytesToPageBoundary+CI_READ_WRITE_ADDR_LEN;i++) // 2 LSB Bytes of Address + Data.
574  {
575  writeFlashCommand[CI_READ_WRITE_HDR_LEN+i] = ((uint8*)pFlashData)[CI_READ_WRITE_ADDR_LEN+i];
576  }
577 
578  writeFlashCommand_2[1] = (*((uint8*)pFlashData+1) == 0)?0x1F&(bytesAfterPageBoundary):0xC0|(bytesAfterPageBoundary);
579  uint8 address[] = { *((uint8*)pFlashData+CI_READ_WRITE_ADDR_LEN+1), *((uint8*)pFlashData+CI_READ_WRITE_ADDR_LEN) };
580  *((uint16*)address) += bytesToPageBoundary;
581  writeFlashCommand_2[CI_READ_WRITE_HDR_LEN] = address[1];
582  writeFlashCommand_2[CI_READ_WRITE_HDR_LEN+1] = address[0];
583  for(int i=0; i<bytesAfterPageBoundary;i++)
584  {
585  writeFlashCommand_2[CI_READ_WRITE_HDR_LEN+CI_READ_WRITE_ADDR_LEN+i] = ((uint8*)pFlashData)[4+bytesToPageBoundary+i];
586  }
587 
588  fxlc95000_Command_t writeFlashSequence[] =
589  {
590  {
591  .offset = 0,
592  .nByteWrite = CI_READ_WRITE_HDR_LEN + CI_READ_WRITE_ADDR_LEN + bytesToPageBoundary,
593  .pWriteBuffer = (uint8* const)writeFlashCommand
594  },
595  {
596  .offset = 0,
597  .nByteWrite = CI_READ_WRITE_HDR_LEN + CI_READ_WRITE_ADDR_LEN + bytesAfterPageBoundary,
598  .pWriteBuffer = (uint8* const)writeFlashCommand_2
599  }
600  };
601  fxlc95000_CommandList_t writeflashCommand =
602  {
603  .numCommands = sizeof(writeFlashSequence)/sizeof(fxlc95000_Command_t),
604  .commands = writeFlashSequence
605  };
606 
607  /* Write Flash Bytes received from Host to Sensor. */
608  status = fxlc95000_send(&pDeviceDescriptor->deviceHandle, &writeflashCommand);
609  }
610  else
611  {
612  uint8 writeFlashCommand[CI_READ_WRITE_MAX_LEN] = { 0x0A, 0x00, 0x12, 0x34, 0x56, 0x78 };
613  writeFlashCommand[1] = (*((uint8*)pFlashData+1)== 0)?0x1F&(numBytes-4):0xC0|(numBytes-4); /* Excluding 4-byte address. */
614 
615  for(int i=CI_READ_WRITE_ADDR_LEN; i<numBytes;i++) // Skip 2 MSBs of Address.
616  {
617  writeFlashCommand[CI_READ_WRITE_HDR_LEN+i-CI_READ_WRITE_ADDR_LEN] = ((uint8*)pFlashData)[i];
618  }
619 
620  fxlc95000_Command_t writeFlashSequence[] =
621  {{
622  .offset = 0,
623  .nByteWrite = CI_READ_WRITE_HDR_LEN + numBytes - CI_READ_WRITE_ADDR_LEN,
624  .pWriteBuffer = (uint8* const)writeFlashCommand
625  }};
626  fxlc95000_CommandList_t writeflashCommand =
627  {
628  .numCommands = sizeof(writeFlashSequence)/sizeof(fxlc95000_Command_t),
629  .commands = writeFlashSequence
630  };
631 
632  /* Write Flash Bytes received from Host to Sensor. */
633  status = fxlc95000_send(&pDeviceDescriptor->deviceHandle, &writeflashCommand);
634  }
635 
636  }
637  else
638  { /* Adapter is not in a state to handle the command. Illegal flash sequence. */
639  status = DSA_ERR_BAD_RESULT_TYPE;
640  }
641 
642  // Unlock the device descriptor.
643  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
644 
645  return status;
646 }
647 
648 /*!
649  * @brief This function converts the raw sample data to the desired output type.
650  */
652  (
653  volatile isf_SensorHandle_t *pSensorHandle,
654  isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType,
655  void *pNativeSample,
656  void *pConvertedSample,
657  int32 *numBytes
658  )
659 {
662 
663  pConverter = NULL;
664 
665  switch (convertToType)
666  {
668  if (resultType == DSA_RESULT_TYPE_ENG_FLOAT)
669  pConverter = float_fxlc95000_converter;
670  else if (resultType == DSA_RESULT_TYPE_ENG_FIXED)
671  pConverter = fixed_fxlc95000_converter;
672  break;
673 
674  default:
676  }
677  if (pConverter == NULL)
679 
680  retStat = pConverter(
682  (fxlc95000_DataBuffer_t *)pNativeSample,
683  pConvertedSample
684  );
685 
686  return retStat;
687 }
688 
689 static isf_dsa_status_t float_fxlc95000_converter(fxlc95000_Sensor_Specific_Settings_t *pSensorSpecificConfig, fxlc95000_DataBuffer_t *nativeSample, void *vpConvertedSample )
690 {
691  isf_Acceleration3D_Float_t *convertedSample = (isf_Acceleration3D_Float_t *)vpConvertedSample;
692  convertedSample->timestamp = nativeSample->timeStamp;
693  // These factors and offsets really depend on the configured sensor Range in pSensorSpecificConfig
694  convertedSample->accel[0] = nativeSample->accel[0] * pSensorSpecificConfig->engFloatConversionFactor;
695  convertedSample->accel[1] = nativeSample->accel[1] * pSensorSpecificConfig->engFloatConversionFactor;
696  convertedSample->accel[2] = nativeSample->accel[2] * pSensorSpecificConfig->engFloatConversionFactor;
697 
698  return ISF_SUCCESS;
699 }
700 
701 static isf_dsa_status_t fixed_fxlc95000_converter(fxlc95000_Sensor_Specific_Settings_t *pSensorSpecificConfig, fxlc95000_DataBuffer_t *nativeSample, void *vpConvertedSample )
702 {
703  isf_Acceleration3D_EngFixed_t *convertedSample = (isf_Acceleration3D_EngFixed_t *)vpConvertedSample;
704  isf_Acceleration3D_Float_t floatSample;
705  fxlc95000_Sensor_Specific_Settings_t *pSC = pSensorSpecificConfig; /* just a shorter name - shoud get optimized away */
706  isf_dsa_status_t st;
707 
708  st = float_fxlc95000_converter(pSC, nativeSample, &floatSample);
709 
710  convertedSample->timestamp = floatSample.timestamp;
711  // These factors and offsets really depend on the configured sensor Range in pSensorSpecificConfig
712  convertedSample->accel[0] = float32_To_AcFixed(floatSample.accel[0], pSC->engFixedTotalWidth, pSC->engFixedIntegerWidth, pSC->engFixedSignBit,&st);
713  convertedSample->accel[1] = float32_To_AcFixed(floatSample.accel[1], pSC->engFixedTotalWidth, pSC->engFixedIntegerWidth, pSC->engFixedSignBit,&st);
714  convertedSample->accel[2] = float32_To_AcFixed(floatSample.accel[2], pSC->engFixedTotalWidth, pSC->engFixedIntegerWidth, pSC->engFixedSignBit,&st);
715 
716  return ISF_SUCCESS;
717 }
718 
719 /*!
720  * @brief This function validates the configuration for the MAG3110 sensor.
721  */
722 void fxlc95000_ValidateConfig(int32_t *status, isf_SensorHandle_t* pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings)
723 {
725  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
727 
728  if ( pConfigSpecific->validateSettingsCommands.numCommands > 0)
729  {
730  *status = fxlc95000_send(pDeviceHandle, &pConfigSpecific->validateSettingsCommands);
731  }
732  else
733  {
734  *status = ISF_SUCCESS;
735  }
736 }
737 
738 /*!
739  * @brief This function retrieves the raw data from the MAG3110 sensor.
740  */
741 int32 fxlc95000_GetData(int32_t *status, isf_SensorHandle_t* pSensorHandle, void* pBuffer)
742 {
745  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
747 
748  // Get fsl_fxlc95000 data.
749  retStat = fxlc95000_read
750  (
751  pDeviceHandle,
752  pConfigSpecific->sampleDataOffset,
753  pConfigSpecific->sampleDataSize,
754  pBuffer
755  );
756 
757  return retStat;
758 }
759 
761 {
764  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
766 
767  // Get fsl_fxlc95000 data.
768  if ( pConfigSpecific->readConfigCommands.numCommands > 0)
769  {
770  fxlc95000_send(&pDeviceDescriptor->deviceHandle, &pConfigSpecific->readConfigCommands);
771  }
772 
773  retStat = fxlc95000_read
774  (
775  pDeviceHandle,
776  pConfigSpecific->configRegisterReadOffset,
777  pConfigSpecific->configRegisterReadSize,
778  (uint8*)pConfig
779  );
780 
781  return retStat;
782 }
784 {
787  dm_DeviceDescriptor_t *pDeviceHandle = &pDeviceDescriptor->deviceHandle;
789  uint8 buffer[32];
790  int i,j;
791 
792  for(i=0; i<pConfigSpecific->writeConfigRegisterPreamble.commands->nByteWrite; i++)
793  {
794  buffer[i] = pConfigSpecific->writeConfigRegisterPreamble.commands->pWriteBuffer[i];
795  }
796 
797  for(j=0; j<pConfigSpecific->configRegisterReadSize; i++,j++)
798  {
799  buffer[i] = ((uint8*)pConfig)[j];
800  }
801 
802  if (ISF_SUCCESS != dm_channel_acquire_lock(pDeviceHandle->pChannelDescriptor, 0)) {
803  return -1;
804  }
805 
806  retStat = dm_device_write
807  (
808  pDeviceHandle,
809  0,
810  buffer,
811  i,
812  i
813  );
814 
815  dm_channel_release_lock(pDeviceHandle->pChannelDescriptor);
816 
817  return retStat;
818 }
819 int32 fxlc95000_read(dm_DeviceDescriptor_t *pDeviceHandle, int32 offset, int32 numBytes, uint8 *pBuffer)
820 {
821  int32_t retStat;
822 
823  if (ISF_SUCCESS != dm_channel_acquire_lock(pDeviceHandle->pChannelDescriptor, 0)) {
824  return -1;
825  }
826  retStat = dm_device_read(pDeviceHandle, offset, pBuffer, numBytes, numBytes);
827 
829 
830  return retStat;
831 }
832 
834 {
835  int32_t retStat = 0;
836 
837  // User Notice: This is a required function call. If it is not here, there may be conflicts, data loss,
838  // or any number of undesirable things may occur.
839  if (ISF_SUCCESS != dm_channel_acquire_lock(pDeviceHandle->pChannelDescriptor, 0)) {
840  return -1;
841  }
842 
843  for (uint32 c=0; c < aCommandList->numCommands; c++)
844  {
845  retStat = dm_device_write
846  (
847  pDeviceHandle,
848  aCommandList->commands[c].offset,
849  aCommandList->commands[c].pWriteBuffer,
850  aCommandList->commands[c].nByteWrite,
851  aCommandList->commands[c].nByteWrite
852  );
853  if (retStat != ISF_SUCCESS)
854  {
855  return retStat;
856  }
857  retStat = fxlc95000_waitForCoCo(pDeviceHandle);
858  }
859 
861  return retStat;
862 }
863 
865 {
866  uint8 cmdComplete = 0;
867  int32_t retStat;
868  uint8 buffer[2] = { 0x00, 0xFF };
869 
870  // This call expects that the dm channel lock is already held
871 
872  while (!cmdComplete)
873  {
874  retStat = dm_device_read(pDeviceHandle, 0, buffer, 2, 2);
875  if (retStat != ISF_SUCCESS)
876  {
877  return retStat;
878  }
879  cmdComplete = buffer[1] & 0x80;
880 
881  if (!cmdComplete) OSA_TimeDelay(1);
882  }
883 
884  return buffer[1] & 0x7F;
885 }
886 
887 /*!
888 ** @}
889 */
890 
891 /* END fsl_fxlc95000. */
892 
isf_status_t dm_channel_start(dm_ChannelDescriptor_t *apChannelDescriptor)
This function starts a channel.
void * pSensorSpecificSettings
unsigned char uint8
Definition: isf_types.h:76
#define TRUE
Definition: isf_types.h:82
isf_dsa_status_t fsl_fxlc95000_Calibrate(isf_SensorHandle_t *pSensorHandle, void *pCalData)
uint8 nMode
sensor specific mode
Definition: fxlc95000.h:27
Standard fixed type for three axis accelerometers.
const fxlc95000_CommandList_t startDataCommands
Definition: fxlc95000.h:81
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.
int32 fxlc95000_read(dm_DeviceDescriptor_t *pDeviceHandle, int32 offset, int32 numBytes, uint8 *pBuffer)
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 ...
isf_dsa_status_t fsl_fxlc95000_Flash(isf_SensorHandle_t *pSensorHandle, void *pFlashData, uint8 numBytes)
isf_SensorDataTypes_t fxlc95000_SupportedDataTypes[]
#define FALSE
Definition: isf_types.h:86
uint8 nCurrentResolution
Expected sensor resolution.
Definition: fxlc95000.h:25
dm_ChannelDescriptor_t * pChannelDescriptor
Definition: isf_devmsg.h:62
const fxlc95000_CommandList_t flashPostprocessCommands
Definition: fxlc95000.h:87
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...
fxlc95000_SensorSetting_t settings
Definition: fxlc95000.h:33
This structure defines the dummy DSA data buffer format.
const fxlc95000_CommandList_t flashPreprocessCommands
Definition: fxlc95000.h:86
isf_dsa_status_t fsl_fxlc95000_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.
isf_dsa_status_t fsl_fxlc95000_Configure(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
isf_dsa_status_t fsl_fxlc95000_EndData(isf_SensorHandle_t *pSensorHandle)
isf_SensorTypes_t
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.
The isf_types.h file contains the ISF data type definitions and some of the globally used macros...
int32 fxlc95000_send(dm_DeviceDescriptor_t *pDeviceHandle, const fxlc95000_CommandList_t *aCommandList)
isf_acceleration_g_fixed_32s1i16_t accel[3]
int32 isf_dsa_status_t
This is the Sensor Manager API return type definition.
int32 fxlc95000_GetData(int32_t *status, isf_SensorHandle_t *pSensorHandle, void *pBuffer)
This function retrieves the raw data from the MAG3110 sensor.
isf_acceleration_g_float_t accel[3]
const fxlc95000_Command_t * commands
Definition: fxlc95000.h:60
int32 fxlc95000_WriteConfig(isf_SensorHandle_t *pSensorHandle, fxlc95000_appConfigRegister_t *pConfig)
isf_SensorTypes_t fxlc95000_SupportedSensorTypes[]
Supported sensor and data types.
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.
int32 fxlc95000_GetConfig(isf_SensorHandle_t *pSensorHandle, fxlc95000_appConfigRegister_t *pConfig)
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.
int32 fxlc95000_status_t
Definition: fxlc95000.h:16
uint32 nCurrentReportRate
Sample period in microseconds.
Definition: fxlc95000.h:24
The fxlc95000.h contains the FXLC95000 Intelligent Sensor register definitions, access macros...
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...
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.
isf_dsa_status_t fsl_fxlc95000_ValidateSettings(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings)
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
unsigned short int uint16
Definition: isf_types.h:77
#define CI_READ_WRITE_HDR_LEN
const fxlc95000_CommandList_t validateSettingsCommands
Definition: fxlc95000.h:78
Standard float type for three axis accelerometers.
isf_dsa_status_t fsl_fxlc95000_StartData(isf_SensorHandle_t *pSensorHandle)
fxlc95000_DataBuffer_t * pCurrentSample
isf_dsa_status_t fsl_fxlc95000_Initialize(isf_SensorHandle_t *pSensorHandle)
isf_dsa_status_t fsl_fxlc95000_Shutdown(isf_SensorHandle_t *pSensorHandle)
#define CI_READ_WRITE_ADDR_LEN
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
void fsl_fxlc95000_PeriodicCallback(void *pSensorHandle)
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
uint32 isf_swap4byte(uint32 n)
4 byte swapping method
Definition: isf_util.c:143
void fxlc95000_ValidateConfig(int32_t *status, isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSubSettings)
This function validates the configuration for the MAG3110 sensor.
const fxlc95000_CommandList_t writeConfigRegisterPreamble
Definition: fxlc95000.h:76
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
fxlc95000_appConfigRegister_t localCfgBuffer
This structure defines a handle for the device.
Definition: isf_devmsg.h:61
uint8 nRange
Expected sensor dynamic range.
Definition: fxlc95000.h:26
#define CI_READ_WRITE_MAX_LEN
uint8 fxlc95000_waitForCoCo(dm_DeviceDescriptor_t *pDeviceHandle)
This structure is a declaration of a channel descriptor type.
Definition: isf_devmsg.h:50
#define ISF_FIFO_FULL
Definition: isf_fifo.h:33
const fxlc95000_CommandList_t readConfigCommands
Definition: fxlc95000.h:77
uint8 *const pWriteBuffer
Definition: fxlc95000.h:54