• 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();

    /* Store OTP structure in OTP area */
     __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));
      err |= HAL_FLASH_Lock();
      if (err != HAL_OK) {
        ErrorHandler();
      }
    4、校准完成后重新上电获取OTP保存的值并写入HSETUNE[5:0]位域,以后HSE输出的时钟就是校准后的精确时钟
      int32_t idx;
      OTP_DATA_t *potp_data;
      /* Get last calibration index in OTP area
      * 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步骤是校准时候需要做的

  • 相关阅读:
    xadmin可视化上传图片
    home数据库设计
    xadmin后台管理
    静态目录
    Git线上操作
    python 学习之JavaScript
    python学习之CSS
    python学习之HTML
    Python之旅(day10&day11 各种运算及基本数据类型)
    python之旅(第一课day9)
  • 原文地址:https://www.cnblogs.com/yeshenmeng/p/10838441.html
Copyright © 2020-2023  润新知