2025-03-02 12:04 PM - edited 2025-03-02 12:04 PM
I am trying to turn on the LD3 LED (connected to PB3) on the NUCLEO-L031K6 board using direct register access without libraries, but it is not working. The LED does not turn on. I have configured the pin control register, enabled the GPIOB clock, and set pin PB3 as output, but the LED still does not light up.
Here is the code I am using:
// NUCLEO-L031K6 #define PERIPH_BASE (0x40000000UL) #define AHB_OFFSET (0x00020000UL) #define AHB_BASE (PERIPH_BASE + AHB_OFFSET) #define IO_OFFSET (0x10000000UL) #define IOPORT_BASE (PERIPH_BASE + IO_OFFSET) #define GPIOB_OFFSET (0x0400UL) #define GPIOB_BASE (IOPORT_BASE + GPIOB_OFFSET) #define RCC_OFFSET (0x00021000UL) #define RCC_BASE (AHB_BASE + RCC_OFFSET) #define RCC_IOPENR_OFFSET (0x2CUL) #define RCC_IOPENR (*(volatile unsigned int *)(RCC_BASE + RCC_IOPENR_OFFSET)) #define GPIOPBEN (1U<<1) #define GPIOBMODER_OFFSET (0x00UL) #define GPIO_MODER (*(volatile unsigned int *)(GPIOB_BASE + GPIOBMODER_OFFSET)) #define GPIO_ODR_OFFSET (0x14UL) #define GPIO_ODR (*(volatile unsigned int *)(GPIOB_BASE + GPIO_ODR_OFFSET)) #define GPIOB_PIN3 (1U<<3) // LD3 user LED int main() { // Enable GPIOB clock RCC_IOPENR |= GPIOPBEN; // Set GPIOB pin 3 as output GPIO_MODER |= (1U<<6); GPIO_MODER &=~ (1U<<7); GPIO_ODR |= GPIOB_PIN3; // Turn on LED (set PB3 to HIGH) while (1) { } }
Details:
However, the LED is not turning on. Is there something I'm missing in the configuration?
What I've tried:
What else can I check or correct to turn on the LED on this board?
2025-03-03 12:51 PM
Nothing prevents you from studying the startup files created by CubeMX. At least these are guaranteed to work. That's what I do with the folks during trainings/workshops. I don't think finding typos in address constants brings great educational value.