MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / efm_accel_auto_zero

Function efm_accel_auto_zero

bsp/efm32/dev_accel.c:542–730  ·  view source on GitHub ↗

/ * @brief * Accelerometer auto-zero calibration function * * @details * * @note * * @param[in] mode * 0, simple mode (assuming the device is placed on flat surface) * 1, interaction method * * @param[in] period * Time period to perform auto-zero calibration * * @return * Error code ******************************************************************************/

Source from the content-addressed store, hash-verified

540 * Error code
541 ******************************************************************************/
542rt_err_t efm_accel_auto_zero(rt_uint8_t mode, rt_tick_t period)
543{
544 RT_ASSERT(accel != RT_NULL);
545
546 rt_timer_t calTimer;
547 struct efm32_accel_result_t min = {0, 0, 0};
548 struct efm32_accel_result_t max = {0, 0, 0};
549 struct efm32_accel_result_t temp, sum;
550 rt_int32_t simpleOffset[] = ACCEL_CAL_1G_VALUE;
551 rt_uint8_t cmd[7] = {0};
552 rt_uint8_t i, j;
553
554 /* Reset offset */
555#if (EFM32_USING_ACCEL == EFM32_INTERFACE_ADC)
556 accelOffset.x = 0;
557 accelOffset.y = 0;
558 accelOffset.z = 0;
559
560#elif (EFM32_USING_ACCEL == EFM32_INTERFACE_IIC)
561 cmd[0] = XOFFL;
562 if (accel->write(accel, ACCEL_IIC_SLAVE_ADDRESS, cmd, sizeof(cmd)) == 0)
563 {
564 return -RT_ERROR;
565 }
566#endif
567
568 if (mode == ACCEL_CAL_SIMPLE)
569 {
570 /* Simple mode */
571 for (j = 0; j < ACCEL_CAL_ROUND; j++)
572 {
573 sum.x = 0x0;
574 sum.y = 0x0;
575 sum.z = 0x0;
576
577 for (i = 0; i < ACCEL_CAL_SAMPLES; i++)
578 {
579#if (EFM32_USING_ACCEL == EFM32_INTERFACE_IIC)
580 /* Waiting for data ready */
581 while(!GPIO_PinInGet(ACCEL_INT1_PORT, ACCEL_INT1_PIN));
582#endif
583 if (efm_accel_get_data(&temp, false) != RT_EOK)
584 {
585 return -RT_ERROR;
586 }
587 sum.x += temp.x;
588 sum.y += temp.y;
589 sum.z += temp.z;
590 }
591
592#if (EFM32_USING_ACCEL == EFM32_INTERFACE_ADC)
593 temp.x = sum.x / ACCEL_CAL_SAMPLES;
594 temp.y = sum.y / ACCEL_CAL_SAMPLES;
595 temp.z = sum.z / ACCEL_CAL_SAMPLES - simpleOffset[ACCEL_G_SELECT];
596 if ((temp.x == 0) && (temp.y == 0) && \
597 (temp.z == 0))
598 {
599 accel_debug("Accel: Offset %+d %+d %+d\n",

Callers 1

accel_calFunction · 0.85

Calls 7

GPIO_PinInGetFunction · 0.85
efm_accel_get_dataFunction · 0.85
rt_thread_delayFunction · 0.85
rt_timer_createFunction · 0.85
rt_timer_startFunction · 0.85
rt_timer_deleteFunction · 0.85
writeMethod · 0.45

Tested by

no test coverage detected