前言
最近一个板子用了STM32H503KBU6芯片,刚用就发现个问题,这个芯片通过调试器调试可以正常工作,但是下电后再冷启动程序就无法正常工作了。最后测试发现这个问题还是稍微有些偏门的(主要原因是使用官方工具生成的代码直接就有问题)。
问题发现
需要说明的是我这里用的是 LL 库,HAL 库不一定存在这个问题。
接上调试器,关闭调试复位芯片选项,可以查看到芯片进入了HardFult_Handler,问题出在MX_USART2_UART_Init:


但是实际上去查看或者调试MX_USART2_UART_Init是没有问题的,这个问题只出现在冷启动的时候。后来发现问题其实在上面一行MX_RTC_Init。这个函数中在冷启动时会去复位backup域:


这个动作会清除SRAM2,


默认情况下编译出来的栈是在 SRAM2 上的,前面的 MX_GPIO_Init() 把 GPIOB=0x42020400 缓存在 [SP+4] ,约为 0x20007E94 。 Backup Domain reset 将其清零。随后 UART 初始化从 [SP+4] 取出 r0=0 ,访问 [r0 + 8] ,即 0x00000008 ,最终进入 HardFault 。
问题处理
发现问题处理起来就比较简单了,只要把栈内存定位到 SRAM1 上就行,我这里选择修改链接脚本( STM32H503xx_FLASH.ld )来实现相关调整。
把原本的RAM中的SRAM2单独拆分出来,这样后面栈就被定位到了SRAM1上:
/* Memories definition */
MEMORY
{
/* RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K */
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 16K
SRAM2 (xrw) : ORIGIN = 0x20004000, LENGTH = 16K
/* BOOT: FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 56K */
/* APP : FLASH (rx) : ORIGIN = 0x08010000, LENGTH = 56K */
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 56K
}
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
_sstack = _estack - _Min_Stack_Size;
这个修改虽然处理好了栈的部分,但是需要使用SRAM2时就需要设置段,然后手动将数据定位到该段上来使用了:
.sram2_bss (NOLOAD) :
{
. = ALIGN(4);
__sram2_bss_start__ = .;
KEEP(*(.sram2_bss))
KEEP(*(.sram2_bss.*))
. = ALIGN(4);
__sram2_bss_end__ = .;
} > SRAM2
使用时可以用下面方式定位:
__attribute__((section(".sram2_bss"), aligned(sizeof(8))))
static uint8_t mem_pool_buf[8192] = {0};
需要注意的是这里定义的 sram2_bss 这个段的内存可能需要手动清零才保险,可以在 MX_RTC_Init 后,最初使用前使用下面代码来清零:
extern uint8_t __sram2_bss_start__[];
extern uint8_t __sram2_bss_end__[];
size_t size = (uintptr_t)__sram2_bss_end__ - (uintptr_t)__sram2_bss_start__;
memset(__sram2_bss_start__, 0, size);
完整的链接脚本如下:
/*
******************************************************************************
**
** @file : LinkerScript.ld
**
** @author : Auto-generated by STM32CubeIDE
**
** @brief : Linker script for STM32H503xx Device from STM32H5 series
** 128Kbytes FLASH
** 32Kbytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used
**
** Target : STMicroelectronics STM32
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
******************************************************************************
** @attention
**
** Copyright (c) 2023 STMicroelectronics.
** All rights reserved.
**
** This software is licensed under terms that can be found in the LICENSE file
** in the root directory of this software component.
** If no LICENSE file comes with this software, it is provided AS-IS.
**
******************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Memories definition */
MEMORY
{
/* RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K */
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 16K
SRAM2 (xrw) : ORIGIN = 0x20004000, LENGTH = 16K
/* BOOT: FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 56K */
/* APP : FLASH (rx) : ORIGIN = 0x08010000, LENGTH = 56K */
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 56K
}
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
_sstack = _estack - _Min_Stack_Size;
_Min_Heap_Size = 0x400; /* required amount of heap */
_Min_Stack_Size = 0x800; /* required amount of stack */
/* Sections */
SECTIONS
{
/* The startup code into "FLASH" Rom type memory */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data into "FLASH" Rom type memory */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH
/* Constant data into "FLASH" Rom type memory */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH
.ARM.extab (READONLY) : /* The READONLY keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
*(.ARM.extab* .gnu.linkonce.armextab.*)
. = ALIGN(4);
} >FLASH
.ARM (READONLY) : /* The READONLY keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
. = ALIGN(4);
} >FLASH
.preinit_array (READONLY) : /* The READONLY keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);
} >FLASH
.init_array (READONLY) : /* The READONLY keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);
} >FLASH
.fini_array (READONLY) : /* The READONLY keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */
{
. = ALIGN(4);
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
. = ALIGN(4);
} >FLASH
/* Used by the startup to initialize data */
_sidata = LOADADDR(.data);
/* Initialized data sections into "RAM" Ram type memory */
.data :
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
*(.RamFunc) /* .RamFunc sections */
*(.RamFunc*) /* .RamFunc* sections */
. = ALIGN(4);
} >RAM AT> FLASH
/* Initialized TLS data section */
.tdata : ALIGN(4)
{
*(.tdata .tdata.* .gnu.linkonce.td.*)
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
PROVIDE(__data_end = .);
PROVIDE(__tdata_end = .);
} >RAM AT> FLASH
PROVIDE( __tdata_start = ADDR(.tdata) );
PROVIDE( __tdata_size = __tdata_end - __tdata_start );
PROVIDE( __data_start = ADDR(.data) );
PROVIDE( __data_size = __data_end - __data_start );
PROVIDE( __tdata_source = LOADADDR(.tdata) );
PROVIDE( __tdata_source_end = LOADADDR(.tdata) + SIZEOF(.tdata) );
PROVIDE( __tdata_source_size = __tdata_source_end - __tdata_source );
PROVIDE( __data_source = LOADADDR(.data) );
PROVIDE( __data_source_end = __tdata_source_end );
PROVIDE( __data_source_size = __data_source_end - __data_source );
/* Uninitialized data section into "RAM" Ram type memory */
/* Uninitialized TLS data section */
.tbss (NOLOAD) : ALIGN(4)
{
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.tbss .tbss.*)
. = ALIGN(4);
PROVIDE( __tbss_end = . );
} >RAM
PROVIDE( __tbss_start = ADDR(.tbss) );
PROVIDE( __tbss_size = __tbss_end - __tbss_start );
PROVIDE( __tbss_offset = ADDR(.tbss) - ADDR(.tdata) );
PROVIDE( __tls_base = __tdata_start );
PROVIDE( __tls_end = __tbss_end );
PROVIDE( __tls_size = __tls_end - __tls_base );
PROVIDE( __tls_align = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)) );
PROVIDE( __tls_size_align = (__tls_size + __tls_align - 1) & ~(__tls_align - 1) );
PROVIDE( __arm32_tls_tcb_offset = MAX(8, __tls_align) );
PROVIDE( __arm64_tls_tcb_offset = MAX(16, __tls_align) );
.bss (NOLOAD) : ALIGN(4)
{
/* This is used by the startup in order to initialize the .bss section */
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
PROVIDE( __bss_end = .);
} >RAM
PROVIDE( __non_tls_bss_start = ADDR(.bss) );
PROVIDE( __bss_start = __tbss_start );
PROVIDE( __bss_size = __bss_end - __bss_start );
.sram2_bss (NOLOAD) :
{
. = ALIGN(4);
__sram2_bss_start__ = .;
KEEP(*(.sram2_bss))
KEEP(*(.sram2_bss.*))
. = ALIGN(4);
__sram2_bss_end__ = .;
} > SRAM2
/* User_heap_stack section, used to check that there is enough "RAM" Ram type memory left */
._user_heap_stack (NOLOAD) :
{
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
/* Remove information from the compiler libraries */
/DISCARD/ :
{
libc.a:* ( * )
libm.a:* ( * )
libgcc.a:* ( * )
}
.ARM.attributes 0 : { *(.ARM.attributes) }
}
后记
不停的换平台带来的就是不停的踩坑,虽然很花时间,不过很多新平台在特定场合下用起来还是很不错的。
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/Naisu_kun/article/details/166598967




