来源:https://www.cnblogs.com/liaigu/p/17838776.html 简介 CH32V20x系列、V30x系列用户选择字区域大小为128字节,除却用户选择字信息结构用到的16字节,还剩余112字节可作为他用,用来存储相关数据等。 注意剩余空间在使用时,和上述一致,若[7:0]为数据,则[15:8]为数据的反码。此外,在对剩余空间进行操作时,需要将用户选择字信息结构部分的内容保存重新写入。
用户选择字剩余区域操作代码如下: //用户选择字编程
//Address地址从0x1FFFF810开始
FLASH_Status User_SWord_ProgramHalfWord(uint32_t Address, uint8_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
uint32_t Addr = 0x1FFFF800;
__IO uint8_t i;
uint16_t pbuf[64];
status = FLASH_WaitForLastOperation(0x00005000);
if(status == FLASH_COMPLETE)
{
FLASH->OBKEYR = ((uint32_t)0x45670123);
FLASH->OBKEYR = ((uint32_t)0xCDEF89AB);
/* Read optionbytes */
for(i = 0; i < 8; i++)
{
pbuf[i] = *(uint16_t *)(Addr + 2 * i);
}
/* Erase optionbytes */
FLASH->CTLR |= ((uint32_t)0x00000020);
FLASH->CTLR |= ((uint32_t)0x00000040);
while(FLASH->STATR & ((uint32_t)0x00000001));
FLASH->CTLR &= ~((uint32_t)0x00000020);
/* Write optionbytes */
pbuf[((Address - 0x1FFFF800) / 2)] = ((((uint16_t) ~(Data)) << 8) | ((uint16_t)Data));
FLASH->CTLR |= 0x00000010;
for(i = 0; i < 8; i++)
{
*(uint16_t *)(Addr + 2 * i) = pbuf[i];
while(FLASH->STATR & ((uint32_t)0x00000001)) ;
}
*(uint16_t *)(Address) = pbuf[((Address - 0x1FFFF800) / 2)];
while(FLASH->STATR & ((uint32_t)0x00000001)) ;
FLASH->CTLR &= ~0x00000010;
}
return status;
}
void User_SWord_ProgramTest(void)
{
FLASH_Unlock();
//写入
User_SWord_ProgramHalfWord(0x1FFFF814,0x12);
printf("0x1FFFF814-%04x\r\n", *(u16*)0x1FFFF814);
}
测试结果如下:
|