cancel
Showing results for 
Search instead for 
Did you mean: 

LED not turning on on Nucleo-L031K6 with direct GPIO register access (bare-metal)

kuhicop
Associate

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:

  • Clock enabling: I am enabling the clock for GPIOB with RCC_IOPENR |= GPIOPBEN.
  • Pin configuration as output: I am setting PB3 as output by modifying the MODER register (clearing and setting bits 6 and 7 of GPIOB_MODER).
  • Turn on the LED: I am writing a 1 to the ODR register to set PB3 to HIGH and turn on the LED.

However, the LED is not turning on. Is there something I'm missing in the configuration?

What I've tried:

  • I have tried with and without the code that sets GPIOB as output.
  • I've also checked that there are no obvious hardware issues.

What else can I check or correct to turn on the LED on this board?

10 REPLIES 10

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.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice