STM 32 cuteMXが自動的に生成するコードにIAPを付けるとHardFault原因が発生する


STM 32 cuteMXが自動的に生成するコードにIAPを付けるとHardFaultが生成されます


以前は標準ライブラリでSTM 32 F 407チップに基づいてプロジェクトを開発していたが、今年はコードを再整理する時間があり、STM 32 cuteMXを使用して自動的にコードを生成し、IAP層がない場合は正常に動作するが、IAP層が加入するとHardFaultが頻繁に発生し、エラーのたびに位置が異なり、法定ビットがない.その後、問題を調べたところ、IAPの動作周波数は168 Mで、APP層はハードウェアの安定化のために120 Mに周波数を下げた.
  • IAPの修正を試みる頻度は120 Mで、APP層ではHardFaultは現れなくなったが、実行速度はIAPのないAPPよりずっと遅い.
  • 以前の標準ライブラリプログラムを比較すると、標準ライブラリはSystemInit()に外部クロックをオフにしてCCRをリセットする操作があり、既存のコードに移植して正常であることが分かった.
  • 修正前のコード
  • void SystemInit(void)
    {
      /* FPU settings ------------------------------------------------------------*/
      #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
        SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
      #endif
    #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
      SystemInit_ExtMemCtl(); 
    #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
    
      /* Configure the Vector Table location add offset address ------------------*/
    #ifdef VECT_TAB_SRAM
      SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
    #else
      SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
    #endif
    }
    
  • 修正後のコード
  • void SystemInit(void)
    {
      /* FPU settings ------------------------------------------------------------*/
      #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
        SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
      #endif
      /* Reset the RCC clock configuration to the default reset state ------------*/
      /* Set HSION bit */
    	//IAP , RCC , 
      RCC->CR |= (uint32_t)0x00000001; // 
    
      /* Reset CFGR register */
      RCC->CFGR = 0x00000000;			// 
    
      /* Reset HSEON, CSSON and PLLON bits */
      RCC->CR &= (uint32_t)0xFEF6FFFF;  // 、 、PLL
    
      /* Reset PLLCFGR register */
      RCC->PLLCFGR = 0x24003010;        // PLL ,  0x24003010 Q=4, P=8, N=192, M=16
    
      /* Reset HSEBYP bit */
      RCC->CR &= (uint32_t)0xFFFBFFFF;   // HSE 
    
      /* Disable all interrupts */
      RCC->CIR = 0x00000000;	           //   
    #if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
      SystemInit_ExtMemCtl(); 
    #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */
    
      /* Configure the Vector Table location add offset address ------------------*/
    #ifdef VECT_TAB_SRAM
      SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
    #else
      SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
    #endif
    }