STM32代码不工作,while循环,延迟问题

STM32 code not working, while loop, delay problems

目前,我在使用 STM32 时遇到了一个奇怪的问题。我刚刚使用 STM32Cube IDE 为所选的 MCU (STM32L031G6) 生成了代码。我几乎没有改变任何东西,除了将一个 GPIO 配置为输出并尝试让连接的 LED blink.

现在的问题:

这段代码可能有什么问题?有什么特别的吗?我是否遗漏了生成 STM32 代码所需的内容?

对于以下代码,我删除了所有未使用的行和注释。

#include "main.h"
void SystemClock_Config(void); 
static void MX_GPIO_Init(void);

int main(void) {
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();

  while(1) {
    //HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);

    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    HAL_Delay(1000);
    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
    HAL_Delay(1000);

  }
}

void SystemClock_Config(void) {   
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};   
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;   
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)   {
    Error_Handler();
  }
  
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;    
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;   
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;   
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
    Error_Handler();
  }
}

static void MX_GPIO_Init(void) {   
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  
  __HAL_RCC_GPIOA_CLK_ENABLE();

  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

  GPIO_InitStruct.Pin = LED_Pin;   
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;   
  GPIO_InitStruct.Pull = GPIO_NOPULL;   
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;   
  HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
}

void Error_Handler(void) {
  __disable_irq();   
  while (1) {}   
}

更新 1: 正如评论中所见,HAL_Delay 无法正常工作。但是如何解决呢?为什么代码在移除 HAL_Delay 后不让 LED 闪烁?

更新二: 也不能按下面的方式使用循环,但是led无论如何都不亮

while (1) {
  HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
} 

配置见下图。

更新 3: 在 STM32L031G6 上执行代码时,调试器很快就会停止。逐步执行代码(有时)。这是在 STM32Cube IDE.

中单击“运行”时的调试日志
SEGGER J-Link GDB Server V7.58 Command Line Version

JLinkARM.dll V7.58 (DLL compiled Nov  4 2021 16:23:13)

Command line: -port 2331 -s -device STM32L031G6 -endian little -speed 4000 -if swd -vd
-----GDB Server start settings-----
GDBInit file:                  none
GDB Server Listening port:     2331
SWO raw output listening port: 2332
Terminal I/O port:             2333
Accept remote connection:      localhost only
Generate logfile:              off
Verify download:               on
Init regs on start:            off
Silent mode:                   off
Single run mode:               on
Target connection timeout:     0 ms
------J-Link related settings------
J-Link Host interface:         USB
J-Link script:                 none
J-Link settings file:          none
------Target related settings------
Target device:                 STM32L031G6
Target interface:              SWD
Target interface speed:        4000kHz
Target endian:                 little

Connecting to J-Link...
J-Link is connected.
Firmware: J-Link V11 compiled Dec  9 2021 14:14:49
Hardware: V11.00
S/N: 261014681
OEM: SEGGER-EDU
Feature(s): FlashBP, GDB
Checking target voltage...
Target voltage: 3.34 V
Listening on TCP/IP port 2331
Connecting to target...
Connected to target
Waiting for GDB connection...Connected to 127.0.0.1
GDB closed TCP/IP connection (Socket 1132)
Connected to 127.0.0.1
Reading all registers
Read 4 bytes @ address 0x1FF000FC (Data = 0x89B8D002)
Read 2 bytes @ address 0x1FF000FC (Data = 0xD002)
Received monitor command: WriteDP 0x2 0xF0
O.K.
Received monitor command: ReadAP 0x2
O.K.:0xF0000003
Read 4 bytes @ address 0x1FF000E4 (Data = 0x05408A28)
Read 2 bytes @ address 0x1FF000E4 (Data = 0x8A28)
Read 4 bytes @ address 0x1FF000E4 (Data = 0x05408A28)
Read 2 bytes @ address 0x1FF000E4 (Data = 0x8A28)
Reading 32 bytes @ address 0xF0000FD0
Connected to 127.0.0.1
Reading all registers
Read 4 bytes @ address 0x1FF000FC (Data = 0x89B8D002)
Read 2 bytes @ address 0x1FF000FC (Data = 0xD002)
Received monitor command: reset
Resetting target
Downloading 192 bytes @ address 0x08000000 - Verified OK
Downloading 6072 bytes @ address 0x080000C0 - Verified OK
Downloading 28 bytes @ address 0x08001878 - Verified OK
Downloading 8 bytes @ address 0x08001894 - Verified OK
Downloading 4 bytes @ address 0x0800189C - Verified OK
Downloading 4 bytes @ address 0x080018A0 - Verified OK
Downloading 12 bytes @ address 0x080018A4 - Verified OK
Writing register (PC = 0x 80006d0)
Starting target CPU...
GDB closed TCP/IP connection (Socket 1128)
Debugger requested to halt target...
...Target halted (PC = 0x1FF000E4)
Reading all registers
Read 4 bytes @ address 0x1FF000E4 (Data = 0x05408A28)
Read 2 bytes @ address 0x1FF000E4 (Data = 0x8A28)
GDB closed TCP/IP connection (Socket 1152)
Restoring target state and closing J-Link connection...
Shutting down...

另一方面,相同的代码在 nucleo 板的 STM32L031K6 上工作,ST link 断开连接。

*更新四: 由于我使用的是定制板,原理图可能存在缺陷。我没有看到电路有任何问题,但也许,你看到了一些。没有 crystal 因为数据表不需要它。有可用的内部振荡器。

TOUCH 网络只是一个电路,如果按下按钮,它会将 GND 连接到引脚。

这是STM32L031G6U6的电路。

这是应该控制的LED电路。在前面的代码中,我只是尝试用网络标签 STATUS_LED 来控制 LED。由于我在逐步执行代码时将 LED 设置为 blink,因此 Mosfet 电路应该可以工作。

我现在很困惑为什么我会遇到这么多问题。试过同样电路的第二块和第三块PCB,问题都是一样的

我发现,我不能使用任何时钟配置,尽管它们由 STM32Cube IDE 提供。使用 MSI 只是不适用于某些频率。代码将在 SystemClock_Config 设置振荡器或时钟时停止。

问题已解决。我下载的Altium包是针对STM32L031的错误包。它适用于 STM32L031G6U6S 而不是 STM32L031G6U6。