/** * @brief Update CSR register with the measured LSI frequency. * @par Note on the APR calculation: * A is the integer part of LSIFreqkHz/4 and x the decimal part. * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1] * but we know that A + x = LSIFreqkHz/4 ==> 4x = LSIFreqkHz-4A * so [F1] can be written : * 4A >= (LSIFreqkHz-4A)(1+2A) * @param LSIFreqHz Low Speed RC frequency measured by timer (in Hz). * @retval None * @par Required preconditions: * - BEEP must be disabled to avoid unwanted interrupts. */ void BEEP_LSICalibrationConfig(uint32_t LSIFreqHz) { uint16_t lsifreqkhz; uint16_t A; /* Check parameter */ assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz)); lsifreqkhz = (uint16_t)(LSIFreqHz / 1000); /* Converts value in kHz */ /* Calculation of BEEPER calibration value */ BEEP->CSR &= (uint8_t)(~BEEP_CSR_BEEPDIV); /* Clear bits */ A = (uint16_t)(lsifreqkhz >> 3U); /* Division by 8, keep integer part only */ if ((8U * A) >= ((lsifreqkhz - (8U * A)) * (1U + (2U * A)))) { BEEP->CSR |= (uint8_t)(A - 2U); } else { BEEP->CSR |= (uint8_t)(A - 1U); } }
/** * @brief Update APR register with the measured LSI frequency. * @par Note on the APR calculation: * A is the integer part of lsifreqkhz/4 and x the decimal part. * x <= A/(1+2A) is equivalent to A >= x(1+2A) and also to 4A >= 4x(1+2A) [F1] * but we know that A + x = lsifreqkhz/4 ==> 4x = lsifreqkhz-4A * so [F1] can be written : * 4A >= (lsifreqkhz-4A)(1+2A) * @param[in] LSIFreqHz Low Speed RC frequency measured by timer (in Hz). * @retval None * @par Required preconditions: * - AWU must be disabled to avoid unwanted interrupts. */ void AWU_LSICalibrationConfig(u32 LSIFreqHz) { u16 lsifreqkhz = 0x0; u16 A = 0x0; /* Check parameter */ assert_param(IS_LSI_FREQUENCY_OK(LSIFreqHz)); lsifreqkhz = (u16)(LSIFreqHz / 1000); /* Converts value in kHz */ /* Calculation of AWU calibration value */ A = (u16)(lsifreqkhz >> 2U); /* Division by 4, keep integer part only */ if ((4U * A) >= ((lsifreqkhz - (4U * A)) * (1U + (2U * A)))) { AWU->APR = (u8)(A - 2U); } else { AWU->APR = (u8)(A - 1U); } /* Set the MR bit to load the new value to the prescalers */ AWU->CSR |= AWU_CSR_MR; }