ISF  2.2 rev 5
Intelligent Sensing Framework for Kinetis with Processor Expert
fsl_Sensor_Custom_1_Functions.c
Go to the documentation of this file.
1 /* ###################################################################
2 ** Filename : fsl_Sensor_Custom_1_Functions.c
3 ** Project : ISF2P2_K64F_MQX_PROJ
4 ** Processor : MK64FN1M0VLL12
5 ** Component : Events
6 ** Version : Driver 01.00
7 ** Compiler : GNU C Compiler
8 ** Date/Time : 2016-10-06, 13:38, # CodeGen: 0
9 ** Abstract :
10 ** This is user's event module.
11 ** Put your event handler code here.
12 ** Settings :
13 ** Contents :
14 ** Custom_1__Void_ - void Custom_1__Void_(void);
15 ** Custom_1_fsl_Sensor_Custom_Convert - void Custom_1_fsl_Sensor_Custom_Convert(void);
16 ** Custom_1_fsl_Sensor_Custom_PeriodicCallback - void Custom_1_fsl_Sensor_Custom_PeriodicCallback(void);
17 ** Custom_1_fsl_Sensor_Custom_Shutdown - void Custom_1_fsl_Sensor_Custom_Shutdown(void);
18 ** Custom_1_fsl_Sensor_Custom_EndData - void Custom_1_fsl_Sensor_Custom_EndData(void);
19 ** Custom_1_fsl_Sensor_Custom_StartData - void Custom_1_fsl_Sensor_Custom_StartData(void);
20 ** Custom_1_fsl_Sensor_Custom_Configure - void Custom_1_fsl_Sensor_Custom_Configure(void);
21 ** Custom_1_fsl_Sensor_Custom_ValidateSettings - void Custom_1_fsl_Sensor_Custom_ValidateSettings(void);
22 ** Custom_1_fsl_Sensor_Custom_Initialize - void Custom_1_fsl_Sensor_Custom_Initialize(void);
23 **
24 ** ###################################################################*/
25 /*!
26 ** @file fsl_Sensor_Custom_1_Functions.c
27 ** @version 01.00
28 ** @brief
29 ** This is user's event module.
30 ** Put your event handler code here.
31 */
32 /*!
33 ** @addtogroup fsl_Sensor_Custom_1_Functions_module fsl_Sensor_Custom_1_Functions module documentation
34 ** @{
35 */
36 /* MODULE fsl_Sensor_Custom_1_Functions */
37 
38 #include "Cpu.h"
39 #include "Events.h"
40 #include "rtos_main_task.h"
41 #include "os_tasks.h"
42 #include "App1_Functions.h"
43 #include "BasicApp1_Functions.h"
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 
52 /* User includes (#include below this line is not maintained by Processor Expert) */
53 
54 
56 {} /* This function is a place holder used to contain TypeDefs and not used. */
57 
58 /*! @brief Define the WHO_AM_I register values for both possible sensor variations. */
59 const uint8_t Custom_1_Who_Am_I_Values[] = {0x4A, 0x5A};
60 
61 /* Updated as per MMA865x sensor configuration. */
63 {
64  .sensorFullScaleRange = 0, // Capture the accelerometer full-scale range for use in conversion routines.
65  .regCount = 2, // Number of register pairs to follow
66  .regPairs =
67  { // System and Control registers.
68  { Custom_1_CTRL_REG1, (Custom_1_SET_FIELD(ASLP_RATE,0)) | (Custom_1_SET_FIELD(DR,0)) | (Custom_1_SET_FIELD(FREAD,0)) },
69  { Custom_1_CTRL_REG2, (Custom_1_SET_FIELD(ST,0)) | (Custom_1_SET_FIELD(RST,0)) | (Custom_1_SET_FIELD(SMODS,0)) | (Custom_1_SET_FIELD(SLPE,0)) | (Custom_1_SET_FIELD(MODS,0)) }
70  }
71 };
72 
73 /*! @brief User specified settings that may change */
74 static Custom_1_Rate_Specific_Settings_t custom_1_settings;
75 
76 #define Custom_1_CONVERT_FLOAT_FACTOR 0.000061
77 #define Custom_1_CONVERT_FIXED_FACTOR 4
78 
79 /*! Updated as per MMA865x device settings. */
80 const i2c_device_t Custom_1_DeviceInfo =
81 {
82  .baudRate_kbps = 400, // default baud rate (kbps).
83  .address = 29 // instance specific device address.
84 };
85 
86 /*! @brief Supported sensor and data types for the Sensor */
88 { /* Updated type for MMA865x Sensor. */
90 };
91 
93 { /* Updated data type for MMA865x Sensor. */
95 };
96 
98 {
99  Custom_1_SensorData_Float_t *convertedSample = (Custom_1_SensorData_Float_t *)vpConvertedSample;
100  convertedSample->timestamp = nativeSample->timeStamp;
101  // Convert the sample based on the full-scale range.
102  convertedSample->sensorData[0] = nativeSample->data[0] * Custom_1_CONVERT_FLOAT_FACTOR;
103  convertedSample->sensorData[1] = nativeSample->data[1] * Custom_1_CONVERT_FLOAT_FACTOR;
104  convertedSample->sensorData[2] = nativeSample->data[2] * Custom_1_CONVERT_FLOAT_FACTOR;
105 
106  return ISF_SUCCESS;
107 }
108 
110 {
111  Custom_1_SensorData_EngFixed_t *convertedSample = (Custom_1_SensorData_EngFixed_t *)vpConvertedSample;
112  convertedSample->timestamp = nativeSample->timeStamp;
113  // Convert the sample based on the full-scale range.
114  convertedSample->sensorData[0] = nativeSample->data[0] * Custom_1_CONVERT_FIXED_FACTOR;
115  convertedSample->sensorData[1] = nativeSample->data[1] * Custom_1_CONVERT_FIXED_FACTOR;
116  convertedSample->sensorData[2] = nativeSample->data[2] * Custom_1_CONVERT_FIXED_FACTOR;
117 
118  return ISF_SUCCESS;
119 }
120 
121 /* Warning: Do not edit the wraper functions below, all changes should be confined to the actual functions defined later. */
122 isf_dsa_status_t fsl_Sensor_Custom_1_Convert(volatile isf_SensorHandle_t* pSensorHandle, isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType, void* pNativeSample, void* pConvertedSample, int32_t *numBytes)
123 {
124  uint8_t retVal = 0;
125  Custom_1_fsl_Sensor_Custom_Convert(pSensorHandle, convertToType, resultType, pNativeSample, pConvertedSample, numBytes, &retVal);
126  return retVal;
127 }
129 {
130  uint8_t retVal = 0;
131  Custom_1_fsl_Sensor_Custom_Configure(pSensorHandle, pSensorSettings, &retVal);
132  return retVal;
133 }
135 {
136  uint8_t retVal = 0;
137  Custom_1_fsl_Sensor_Custom_ValidateSettings(pSensorHandle, pSensorSettings, &retVal);
138  return retVal;
139 }
141 {
142  uint8_t retVal = 0;
143  Custom_1_fsl_Sensor_Custom_Initialize(pSensorHandle, &retVal);
144  return retVal;
145 }
147 {
148  uint8_t retVal = 0;
149  Custom_1_fsl_Sensor_Custom_StartData(pSensorHandle, &retVal);
150  return retVal;
151 }
153 {
154  uint8_t retVal = 0;
155  Custom_1_fsl_Sensor_Custom_EndData(pSensorHandle, &retVal);
156  return retVal;
157 }
159 {
160  uint8_t retVal = 0;
161  Custom_1_fsl_Sensor_Custom_Shutdown(pSensorHandle, &retVal);
162  return retVal;
163 }
164 
165 
166 /*
167 ** ===================================================================
168 ** Event : Custom_1_fsl_Sensor_Custom_Convert (module fsl_Sensor_Custom_1_Functions)
169 **
170 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
171 ** @brief
172 ** Parameters :
173 ** NAME - DESCRIPTION
174 ** @param
175 ** @param
176 ** @param
177 ** @param
178 ** @param
179 ** @param
180 ** @param
181 ** @return
182 ** Returns : Nothing
183 ** ===================================================================
184 */
185 void Custom_1_fsl_Sensor_Custom_Convert(volatile void* pSensorHandle, uint32_t convertToType, uint8_t resultType, void* pNativeSample, void* pConvertedSample, int32_t *numBytes, uint8_t *retVal)
186 {
189 
190  pConverter = NULL;
191  switch (convertToType)
192  {
194  if (resultType == DSA_RESULT_TYPE_ENG_FLOAT)
195  {
196  pConverter = Custom_1_float_Custom_converter;
197  }
198  else if (resultType == DSA_RESULT_TYPE_ENG_FIXED)
199  {
200  pConverter = Custom_1_fixed_Custom_converter;
201  }
202  break;
203 
204  default:
206  }
207  if (pConverter == NULL)
208  {
209  *retVal = DSA_ERR_BAD_RESULT_TYPE;
210  }
211 
212  retStat = pConverter(
213  (Custom_1_Sensor_Specific_Settings_t*)((isf_SensorHandle_t*)pSensorHandle)->pSensorStaticConfig->pSensorSpecificSettings,
214  (Custom_1_DataBuffer_t *)pNativeSample, pConvertedSample);
215 
216  *retVal = retStat;
217 }
218 
219 /*
220 ** ===================================================================
221 ** Event : Custom_1_fsl_Sensor_Custom_PeriodicCallback (module fsl_Sensor_Custom_1_Functions)
222 **
223 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
224 ** @brief
225 ** Parameters :
226 ** NAME - DESCRIPTION
227 ** @param
228 ** @return
229 ** Returns : Nothing
230 ** ===================================================================
231 */
233 {
234  // Check the input argument
235  if (NULL == pSensorHandle)
236  {
237  return;
238  }
239 
240  // Get the device descriptor and specific settings
241  volatile isf_SensorHandle_t *pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
243  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHdl->pDeviceDescriptor;
244  isf_status_t st;
245  int32 numBytes;
246 
247  // Check the device descriptor
248  if (NULL == pDeviceDescriptor)
249  {
250  return;
251  }
252 
253  // Create a helper pointer to the current sample buffer.
254  Custom_1_DataBuffer_t *pCurrentSampleBuffer = (Custom_1_DataBuffer_t *)pDeviceDescriptor->pCurrentSample;
255 
256  // Apply sample skip if necessary.
257  if (pDeviceDescriptor->skipFramecnt)
258  {// Not done skipping samples.
259  --pDeviceDescriptor->skipFramecnt;
260  return;
261  }
262 
263  // Get the time stamp
264  pCurrentSampleBuffer->timeStamp = isf_time_util_get_usec();
265 
266  // Lock the device descriptor.
267  if (kStatus_OSA_Success != OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER))
268  {
269  goto unlockdescriptor;
270  }
271 
272  // Check the driver state.
273  if (DSA_STATE_CONFIGURED_STARTED != pSensorHdl->adapterStatus)
274  {
275  goto unlockdescriptor;
276  }
277 
278  #define BYTES_FOR_FULL_READ 7
279  // Get sensor data.
280  uint16* pSensorData = (uint16*)&pCurrentSampleBuffer->data[0];
281  uint8 registerData[BYTES_FOR_FULL_READ];
282 
283  // read the full width of the data for both full or short reads
285  { // If there is any data, then format and copy it to the output buffer.
286  if ( Custom_1_GET_FIELD(ZYXDR, registerData[0] ) > 0 )
287  { // collect the full 16-bit data for each of x,y,z
288  pSensorData[0] = ((int16)registerData[1])<<8 | registerData[2];
289  pSensorData[1] = ((int16)registerData[3])<<8 | registerData[4];
290  pSensorData[2] = ((int16)registerData[5])<<8 | registerData[6];
291  }
292  }
293  // Lock the fifo for update.
294  if(kStatus_OSA_Success != isf_fifo_lock(pFifo))
295  {
296  return;
297  }
298 
300  // write the new data
302  {
303  *pFifoEntry = *pCurrentSampleBuffer;
304  }
305  else
306  {
308  pSensorHdl,
311  pCurrentSampleBuffer, pFifoEntry, &numBytes);
312  }
313 
314  // Increment the fifo to the next sample entry.
315  st = isf_fifo_el_increment(pFifo);
316  // Unlock the fifo.
317  isf_fifo_unlock(pFifo);
318  if (st == ISF_FIFO_FULL)
319  { // Notify the user using their registered event information
320  OSA_EventSet(pSensorHdl->controlData.pEventGroup,(uint32_t)pSensorHdl->controlData.nEventFieldIndex);
321  }
322 
323  unlockdescriptor:
324  // Unlock the device descriptor.
325  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
326 }
327 /*
328 ** ===================================================================
329 ** Event : Custom_1_fsl_Sensor_Custom_Shutdown (module fsl_Sensor_Custom_1_Functions)
330 **
331 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
332 ** @brief
333 ** Parameters :
334 ** NAME - DESCRIPTION
335 ** @param
336 ** @param
337 ** @return
338 ** Returns : Nothing
339 ** ===================================================================
340 */
341 void Custom_1_fsl_Sensor_Custom_Shutdown(void* pSensorHandle, uint8_t *retVal)
342 {
343  if(NULL == pSensorHandle)
344  {
345  *retVal = DSA_ERR_PARAM;
346  }
347 
348  // Create helper reference pointers.
349  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)((isf_SensorHandle_t *)pSensorHandle)->pDeviceDescriptor;
350 
351  // Remove the sensor adapter from the Bus Manager callbacks.
352  bm_unregister_callback(pDeviceDescriptor->token);
353  ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus = DSA_STATE_INITIALIZED;
354 
355  *retVal = ISF_SUCCESS;
356 }
357 
358 /*
359 ** ===================================================================
360 ** Event : Custom_1_fsl_Sensor_Custom_EndData (module fsl_Sensor_Custom_1_Functions)
361 **
362 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
363 ** @brief
364 ** Parameters :
365 ** NAME - DESCRIPTION
366 ** @param
367 ** @param
368 ** @return
369 ** Returns : Nothing
370 ** ===================================================================
371 */
372 void Custom_1_fsl_Sensor_Custom_EndData(void* pSensorHandle, uint8_t *retVal)
373 {
374  // Check pointers.
375  if(NULL == pSensorHandle)
376  {
377  *retVal = DSA_ERR_PARAM;
378  }
379 
380  int32_t status = ISF_SUCCESS;
381  uint8 buffer[1] = {0x00};
382  // Set the default return status.
383  int32_t retStat = DSA_ERR_END_DATA;
384 
385  // Create helper reference pointers.
386  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)((isf_SensorHandle_t *)pSensorHandle)->pDeviceDescriptor;
387 
388  // Check the device descriptor.
389  if (NULL == pDeviceDescriptor)
390  {
391  *retVal = retStat;
392  }
393 
394  // Lock the device descriptor.
395  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
396 
397  // Check the driver state.
398  if (DSA_STATE_CONFIGURED_STOPPED >= ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus)
399  {
400  goto unlockdescriptor;
401  }
402 
403  // Check that the adapter is not already started.
404  if (DSA_STATE_CONFIGURED_STARTED == ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus)
405  {
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  {
410  goto unlockdescriptor;
411  }
412  }
413 
414  // Set the sensor into STANDBY mode for configuration.
415  if (ISF_SUCCESS != dm_device_read(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1, buffer, 1, 1))
416  {
417  status = SENSOR_ERROR_SETMODE;
418  }
419  buffer[0] = (buffer[0] & ~Custom_1_ACTIVE_MASK);
420  if (ISF_SUCCESS != dm_device_write(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1, buffer, 1, 1))
421  {
422  status = SENSOR_ERROR_SETMODE;
423  }
424  if (ISF_SUCCESS != status)
425  {
426  goto unlockdescriptor;
427  }
428  ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus = DSA_STATE_CONFIGURED_STOPPED;
429  retStat = ISF_SUCCESS;
430 
431  unlockdescriptor:
432  // Unlock the device descriptor.
433  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
434 
435  *retVal = retStat;
436 }
437 
438 /*
439 ** ===================================================================
440 ** Event : Custom_1_fsl_Sensor_Custom_StartData (module fsl_Sensor_Custom_1_Functions)
441 **
442 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
443 ** @brief
444 ** Parameters :
445 ** NAME - DESCRIPTION
446 ** @param
447 ** @param
448 ** @return
449 ** Returns : Nothing
450 ** ===================================================================
451 */
452 void Custom_1_fsl_Sensor_Custom_StartData(void* pSensorHandle, uint8_t *retVal)
453 {
454  // Check pointers.
455  if(NULL == pSensorHandle)
456  {
457  *retVal = DSA_ERR_PARAM;
458  }
459 
460  int32_t status = ISF_SUCCESS;
461  uint8 buffer[1] = {0x00};
462  int32_t retStat = DSA_ERR_START_DATA;
463 
464  // Create helper reference pointers.
465  DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)((isf_SensorHandle_t *)pSensorHandle)->pDeviceDescriptor;
466 
467  // Check the device descriptor.
468  if (NULL == pDeviceDescriptor)
469  {
470  *retVal = DSA_ERR_PARAM;
471  }
472 
473  // Lock the device descriptor.
474  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
475 
476  // Check the driver state.
477  if (DSA_STATE_CONFIGURED_STOPPED > ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus)
478  {
479  goto unlockdescriptor;
480  }
481 
482  // Set the sensor into ACTIVE mode for operation.
483  // this sensor requires read-modify-write
484  if (ISF_SUCCESS != dm_device_read(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1, buffer, 1, 1))
485  {
486  status = SENSOR_ERROR_SETMODE;
487  return;
488  }
489  buffer[0] = (buffer[0] | Custom_1_ACTIVE_MASK);
490  if (ISF_SUCCESS != dm_device_write(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1, buffer, 1, 1))
491  {
492  status = SENSOR_ERROR_SETMODE;
493  return;
494  }
495  // Read the value of the status register to wait for valid data
496  buffer[0] = 0;
497  do {
498  if (ISF_SUCCESS != dm_device_read(&pDeviceDescriptor->deviceHandle, Custom_1_STATUS_00, buffer, 1, 1))
499  {
500  status = SENSOR_ERROR_SETMODE;
501  return;
502  }
503  OSA_TimeDelay(1); // short delay to yield CPU/RTOS.
504  } while (0 == Custom_1_GET_FIELD(ZYXDR,buffer[0]) ); // Wait for Data Ready.
505  if (ISF_SUCCESS != status)
506  {
507  goto unlockdescriptor;
508  }
509 
510  // Start the Bus Manager sampling timer.
511  if (BM_ERROR & bm_start(FALSE, pDeviceDescriptor->token)) // Check for Bus Manager error and return on failure.
512  {
513  goto unlockdescriptor;
514  }
515  // Set device to active state.
516  ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus = DSA_STATE_CONFIGURED_STARTED;
517  retStat = ISF_SUCCESS;
518 
519  unlockdescriptor:
520  // Unlock the device descriptor.
521  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
522 
523  *retVal = retStat;
524 }
525 
526 /*
527 ** ===================================================================
528 ** Event : Custom_1_fsl_Sensor_Custom_Configure (module fsl_Sensor_Custom_1_Functions)
529 **
530 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
531 ** @brief
532 ** Parameters :
533 ** NAME - DESCRIPTION
534 ** @param
535 ** @param
536 ** @param
537 ** @return
538 ** Returns : Nothing
539 ** ===================================================================
540 */
541 void Custom_1_fsl_Sensor_Custom_Configure(void* pSensorHandle, void* pSensorSettings, uint8_t *retVal)
542 {
543  // Check pointers.
544  if((NULL == pSensorHandle) || (NULL == pSensorSettings))
545  {
546  *retVal = DSA_ERR_PARAM;
547  }
548 
549  int32_t status = ISF_SUCCESS;
550  uint8 buffer[1] = {0x00};
551  int32_t retStat = DSA_ERR_CONFIGURE; // Return Status
552 
553  // Create helper reference pointers.
554  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*) ((isf_SensorHandle_t *)pSensorHandle)->pDeviceDescriptor;
555 
556  // Check the device descriptor pointer.
557  if (NULL == pDeviceDescriptor)
558  {
559  *retVal = retStat;
560  }
561 
562  // Lock the device descriptor.
563  OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
564 
565  // Check the driver state.
566  if (DSA_STATE_INITIALIZED > ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus)
567  {
568  goto unlockdescriptor;
569  }
570 
571  // User Notice: This is a required function call. If it is not here, there may be conflicts, data loss,
572  // or any number of undesirable things may occur.
573  if (ISF_SUCCESS != dm_channel_acquire_lock(&pDeviceDescriptor->cDescriptor, 0))
574  {
575  goto unlockdescriptor;
576  }
577 
578  // Set the sensor into STANDBY mode for configuration.
579  if (ISF_SUCCESS != dm_device_read(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1, buffer, 1, 1))
580  {
581  status = SENSOR_ERROR_SETMODE;
582  }
583  buffer[0] = (buffer[0] & ~Custom_1_ACTIVE_MASK);
584  if (ISF_SUCCESS != dm_device_write(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1, buffer, 1, 1))
585  {
586  status = SENSOR_ERROR_SETMODE;
587  }
588  if (ISF_SUCCESS != status)
589  {
590  goto unlockchannel;
591  }
592  // Configure the sensor.
593  buffer[0] = (Custom_1_SET_FIELD(ASLP_RATE,0)) | (Custom_1_SET_FIELD(DR,0)) | (Custom_1_SET_FIELD(FREAD,0));
594  if (ISF_SUCCESS != dm_device_write(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1,&buffer[0],1,1))
595  {
596  retStat = DSA_ERR_CONFIGURE;
597  }
598  buffer[0] = (Custom_1_SET_FIELD(ST,0)) | (Custom_1_SET_FIELD(RST,0)) | (Custom_1_SET_FIELD(SMODS,0)) | (Custom_1_SET_FIELD(SLPE,0)) | (Custom_1_SET_FIELD(MODS,0));
599  if (ISF_SUCCESS != dm_device_write(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG2,&buffer[0],1,1))
600  {
601  retStat = DSA_ERR_CONFIGURE;
602  }
603 
604  // Set up the Control register #1 functions: ODR override for SLEEP mode and the WAKE mode ODR.
605  buffer[0] = Custom_1_SET_FIELD(DR,custom_1_settings.sampleRateIndex);
606  if (ISF_SUCCESS != dm_device_write(&pDeviceDescriptor->deviceHandle, Custom_1_CTRL_REG1, buffer, 1, 1))
607  {
608  retStat = Custom_1_ERROR;
609  }
610  retStat = ISF_SUCCESS;
611 
612  if (ISF_SUCCESS != retStat)
613  {
614  goto unlockchannel;
615  }
616 
617  // Register the periodic callback function with the Bus Manager.
618  pDeviceDescriptor->token = bm_register_periodic_callback(((isf_dsa_SensorSettings_t*)pSensorSettings)->nSamplePeriod, Custom_1_fsl_Sensor_Custom_PeriodicCallback , ((isf_SensorHandle_t *)pSensorHandle));
619  if(BM_ERROR & pDeviceDescriptor->token)
620  {
621  goto unlockchannel;
622  }
623  //Store the configured settings
624  ((isf_SensorHandle_t *)pSensorHandle)->controlData.sensorSettings = *((isf_dsa_SensorSettings_t*)pSensorSettings);
625  ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus = DSA_STATE_CONFIGURED_STOPPED;
626  retStat = ISF_SUCCESS;
627 
628  unlockchannel:
629  // Unlock the channel.
630  // The explicit lock must be released when the configuration is done allowing the bus to be used by others.
631  status = dm_channel_release_lock(&pDeviceDescriptor->cDescriptor);
632  if (ISF_SUCCESS != status)
633  {
634  retStat = status;
635  }
636 
637  unlockdescriptor:
638  // Unlock the device descriptor.
639  OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
640 
641  *retVal = retStat;
642 }
643 
644 /*
645 ** ===================================================================
646 ** Event : Custom_1_fsl_Sensor_Custom_ValidateSettings (module fsl_Sensor_Custom_1_Functions)
647 **
648 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
649 ** @brief
650 ** Parameters :
651 ** NAME - DESCRIPTION
652 ** @param
653 ** @param
654 ** @param
655 ** @return
656 ** Returns : Nothing
657 ** ===================================================================
658 */
659 void Custom_1_fsl_Sensor_Custom_ValidateSettings(void* pSensorHandle, void* pSensorSettings, uint8_t *retVal)
660 {
661  // Check the input arguments.
662  if ((NULL == pSensorHandle) || (NULL == pSensorSettings))
663  {
664  *retVal = DSA_ERR_PARAM;
665  }
666 
667  // This f/n is to be used to validate sample period range setting and other configuration parameters.
668  *retVal = ISF_SUCCESS;
669 }
670 
671 /*
672 ** ===================================================================
673 ** Event : Custom_1_fsl_Sensor_Custom_Initialize (module fsl_Sensor_Custom_1_Functions)
674 **
675 ** Component : Custom_1 [ISF_KSDK_Sensor_Custom_Adaptor]
676 ** @brief
677 ** Parameters :
678 ** NAME - DESCRIPTION
679 ** @param
680 ** @param
681 ** @return
682 ** Returns : Nothing
683 ** ===================================================================
684 */
685 void Custom_1_fsl_Sensor_Custom_Initialize(void* pSensorHandle, uint8_t *retVal)
686 {
687  if ( NULL == pSensorHandle)
688  {
689  *retVal = DSA_ERR_PARAM;
690  }
691 
692  int32_t status;
693 
694  // Check if the sensor is available, and that it has already been initialized.
695  if (((isf_SensorHandle_t *)pSensorHandle)->adapterStatus > DSA_STATE_NOT_INITIALIZED)
696  {
697  *retVal = DSA_ERR_INITIALIZE;
698  }
699 
700  // Allocate the adapter specific memory for the descriptor
701  ((isf_SensorHandle_t *)pSensorHandle)->pDeviceDescriptor = (DeviceDescriptor_t *)OSA_MemAllocZero(sizeof(DeviceDescriptor_t));
702 
703  // Create and validate helper reference pointers.
704  DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*)((isf_SensorHandle_t *)pSensorHandle)->pDeviceDescriptor;
705 
706  if (NULL == pDeviceDescriptor)
707  {
708  *retVal = DSA_ERR_INITIALIZE;
709  }
710 
711  // Allocate the current sample data buffer and initialize it.
712  pDeviceDescriptor->pCurrentSample = OSA_MemAllocZero(sizeof(Custom_1_DataBuffer_t));
713  Custom_1_DataBuffer_t *pCurrentSampleBuffer = (Custom_1_DataBuffer_t *)pDeviceDescriptor->pCurrentSample;
714  pCurrentSampleBuffer->timeStamp = 0;
715  pCurrentSampleBuffer->data[0] = 0;
716  pCurrentSampleBuffer->data[1] = 0;
717  pCurrentSampleBuffer->data[2] = 0;
718  pCurrentSampleBuffer->addr = ((i2c_device_t *)((isf_SensorHandle_t *)pSensorHandle)->pSensorStaticConfig->commInfo)->address;
719 
720  dm_ChannelDescriptor_t *pChannelDescriptor = &pDeviceDescriptor->cDescriptor;
721 
722  // Initialize the channel
723  if(ISF_SUCCESS != dm_channel_init(((isf_SensorHandle_t *)pSensorHandle)->pSensorStaticConfig->channelId, pChannelDescriptor))
724  {
725  *retVal = DSA_ERR_INITIALIZE;
726  }
727  // Start the Bus
728  if(ISF_SUCCESS != dm_channel_start(pChannelDescriptor))
729  {
730  *retVal = DSA_ERR_INITIALIZE;
731  }
732 
733  // Get the Channel state to check if it is ready to use
734  if (COMM_STATE_OK != dm_channel_get_state(pChannelDescriptor))
735  {
736  *retVal = DSA_ERR_INITIALIZE;
737  }
738 
739  // Open the device and get the device handler
740  if(ISF_SUCCESS != dm_device_open(pChannelDescriptor, (void *)((isf_SensorHandle_t *)pSensorHandle)->pSensorStaticConfig->commInfo, &pDeviceDescriptor->deviceHandle))
741  {
742  *retVal = DSA_ERR_INITIALIZE;
743  }
744 
745  // A very short delay is needed for device startup.
746  OSA_TimeDelay(1);
747 
748  // Query the sensor to validate Device Messaging initialization and the sensor identity.
749  uint8 buffer[1] = {0};
750  int32 retStat = SENSOR_ERROR_CHECKID;
751  // Create and validate helper reference pointers.
752  dm_DeviceDescriptor_t* pDeviceHandle = &((DeviceDescriptor_t*)((isf_SensorHandle_t *)pSensorHandle)->pDeviceDescriptor)->deviceHandle;
753 
754  if (ISF_SUCCESS != dm_device_read(pDeviceHandle, Custom_1_WHO_AM_I, &buffer[0], 1, 1))
755  {
756  status = retStat;
757  }
758  if ((Custom_1_Who_Am_I_Values[0] != buffer[0]) && (Custom_1_Who_Am_I_Values[1] != buffer[0]))
759  {
760  status = retStat;
761  }
762  status = ISF_SUCCESS;
763 
764  if (ISF_SUCCESS != status)
765  {
766  *retVal = DSA_ERR_INITIALIZE;
767  }
768 
769  // Set the adapter state to be initialized.
770  ((isf_SensorHandle_t *)pSensorHandle)->adapterStatus = DSA_STATE_INITIALIZED;
771  // Create a semaphore to synchronize the device descriptor across tasks
772  if (kStatus_OSA_Success != OSA_SemaCreate(&pDeviceDescriptor->deviceSemaphore, 1))
773  {
774  *retVal = DSA_ERR_INITIALIZE;
775  }
776 
777  pDeviceDescriptor->skipFramecnt = 0;
778 
779  *retVal = ISF_SUCCESS;
780 }
781 
782 /* END fsl_Sensor_Custom_1_Functions */
783 
784 #ifdef __cplusplus
785 } /* extern "C" */
786 #endif
787 
788 /*!
789 ** @}
790 */
791 /*
792 ** ###################################################################
793 **
794 ** This file was created by Processor Expert 10.5 [05.21]
795 ** for the Freescale Kinetis series of microcontrollers.
796 **
797 ** ###################################################################
798 */
isf_status_t dm_channel_start(dm_ChannelDescriptor_t *apChannelDescriptor)
This function starts a channel.
unsigned char uint8
Definition: isf_types.h:76
isf_SensorDataTypes_t Custom_1_SupportedDataTypes[]
isf_dsa_status_t Custom_1_float_Custom_converter(Custom_1_Sensor_Specific_Settings_t *pSensorSpecificConfig, Custom_1_DataBuffer_t *nativeSample, void *vpConvertedSample)
#define Custom_1_CONVERT_FLOAT_FACTOR
uint32 isf_time_util_get_usec(void)
This API returns the time in microseconds.
Definition: isf_util.c:55
This is user's event module. Put your event handler code here.
isf_dsa_status_t fsl_Sensor_Custom_1_Initialize(isf_SensorHandle_t *pSensorHandle)
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_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_dsa_status_t fsl_Sensor_Custom_1_EndData(isf_SensorHandle_t *pSensorHandle)
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_Sensor_Custom_1_Convert(volatile isf_SensorHandle_t *pSensorHandle, isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType, void *pNativeSample, void *pConvertedSample, int32_t *numBytes)
#define FALSE
Definition: isf_types.h:86
dm_DeviceDescriptor_t deviceHandle
Definition: isf_sensors.h:52
This defines the DSA sensor device handle structure used to invoke the adapter access functions...
This structure defines important settings.
Define the sensor device descriptor.
Definition: isf_sensors.h:49
void Custom_1_fsl_Sensor_Custom_EndData(void *pSensorHandle, uint8_t *retVal)
void * pSensorData[]
Definition: BasicApp1.c:408
Custom_1_Sensor_Specific_Settings_t Custom_1_Sensor_Specific_Config
This allows access to the sensor specific configuration for the target sensor.
This is user's event module. Put your event handler code here.
void Custom_1_fsl_Sensor_Custom_Shutdown(void *pSensorHandle, uint8_t *retVal)
isf_dsa_status_t fsl_Sensor_Custom_1_ValidateSettings(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
isf_dsa_status_t fsl_Sensor_Custom_1_Configure(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
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.
void Custom_1_fsl_Sensor_Custom_PeriodicCallback(void *pSensorHandle)
#define Custom_1_CONVERT_FIXED_FACTOR
isf_dsa_status_t Custom_1_fixed_Custom_converter(Custom_1_Sensor_Specific_Settings_t *pSensorSpecificConfig, Custom_1_DataBuffer_t *nativeSample, void *vpConvertedSample)
Standard fixed type for three axis accelerometers.
int32 isf_dsa_status_t
This is the Sensor Manager API return type definition.
signed short int int16
Definition: isf_types.h:73
#define BYTES_FOR_FULL_READ
isf_status_t dm_channel_acquire_lock(dm_ChannelDescriptor_t *apChannelDescriptor, isf_duration_t aTimeout)
This function locks the channel for exclusive access.
isf_dsa_status_t fsl_Sensor_Custom_1_StartData(isf_SensorHandle_t *pSensorHandle)
enum isf_dsa_result_enums isf_dsa_result_types_t
isf_Custom_1_float_data_t sensorData[3]
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.
const uint8_t Custom_1_Who_Am_I_Values[]
Define the WHO_AM_I register values for both possible sensor variations.
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.
This is user's event module. Put your event handler code here.
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_status_t bm_start(boolean aSync, bm_callback_token_t aTokens)
This API sets one or more callback(s) to the active state.
#define Custom_1_ACTIVE_MASK
isf_SensorDataTypes_t resultType
isf_dsa_AdapterStatus_t adapterStatus
signed long int int32
Definition: isf_types.h:74
unsigned short int uint16
Definition: isf_types.h:77
#define Custom_1_GET_FIELD(name, val)
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
int32 isf_status_t
ISF return status type.
Definition: isf.h:76
isf_SensorTypes_t Custom_1_SupportedSensorTypes[]
Supported sensor and data types for the Sensor.
This defines the DSA sensor configuration parameter structure configuring the sensor settings by a su...
This is user's event module. Put your event handler code here.
isf_dsa_SensorSettings_t sensorSettings
#define Custom_1_SET_FIELD(name, val)
void Custom_1_fsl_Sensor_Custom_Initialize(void *pSensorHandle, uint8_t *retVal)
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.
void Custom_1__Void_()
This structure defines the dummy DSA data buffer format.
This is user's event module. Put your event handler code here.
void Custom_1_fsl_Sensor_Custom_StartData(void *pSensorHandle, uint8_t *retVal)
void Custom_1_fsl_Sensor_Custom_Configure(void *pSensorHandle, void *pSensorSettings, uint8_t *retVal)
This structure defines a handle for the device.
Definition: isf_devmsg.h:61
void Custom_1_fsl_Sensor_Custom_ValidateSettings(void *pSensorHandle, void *pSensorSettings, uint8_t *retVal)
const i2c_device_t Custom_1_DeviceInfo
This allows access to the sensor communications specific configuration.
void Custom_1_fsl_Sensor_Custom_Convert(volatile void *pSensorHandle, uint32_t convertToType, uint8_t resultType, void *pNativeSample, void *pConvertedSample, int32_t *numBytes, uint8_t *retVal)
semaphore_t deviceSemaphore
Definition: isf_sensors.h:54
This structure is a declaration of a channel descriptor type.
Definition: isf_devmsg.h:50
#define ISF_FIFO_FULL
Definition: isf_fifo.h:33
isf_dsa_status_t fsl_Sensor_Custom_1_Shutdown(isf_SensorHandle_t *pSensorHandle)
bm_callback_token_t token
Definition: isf_sensors.h:53