关于数组字节对齐引起的HardFault异常中断
最近用到ST的M0核芯片STM32F030,在移植官方的IAP例程的时候问题有如滔滔江水连三绵绵不绝呀,始终的运行不起来。我不得不对ST的例程创作人员的业务能力表示严重的怀疑。问题1:
临时变量定义大数组导致堆栈溢出:
/**
* @briefReceive a file using the ymodem protocol
* @parambuf: Address of the first byte
* @retval The size of the file
*/
int32_t Ymodem_Receive (uint8_t *buf)
{
uint8_t packet_data, file_size, *file_ptr, *buf_ptr;
...
数组“packet_data”长度有1024以上,而堆栈大小默认只有1024字节(临时变量都是放在堆栈中的),从而导致堆栈溢出。
推荐解决方案:
/**
* @briefReceive a file using the ymodem protocol
* @parambuf: Address of the first byte
* @retval The size of the file
*/
int32_t Ymodem_Receive (uint8_t *buf)
{
static uint8_t packet_data;
uint8_t file_size, *file_ptr, *buf_ptr;
...
问题2:
字节型数组未作word(4字节)对齐,导致强制类型转换uint32_t访问时出现HardFault异常中断
uint8_t tab_1024 =
{
0
};
推荐解决方案:
uint8_t __attribute__((aligned(4))) tab_1024 =
{
0
};
STM32的HAL库是一个制杖逼着一群苦逼程序员写的,要不是还有LL库我就放弃STM32了。 不一定吧,堆栈是从高往低的,变量是从低往高分的,堆栈虽然定义是1K,实际中间空闲的变量自动地可以做堆栈 我之前自己的写的程序也遇到过默认栈太小导致的这个问题,改大就好
页:
[1]