近期有一个STM32F107的项目需要移植到GD32F307。遇到一个问题发现其他移植教程中都没有提到。
目录
1.首先是按照网上的教程:
1)修改启动时间宏定义
#define HSE_STARTUP_TIMEOUT ((uint16_t)0x0500)
修改为:
#define HSE_STARTUP_TIMEOUT ((uint16_t)0xFFFF)
2)修改内部flash
在写完KEY 序列以后,需要读该位,确认key 已生效。
需要插入:while((FLASH->CR&CR_OPTWRE_Set)!=CR_OPTWRE_Set ){}
#define CR_OPTWRE_Set ((uint32_t)0x00000200)
在ST库中,有以下四个函数中需要修改:
FLASH_Status FLASH_EraseOptionBytes(void)
FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)
FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
示例:
FLASH_Status FLASH_EraseOptionBytes(void)
{
uint16_t rdptmp = RDP_Key;
FLASH_Status status = FLASH_COMPLETE;
/* Get the actual read protection Option Byte value */
if(FLASH_GetReadOutProtectionStatus() != RESET)
{
rdptmp = 0x00;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(EraseTimeout);
if(status == FLASH_COMPLETE)
{
/* Authorize the small information block programming */
FLASH->OPTKEYR = FLASH_KEY1;
FLASH->OPTKEYR = FLASH_KEY2;
/********看这里看这里看这里看这里看这里看这里**********/
while((FLASH->CR&CR_OPTWRE_Set)!=CR_OPTWRE_Set )
{}
/****************************************************/
/* if the previous operation is completed, proceed to erase the option bytes */
FLASH->CR |= CR_OPTER_Set;
FLASH->CR |= CR_STRT_Set;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(EraseTimeout);
if(status == FLASH_COMPLETE)
{
/* if the erase operation is completed, disable the OPTER Bit */
FLASH->CR &= CR_OPTER_Reset;
/* Enable the Option Bytes Programming operation */
FLASH->CR |= CR_OPTPG_Set;
/* Restore the last read protection Option Byte value */
OB->RDP = (uint16_t)rdptmp;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(ProgramTimeout);
if(status != FLASH_TIMEOUT)
{
/* if the program operation is completed, disable the OPTPG Bit */
FLASH->CR &= CR_OPTPG_Reset;
}
}
else
{
if (status != FLASH_TIMEOUT)
{
/* Disable the OPTPG Bit */
FLASH->CR &= CR_OPTPG_Reset;
}
}
}
/* Return the erase status */
return status;
}
3)flash访问时间
GD32 由于自有flash 的0 访问时序,同STM32 在Flash 的Erase 和Program 上存在差别,GD32 的Erase 和Program 时间比STM32 的稍微长些,建议对Erase 和Program 时间进行修改。
解决方法:将宏定义
#define EraseTimeout ((uint32_t)0x000B0000)
#define ProgramTimeout ((uint32_t)0x00002000)
修改为:
#define EraseTimeout ((uint32_t)0x000FFFFF)
#define ProgramTimeout ((uint32_t)0x0000FFFF)
2.这个是其他教程中没有提到的
1)floating point hardware设置
一般来说,完成以上三个步骤移植就算完成了,但是我的固件烧录在固件中后,程序会不断重启。折腾了大半天,发现把下面这个配置关掉就好了。
点击“魔术棒”-“Code Generation”中的“Floating Point Hardware”,选择“Not Used”。
总结:
这个选项在STM32中貌似是没有的,把工程中的芯片包换成GD32F30x之后,就会冒出这个选项。暂不清楚有什么作用,有知道的大佬也可以在评论区解惑,谢谢啦!