GD32E230X字节指针转成整形指针访问
定义一个串口缓冲区 uint8_t TxBuf[1024],接收到的数据需要写入Flsh,写入按字写入,uint32_t da;da= *(uint32_t *)TxBuf , 执行这个操作MCU进入HardFault_Handler。找了半天,发现TxBuf的地址不是整4倍,转成32位访问就出错。所以写个将uint8_t 转uint32_t的函数转成U32再使用。
uint32_t bytes_to_uint32(const uint8_t* bytes) {
return (uint32_t)bytes[0] |
((uint32_t)bytes[1] << 8) |
((uint32_t)bytes[2] << 16) |
((uint32_t)bytes[3] << 24);
}
TestStatus Fmc_Write_Page(uint8_t *buf,uint32_t position,uint16_t Leng)
{
uint32_t Address;
TestStatus TransferStatus = FAILED;
uint32_t i;
uint32_t da;
Address = USER_APP2_ADDRESS;
Address += 1024 * (position/1024); //
// 关闭总中断
__set_PRIMASK(1);
/* unlock the flash program/erase controller */
fmc_unlock();
/* clear all pending flags */
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
fmc_page_erase(Address);
for(i=0;i<n;i++)
{
da = bytes_to_uint32(buf);
/* The operation will be done by word and Check the written value*/
if (fmc_word_program(Address,da) != FMC_READY)
{
TransferStatus = ERROR;
break;
}
Address += 4;
buf += 4;
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
}
/* lock the main FMC after the erase operation */
fmc_lock();
// 开启总中断
__set_PRIMASK(0);
return TransferStatus;
}