cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO-L011K4 Low Level Blinky not working

tim-hilt
Visitor

I'm starting to learn about low-level embedded development. My goal is to learn how I can write my own linker scripts and startup code. I also want to learn how to use raw register calls to do something useful. I want to use the NUCLEO-L011K4 board and tried reading through the reference manual to find register addresses and the expected values.

For some reason, my minimal blinky (LED connected to PB13) app doesn't work. It flashes just fine, but the board does nothing after that.

I am pretty sure (keep in mind: Still a newbie), that the registers are the right ones. However, I'm not so sure about the linker script.

Here's my linker script:

 

MEMORY 
{
    FLASH (rx)  : ORIGIN = 0x08000000, LENGTH = 16K
    RAM   (rwx) : ORIGIN = 0x20000000, LENGTH = 2K
}

ENTRY(main)
__reset_stack_pointer = ORIGIN(RAM) + LENGTH(RAM);

SECTIONS {
    .text : {
        LONG(__reset_stack_pointer);
        LONG(main | 1);
        /* The whole interrupt table is 332 bytes long. Advance to that position. */
        . += 332;
        *(.text)
        *(.rodata*)
        . = ALIGN(4);
    } > FLASH
}

 

and here's my main.c:

 

void main(void)
{
  volatile int *RCC_IOPENR = (int *)0X4002102C;
  volatile int *GPIOB_MODER = (int *)0X50000400;
  volatile int *GPIOB_ODR = (int *)0X50000410;

  *RCC_IOPENR |= 0b10;        // Enable GPIO Port B
  *GPIOB_MODER |= 0b10 << 26; // Set Pin 13 as output

  while (1)
  {
    *GPIOB_ODR ^= 0x0020; // Toggle output Pin

    for (int i = 0; i < 1000000; i++)
    {
      __asm__("nop");
    }
  }
}

 

I compile this code with

 

arm-none-eabi-gcc main.c -T LinkerScript.ld -o blink.elf -mcpu=cortex-m0plus -mthumb -nostdlib -Os

 

and I flash it to the target using the integrated STLink and this OpenOCD command:

 

openocd -f interface/stlink.cfg -f target/stm32l0.cfg -c "program blink.elf verify reset exit"

 

The commands are adapted from this blogpost, which is for the blue pill board and Zig. I adapted the code to C and it worked as expected.

I'm not sure if there are other things required in the linker script or if the size of the NVIC table is a different one for the L011K4. I have not found its size in any documentation.

Please tell me if this is not the right place to ask such questions. This is my first post to the forum and I'm still trying to find my way around.

Thanks!

3 REPLIES 3
gbm
Lead III

1. Use ST headers with register/bit definitions to avoid extra work and extra mistakes.

2. 0b10 MODER field means AF, 0b01 is OUT.

3. xor with 0x20 toggles bit 7, not 13.

4. Don't write to ODR, use BSRR and BRR to change output state.

5. Your "delay loop" may or may not delay at all.

6. Vector table size is 48 words for every Cortex-M0(+).

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

@gbm wrote:

5. Your "delay loop" may or may not delay at all.


Need to declare i as volatile.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Pavel A.
Evangelist III

>My goal is to learn how I can write my own linker scripts and startup code. I also want to learn how to use raw register calls to do something useful.

Quite strange goal. These days, folks learn edge AI, vision, motor control, communication. All for drones and robotics, all for the victory. Linker scripts... Chatgpt will write you a dozen, and explain.