【每周学习摘要12(23/12/09-23/12/15)】

STM32

STM32F334

官方HAL库如果用gdb调试则会卡在时钟设置,
但是用ChibiOS 的HAL/RT则没有任何问题。
尝试探索原因才发现,RCC->CR和RCC->CFGR并非上电后自动变成0,比如CFGR里面的SW一开始就是选择PLL的,但是官方HAL库没有进行检查
因此ChibiOS的启动文件里面手动对寄存器进行了清零
下面是ChibiOS里面的STM32F3xx/hal_lld.c的初始化函数,
HSI是默认开启的,因为手册里写RCC->CR resetval=0x0000 XX83
这里做了一次检查保证HSI先开起来。
总流程大概是

  • 1.先检查HSI开启
  • 2.配置CFGR,切换时钟源为HSI
  • 3.清空CFGR和CR(为初始值)
    HSI校正是自动的,

    CFGR2(HSEPrescale)不能写入

    要提前将SW切换到HSI


void stm32_clock_init(void) {

#if !STM32_NO_INIT
  /* HSI setup, it enforces the reset situation in order to handle possible
     problems with JTAG probes and re-initializations.*/
  RCC->CR |= RCC_CR_HSION;                  /* Make sure HSI is ON.         */
  while (!(RCC->CR & RCC_CR_HSIRDY))
    ;                                       /* Wait until HSI is stable.    */

  /* HSI is selected as new source without touching the other fields in
     CFGR. Clearing the register has to be postponed after HSI is the
     new source.*/
  RCC->CFGR &= ~RCC_CFGR_SW;                /* Reset SW, selecting HSI.     */
  while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
    ;                                       /* Wait until HSI is selected.  */

  /* Registers finally cleared to reset values.*/
  RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value.              */
  RCC->CFGR = 0;                            /* CFGR reset value.            */

然后在STM32官方HAL库的HAL_RCC_OscConfig函数最前面加上清零

HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef  *RCC_OscInitStruct)
{
  uint32_t tickstart;
  uint32_t pll_config;
#if defined(RCC_CFGR_PLLSRC_HSI_PREDIV)
  uint32_t pll_config2;
#endif /* RCC_CFGR_PLLSRC_HSI_PREDIV */
  // Added by TonyZhang, following ChibiOS's startup file
    RCC->CFGR &= ~RCC_CFGR_SW;                /* Reset SW, selecting HSI.     */
    while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
        ;                                       /* Wait until HSI is selected.  */
    RCC->CR &= RCC_CR_HSITRIM | RCC_CR_HSION; /* CR Reset value.              */
    RCC->CFGR = 0;

对于什么是HSE Bypass

寄存器里可以配置HSE(告诉外部晶振)是否bypass,
如果选择bypass, 则接收外部时钟信号(有源晶振)
如果关闭bypass,则外部为无源晶振+陶瓷电容组成的振荡器,内部会开启反相器构成皮尔斯振荡器进行震荡

Leave a Reply

Your email address will not be published. Required fields are marked *