赞
踩
现基于stm32CubeIDE 工具 开发stm32L051R8T6d 工程,碰到了一个有趣的问题,问题代码大致如下:
#define UNIQUE_ID_BYTE_LEN 67
static uint8_t config_buff[UNIQUE_ID_BYTE_LEN] = {0};
static uint8_t isDHCP = 0;
static uint32_t ip = 0;
static uint32_t gateway = 0;
static uint32_t mask = 0;
void test(){
//读取flash 中的con'fig信息;
BSP_W25Qx_Read(config_buff, CFG_ADDR, UNIQUE_ID_BYTE_LEN)
uint8_t *ptr = config_buff;
isDHCP = *ptr;
++ptr;
ip = *(uint32_t *)ptr;
ptr += 4;
gateway = *(uint32_t *)ptr;
ptr += 4;
mask = *(uint32_t *)ptr;
}
很明显,当执行到 ip = *(uint32_t *)ptr 时, 程序进入了 HardFault_Handler;跟踪程序发现ptr变更正常,没有什么问题,反复跟踪无果;
后来修改了一下,如下;
void test(){
//读取flash 中的con'fig信息;
BSP_W25Qx_Read(config_buff, CFG_ADDR, UNIQUE_ID_BYTE_LEN)
uint8_t *ptr = config_buff;
isDHCP = *ptr;
memcpy(&ip, ptr, 4);
ptr += 4;
memcpy(&gateway , ptr, 4);
ptr += 4;
memcpy(&mask , ptr, 4);
}
程序执行毫无问题,通过对比猜测,可能是字节对齐之类的问题导致的;但是记得在keil MDK 中开发,采用同样的代码,并没有出现问题;
鉴于项目紧张,在此记录,待后续验证。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。