Arm Mbed OS support forum

[NUCLEO-F103RB ] Help my code!

I am trying to turn on the built-in LED by register of NUCLEO-F103RB.
I referred to datasheet and reference manual.
But LED didn’t turn on.
My code is below.
Help me plz.

=======================

#include “mbed.h”

int main()
{
(*(volatile unsigned )0x40021018) |= 0x4; // active
(
(volatile unsigned )0x40010800) |= 0xFFFFFF0F;
(
(volatile unsigned *)0x40010800) |= 0x900000;
}

Hello Lee,

Before writing to a GPIO we should initialize the related port and pin (Pin 5 of Port A in this case):

  • Enable GPIO clock.
  • Fill GPIO object structure.
  • Set pin mode.
  • Configure internal pull resistor.
#include "mbed.h"
#include "stm32f1xx_ll_gpio.h"
#include "pin_device.h"

gpio_t                  led_gpio;
extern const uint32_t   ll_pin_defines[16];
extern "C" GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);

int main()
{
    // Enable GPIO clock
    GPIO_TypeDef *gpio = Set_GPIO_Clock(PortA);

    // Fill GPIO object structure
    led_gpio.mask    = (uint32_t)(1 << (uint32_t)(0x05) & 0xF);  // pin 5
    led_gpio.gpio    = gpio;
    led_gpio.ll_pin  = ll_pin_defines[((uint32_t)(0x05) & 0xF)]; // pin 5
    led_gpio.reg_in  = &gpio->IDR;
    led_gpio.reg_set = &gpio->BSRR;
    led_gpio.reg_clr = &gpio->BRR;

    // Set pin mode
    register uint32_t *pReg = (uint32_t *)((uint32_t)((uint32_t)(&led_gpio.gpio->CRL) + (led_gpio.ll_pin >> 24)));
    MODIFY_REG(*pReg, ((GPIO_CRL_CNF0 | GPIO_CRL_MODE0) << (POSITION_VAL(led_gpio.ll_pin) * 4U)), (LL_GPIO_MODE_OUTPUT << (POSITION_VAL(led_gpio.ll_pin) * 4U)));

    // Configure internal pull resistor. 
    // Default is GPIO_NOPULL so no action is needed.

    // Turn on the LED (GPIO PA_5).
    *led_gpio.reg_set = led_gpio.mask;

    return 0;
}

Tnk, I’ll try this.