| ISF 2.2 rev 5 Intelligent Sensing Framework for Kinetis with Processor Expert

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

fsl_mma8491q_i2c_3D_accel.c
Go to the documentation of this file.
1 /**
2 * @file fsl_mma8491q_i2c_3D_accel.c
3 * @brief The \b fsl_mma8491q_i2c_3D_accel.c contains the MMA8491Q Accelerometer family routine Set
4 * implementations. Freescale supports several families of 3-axis intelligent accelerometers
5 * with overlapping register definitions and feature sets. This file implements a sensor adapter
6 * for this part families.
7 *
8 * @copyright Copyright (c) 2015, Freescale Semiconductor, Inc.
9 */
10
11 #include "mma8491q.h"
12 #include "isf_devmsg.h"
13 #include "isf_sensors.h"
14 #include "isf_accelerometer_types.h"
15 #include "fsl_mma8491q_i2c_3D_accel.h"
16 //This adapter works only with 4 GPIO component support.
17 #include "SensorEnable.h"
18 #include "OutX.h"
19 #include "OutY.h"
20 #include "OutZ.h"
21
22 /*
23 * Public externally declared functions
24 */
25 // This is used for a one time call-back to delay sensor data reads after EN =1 is set.
26 #define T_ON_8491Q_MICROSECONDS (700)
27
28 /*
29 * Formats the MMA8491Q sensor data according to the specification and returns
30 * the destination data in the form MSB,LSB, for two bytes in one 16-bit type
31 */
32 #define mma8491q_xyz_format(destination, msb, lsb) destination = (((uint16)msb) << 8) | lsb;
33
34 // Sensor EN pin.
35 const uint32_t en_pin = J199_3;
36 const uint32_t x_out_pin = J1_5;
37 const uint32_t y_out_pin = J1_5;
38 const uint32_t z_out_pin = J1_5;
39
40 // Check Sensor who am I value.
41 void mma8491q_CheckId(int32_t *status, void* pSensorHandle);
42
43 // Conversion factors for supported Data Types.
44 #define MMA8491Q_ACCEL_FLOAT_CONVERSION_FACTOR 0.000244
45 #define MMA8491Q_ACCEL_FIXED_CONVERSION_FACTOR 16
46
47 // Converter routines for the supported data types.
48 static isf_dsa_status_t float_accel3d_converter(mma8491q_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma8491q_DataBuffer_t *nativeSample, void *vpConvertedSample);
49 static isf_dsa_status_t fixed_accel3d_converter(mma8491q_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma8491q_DataBuffer_t *nativeSample, void *vpConvertedSample);
50
51 // routine to get the accelerometer data from the sensor
52 isf_dsa_status_t mma8491q_getAccelData (DeviceDescriptor_t *pDescriptor, int16 *pAccelData);
53
54 // declare a static sensor mode.
55 volatile mma8491q_mode_t mma8491q_mode;
56
57 //Since for this Sensor we need to read data after a certain delay, create a holder for the additional callback.
58 static bm_callback_token_t token_cb_Read_Data;
59
60 isf_SensorTypes_t mma8491q_SupportedSensorTypes[] = { TYPE_ACCELEROMETER_3D, TYPE_NATIVE_SENSOR_TYPE };
61 isf_SensorDataTypes_t mma8491q_SupportedDataTypes[] = { TYPE_RAW_ACCELERATION_3D, TYPE_NATIVE_SENSOR_DATA_TYPE };
62
63 // This is the MMA8491Q accelerometer adapter initialization routine.
64 isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Initialize(isf_SensorHandle_t *pSensorHandle)
65 {
66 isf_status_t retStat;
67
68 // Check that the sensor handle is valid.
69 if( NULL == pSensorHandle)
70 {
71 return DSA_ERR_PARAM;
72 }
73
74 // Check if the sensor is available, already initialized.
75 if(pSensorHandle->adapterStatus > DSA_STATE_NOT_INITIALIZED)
76 {
77 return DSA_ERR_INITIALIZE;
78 }
79
80 // Allocate the adapter specific memory for the descriptor and cast a direct pointer to it.
81 //pSensorHandle->pDeviceDescriptor = (DeviceDescriptor_t *)_lwmem_alloc_system_zero(sizeof(DeviceDescriptor_t));
82 pSensorHandle->pDeviceDescriptor = (DeviceDescriptor_t *)OSA_MemAllocZero(sizeof(DeviceDescriptor_t));
83
84 // Create helper reference pointers into the descriptor.
85 DeviceDescriptor_t *pDeviceDescriptor = pSensorHandle->pDeviceDescriptor;
86 if(NULL == pDeviceDescriptor)
87 {
88 return DSA_ERR_INITIALIZE;
89 }
90
91 // Allocate the current sample data buffer and initialize it.
92 //pDeviceDescriptor->pCurrentSample = _lwmem_alloc_system_zero(sizeof(mma8491q_DataBuffer_t));
93 pDeviceDescriptor->pCurrentSample = OSA_MemAllocZero(sizeof(mma8491q_DataBuffer_t));
94 mma8491q_DataBuffer_t *pCurrentSampleBuffer = (mma8491q_DataBuffer_t *)pDeviceDescriptor->pCurrentSample;
95 pCurrentSampleBuffer->timestamp = 0;
96 pCurrentSampleBuffer->accel[0] = 0;
97 pCurrentSampleBuffer->accel[1] = 0;
98 pCurrentSampleBuffer->accel[2] = 0;
99 pCurrentSampleBuffer->xOut = 0;
100 pCurrentSampleBuffer->yOut = 0;
101 pCurrentSampleBuffer->zOut = 0;
102
103 dm_ChannelDescriptor_t *pChannelDescriptor = &pDeviceDescriptor->cDescriptor;
104
105 // Initialize the channel.
106 if(ISF_SUCCESS != dm_channel_init(pSensorHandle->pSensorStaticConfig->channelId, pChannelDescriptor))
107 {
108 return DSA_ERR_INITIALIZE;
109 }
110
111 // Start the bus.
112 if(ISF_SUCCESS != dm_channel_start(pChannelDescriptor))
113 {
114 return DSA_ERR_INITIALIZE;
115 }
116
117 // Get the channel state to check if it is ready to use.
118 if(COMM_STATE_OK != dm_channel_get_state(pChannelDescriptor))
119 {
120 return DSA_ERR_INITIALIZE;
121 }
122
123 // Turn on Plutino Enable so that I2C communication is active for RLI etc.
124 GPIO_DRV_SetPinOutput(en_pin);
125
126 // takes 900usec to enable the I2C bus
127 OSA_TimeDelay(1);
128
129 // Open the device and get the device handler.
130 if(ISF_SUCCESS != dm_device_open(pChannelDescriptor, (void *)pSensorHandle->pSensorStaticConfig->commInfo, &pDeviceDescriptor->deviceHandle))
131 {
132 return DSA_ERR_INITIALIZE;
133 }
134
135 // Query the sensor to validate Device Messaging initialization and the sensor identity.
136 mma8491q_CheckId(&retStat,pSensorHandle);
137 if(ISF_SUCCESS != retStat)
138 {
139 return DSA_ERR_INITIALIZE;
140 }
141
142 // Set the adapter state to be initialized.
143 pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
144
145 // Create a semaphore to synchronize the device descriptor across tasks
146 //if(kStatus_OSA_Success != _lwsem_create(&pDeviceDescriptor->deviceSemaphore, 1))
147 if(kStatus_OSA_Success != OSA_SemaCreate(&pDeviceDescriptor->deviceSemaphore, 1))
148 {
149 return DSA_ERR_INITIALIZE;
150 }
151
152 pDeviceDescriptor->skipFramecnt = 0;
153
154 return ISF_SUCCESS;
155 }
156
157 // This is the MMA8491Q Accelerometer adapter settings validation routine.
158 isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_ValidateSettings(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
159 { // Check the input arguments.
160 if((NULL == pSensorHandle) || (NULL == pSensorSettings))
161 {
162 return DSA_ERR_PARAM;
163 }
164
165 return ISF_SUCCESS;
166 }
167
168 // This is the MMA8491Q accelerometer adapter settings configuration
169 isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Configure(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
170 { // Check the input arguments.
171 if((NULL == pSensorHandle) || (NULL == pSensorSettings))
172 {
173 return DSA_ERR_PARAM;
174 }
175
176 isf_status_t retStat = DSA_ERR_CONFIGURE;
177 DeviceDescriptor_t *pDeviceDescriptor = (DeviceDescriptor_t*)(pSensorHandle->pDeviceDescriptor);
178
179 // Check the device descriptor, sensor specific settings.
180 if(NULL == pDeviceDescriptor)
181 {
182 return retStat;
183 }
184
185 // Lock the device descriptor.
186 //_lwsem_wait_ticks(&pDeviceDescriptor->deviceSemaphore, 0);
187 OSA_SemaWait(&pDeviceDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
188
189 // Check the driver state.
190 if(DSA_STATE_INITIALIZED > pSensorHandle->adapterStatus)
191 {
192 goto unlockdescriptor;
193 }
194
195 // User Notice: This is a required function call. If it is not here, there may be conflicts, data loss,
196 // or any number of undesirable things may occur.
197 if(ISF_SUCCESS != dm_channel_acquire_lock(&pDeviceDescriptor->cDescriptor, 0))
198 {
199 goto unlockchannel;
200 }
201
202 /* Initialize the Sensor state machine to start from SHUTDOWN. */
203 mma8491q_mode = MMA8491Q_SHUTDOWN;
204
205 // Register the periodic callback function with the Bus Manager.
206 // This is callback 1 which establishes the sample rate up to 800 Hz for the MMA8491Q to enable the sensor
207 pDeviceDescriptor->token = bm_register_periodic_callback(pSensorSettings->nSamplePeriod,
208 fsl_mma8491q_i2c_3D_accel_PeriodicCallback, pSensorHandle);
209 if(BM_ERROR & pDeviceDescriptor->token) // Check for Bus Manager error and return on failure.
210 {
211 goto unlockchannel;
212 }
213
214 // This is the callback 2 which will be OneShot to collect data and disable the sensor.
215 token_cb_Read_Data = bm_register_periodic_callback(T_ON_8491Q_MICROSECONDS,
216 fsl_mma8491q_read_data_OneShotCallback, pSensorHandle);
217 if(BM_ERROR & token_cb_Read_Data) // Check for Bus Manager error and return on failure.
218 {
219 goto unlockchannel;
220 }
221
222 //Store the configured settings
223 pSensorHandle->controlData.sensorSettings = *pSensorSettings;
224
225 // Set the adapter to indicate fully configured.
226 pSensorHandle->adapterStatus = DSA_STATE_CONFIGURED_STOPPED;
227 retStat = ISF_SUCCESS;
228
229 unlockchannel:
230 // Unlock the channel.
231 // The explicit lock must be released when the configuration is done allowing the bus to be used by others.
232 // User Notice: This is a required function call.
233 retStat = dm_channel_release_lock(&pDeviceDescriptor->cDescriptor);
234 if(ISF_SUCCESS != retStat)
235 {
236 return retStat;
237 }
238
239 unlockdescriptor:
240 // Unlock the device descriptor.
241 //_lwsem_post(&pDeviceDescriptor->deviceSemaphore);
242 OSA_SemaPost(&pDeviceDescriptor->deviceSemaphore);
243
244 return retStat;
245 }
246
247 // This is the MMA8491Q accelerometer adapter routine to calibrate the sensor.
248 // Calibration has not yet been implemented in ISF.
249 isf_status_t fsl_mma8491q_i2c_3D_accel_Calibrate(isf_SensorHandle_t *pSensorHandle, void *pCalData)
250 {
251 if((NULL == pSensorHandle) || (NULL == pCalData))
252 {
253 return DSA_ERR_PARAM;
254 }
255
256 return ISF_SUCCESS;
257 }
258
259 // This is the MMA8491Q accelerometer adapter routine to start sensor data.
260 isf_status_t fsl_mma8491q_i2c_3D_accel_StartData(isf_SensorHandle_t *pSensorHandle)
261 {
262 if(NULL == pSensorHandle)
263 {
264 return DSA_ERR_PARAM;
265 }
266
267 isf_status_t retStat = DSA_ERR_START_DATA;
268 DeviceDescriptor_t *pDescriptor = pSensorHandle->pDeviceDescriptor;
269
270 // Check the device descriptor.
271 if(NULL == pDescriptor)
272 {
273 return DSA_ERR_PARAM;
274 }
275
276 // Lock the device descriptor.
277 //_lwsem_wait_ticks(&pDescriptor->deviceSemaphore, 0);
278 OSA_SemaWait(&pDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
279
280 // Check the driver state.
281 if(DSA_STATE_CONFIGURED_STOPPED > pSensorHandle->adapterStatus)
282 {
283 goto unlockdescriptor;
284 }
285
286 // Schedule the Bus Manager sampling timer for the sensor selected by the user ( > 1 up to 200 Hz)
287 // and schedule a periodic callback that will only run once
288 if(BM_ERROR & bm_start(FALSE, pDescriptor->token))
289 { // Check for Bus Manager error and return on failure.
290 goto unlockdescriptor;
291 }
292
293 // Set the adapter state to indicate that the sensor is transitioned to starting
294 pSensorHandle->adapterStatus = DSA_STATE_CONFIGURED_STARTED;
295
296 retStat = ISF_SUCCESS;
297
298 unlockdescriptor:
299 // Unlock the device descriptor.
300 //_lwsem_post(&pDescriptor->deviceSemaphore);
301 OSA_SemaPost(&pDescriptor->deviceSemaphore);
302
303 return retStat;
304 }
305
306 // This is the MMA8491Q accelerometer adapter routine to stop sensor data.
307 isf_status_t fsl_mma8491q_i2c_3D_accel_EndData(isf_SensorHandle_t *pSensorHandle)
308 {
309 if(NULL == pSensorHandle)
310 {
311 return DSA_ERR_PARAM;
312 }
313
314 isf_status_t retStat = DSA_ERR_END_DATA;
315 // Cast the void input pointer to a typed pointer.
316 DeviceDescriptor_t* pDescriptor = pSensorHandle->pDeviceDescriptor;
317
318 // Check the device descriptor.
319 if(NULL == pDescriptor)
320 {
321 return retStat;
322 }
323
324 // Lock the device descriptor.
325 //_lwsem_wait_ticks(&pDescriptor->deviceSemaphore, 0);
326 OSA_SemaWait(&pDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
327
328 // Check the driver state.
329 if(DSA_STATE_CONFIGURED_STOPPED >= pSensorHandle->adapterStatus)
330 {
331 goto unlockdescriptor;
332 }
333
334 // Check that the adapter is not already started.
335 if(DSA_STATE_CONFIGURED_STARTED == pSensorHandle->adapterStatus)
336 { // If it is, then stop the Bus Manager (BM) periodic callbacks for
337 // start and end
338 isf_status_t bmStopStatus = bm_stop(pDescriptor->token);
339 if(BM_ERROR & bmStopStatus)
340 {
341 goto unlockdescriptor;
342 }
343 }
344
345 // Leave on Plutino Enable so that I2C subsystem is active.
346 GPIO_DRV_SetPinOutput(en_pin);
347
348 // Set the adapter back to the initialized state.
349 pSensorHandle->adapterStatus = DSA_STATE_CONFIGURED_STOPPED;
350 retStat = ISF_SUCCESS;
351
352 unlockdescriptor:
353 // Unlock the device descriptor.
354 //_lwsem_post(&pDescriptor->deviceSemaphore);
355 OSA_SemaPost(&pDescriptor->deviceSemaphore);
356
357 return retStat;
358 }
359
360 isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Shutdown(isf_SensorHandle_t *pSensorHandle)
361 {
362 isf_status_t retStat = DSA_ERR_END_DATA;
363
364 if(NULL == pSensorHandle)
365 {
366 return DSA_ERR_PARAM;
367 }
368
369 // Create helper reference pointers.
370 DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHandle->pDeviceDescriptor;
371
372 // Remove the sensor adapter from the Bus Manager callbacks.
373 isf_status_t bmUnregisterStatus = bm_unregister_callback(token_cb_Read_Data);
374 if(BM_ERROR & bmUnregisterStatus)
375 {
376 return DSA_ERR_SHUTDOWN;
377 }
378
379 bmUnregisterStatus = bm_unregister_callback(pDeviceDescriptor->token);
380 if(BM_ERROR & bmUnregisterStatus)
381 {
382 return DSA_ERR_SHUTDOWN;
383 }
384
385 /* Reset the Sensor State machine to reflect shutdown. */
386 mma8491q_mode = MMA8491Q_SHUTDOWN;
387
388 // Set the adapter back to the initialized state.
389 pSensorHandle->adapterStatus = DSA_STATE_INITIALIZED;
390 return ISF_SUCCESS;
391 }
392
393 /*
394 * This routine sets the enable GPIO pin of the sensor to transition to ACTIVE
395 */
396 void fsl_mma8491q_i2c_3D_accel_PeriodicCallback(void* pSensorHandle)
397 {
398 volatile isf_SensorHandle_t* pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
399
400 if(NULL == pSensorHandle)
401 {
402 return;
403 }
404 DeviceDescriptor_t* pDescriptor = (DeviceDescriptor_t*)(pSensorHdl->pDeviceDescriptor);
405 if(NULL == pDescriptor)
406 {
407 return;
408 }
409
410 // Lock the device descriptor.
411 //_lwsem_wait_ticks(&pDescriptor->deviceSemaphore, 0);
412 OSA_SemaWait(&pDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
413
414 // Check that the adaptor state is STARTED and the sensor mode is SHUTDOWN
415 if((DSA_STATE_CONFIGURED_STARTED == pSensorHdl->adapterStatus)
416 && (MMA8491Q_SHUTDOWN == mma8491q_mode))
417 {
418 // Set GPIO EN=1 to Enable the sensor and set its state to ACTIVE and start the callback.
419 GPIO_DRV_SetPinOutput(en_pin);
420 mma8491q_mode = MMA8491Q_ACTIVE;
421 (void) bm_start(FALSE, token_cb_Read_Data);
422 }
423
424 // Unlock the device descriptor.
425 //_lwsem_post(&pDescriptor->deviceSemaphore);
426 OSA_SemaPost(&pDescriptor->deviceSemaphore);
427 }
428
429 /*
430 * This is the STANDBY mode of the sensor that collects data
431 */
432 void fsl_mma8491q_read_data_OneShotCallback(void* pSensorHandle)
433 {
434 int32 numBytes;
435 isf_status_t st;
436 volatile isf_SensorHandle_t* pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
437
438 if(NULL == pSensorHandle)
439 {
440 return;
441 }
442
443 isf_fifo_t *pFifo = pSensorHdl->controlData.sensorSettings.pSampleBufferInfo;
444 DeviceDescriptor_t *pDescriptor = (DeviceDescriptor_t*)(pSensorHdl->pDeviceDescriptor);
445 if(NULL == pDescriptor)
446 {
447 return;
448 }
449
450 mma8491q_DataBuffer_t *pCurrentSampleBuffer = (mma8491q_DataBuffer_t *)pDescriptor->pCurrentSample;
451
452 // Apply sample skip if necessary.
453 if (pDescriptor->skipFramecnt)
454 { // Not done skipping samples.
455 --pDescriptor->skipFramecnt;
456 return;
457 }
458
459 // Read the timestamp and put it into the Sensor Manager(SM) data buffer.
460 pCurrentSampleBuffer->timestamp = isf_time_util_get_usec();
461
462 // Lock the device descriptor.
463 //_lwsem_wait_ticks(&pDescriptor->deviceSemaphore, 0);
464 OSA_SemaWait(&pDescriptor->deviceSemaphore, OSA_WAIT_FOREVER);
465
466 // Check that the sensor adaptor is STARTED and that the mode is ACTIVE
467 if((DSA_STATE_CONFIGURED_STARTED == pSensorHdl->adapterStatus)
468 && (MMA8491Q_ACTIVE == mma8491q_mode))
469 {
470 // acquire the sensor data
471 st = mma8491q_getAccelData(pDescriptor, &pCurrentSampleBuffer->accel[0]);
472 if(st != MMA8491Q_DATA_SUCCESS)
473 {
474 goto unlockdescriptor;
475 }
476
477 // After acquisition of the data set the sensor mode to SHUTDOWN
478 // and Turn off the GPIO EN=0 the enable pin of the MMA8491Q and disable the callback.
479 GPIO_DRV_ClearPinOutput(en_pin);
480 mma8491q_mode = MMA8491Q_SHUTDOWN;
481 (void) bm_stop(token_cb_Read_Data);
482
483 // Lock the fifo for update.
484 if(kStatus_OSA_Success != isf_fifo_lock(pFifo))
485 {
486 goto unlockdescriptor;
487 }
488
489 mma8491q_DataBuffer_t *pFifoEntry = (mma8491q_DataBuffer_t *)isf_fifo_el_get_insert_pointer(pFifo);
490 pCurrentSampleBuffer->xOut = GPIO_DRV_ReadPinInput(x_out_pin);
491 pCurrentSampleBuffer->yOut = GPIO_DRV_ReadPinInput(y_out_pin);
492 pCurrentSampleBuffer->zOut = GPIO_DRV_ReadPinInput(z_out_pin);
493
494 // write the new data
495 if (pSensorHdl->controlData.sensorSettings.resultType == TYPE_NATIVE_SENSOR_DATA_TYPE)
496 {
497 *pFifoEntry = *pCurrentSampleBuffer;
498 }
499 else
500 {
501 fsl_mma8491q_i2c_3D_accel_Convert(
502 pSensorHdl,
503 pSensorHdl->controlData.sensorSettings.resultType,
504 pSensorHdl->controlData.sensorSettings.resultFormat,
505 pCurrentSampleBuffer,
506 pFifoEntry,
507 &numBytes);
508 }
509
510 // Increment the fifo to the next sample entry.
511 st = isf_fifo_el_increment(pFifo);
512
513 // Unlock the fifo.
514 isf_fifo_unlock(pFifo);
515
516 if (st == ISF_FIFO_FULL)
517 {
518 // Notify the user using their registered event information
519 //_lwevent_set(pSensorHdl->controlData.pEventGroup,(uint32_t)pSensorHdl->controlData.nEventFieldIndex);
520 OSA_EventSet(pSensorHdl->controlData.pEventGroup,(uint32_t)pSensorHdl->controlData.nEventFieldIndex);
521 }
522 }
523
524 unlockdescriptor:
525 // Unlock the device descriptor.
526 //_lwsem_post(&pDescriptor->deviceSemaphore);
527 OSA_SemaPost(&pDescriptor->deviceSemaphore);
528 }
529
530 isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Convert
531 (
532 volatile isf_SensorHandle_t *pSensorHandle,
533 isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType,
534 void *pNativeSample,
535 void *pConvertedSample,
536 int32 *numBytes
537 )
538 {
539 isf_dsa_status_t retStat = DSA_ERR_BAD_SENSOR_DATA_TYPE;
540 isf_dsa_status_t (*pConverter)(mma8491q_Sensor_Specific_Settings_t *, mma8491q_DataBuffer_t *, void *);
541
542 pConverter = NULL;
543 switch (convertToType)
544 {
545 case TYPE_RAW_ACCELERATION_3D:
546 if(resultType == DSA_RESULT_TYPE_ENG_FLOAT)
547 {
548 pConverter = float_accel3d_converter;
549 }
550 else if(resultType == DSA_RESULT_TYPE_ENG_FIXED)
551 {
552 pConverter = fixed_accel3d_converter;
553 }
554 break;
555
556 default:
557 return DSA_ERR_BAD_SENSOR_DATA_TYPE;
558 }
559 if(pConverter == NULL)
560 {
561 return DSA_ERR_BAD_RESULT_TYPE;
562 }
563
564 retStat = pConverter(
565 (mma8491q_Sensor_Specific_Settings_t*)pSensorHandle->pSensorStaticConfig->pSensorSpecificSettings,
566 (mma8491q_DataBuffer_t *)pNativeSample, pConvertedSample);
567
568 return retStat;
569 }
570
571 /*!
572 * @brief This function validates the MMA8491Q sensor is connected via the I2C bus.
573 */
574 void mma8491q_CheckId(int32_t *status, void* pSensorHandle)
575 {
576 uint8 buffer[1] = {0};
577 int32 retStat = SENSOR_ERROR_CHECKID;
578
579 // Create and validate helper reference pointers.
580 isf_SensorHandle_t* pSensorHdl = (isf_SensorHandle_t *)pSensorHandle;
581 DeviceDescriptor_t* pDeviceDescriptor = (DeviceDescriptor_t*)pSensorHdl->pDeviceDescriptor;
582 dm_DeviceDescriptor_t* pDeviceHandle = &pDeviceDescriptor->deviceHandle;
583
584 if(ISF_SUCCESS != dm_device_read(pDeviceHandle, MMA8491Q_WHOAMI_VALUE, &buffer[0], 1, 1))
585 {
586 *status = retStat;
587 }
588 if(MMA8491Q_WHOAMI_VALUE != buffer[0])
589 {
590 *status = retStat;
591 }
592
593 *status = ISF_SUCCESS;
594 }
595
596 #define DATA_BUFFER_SIZE_8491Q (7)
597 /*
598 * Get the acceleration data from the MMA8491Q by using device messaging to interface with I2C
599 */
600 isf_dsa_status_t mma8491q_getAccelData (DeviceDescriptor_t *pDescriptor, int16 *pAccelData)
601 {
602 uint8 registerData[DATA_BUFFER_SIZE_8491Q];
603
604 // Read the i2c buffer
605 if(ISF_SUCCESS != dm_device_read(&pDescriptor->deviceHandle,
606 MMA8491Q_STATUS_00, registerData, DATA_BUFFER_SIZE_8491Q, DATA_BUFFER_SIZE_8491Q))
607 {
608 return SENSOR_ERROR_GETDATA;
609 }
610
611 // Check the status bit for good data in the buffer
612 if( registerData[0] & (uint8)ZYXDR_MASK)
613 {
614 // copy the data into the destination buffer
615 mma8491q_xyz_format(pAccelData[0], registerData[1], registerData[2]); // x
616 mma8491q_xyz_format(pAccelData[1], registerData[3], registerData[4]); // y
617 mma8491q_xyz_format(pAccelData[2], registerData[5], registerData[6]); // z
618
619 return MMA8491Q_DATA_SUCCESS;
620 }
621 else
622 {
623 return MMA8491Q_DATA_FAIL;
624 }
625 }
626
627 static isf_dsa_status_t float_accel3d_converter(mma8491q_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma8491q_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] * MMA8491Q_ACCEL_FLOAT_CONVERSION_FACTOR;
633 convertedSample->accel[1] = nativeSample->accel[1] * MMA8491Q_ACCEL_FLOAT_CONVERSION_FACTOR;
634 convertedSample->accel[2] = nativeSample->accel[2] * MMA8491Q_ACCEL_FLOAT_CONVERSION_FACTOR;
635
636 return ISF_SUCCESS;
637 }
638
639 static isf_dsa_status_t fixed_accel3d_converter(mma8491q_Sensor_Specific_Settings_t *pSensorSpecificConfig, mma8491q_DataBuffer_t *nativeSample, void *vpConvertedSample)
640 {
641 isf_Acceleration3D_EngFixed_t *convertedSample = (isf_Acceleration3D_EngFixed_t *)vpConvertedSample;
642 convertedSample->timestamp = nativeSample->timestamp;
643 // These factors and offsets really depend on the configured sensor Range in pSensorSpecificConfig
644 convertedSample->accel[0] = nativeSample->accel[0] * MMA8491Q_ACCEL_FIXED_CONVERSION_FACTOR;
645 convertedSample->accel[1] = nativeSample->accel[1] * MMA8491Q_ACCEL_FIXED_CONVERSION_FACTOR;
646 convertedSample->accel[2] = nativeSample->accel[2] * MMA8491Q_ACCEL_FIXED_CONVERSION_FACTOR;
647
648 return ISF_SUCCESS;
649 }
isf_SensorHandle_st::pDeviceDescriptor
void * pDeviceDescriptor
Definition: isf_dsa_adapter.h:109
fsl_mma8491q_i2c_3D_accel_Calibrate
isf_status_t fsl_mma8491q_i2c_3D_accel_Calibrate(isf_SensorHandle_t *pSensorHandle, void *pCalData)
Definition: fsl_mma8491q_i2c_3D_accel.c:249
mma8491q_mode
volatile mma8491q_mode_t mma8491q_mode
Definition: fsl_mma8491q_i2c_3D_accel.c:55
dm_channel_start
isf_status_t dm_channel_start(dm_ChannelDescriptor_t *apChannelDescriptor)
This function starts a channel.
Definition: isf_device_messaging.c:137
SensorConfig_st::pSensorSpecificSettings
void * pSensorSpecificSettings
Definition: isf_dsa_adapter.h:157
uint8
unsigned char uint8
Definition: isf_types.h:76
OutX.h
DSA_ERR_BAD_RESULT_TYPE
Definition: isf_dsa_adapter.h:39
isf_Acceleration3D_EngFixed_t
Standard fixed type for three axis accelerometers.
Definition: isf_accelerometer_types.h:39
SensorEnable.h
isf_Acceleration3D_Float_t::timestamp
isf_timestamp_t timestamp
Definition: isf_accelerometer_types.h:81
ISF_SUCCESS
Definition: isf.h:31
x_out_pin
const uint32_t x_out_pin
Definition: fsl_mma8491q_i2c_3D_accel.c:36
isf_time_util_get_usec
uint32 isf_time_util_get_usec(void)
This API returns the time in microseconds.
Definition: isf_util.c:55
dm_device_open
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.
Definition: isf_device_messaging.c:227
DSA_ERR_BAD_SENSOR_DATA_TYPE
Definition: isf_dsa_adapter.h:38
mma8491q_DataBuffer_t::timestamp
uint32 timestamp
Time stamp value in micro-seconds.
Definition: fsl_mma8491q_i2c_3D_accel.h:23
SensorConfig_st::commInfo
const void * commInfo
Definition: isf_dsa_adapter.h:159
isf_SensorHandle_st::controlData
isf_dsa_ControlData_t controlData
Definition: isf_dsa_adapter.h:111
isf_fifo_lock
isf_fifo_status_t isf_fifo_lock(isf_fifo_t *pFifo)
Lock a sample buffer for exclusive access.
Definition: isf_fifo.c:170
dm_channel_get_state
comm_State_t dm_channel_get_state(dm_ChannelDescriptor_t *apChannelDescriptor)
This function returns the channel state.
Definition: isf_device_messaging.c:100
isf_fifo_unlock
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
fsl_mma8491q_i2c_3D_accel_Shutdown
isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Shutdown(isf_SensorHandle_t *pSensorHandle)
Definition: fsl_mma8491q_i2c_3D_accel.c:360
DSA_STATE_CONFIGURED_STARTED
Definition: isf_dsa_adapter.h:60
isf_fifo_el_increment
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
mma8491q_DataBuffer_t::accel
int16 accel[3]
Accelerometer raw data buffer. MMA8491Q supports X,Y,Z axes(3).
Definition: fsl_mma8491q_i2c_3D_accel.h:24
isf_sensors.h
isf_sensors.h contains the ISF Generic Sensor definitions and data structures required when a client ...
MMA8491Q_ACCEL_FLOAT_CONVERSION_FACTOR
#define MMA8491Q_ACCEL_FLOAT_CONVERSION_FACTOR
Definition: fsl_mma8491q_i2c_3D_accel.c:44
mma8491q_mode_t
mma8491q_mode_t
Definition: mma8491q.h:67
FALSE
#define FALSE
Definition: isf_types.h:86
ZYXDR_MASK
#define ZYXDR_MASK
Definition: mma8491q.h:35
DeviceDescriptor_struct::deviceHandle
dm_DeviceDescriptor_t deviceHandle
Definition: isf_sensors.h:52
isf_SensorHandle_st
This defines the DSA sensor device handle structure used to invoke the adapter access functions...
Definition: isf_dsa_adapter.h:107
DeviceDescriptor_struct::skipFramecnt
uint8 skipFramecnt
Definition: isf_sensors.h:59
mma8491q_DataBuffer_t::yOut
uint8 yOut
The Tilt Y data from the sensor.
Definition: fsl_mma8491q_i2c_3D_accel.h:26
isf_dsa_SensorSettings_st::nSamplePeriod
uint32 nSamplePeriod
Definition: isf_dsa_adapter.h:82
DeviceDescriptor_struct
Define the sensor device descriptor.
Definition: isf_sensors.h:49
fsl_mma8491q_i2c_3D_accel_EndData
isf_status_t fsl_mma8491q_i2c_3D_accel_EndData(isf_SensorHandle_t *pSensorHandle)
Definition: fsl_mma8491q_i2c_3D_accel.c:307
DATA_BUFFER_SIZE_8491Q
#define DATA_BUFFER_SIZE_8491Q
Definition: fsl_mma8491q_i2c_3D_accel.c:596
fsl_mma8491q_i2c_3D_accel_ValidateSettings
isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_ValidateSettings(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
Definition: fsl_mma8491q_i2c_3D_accel.c:158
bm_callback_token_t
uint32 bm_callback_token_t
This type defines the Bus Manager token. Each callback registered with the Bus Manager receives a uni...
Definition: isf_bm.h:79
isf_SensorTypes_t
isf_SensorTypes_t
Definition: isf_sensor_types.h:14
DeviceDescriptor_struct::cDescriptor
dm_ChannelDescriptor_t cDescriptor
Definition: isf_sensors.h:51
z_out_pin
const uint32_t z_out_pin
Definition: fsl_mma8491q_i2c_3D_accel.c:38
bm_unregister_callback
isf_status_t bm_unregister_callback(bm_callback_token_t aToken)
This API unregisters one or more callbacks.
Definition: bus_management.c:178
isf_dsa_ControlData_st::pEventGroup
event_t * pEventGroup
Definition: isf_dsa_adapter.h:92
MMA8491Q_DATA_FAIL
Definition: mma8491q.h:63
mma8491q_DataBuffer_t::xOut
uint8 xOut
The Tilt X data from the sensor.
Definition: fsl_mma8491q_i2c_3D_accel.h:25
DSA_ERR_INITIALIZE
Definition: isf_dsa_adapter.h:29
isf_Acceleration3D_EngFixed_t::accel
isf_acceleration_g_fixed_32s1i16_t accel[3]
Definition: isf_accelerometer_types.h:41
SENSOR_ERROR_CHECKID
Definition: isf_sensors.h:41
DSA_ERR_END_DATA
Definition: isf_dsa_adapter.h:33
DSA_RESULT_TYPE_ENG_FIXED
Definition: isf_dsa_adapter.h:48
J199_3
Definition: SensorEnable.h:82
DSA_STATE_INITIALIZED
Definition: isf_dsa_adapter.h:58
isf_dsa_status_t
int32 isf_dsa_status_t
This is the Sensor Manager API return type definition.
Definition: isf_dsa_adapter.h:21
TYPE_NATIVE_SENSOR_TYPE
Definition: isf_sensor_types.h:15
MMA8491Q_WHOAMI_VALUE
#define MMA8491Q_WHOAMI_VALUE
Definition: mma8491q.h:16
MMA8491Q_ACCEL_FIXED_CONVERSION_FACTOR
#define MMA8491Q_ACCEL_FIXED_CONVERSION_FACTOR
Definition: fsl_mma8491q_i2c_3D_accel.c:45
isf_Acceleration3D_Float_t::accel
isf_acceleration_g_float_t accel[3]
Definition: isf_accelerometer_types.h:82
int16
signed short int int16
Definition: isf_types.h:73
fsl_mma8491q_i2c_3D_accel_PeriodicCallback
void fsl_mma8491q_i2c_3D_accel_PeriodicCallback(void *pSensorHandle)
Definition: fsl_mma8491q_i2c_3D_accel.c:396
dm_channel_acquire_lock
isf_status_t dm_channel_acquire_lock(dm_ChannelDescriptor_t *apChannelDescriptor, isf_duration_t aTimeout)
This function locks the channel for exclusive access.
Definition: isf_device_messaging.c:174
isf_dsa_result_types_t
enum isf_dsa_result_enums isf_dsa_result_types_t
MMA8491Q_STATUS_00
Definition: mma8491q.h:50
y_out_pin
const uint32_t y_out_pin
Definition: fsl_mma8491q_i2c_3D_accel.c:37
mma8491q_Sensor_Specific_Settings_t
Definition: mma8491q.h:75
bm_register_periodic_callback
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.
Definition: bus_management.c:94
MMA8491Q_SHUTDOWN
Definition: mma8491q.h:69
fsl_mma8491q_i2c_3D_accel_StartData
isf_status_t fsl_mma8491q_i2c_3D_accel_StartData(isf_SensorHandle_t *pSensorHandle)
Definition: fsl_mma8491q_i2c_3D_accel.c:260
fsl_mma8491q_i2c_3D_accel_Configure
isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Configure(isf_SensorHandle_t *pSensorHandle, isf_dsa_SensorSettings_t *pSensorSettings)
Definition: fsl_mma8491q_i2c_3D_accel.c:169
COMM_STATE_OK
Definition: isf_comm.h:28
isf_dsa_SensorSettings_st::resultFormat
isf_dsa_result_types_t resultFormat
Definition: isf_dsa_adapter.h:84
MMA8491Q_DATA_SUCCESS
Definition: mma8491q.h:62
mma8491q.h
The mma8491q.h contains register definitions and macros as well as the type definitions and public fu...
fsl_mma8491q_i2c_3D_accel.h
The fsl_mma8491q_i2c_3D_accel.h file contains the definitions and functions supporting the MMA8491q S...
isf_SensorDataTypes_t
isf_SensorDataTypes_t
Definition: isf_sensor_types.h:61
dm_channel_init
isf_status_t dm_channel_init(dm_ChannelId_t aChannelId, dm_ChannelDescriptor_t *apChannelDescriptor)
This function initializes a channel.
Definition: isf_device_messaging.c:48
bm_stop
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.
Definition: bus_management.c:349
isf_fifo_t
Definition: isf_fifo.h:19
isf_SensorHandle_st::pSensorStaticConfig
const isf_SensorConfig_t * pSensorStaticConfig
Definition: isf_dsa_adapter.h:110
bm_start
isf_status_t bm_start(boolean aSync, bm_callback_token_t aTokens)
This API sets one or more callback(s) to the active state.
Definition: bus_management.c:278
isf_dsa_SensorSettings_st::resultType
isf_SensorDataTypes_t resultType
Definition: isf_dsa_adapter.h:83
mma8491q_CheckId
void mma8491q_CheckId(int32_t *status, void *pSensorHandle)
This function validates the MMA8491Q sensor is connected via the I2C bus.
Definition: fsl_mma8491q_i2c_3D_accel.c:574
mma8491q_DataBuffer_t::zOut
uint8 zOut
The Tilt Z data from the sensor.
Definition: fsl_mma8491q_i2c_3D_accel.h:27
TYPE_ACCELEROMETER_3D
Definition: isf_sensor_types.h:20
mma8491q_DataBuffer_t
MMA8491Q data buffer format.
Definition: fsl_mma8491q_i2c_3D_accel.h:21
isf_SensorHandle_st::adapterStatus
isf_dsa_AdapterStatus_t adapterStatus
Definition: isf_dsa_adapter.h:112
int32
signed long int int32
Definition: isf_types.h:74
SensorConfig_st::channelId
sys_channelId_t channelId
Definition: isf_dsa_adapter.h:158
DSA_STATE_CONFIGURED_STOPPED
Definition: isf_dsa_adapter.h:59
DSA_ERR_PARAM
Definition: isf_dsa_adapter.h:36
en_pin
const uint32_t en_pin
Definition: fsl_mma8491q_i2c_3D_accel.c:35
isf_Acceleration3D_Float_t
Standard float type for three axis accelerometers.
Definition: isf_accelerometer_types.h:80
DSA_RESULT_TYPE_ENG_FLOAT
Definition: isf_dsa_adapter.h:49
isf_dsa_ControlData_st::nEventFieldIndex
uint32 nEventFieldIndex
Definition: isf_dsa_adapter.h:93
isf_dsa_SensorSettings_st::pSampleBufferInfo
isf_fifo_t * pSampleBufferInfo
Definition: isf_dsa_adapter.h:81
dm_device_read
isf_status_t dm_device_read(dm_DeviceDescriptor_t *apDeviceDescriptor, int32 aOffset, uint8 *apReadBuffer, uint32 aBuffsize, uint32 aNbyteRead)
This function reads from a device.
Definition: isf_device_messaging.c:271
DSA_ERR_SHUTDOWN
Definition: isf_dsa_adapter.h:35
BM_ERROR
#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_status_t
int32 isf_status_t
ISF return status type.
Definition: isf.h:76
mma8491q_SupportedDataTypes
isf_SensorDataTypes_t mma8491q_SupportedDataTypes[]
Definition: fsl_mma8491q_i2c_3D_accel.c:61
DSA_STATE_NOT_INITIALIZED
Definition: isf_dsa_adapter.h:57
isf_dsa_SensorSettings_st
This defines the DSA sensor configuration parameter structure configuring the sensor settings by a su...
Definition: isf_dsa_adapter.h:79
DeviceDescriptor_struct::pCurrentSample
void * pCurrentSample
Definition: isf_sensors.h:56
isf_dsa_ControlData_st::sensorSettings
isf_dsa_SensorSettings_t sensorSettings
Definition: isf_dsa_adapter.h:94
isf_Acceleration3D_EngFixed_t::timestamp
isf_timestamp_t timestamp
Definition: isf_accelerometer_types.h:40
fsl_mma8491q_i2c_3D_accel_Convert
isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Convert(volatile isf_SensorHandle_t *pSensorHandle, isf_SensorDataTypes_t convertToType, isf_dsa_result_types_t resultType, void *pNativeSample, void *pConvertedSample, int32 *numBytes)
Definition: fsl_mma8491q_i2c_3D_accel.c:531
fsl_mma8491q_i2c_3D_accel_Initialize
isf_dsa_status_t fsl_mma8491q_i2c_3D_accel_Initialize(isf_SensorHandle_t *pSensorHandle)
Definition: fsl_mma8491q_i2c_3D_accel.c:64
mma8491q_xyz_format
#define mma8491q_xyz_format(destination, msb, lsb)
Definition: fsl_mma8491q_i2c_3D_accel.c:32
isf_fifo_el_get_insert_pointer
void * isf_fifo_el_get_insert_pointer(isf_fifo_t *pFifo)
Routine returns the insert pointer for direct access.
Definition: isf_fifo.c:229
dm_channel_release_lock
isf_status_t dm_channel_release_lock(dm_ChannelDescriptor_t *apChannelDescriptor)
This function releases exclusive channel access.
Definition: isf_device_messaging.c:191
DSA_ERR_START_DATA
Definition: isf_dsa_adapter.h:32
SENSOR_ERROR_GETDATA
Definition: isf_sensors.h:43
fsl_mma8491q_read_data_OneShotCallback
void fsl_mma8491q_read_data_OneShotCallback(void *pSensorHandle)
Definition: fsl_mma8491q_i2c_3D_accel.c:432
dm_DeviceDescriptor_struct
This structure defines a handle for the device.
Definition: isf_devmsg.h:61
OutZ.h
isf_devmsg.h
isf_devmsg.h defines the API definitions and types for the Intelligent Sensing (ISF) Device Messaging...
TYPE_RAW_ACCELERATION_3D
Definition: isf_sensor_types.h:71
isf_accelerometer_types.h
The isf_accelerometer_types.h file contains the ISF data type definitions for use with the ISF generi...
J1_5
Definition: OutX.h:82
TYPE_NATIVE_SENSOR_DATA_TYPE
Definition: isf_sensor_types.h:63
MMA8491Q_ACTIVE
Definition: mma8491q.h:70
DeviceDescriptor_struct::deviceSemaphore
semaphore_t deviceSemaphore
Definition: isf_sensors.h:54
dm_ChannelDescriptor_struct
This structure is a declaration of a channel descriptor type.
Definition: isf_devmsg.h:50
mma8491q_SupportedSensorTypes
isf_SensorTypes_t mma8491q_SupportedSensorTypes[]
Supported sensor and data types for MMA8491q.
Definition: fsl_mma8491q_i2c_3D_accel.c:60
ISF_FIFO_FULL
#define ISF_FIFO_FULL
Definition: isf_fifo.h:33
DSA_ERR_CONFIGURE
Definition: isf_dsa_adapter.h:31
T_ON_8491Q_MICROSECONDS
#define T_ON_8491Q_MICROSECONDS
Definition: fsl_mma8491q_i2c_3D_accel.c:26
DeviceDescriptor_struct::token
bm_callback_token_t token
Definition: isf_sensors.h:53
mma8491q_getAccelData
isf_dsa_status_t mma8491q_getAccelData(DeviceDescriptor_t *pDescriptor, int16 *pAccelData)
Definition: fsl_mma8491q_i2c_3D_accel.c:600
OutY.h
- © Freescale Semiconductor, Inc. 2016. All Rights Reserved.