STM32WB HSE校准
通过改变RCC_HSECR寄存器中的HSETUNE[5:0]位域的值来校准HSE的输出频率
1、将HSE时钟配置为MCO模式输出到PA8引脚
HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1);
2、改变RCC_HSECR寄存器中的HSETUNE[5:0]位域的值(0-63)
LOOP((0<=p_otp->hse_tuning<=63):
__HAL_RCC_HSE_CONFIG(RCC_HSE_OFF);
while (LL_RCC_HSE_IsReady());
LL_RCC_HSE_SetCapacitorTuning(p_otp->hse_tuning);
__HAL_RCC_HSE_CONFIG(RCC_HSE_ON);
while (LL_RCC_HSE_IsReady());
通过不断改变p_otp->hse_tuning的值调整HSE的时钟频率直到PA8引脚上的时钟信号满足要求
3、保存HSETUNE[5:0]位域的值,将校准后的值保存在OTP区
#ifdef __GNUC__
typedef struct __attribute__((packed))
#else
typedef __packed struct
#endif
{
uint8_t additional_data[6]; /*!< 48 bits of data to fill OTP slot (e.g: BD or MAC address, key..) */
uint8_t hse_tuning; /*!< Load capacitance to be applied on HSE pad */
uint8_t index; /*!< Structure index */
} OTP_DATA_t;
OTP_DATA_t otp_data;
memcpy(otp_data.additional_data, (void*)ADDITIONAL_DATA_PTR, 6);
otp_data.hse_tuning = val & 0x3F;
otp_data.index = OTP_HSE_STR_IDX;
idx = GetOTPFreeIdx();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); /* Clear all Flash flags before write operation*/
err = HAL_FLASH_Unlock();
err |= HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, STORE_ADDRESS + idx, *(uint64_t*) (&otp_data));
if (err != HAL_OK) {
ErrorHandler();
}
OTP_DATA_t *potp_data;
* if idx<0 it means that no structure with idx=OTP_HSE_STR_IDX has been found in OTP;
* => so calibration is not done yet
*/
idx = CheckOTPIndex(OTP_HSE_STR_IDX);
if (idx >= 0)
{
potp_data = (OTP_DATA_t *) (STORE_ADDRESS + idx);
/* load capacitance value is then set in HSE config register */
LL_RCC_HSE_SetCapacitorTuning(potp_data->hse_tuning);
return (HAL_OK);
else
{
return (HAL_ERROR);
}
当校准之后后续程序启动就只有第4步骤,前123步骤是校准时候需要做的