_start和main之间的平台配置丢失

问题描述

我正在使用https://github.com/ARM-software/CMSIS_5/blob/develop/Device/ARM/ARMCM33/Source/startup_ARMCM33.c文件进行以下修改

_NO_RETURN void Reset_Handler(void)
{
  __set_MSPLIM((uint32_t)(&__STACK_LIMIT));

  SystemInit();                             /* CMSIS System Initialization */
  lpuart_init(&m33_uart,(void*)LPUART0_BASE,LPUART); // Initialize LPUART
  __PROGRAM_START();                        /* Enter PreMain (C library entry point) */
}

__PROGRAM_START();将跳至_start,它将执行crt0.o中提到的所有运行时配置,然后它将跳至main(请参阅https://embeddedartistry.com/blog/2019/04/08/a-general-overview-of-what-happens-before-main/有关更多详细信息)。

在以上代码段中,我正在_start之前进行LPUART初始化。调试.elf之后,我知道当程序到达main时,此LPUART初始化会丢失。 出乎意料的是,如果我在main内进行LPUART初始化,则同一程序可以工作:

void main() {
lpuart_init(&m33_uart,LPUART);

/* some more code 
...... */
}

似乎crt0.o所做的事情会导致LPUART(或平台)配置丢失。我不知道原因。有帮助吗?

编辑:

void lpuart_init(lpuart_info_t* p_info,void* base_addr,uint32_t version)
{
    p_info->base_addr = base_addr;
    p_info->version = version;
}

解决方法

在执行函数后, 会初始化全局变量。因此,.data and .bss segments中对变量的任何修改都将丢失,并被静态初始化例程覆盖。

删除正在执行的方法,并使用gcc不可移植扩展名__attribute__((__constructor__))在main之前但在静态初始化之后执行函数,或者将函数的地址添加到.init部分。链接newlib/arm/crt0.S gcc function attributes newlib/init.c gcc initialization

一个伪代码示例来说明正在发生的事情:

int some_global_explicitly_initialized_var = 1; // in .data section
static int some_global_var = 2; // in .data section
int global_vars_without_initialization_are_default_initialized_to_zero; // in .bss section
static int m33_uart; // in .bss section

int your_func() {
   // you set your variables,but it will be overwritten in _init
   m33_uart = 12354;
}

// this function is called first,ie. entrypoint before main
void Reset_Handler(void) {
    your_func();
    _init();
}

void _init(void) {
    // variables in .data section are initialized explicitly
    // ie. some_global_explicitly_initialized_var is set to 1 and 
    // and some_global_var is set to 2
    // this is done by copying a section from flash memory into ram into .data section
    // linker takes care of properly placing the variables
    memcpy(&_data_section,&_data_initialization_from_flash,sizeof(_data_initialization_from_flash));

    // variables in .bss section are initialized to 0
    // and uninitialized pointers are set to NULL
    // so all global variables are cleared
    // so global_vars_without_initialization_are_default_initialized_to_zero is set to 0
    // and m33_uart is also set to 0
    memset(&_bss_section,sizeof(_bss_section));

    // after that main is called
    main();
}

int main() {
    // m33_uart will be set to 0
    // because m33_uart is inside .bss section
    // and will be cleared to 0 by the memset in _init
}

在我的评论中,术语“静态变量”是指带有static storage duration的变量(不是带有static关键字的internal linkage的变量,如果有的话,很抱歉)。请注意有关initialization个具有静态存储持续时间的变量-它们被初始化为零。

,

从复位向量调用C函数并不总是安全的,以防复位向量也设置SP。幸运的是,这不是ARM的问题,但是在写入RAM变量之前,您仍然必须确保所有内存设置都已完成(有人建议如何手动here推出所有这些设置)。正如其他人所提到的,这里最可能出现的问题是您在.bss初始化完成之前写入了静态文件作用域变量,因此它们的值将被删除。

(如果您不知道.bss.data的含义,那么您现在不应该干预CRT。这是一种解释:What resides in the different memory types of a microcontroller?

有两种可行的解决方案:

  • 禁用.bss.data初始化作为非标准项目设置。这在嵌入式系统中非常常见,但是这意味着您不再可以依靠static或文件范围变量的标准C变量初始化。
  • 或将内部UART驱动程序变量放在未默认初始化的专用RAM段中。

真正的问题是,为什么您需要这么早设置UART。 UART寄存器的时间不能太紧,因为UART本身很慢。如果您设置数据方向并从复位向量中拉出电阻,然后在main()中进行其余的非关键初始化,就足够了。

此外,在设置系统时钟之前初始化UART几乎没有意义,否则波特率可能会错误。在该主题上,CRT / CMSIS库很有可能是由猴子编写的,他们认真地打算在默认的内部RC振荡器设置之前在默认的内部RC振荡器设置上运行.bss / .data初始化。 PLL设置已执行。由于大多数电路板都使用外部夸脱,因此您不希望启动缓慢而无缘无故地消耗电流-为了防止这种可怕的设计,您需要自己通过复位向量设置时钟,或者禁用{{1 }} / .bss初始化。