Search code examples
cstm32gpio

Turning LED on in Nucleo 64 F103RB via Registers


I have a STM32 NUCLEO-64 F103RB and I am using Keil uVision 5. I was just testing the device with CubeMX software and was able to blink or turn on the green LED in my device. I then decided to do the same by changing the values in the registers on the board in C code directly. This is an schematic of the device:

enter image description here

Where I have highlited the GPIO A and the Bus that connects to the referred port. According to my understanding, two things should be done before actually turning the LED on: 1 - Activating the clock for the APB2 bus 2 - Setting the GPIOA Port 5 (which corresponds to the LED) to output mode.

I have done these two steps. However, the LED still won't turn on. I've tried looking at the documentation and found that the PA5 could be used as SPI, and I tried to change the register AFIO_MAPR (Page 184 of the reference manual) but that also didn't work. I tried looking at the clock activation for AHB2, but I didn't quite understand how it would work. My C code is:

#include "stm32f10x.h"                  // Device header


int main() {
// Initialise clock of APB2 Bus
    RCC->APB2ENR = (RCC->APB2ENR & 0x0) | RCC_APB2ENR_IOPAEN;

// Put the GPIOA in Output mode
    GPIOA->CRL = (GPIOA->CRL & 0x44444444) | GPIO_CRL_MODE5_1;


// Changinging the ODR Register (Lighting the LED)
    while(1) {
    GPIOA->ODR = (GPIOA->ODR & 0x0) | GPIO_ODR_ODR5;
    }

}

Nucleo64 F103RB Reference Manual

Nucleo64 F103RB User Manual


Solution

  • What step?

    1. Enable GPIOA clock.
    2. Configure pin to be push-pull output.
    3. Toggle the pin.

    For general GPIO do not set any AFIOs.

    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
    __DSB();
      GPIOA -> CRL &= ~GPIO_CRL_CNF5_Msk;
      GPIOA -> CRL |= GPIO_CRL_MODE5_Msk;
    
      while(1)
      {
          GPIOA -> ODR ^= GPIO_ODR_ODR5;
          for(volatile unsigned x = 0; x < 500000; x++);
      }
    

    you need also to check the solder bridges on the board: enter image description here