pill_blink icon indicating copy to clipboard operation
pill_blink copied to clipboard

Can you help me grok your bare_metal code?

Open kxygk opened this issue 6 years ago • 1 comments

Hey,

I really love the write up you did and I'm trying to expand on it using CMake It's working, but kinda WIP - https://geokon-gh.github.io/bluepill/ (and I need to add a link to your great work :smile: )

I'm trying to understand how you setup the linker script and vector table in bare-metal/

I get the last section of the linker script: You tell the linker you want the interrupt vector table to come first in ROM, followed by the code. Then in the code you define this vector table

__attribute__ ((section(".vectors")))
struct {
    unsigned int *initial_sp_value;
    void (*reset)(void);
    void (*nmi)(void);
    void (*hard_fault)(void);
    void (*memory_manage_fault)(void);
    void (*bus_fault)(void);
    void (*usage_fault)(void);
    void (*reserved_x001c[4])(void);
    void (*sv_call)(void);
    void (*debug_monitor)(void);
    void (*reserved_x0034)(void);
    void (*pend_sv)(void);
    void (*systick)(void);
    void (*irq[68])(void);
} vector_table = {
    .reset = reset_handler,
};

There are two pieces i don't quite get. 1 - Why do you need to declare the vector_table extern in the linker script at all? 2 - In that last line are you setting the .reset value from the vector table to be a pointer to the reset handler AND also assigning the initial_sp_value to be the reset handler? (that's how it's supposed to go anyway.. right?)

(b/c my_struct = func_ptr will assign func_ptr to the first value in the struct?)

kxygk avatar Aug 26 '18 09:08 kxygk

Haven't looked at this in a while, but I think I basically created the bare metal example by reverse-engineering what the higher-level library code examples generated. In this case, the vector table is extern so the linker can link and put it at a known fixed address for the CPU to find it. Not sure if everything is strictly needed (try removing further code until it breaks?).

I don't think my_struct = func_ptr would work, or at least not well, but the { .foo = } syntax allows initializing certain values of the struct which is very useful in this case since only the reset handler is required. Isn't initial_sp_value for the stack pointer, not the reset vector (program counter)?

satoshinm avatar Feb 20 '19 03:02 satoshinm