Use linker to relocate const PALConfig pal_default_config to RAM

Discussions and support about ChibiOS/RT, the free embedded RTOS.
andresv
Posts: 12
Joined: Tue Nov 29, 2016 11:09 am
Been thanked: 3 times

Use linker to relocate const PALConfig pal_default_config to RAM

Postby andresv » Mon Sep 25, 2017 12:15 pm

I would like to do a little hack. Basically I would like to choose between 2 different PALConfigs without modifying ChibiOS.
There is those lines in hal.c

Code: Select all

void halInit() {
   ...
    palInit(&pal_default_config);
   ...
}


Where pal_default_config is defined in
board.c file as

Code: Select all

const PALConfig pal_default_config = {...


and in hal_pal_lld.h as

Code: Select all

extern const PALConfig pal_default_config;


My idea is to modify pal_default_config in main.c before calling halInit(). So basically some conf is readout from flash which tells if one particular pin should be defined LOW or HIGH during palInit(1). Something like this in main.c:

Code: Select all

stm32_gpio_setup_t portb = {VAL_GPIOB_MODER, VAL_GPIOB_OTYPER, VAL_GPIOB_OSPEEDR, VAL_GPIOB_PUPDR, VAL_GPIOB_ODR | 0x01,  VAL_GPIOB_AFRL, VAL_GPIOB_AFRH};
PALConfig* config = (PALConfig*)&pal_default_config; // do not use const qualifier
if (conf == 1) {
    memcpy(&config->PBData, &portb, sizeof(stm32_gpio_setup_t));
}
halInit();
chSysInit();


It works fine if I modify const PALConfig pal_default_config (hal_pal_lld.h and board.c) to be not const. But I would prefer if hal_pal_lld.h is not touched, however it is ok to modify board.c file.

What I tried to do was to modify where this variable is linked. So I came up with this in board.c.

Code: Select all

const PALConfig pal_default_config __attribute__((section(".ram0"))) = {


But I guess section name should be something different or I have to actually modify .ld file to accomplish this.
I am using STM32F030x8

Current .ld is something like that (I remember that old ld scripts were different):

Code: Select all

/*
 * STM32F030x8 memory setup.
 */
MEMORY
{
    flash   : org = 0x08000000, len = 28k
    newapp  : org = 0x08009000, len = 28k
    ram0    : org = 0x20000000, len = 8k
    ram1    : org = 0x00000000, len = 0
    ram2    : org = 0x00000000, len = 0
    ram3    : org = 0x00000000, len = 0
    ram4    : org = 0x00000000, len = 0
    ram5    : org = 0x00000000, len = 0
    ram6    : org = 0x00000000, len = 0
    ram7    : org = 0x00000000, len = 0
}

/* For each data/text section two region are defined, a virtual region
   and a load region (_LMA suffix).*/

/* Flash region to be used for exception vectors.*/
REGION_ALIAS("VECTORS_FLASH", flash);
REGION_ALIAS("VECTORS_FLASH_LMA", flash);

/* Flash region to be used for constructors and destructors.*/
REGION_ALIAS("XTORS_FLASH", flash);
REGION_ALIAS("XTORS_FLASH_LMA", flash);

/* Flash region to be used for code text.*/
REGION_ALIAS("TEXT_FLASH", flash);
REGION_ALIAS("TEXT_FLASH_LMA", flash);
...


In short, how pal_default_config must be defined in board.c and what should be modified in .ld file to accomplish this.

User avatar
Giovanni
Site Admin
Posts: 10240
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 212 times
Been thanked: 198 times
Contact:

Re: Use linker to relocate const PALConfig pal_default_config to RAM

Postby Giovanni » Mon Sep 25, 2017 12:58 pm

Hi,

I can't see an easy way to do that.

One of the next changes to HAL is to move the board stuff out of the PAL driver and have board.c include everything, you may just wait that the change is done. Anyway, this is the current direction: remove the dependency, in the meanwhile a little hack in the code would not hurt.

Giovanni

andresv
Posts: 12
Joined: Tue Nov 29, 2016 11:09 am
Been thanked: 3 times

Re: Use linker to relocate const PALConfig pal_default_config to RAM

Postby andresv » Mon Sep 25, 2017 1:25 pm

Ok, I think that messing with liker is actually nice hack, however there is something wrong with my current approach. Probably

Code: Select all

const PALConfig pal_default_config __attribute__((section(".ram0"))) = {...

must be defined differently or some more info must be added to .ld file, but currently I do not know what exactly.

apmorton
Posts: 3
Joined: Fri Sep 29, 2017 10:26 am

Re: Use linker to relocate const PALConfig pal_default_config to RAM

Postby apmorton » Fri Sep 29, 2017 11:08 am

A dirty trick I have used to accomplish a similar goal is to skip initialization of pal_default_config entirely.
GCC will then drop it into the BSS segment even though it is defined as const - although note this may be on the edge of defined behavior, and may be subject to certain compiler optimizations/options being enabled.

in board.c you could do this:

Code: Select all

const PALConfig pal_default_config;
const PALConfig pal_default_config_real = {
   ...
};


in main.c you then do this:

Code: Select all

memcpy((PALConfig*)&pal_default_config, &pal_default_config_real, sizeof(pal_default_config));

halInit();
chSysInit();


Your approach would also work, but you are using the wrong value for the section attribute.

Code: Select all

const PALConfig pal_default_config __attribute__((section(".data")))

andresv
Posts: 12
Joined: Tue Nov 29, 2016 11:09 am
Been thanked: 3 times

Re: Use linker to relocate const PALConfig pal_default_config to RAM

Postby andresv » Fri Sep 29, 2017 11:17 am

Nice one, thanks a lot.


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 1 guest