STM32F4程序在按下复位键后只会运行

STM32F4 Program Will Only Run After Pressing Reset Button

我最近购买了一块 NUCLEO-F446RE 板(一种 STM32F4 产品),一个小问题一直困扰着我。我编写的所有代码都可以正常执行和工作,但它们只有在按下 NUCLEO 板上的重置按钮后才能工作。

IDE: Keil v5

例如,我为闪烁的 LED 编写了代码:

#include "stm32f446xx.h"    
int main(void) {

    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
    GPIOA->MODER |= GPIO_MODER_MODE5_0;
    GPIOA->ODR |= GPIO_ODR_OD5;
    volatile int i;

    while(1) {

        for(i=0; i<100000; i++)
            GPIOA->ODR |= GPIO_ODR_OD5;

        for(i=0; i<100000; i++)
            GPIOA->ODR &= ~GPIO_ODR_OD5;
    }
}

我运行将代码下载到板上后,什么也不会发生。一旦我按下重置,LED 就会按预期闪烁。

我相当确定它没有包含在我的代码中,因为当我 运行 示例程序时它会立即执行。

例如KIEL提供的闪烁LED:

#include <stdio.h>

#include "Board_LED.h"                  // ::Board Support:LED
#include "Board_Buttons.h"              // ::Board Support:Buttons

#include "stm32f4xx.h"                  // Device header


extern int stdout_init (void);

volatile uint32_t msTicks;                            /* counts 1ms timeTicks */
/*----------------------------------------------------------------------------
 * SysTick_Handler:
 *----------------------------------------------------------------------------*/
void SysTick_Handler(void) {
  msTicks++;
}

/*----------------------------------------------------------------------------
 * Delay: delays a number of Systicks
 *----------------------------------------------------------------------------*/
void Delay (uint32_t dlyTicks) {
  uint32_t curTicks;

  curTicks = msTicks;
  while ((msTicks - curTicks) < dlyTicks) { __NOP(); }
}

/*----------------------------------------------------------------------------
 * SystemCoreClockConfigure: configure SystemCoreClock using HSI
                             (HSE is not populated on Nucleo board)
 *----------------------------------------------------------------------------*/
void SystemCoreClockConfigure(void) {

  RCC->CR |= ((uint32_t)RCC_CR_HSION);                     /* Enable HSI */
  while ((RCC->CR & RCC_CR_HSIRDY) == 0);                  /* Wait for HSI Ready */

  RCC->CFGR = RCC_CFGR_SW_HSI;                             /* HSI is system clock */
  while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI);  /* Wait for HSI used as system clock */

  FLASH->ACR  = (FLASH_ACR_PRFTEN     |                    /* Enable Prefetch Buffer */
                 FLASH_ACR_ICEN       |                    /* Instruction cache enable */
                 FLASH_ACR_DCEN       |                    /* Data cache enable */
                 FLASH_ACR_LATENCY_5WS );                  /* Flash 5 wait state */

  RCC->CFGR |= (RCC_CFGR_HPRE_DIV1  |                      /* HCLK = SYSCLK */
                RCC_CFGR_PPRE1_DIV2 |                      /* APB1 = HCLK/2 */
                RCC_CFGR_PPRE2_DIV1  );                    /* APB2 = HCLK/1 */

  RCC->CR &= ~RCC_CR_PLLON;                                /* Disable PLL */

  /* PLL configuration:  VCO = HSI/M * N,  Sysclk = VCO/P */
  RCC->PLLCFGR = ( 16ul                   |                /* PLL_M =  16 */
                 (200ul <<  6)            |                /* PLL_N = 200 */
                 (  0ul << 16)            |                /* PLL_P =   2 */
                 (RCC_PLLCFGR_PLLSRC_HSI) |                /* PLL_SRC = HSI */
                 (  7ul << 24)            |                /* PLL_Q =   7 */
                 (  2ul << 28)             );              /* PLL_R =   2 */

  RCC->CR |= RCC_CR_PLLON;                                 /* Enable PLL */
  while((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP();           /* Wait till PLL is ready */

  RCC->CFGR &= ~RCC_CFGR_SW;                               /* Select PLL as system clock source */
  RCC->CFGR |=  RCC_CFGR_SW_PLL;
  while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);  /* Wait till PLL is system clock src */
}

/*----------------------------------------------------------------------------
 * main: blink LED and check button state
 *----------------------------------------------------------------------------*/
int main (void) {
  int32_t max_num = LED_GetCount();
  int32_t num = 0;

  SystemCoreClockConfigure();                              /* configure HSI as System Clock */
  SystemCoreClockUpdate();

  LED_Initialize();
  Buttons_Initialize();
  stdout_init();                                           /* Initializ Serial interface */

  SysTick_Config(SystemCoreClock / 1000);                  /* SysTick 1 msec interrupts */

  for (;;) {
    LED_On(num);                                           /* Turn specified LED on */
    Delay(500);                                            /* Wait 500ms */
    while (Buttons_GetState() & (1 << 0));                 /* Wait while holding USER button */
    LED_Off(num);                                          /* Turn specified LED off */
    Delay(500);                                            /* Wait 500ms */
    while (Buttons_GetState() & (1 << 0));                 /* Wait while holding USER button */

    num++;                                                 /* Change LED number */
    if (num >= max_num) {
      num = 0;                                             /* Restart with first LED */
    }

    printf ("Hello World\n\r");
  }

}

示例代码似乎没有任何特殊之处可以立即 运行。

非常感谢任何帮助。

我猜它在项目闪存工具设置中,可能是 "run to main" 或 "Load Application at Startup"(不是您的代码)。要检查,copy/paste 您的代码位于其中一个示例的顶部。

我遇到了同样的问题。在 Keil 中,您需要转到 "Flash -> Configure Flash Tools -> Utilities" 并打开 "Run and reset" 选项

此致:)

在基尔遵循以下步骤:

  1. 转到“Flash”选项。
  2. 在 Flash 中,转到“配置 Flash 工具”。
  3. 转到最后一个bar/option“Utilities
  4. 在实用程序中选择“设置”选项。
  5. 最后,勾选“运行 和重置”选项。

上传代码,无需点击重置按钮即可运行。