who9vy
who9vy

Reputation: 1101

STM32 code not working, while loop, delay problems

Currently, I'm facing a weird problem with the STM32. I just generated code with the STM32Cube IDE for the chosen MCU (STM32L031G6). I nearly didn't change anything, except configuring one GPIO as output and trying to let a connected LED blink.

Now the problem:

What can be the problem with this code? There is nothing special about it? Did I miss something which is required for generated STM32 code?

For the following code I removed any unused line and comment.

#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) {}   
}

Update 1: As seen in the comments the HAL_Delay is not working properly. But how to fix it? And why the code does not let the led flicker when the HAL_Delay is removed?

Update 2: It is also not possible to use the loop the following way, but the led is not turned on in any way.

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

See the following images for the configuration. Pin Configuration Overview GPIO Configuration NVIC Configuration SYS Configuration Clock Configuration

Update 3: When executing the code on the STM32L031G6, the debugger stops pretty soon. Stepping through the code works (sometimes). Here is the debug log when clicking "Run" in the 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...

On the other hand, the same code works on a STM32L031K6 of the nucleo board with the ST link disconnected.

*Update 4: Since I'm using a custom board, there may be a flaw in the schematics. I don't see any issues with the circuit, but maybe, you see some. There is no crystal since it shouldn't be required regarding the datasheet. There are internal oszillators available.

The TOUCH net is just a circuit which connects GND to the pin if a button is pressed.

This is the circuit of the STM32L031G6U6. STM32L031G6U6

This is the circuit of the LEDs that should be controlled. In the previous code I just try to control the LED with the net label STATUS_LED. Since I got the LED to blink while stepping through the code, the Mosfet circuit should work. enter image description here

I'm currently very confused why I got so many problems. I tried a second and third PCB of the same circuit, but the problems are the same.

I figured out, that I cannot use any clock configurations although they are offered by the STM32Cube IDE. Using the MSI just doesn't work for some frequencies. The code will stall in the SystemClock_Config setting the oscillator or the clock.

Upvotes: 0

Views: 2520

Answers (1)

who9vy
who9vy

Reputation: 1101

Problem solved. The Altium package I downloaded was for the wrong package of the STM32L031. It ia for the STM32L031G6U6S and not the STM32L031G6U6.

Upvotes: 0

Related Questions