5.8 Flash 使用说明
本章说明 N32G41x 标准外设库中 Flash 驱动的文件组成与 50 个公开函数,内容包括等待周期与预取缓冲、ICache 与缓存锁定、编程擦除的解锁与锁定、页擦除与整片擦除、按字编程、选项字节(含写保护与读保护)的编程与回读,以及中断与状态标志管理,并给出 Flash_Program、Option_Byte_Configure 两个示例。FLASH 的地址空间、页划分、擦写流程与选项字节定义请配合用户手册的“存储器和总线架构”章节阅读,芯片已知问题以勘误手册为准。
5.8.1 相关文件
| 相对路径 | 作用 |
|---|---|
firmware/n32g41x_std_periph_driver/inc/n32g41x_flash.h |
定义 FLASH_STS 与配置、缓存、密钥、选项字节等宏,并按配置、缓存管理、解锁与锁定、擦除、编程、选项字节编程、写保护、读保护、选项字节回读、状态与保护状态、中断与标志十一个区段声明 50 个公开函数 |
firmware/n32g41x_std_periph_driver/src/n32g41x_flash.c |
实现上述 50 个函数:等待周期与预取设置、ICache 复位与锁定、控制器解锁序列、页与整片擦除、按字编程、选项字节擦写与回读、写保护与读保护配置、状态标志读取与超时等待 |
firmware/CMSIS/device/n32g41x.h |
定义 FLASH、FLASH_OB、FLASH_OB2 寄存器结构与基地址,以及 FLASH_KEY1、FLASH_KEY2 等位域宏 |
projects/n32g41x_EVAL/examples/Flash/ |
Flash 示例根目录,含 Flash_Program、Option_Byte_Configure 2 个示例 |
projects/n32g41x_EVAL/examples/Flash/Flash_Program/ |
该示例的工程目录组成:MDK-ARM/、EWARM/、inc/、src/、readme.txt |
projects/n32g41x_EVAL/bsp/ |
两个示例共用的板级支持文件:串口 log 初始化与毫秒延时函数 |
5.8.2 API 参考
以下 50 个公开函数按 n32g41x_flash.h 的声明顺序分为 11 组;每个条目给出功能摘要与 n32g41x_flash.c 中该函数正上方的注释原文。
配置
FLASH_SetLatency、FLASH_GetLatency、FLASH_PrefetchBufSet、Flash_GetPrefetchBufferStatus
缓存管理
FLASH_iCacheRST、FLASH_iCacheCmd、FLASH_StartCacheLock、FLASH_StopCacheLock、FLASH_CancelCacheLock
解锁与锁定
FLASH_Unlock、FLASH_Lock、Flash_GetLockStatus、Option_Bytes_Unlock、Option_Bytes_Lock、OB_GetLockStatus
擦除
FLASH_EraseOnePage、FLASH_MassErase、FLASH_EraseOB
编程
FLASH_ProgramWord
选项字节编程
FLASH_ProgramOptionBytes_RDP1、FLASH_ProgramOptionBytes_USER0、FLASH_ProgramOptionBytes_USER1、FLASH_ProgramOptionBytes_USER3_USER2、FLASH_ProgramOptionBytes_USER5_USER4、FLASH_ProgramOptionBytes_USER6、FLASH_ProgramOptionBytes_USER7、FLASH_ProgramOptionBytes_DATA1_DATA0
写保护
FLASH_EnWriteProtection
读保护
FLASH_ProgramOptionBytes_RDP2、FLASH_ReadOutProtectionL1、FLASH_ReadOutProtectionL2_ENABLE
选项字节回读
FLASH_GetOptionBytes_User0、FLASH_GetOptionBytes_User1、FLASH_GetOptionBytes_User2、FLASH_GetOptionBytes_User3、FLASH_GetBOOTUartPIN、FLASH_GetBOOTI2cPIN、FLASH_GetBOOTCanPIN、FLASH_GetOptionBytes_Data0、FLASH_GetOptionBytes_Data1
状态与保护状态
FLASH_GetReadOutProtectionSTS、FLASH_GetReadOutProtectionL2STS、FLASH_GetWriteProtectionSTS、FLASH_GetBOOTWriteProtectionSTS
中断与标志
FLASH_INTConfig、FLASH_GetFlagSTS、FLASH_GetOBFlagSTS、FLASH_ClearFlag、FLASH_GetSTS、FLASH_WaitForLastOpt
void FLASH_SetLatency(uint32_t FLASH_Latency)设置代码等待周期值。
name FLASH_SetLatency
fun Sets the code latency value.
param FLASH_Latency :(The input parameters must be the following values)
- FLASH_LATENCY_0 FLASH Zero Latency cycle, 0 < SYSCLK <= 36MHz
- FLASH_LATENCY_1 FLASH One Latency cycle, 36MHZ < SYSCLK <= 72MHz
- FLASH_LATENCY_2 FLASH two Latency cycle, N32G412: 72Mhz < SYSCLK <= 80Mhz; N32G415: 72Mhz < SYSCLK <= 96Mhz;
return none
note The larger the latency, the longer the flash read operation will take.
If not necessary, it is recommended to use a smaller latency.
使用提示: 实现读 FLASH_AC、清 LATENCY 字段再写回,不改动同一寄存器中的预取缓冲与 ICache 位。
uint8_t FLASH_GetLatency(void)获取代码等待周期值。
name FLASH_GetLatency
fun Get the code latency value.
param none
return FLASH_LATENCY :(The input parameters must be the following values)
- FLASH_LATENCY_0 FLASH Zero Latency cycle, 0 < SYSCLK <= 36MHz
- FLASH_LATENCY_1 FLASH One Latency cycle, 36MHZ < SYSCLK <= 72MHz
- FLASH_LATENCY_2 FLASH two Latency cycle, N32G412: 72Mhz < SYSCLK <= 80Mhz; N32G415: 72Mhz < SYSCLK <= 96Mhz;
void FLASH_PrefetchBufSet(uint32_t FLASH_PrefetchBuf)使能或禁用预取缓冲。
name FLASH_PrefetchBufSet
fun Enables or disables the Prefetch Buffer.
param FLASH_PrefetchBuf:(The input parameters must be the following values)
- FLASH_PrefetchBuf_EN
- FLASH_PrefetchBuf_DIS
return none
FlagStatus Flash_GetPrefetchBufferStatus(void)获取预取缓冲状态。
name Flash_GetPrefetchBufferStatus
fun Get the Prefetch Buffer status.
param none
return FlagStatus :
- SET FLASH is in Prefetch Buffer enabled state
- RESET FLASH is in Prefetch Buffer disabled state
void FLASH_iCacheRST(void)复位 ICache。
name FLASH_iCacheRST
fun ICache Reset.
param none
return none
void FLASH_iCacheCmd(uint32_t FLASH_iCache)使能或禁用 iCache。
name FLASH_iCacheCmd
fun Enables or disables the iCache.
param FLASH_iCache:(The input parameters must be the following values)
- FLASH_iCache_EN FLASH iCache Enable
- FLASH_iCache_DIS FLASH iCache Disable
return none
note When the N32G412 series runs above 72MHz, can't enable Icache.
void FLASH_StartCacheLock(uint32_t lock_start_way)开始缓存锁定。
name FLASH_StartCacheLock
fun Start cache lock.
param lock_start_way :
- FLASH_CAHR_LOCKSTRT_WAY0
- FLASH_CAHR_LOCKSTRT_WAY1
- FLASH_CAHR_LOCKSTRT_WAY2
- FLASH_CAHR_LOCKSTRT_WAY3
return none
void FLASH_StopCacheLock(uint32_t lock_stop_way)停止缓存锁定。
name FLASH_StopCacheLock
fun Stop cache lock.
param lock_stop_way :
- FLASH_CAHR_LOCKSTOP_WAY0
- FLASH_CAHR_LOCKSTOP_WAY1
- FLASH_CAHR_LOCKSTOP_WAY2
- FLASH_CAHR_LOCKSTOP_WAY3
return none
void FLASH_CancelCacheLock(uint32_t lock_stop_way)取消缓存锁定。
name FLASH_CancelCacheLock
fun Cancel cache lock.
param lock_stop_way :
- FLASH_CAHR_LOCKSTOP_WAY0
- FLASH_CAHR_LOCKSTOP_WAY1
- FLASH_CAHR_LOCKSTOP_WAY2
- FLASH_CAHR_LOCKSTOP_WAY3
return none
void FLASH_Unlock(void)解锁 Flash 编程擦除控制器。
name FLASH_Unlock
fun Unlocks the FLASH Program Erase Controller.
param none
return none
note Before operating the FLASH_CTRL register, you need to call the FLASH_Unlock function to unlock the LOCK bit.
使用提示: 实现依次向 FLASH_KEY 写入 FLASH_KEY1(0x45670123)与 FLASH_KEY2(0xCDEF89AB)。
void FLASH_Lock(void)锁定 Flash 编程擦除控制器。
name FLASH_Lock
fun Locks the FLASH Program Erase Controller.
param none
return none
FlagStatus Flash_GetLockStatus(void)获取 Flash 锁定状态。
name Flash_GetLockStatus
fun Get the Flash lock status.
param none
return FlagStatus :
- SET FLASH is in Lock state
- RESET FLASH is in Unlock state
void Option_Bytes_Unlock(void)解锁选项字节编程擦除控制器。
name Option_Bytes_Unlock
fun Unlocks the Option_Bytes Program Erase Controller.
param none
return none
使用提示: 实现依次向 FLASH_OPTKEY 写入 FLASH_KEY1 与 FLASH_KEY2,与 FLASH_Unlock 使用的是同一组密钥。
void Option_Bytes_Lock(void)锁定选项字节编程擦除控制器。
name Option_Bytes_Lock
fun Locks the Option_Bytes Program Erase Controller.
param none
return none
FlagStatus OB_GetLockStatus(void)获取选项字节锁定状态。
name OB_GetLockStatus
fun Get the Option Bytes lock status.
param none
return FlagStatus :
- SET Option byte is in Unlock state
- RESET Option byte is in Lock state
FLASH_STS FLASH_EraseOnePage(uint32_t Page_Address)擦除指定的主存储区页。
name FLASH_EraseOnePage
fun Erases a specified main FLASH page.
param Page_Address :(The input parameters must be the following values)
- main flash, it ranges from 0x08000000 to 0x0801FFFF, it must be a multiple of 0x200
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_MassErase(void)擦除全部主存储区页。
name FLASH_MassErase
fun Erases all main FLASH pages.
param none
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_EraseOB(void)擦除 Flash 选项字节。
name FLASH_EraseOB
fun Erases the FLASH option bytes.
param none
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
使用提示: 实现先读取 RDP2 状态,已使能 L2 读保护时直接返回 FLASH_ERR_RDP2;擦除前自行调用 Option_Bytes_Unlock 解锁选项字节。
FLASH_STS FLASH_ProgramWord(uint32_t address, uint32_t data)在指定地址编程一个字。
name FLASH_ProgramWord
fun Programs one word at a specified address.
param address :(The input parameters must be the following values)
- it ranges from 0x08000000 to 0x0801FFFF, it must be a multiple of 0x04
param data :(The input parameters must be the following values)
- It ranges from 0x00000000 to 0xFFFFFFFF
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_ADD FLASH address error
- FLASH_TIMEOUT FLASH operation timeout
使用提示: 实现先检查 address 是否按字(4 字节)对齐,未对齐时返回 FLASH_ERR_ADD 且不启动编程。
FLASH_STS FLASH_ProgramOptionBytes_RDP1(uint8_t option_byte_rpd1)编程选项字节 RDP1。
name FLASH_ProgramOptionBytes_RDP1
fun Programs the Option Byte: RDP1.
param option_byte_rpd1 :(The input parameters must be the following values)
- FLASH_OB_RDP1_ENABLE Enable read protection L1
- FLASH_OB_RDP1_DISABLE Disable read protection L1
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
使用提示: 实现先检查 RDP2 状态并在编程前自行调用 Option_Bytes_Unlock,L2 已使能时返回 FLASH_ERR_RDP2。
FLASH_STS FLASH_ProgramOptionBytes_USER0(uint32_t option_byte_iwdg)编程选项字节 IWDG_SW。
name FLASH_ProgramOptionBytes_USER0
fun Programs the Option Byte: IWDG_SW.
param option_byte_iwdg :(The input parameters must be the following values)
- FLASH_OB_IWDG_SOFTWARE Software IWDG selected
- FLASH_OB_IWDG_HARDWARE Hardware IWDG selected
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ProgramOptionBytes_USER1(uint32_t option_byte_PD7)编程选项字节 NRST_PD7。
name FLASH_ProgramOptionBytes_USER1
fun Programs the Option Byte: NRST_PD7.
param option_byte_PD7 :(The input parameters must be the following values)
- FLASH_OB_PD7_GPIO PD7 set GPIO
- FLASH_OB_PD7_NRST PD7 set NRST
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ProgramOptionBytes_USER3_USER2(uint32_t option_byte_nBOOT0, uint32_t option_byte_nBOOT1, uint32_t option_byte_nSWBOOT0, uint32_t option_byte_BOOT0_CFG, uint32_t option_byte_iwdg_stop, uint32_t option_byte_iwdg_sleep, uint32_t option_byte_user2)编程选项字节 USER3 与 USER2。
name FLASH_ProgramOptionBytes_USER3_USER2
fun Programs the Option Byte: USER3 and USER2.
param option_byte_nBOOT0 :(The input parameters must be the following values)
- FLASH_OB_NBOOT0_SET Set nBOOT0
- FLASH_OB_NBOOT0_CLR Clear nBOOT0
param option_byte_nBOOT1 :(The input parameters must be the following values)
- FLASH_OB_NBOOT1_SET Set nBOOT1
- FLASH_OB_NBOOT1_CLR Clear nBOOT1
param option_byte_nSWBOOT0 :(The input parameters must be the following values)
- FLASH_OB_NSWBOOT0_SET Set nSWBOOT0
- FLASH_OB_NSWBOOT0_CLR Clear nSWBOOT0
param option_byte_BOOT0_CFG :(The input parameters must be the following values)
- FLASH_OB_BOOT0_CFG_HIGH Boot0 high active
- FLASH_OB_BOOT0_CFG_LOW Boot0 low active
param option_byte_iwdg_stop :(The input parameters must be the following values)
- FLASH_OB_IWDG_STOP_NOFRZ IWDG not frozen in Stop mode
- FLASH_OB_IWDG_STOP_FRZ IWDG frozen in Stop mode
param option_byte_iwdg_sleep :(The input parameters must be the following values)
- FLASH_OB_IWDG_SLEEP_NOFRZ IWDG not frozen in Sleep mode
- FLASH_OB_IWDG_SLEEP_FRZ IWDG frozen in Sleep mode
param option_byte_user2:(The input parameters must be the following values)
- 0x00 to 0xFF PDR/POR delay count; 0x00 = longest, 0xFF = no delay; 30~8192us
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ProgramOptionBytes_USER5_USER4(uint32_t option_byte_user4, uint32_t option_byte_user5)编程选项字节 USER5 与 USER4。
name FLASH_ProgramOptionBytes_USER5_USER4
fun Programs the Option Byte: USER5 and USER4.
param option_byte_user4 :(The input parameters must be the following values)
- BOOT_UARTPIN_PA9PA10
- BOOT_UARTPIN_PA0PA1
- BOOT_UARTPIN_PA4PA5
- BOOT_UARTPIN_PB4PB5
- BOOT_UARTPIN_PB6PB7
- BOOT_UARTPIN_NUM5 N32G412 serial BOOT uart pins are PB8 and PB9, N32G415 serial BOOT uart pins are PB9 and PC10
- BOOT_UARTPIN_PC4PC5
- BOOT_UARTPIN_NUM7 N32G412 serial BOOT uart pins are PC7 and PC6, N32G415 serial BOOT uart pins are PA9 and PA8
- BOOT_UARTPIN_NUM8 N32G412 serial BOOT uart pins are PC12 and PD2, N32G415 serial BOOT uart pins are PD0 and PD2
param option_byte_user5 :(The input parameters must be the following values)
- BOOT_I2C_PIN_PA4PA5
- BOOT_I2C_PIN_PA9PA10
- BOOT_I2C_PIN_PA15PA14
- BOOT_I2C_PIN_PB6PB7
- BOOT_I2C_PIN_NUM4 N32G412 serial BOOT i2c pins are PB7 and PB8, N32G415 serial BOOT i2c pins are PB7 and PB9
- BOOT_I2C_PIN_NUM5 N32G412 serial BOOT i2c pins are PB8 and PB9, N32G415 serial BOOT i2c pins are PB9 and PC10
- BOOT_I2C_PIN_PC0PC1
- BOOT_I2C_PIN_PC4PC5
- BOOT_I2C_PIN_PD15PD14
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ProgramOptionBytes_USER6(uint32_t option_byte_user6)编程选项字节 USER6。
name FLASH_ProgramOptionBytes_USER6
fun Programs the Option Byte: USER6.
param option_byte_user6 :(The input parameters must be the following values)
- BOOT_CAN_PIN_PA12PA11 CAN pins are PA12 and PA11
- BOOT_CAN_PIN_NUM1 N32G412 serial CAN pins are PB9 and PB8, N32G415 serial CAN pins are PC10 and PB9
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ProgramOptionBytes_USER7(uint32_t option_byte_user7)编程选项字节 BOOT 写保护的使能或禁用位。
name FLASH_ProgramOptionBytes_USER7
fun Programs the Option Byte: BOOT write protection Enable or disable.
param option_byte_user7:
- BOOT_WRP_ENABLE
- BOOT_WRP_DISABLE
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ProgramOptionBytes_DATA1_DATA0(uint8_t option_byte_data1, uint8_t option_byte_data0)在指定的选项字节 Data1 与 Data0 编程 2 个半字。
name FLASH_ProgramOptionBytes_DATA1_DATA0
fun Programs 2 half words at a specified Option Byte Data1 and Data0.
param option_byte_data1:(The input parameters must be the following values)
- 0x00 to 0xFF
param option_byte_data0:(The input parameters must be the following values)
- 0x00 to 0xFF
return FLASH_STS: The returned value can be:
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_EnWriteProtection(uint32_t FLASH_Pages)对所选的页进行写保护。
name FLASH_EnWriteProtection
fun Write protects the desired pages
param FLASH_Pages :(The input parameters must be the following values)
- FLASH_WRP_Pages0to7 Enable Write protection of page 0 to 7
- FLASH_WRP_Pages8to15 Enable Write protection of page 8 to 15
- FLASH_WRP_Pages16to23 Enable Write protection of page 16 to 23
- FLASH_WRP_Pages24to31 Enable Write protection of page 24 to 31
- FLASH_WRP_Pages32to39 Enable Write protection of page 32 to 39
- FLASH_WRP_Pages40to47 Enable Write protection of page 40 to 47
- FLASH_WRP_Pages48to55 Enable Write protection of page 48 to 55
- FLASH_WRP_Pages56to63 Enable Write protection of page 56 to 63
- FLASH_WRP_Pages64to71 Enable Write protection of page 64 to 71
- FLASH_WRP_Pages72to79 Enable Write protection of page 72 to 79
- FLASH_WRP_Pages80to87 Enable Write protection of page 80 to 87
- FLASH_WRP_Pages88to95 Enable Write protection of page 88 to 95
- FLASH_WRP_Pages96to103 Enable Write protection of page 96 to 103
- FLASH_WRP_Pages104to111 Enable Write protection of page 104 to 111
- FLASH_WRP_Pages112to119 Enable Write protection of page 112 to 119
- FLASH_WRP_Pages120to127 Enable Write protection of page 120 to 127
- FLASH_WRP_Pages128to135 Enable Write protection of page 128 to 135
- FLASH_WRP_Pages136to143 Enable Write protection of page 136 to 143
- FLASH_WRP_Pages144to151 Enable Write protection of page 144 to 151
- FLASH_WRP_Pages152to159 Enable Write protection of page 152 to 159
- FLASH_WRP_Pages160to167 Enable Write protection of page 160 to 167
- FLASH_WRP_Pages168to175 Enable Write protection of page 168 to 175
- FLASH_WRP_Pages176to183 Enable Write protection of page 176 to 183
- FLASH_WRP_Pages184to191 Enable Write protection of page 184 to 191
- FLASH_WRP_Pages192to199 Enable Write protection of page 192 to 199
- FLASH_WRP_Pages200to207 Enable Write protection of page 200 to 207
- FLASH_WRP_Pages208to215 Enable Write protection of page 208 to 215
- FLASH_WRP_Pages216to223 Enable Write protection of page 216 to 223
- FLASH_WRP_Pages224to231 Enable Write protection of page 224 to 231
- FLASH_WRP_Pages232to239 Enable Write protection of page 232 to 239
- FLASH_WRP_Pages240to247 Enable Write protection of page 240 to 247
- FLASH_WRP_Pages248to255 Enable Write protection of page 248 to 255
- FLASH_WRP_AllPages Enable Write protection of all Pages
- ~FLASH_WRP_AllPages Disable Write protection of all Pages
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
使用提示: 实现对传入的 FLASH_Pages 取反后拆成 WRP0~WRP3 写入选项字节,并自行调用 Option_Bytes_Unlock;实参写 ~FLASH_WRP_AllPages 表示解除全部页写保护。
FLASH_STS FLASH_ProgramOptionBytes_RDP2(uint8_t option_byte_rpd2)编程选项字节 RDP2。
name FLASH_ProgramOptionBytes_RDP2
fun Programs the Option Byte: RDP2.
param option_byte_rpd2 :(The input parameters must be the following values)
- FLASH_OB_RDP2_ENABLE Enable read protection L2
- FLASH_OB_RDP2_DISABLE Disable read protection L2
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ReadOutProtectionL1(FunctionalState Cmd)使能读出保护 L1。
name FLASH_ReadOutProtectionL1
fun Enables the read out protection.
param Cmd:(The input parameters must be the following values)
- ENABLE Enable read protection L1
- DISABLE Disable read protection L1
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FLASH_STS FLASH_ReadOutProtectionL2_ENABLE(void)使能读出保护 L2。
name FLASH_ReadOutProtectionL2_ENABLE
fun Enables the read out protection L2.
param none
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_ERR_RDP2 FLASH is in read protection L2 status
- FLASH_TIMEOUT FLASH operation timeout
FlagStatus FLASH_GetOptionBytes_User0(void)返回 Flash 用户选项字节的值。
name FLASH_GetOptionBytes_User0
fun Returns the FLASH User Option Bytes values.
param none
return FlagStatus :
- SET Software watchdog
- RESET Hardware watchdog
FlagStatus FLASH_GetOptionBytes_User1(void)返回 Flash 用户选项字节的值。
name FLASH_GetOptionBytes_User1
fun Returns the FLASH User Option Bytes values.
param none
return FlagStatus :
- SET PD7 used as NRST
- RESET PD7 used as GPIO
uint32_t FLASH_GetOptionBytes_User2(void)返回 Flash 选项字节的 POR_DELAY 值。
name FLASH_GetOptionBytes_User2
fun Returns the FLASH Option Bytes POR_DELAY values.
param none
return POR_DELAY:
- 0x00 to 0xFF.
FlagStatus FLASH_GetOptionBytes_User3(uint32_t option_byte_bit)返回 Flash 用户选项字节的值。
name FLASH_GetOptionBytes_User3
fun Returns the FLASH User Option Bytes values.
param option_byte_bit: (The input parameters must be the following values)
- FLASH_OB_NBOOT0_SET nBOOT0 configuration bit
- FLASH_OB_NBOOT1_SET nBOOT1 configuration bit
- FLASH_OB_NSWBOOT0_SET nSWBOOT0 configuration bit
- FLASH_OB_BOOT0_CFG_HIGH BOOT0_CFG configuration bit
- FLASH_OB_IWDG_SLEEP_NOFRZ IWDG_SLEEP_NOFRZ configuration bit
- FLASH_OB_IWDG_STOP_NOFRZ IWDG_STOP_NOFRZ configuration bit
return FlagStatus :
- SET The selected option bit is 1
- RESET The selected option bit is 0
使用提示: 实现把 option_byte_bit 右移 4 位后与 FLASH_OB 相与,传入的宏须与 FLASH_OB 中的位对齐。
uint32_t FLASH_GetBOOTUartPIN(void)返回 Flash 用户选项字节的值。
name FLASH_GetBOOTUartPIN
fun Returns the FLASH User Option Bytes values.
param none
return option_byte_user4 0x0~0x8:
- BOOT_UARTPIN_PA9PA10 BOOT uart pins are PA9 and PA10
- BOOT_UARTPIN_PA0PA1 BOOT uart pins are PA0 and PA1
- BOOT_UARTPIN_PA4PA5 BOOT uart pins are PA4 and PA5
- BOOT_UARTPIN_PB4PB5 BOOT uart pins are PB4 and PB5
- BOOT_UARTPIN_PB6PB7 BOOT uart pins are PB6 and PB7
- BOOT_UARTPIN_NUM5 N32G412 serial BOOT uart pins are PB8 and PB9, N32G415 serial BOOT uart pins are PB9 and PC10
- BOOT_UARTPIN_PC4PC5 BOOT uart pins are PC4 and PC5
- BOOT_UARTPIN_NUM7 N32G412 serial BOOT uart pins are PC7 and PC6, N32G415 serial BOOT uart pins are PA9 and PA8
- BOOT_UARTPIN_NUM8 N32G412 serial BOOT uart pins are PC12 and PD2, N32G415 serial BOOT uart pins are PD0 and PD2
uint32_t FLASH_GetBOOTI2cPIN(void)返回 Flash 用户选项字节的值。
name FLASH_GetBOOTI2cPIN
fun Returns the FLASH User Option Bytes values.
param none
return option_byte_user5 0x00000000~0x00080000:
- BOOT_I2C_PIN_PA4PA5 I2C SCL SDA pins are PA4 and PA5
- BOOT_I2C_PIN_PA9PA10 I2C SCL SDA pins are PA9 and PA10
- BOOT_I2C_PIN_PA15PA14 I2C SCL SDA pins are PA15 and PA14
- BOOT_I2C_PIN_PB6PB7 I2C SCL SDA pins are PB6 and PB7
- BOOT_I2C_PIN_NUM4 N32G412 serial I2C SCL SDA pins are PB7 and PB8 , N32G415 serial I2C SCL SDA pins are PB7 and PB9
- BOOT_I2C_PIN_NUM5 N32G412 serial I2C SCL SDA pins are PB8 and PB9 , N32G415 serial I2C SCL SDA pins are PB9 and PC10
- BOOT_I2C_PIN_PC0PC1 I2C SCL SDA pins are PC0 and PC1
- BOOT_I2C_PIN_PC4PC5 I2C SCL SDA pins are PC4 and PC5
- BOOT_I2C_PIN_PD15PD14 I2C SCL SDA pins are PD15 and PD14
使用提示: 实现把 FLASH_OB2 的 I2C 引脚选择字段左移 16 位后返回。
uint32_t FLASH_GetBOOTCanPIN(void)返回 Flash 用户选项字节的值。
name FLASH_GetBOOTCanPIN
fun Returns the FLASH User Option Bytes values.
param none
return option_byte_user6 0x0~0x1:
- BOOT_CAN_PIN_PA12PA11 N32G412 serial CAN TX pins are PA12 and PA11
- BOOT_CAN_PIN_NUM1 N32G412 serial CAN TX pins are PB9 and PB8, N32G415 serial CAN TX pins are PC10 and PB9
uint32_t FLASH_GetOptionBytes_Data0(void)返回 Flash 用户选项字节的值。
name FLASH_GetOptionBytes_Data0
fun Returns the FLASH User Option Bytes values.
param none
return data0:
- 0x00 to 0xFF.
uint32_t FLASH_GetOptionBytes_Data1(void)返回 Flash 用户选项字节的值。
name FLASH_GetOptionBytes_Data1
fun Returns the FLASH User Option Bytes values.
param none
return data1:
- 0x00 to 0xFF.
FlagStatus FLASH_GetReadOutProtectionSTS(void)检查 Flash 读出保护 L1 状态是否置位。
name FLASH_GetReadOutProtectionSTS
fun Checks whether the FLASH Read Out Protection L1 status_value is set or not.
param none
return FlagStatus :
- SET Read protection L1 enable
- RESET Read protection L1 disable
FlagStatus FLASH_GetReadOutProtectionL2STS(void)检查 Flash 读出保护 L2 状态是否置位。
name FLASH_GetReadOutProtectionL2STS
fun Checks whether the FLASH Read Out Protection L2 status_value is set or not.
param none
return FlagStatus :
- SET Read protection L2 enable
- RESET Read protection L2 disable
uint32_t FLASH_GetWriteProtectionSTS(void)返回 Flash 写保护选项字节寄存器的值。
name FLASH_GetWriteProtectionSTS
fun Returns the FLASH Write Protection Option Bytes Register value.
param none
return The FLASH Write Protection Option Bytes Register value :
- Bit31 - Bit0 write-protect pages (248~255) ... pages (0~7)
FlagStatus FLASH_GetBOOTWriteProtectionSTS(void)检查 BOOT 保护状态是否置位。
name FLASH_GetBOOTWriteProtectionSTS
fun Checks whether the BOOT Protection status_value is set or not.
param none
return FlagStatus :
- SET BOOT write protection enable
- RESET BOOT write protection disable
void FLASH_INTConfig(uint32_t FLASH_INT, FunctionalState Cmd)使能指定的 Flash 中断。
name FLASH_INTConfig
fun Enables the specified FLASH interrupts.
param FLASH_INT :
- FLASH_INT_ERR FLASH Error Interrupt
- FLASH_INT_EOP FLASH end of operation Interrupt
param Cmd:(The input parameters must be the following values)
- ENABLE
- DISABLE
return none
FlagStatus FLASH_GetFlagSTS(uint32_t FLASH_FLAG)检查指定的 Flash 标志是否置位。
name FLASH_GetFlagSTS
fun Checks whether the specified FLASH flag is set or not.
param FLASH_FLAG :(The input parameters must be the following values)
- FLASH_FLAG_BUSY FLASH Busy flag
- FLASH_FLAG_PGERR FLASH Program error flag
- FLASH_FLAG_WRPERR FLASH Write protected error flag
- FLASH_FLAG_EOP FLASH End of Operation flag
- FLASH_FLAG_FKEYF FKEYR write KEY1 flag
- FLASH_FLAG_OPTKEYF OPTKEYR write KEY1 flag
return FlagStatus :
- SET Flag status is set
- RESET Flag status is reset
FlagStatus FLASH_GetOBFlagSTS(uint32_t FLASH_FLAG)检查指定的 Flash 标志是否置位。
name FLASH_GetOBFlagSTS
fun Checks whether the specified FLASH flag is set or not.
param FLASH_FLAG :(The input parameters must be the following values)
- FLASH_FLAG_OBERR FLASH Option Byte error flag
return FlagStatus :
- SET Flag status is set
- RESET Flag status is reset
void FLASH_ClearFlag(uint32_t FLASH_FLAG)清除 Flash 的状态标志。
name FLASH_ClearFlag
fun Clears the FLASH's status flags.
param FLASH_FLAG :(The input parameters must be the following values)
- FLASH_FLAG_PGERR FLASH Program error flag
- FLASH_FLAG_WRPERR FLASH Write protected error flag
- FLASH_FLAG_EOP FLASH End of Operation flag
return none
FLASH_STS FLASH_GetSTS(void)返回 FLASH_STS。
name FLASH_GetSTS
fun Returns the FLASH_STS.
param none
return FLASH_STS :
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
使用提示: 实现按 FLASH_BUSY、FLASH_ERR_PG、FLASH_ERR_WRP 的顺序判定,三者都未置位时返回 FLASH_EOP。
FLASH_STS FLASH_WaitForLastOpt(uint32_t Timeout)等待 Flash 操作完成或超时。
name FLASH_WaitForLastOpt
fun Waits for a Flash operation to complete or a timeout to occur.
param Timeout :(The input parameters must be the following values)
- EraseTimeout
- ProgramTimeout
return FLASH_STS: The returned value can be:
- FLASH_BUSY FLASH is busy
- FLASH_ERR_PG FLASH programming error
- FLASH_ERR_WRP FLASH Write protected error
- FLASH_EOP FLASH End of Operation
- FLASH_TIMEOUT FLASH operation timeout
使用提示: 实现递减超时计数,计数到 0 而 FLASH_STS 的 BUSY 位仍为 1 时返回 FLASH_TIMEOUT。
5.8.3 Demo 示例说明
本节说明 Flash 的 2 个示例:Flash_Program 演示页擦除、按字编程与回读校验,Option_Byte_Configure 演示选项字节的擦除与逐项配置。
5.8.3.1 Flash_Program Flash 编程
工程目录: projects/n32g41x_EVAL/examples/Flash/Flash_Program/
初始化打印串口→解锁 Flash 编程擦除控制器→擦除起始地址所在页→从起始地址起每 4 字节写入一个字→锁定控制器→逐字回读并与写入值比较
| 项目 | 源码解读 |
|---|---|
| 功能 | 擦除以 0x08008000 起始的一页,把 0x12345678 逐字写入 0x08008000 到 0x08008200 的区间,锁定控制器后回读校验,全部一致时打印测试通过。 |
| 源码组成 | src/main.c:main 主流程与页大小、起始与结束地址宏;inc/main.h:包含 n32g41x.h、log.h 并定义 TestStatus 枚举;src/n32g41x_it.c、inc/n32g41x_it.h:中断服务函数;readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程。 |
| 关键配置 | FLASH_PAGE_SIZE 为 0x200(512 字节),FLASH_WRITE_START_ADDR 为 0x08008000,FLASH_WRITE_END_ADDR 为 0x08008200,写入数据为 0x12345678。系统时钟为 HSI+PLL,N32G412 为 80 MHz,N32G415 为 96 MHz,USART1 以 115200 波特率输出 log。 |
| 程序与数据路径 | main 调用 log_init 后 FLASH_Unlock;FLASH_EraseOnePage 擦除起始页并检查返回值是否为 FLASH_EOP;while 循环在 Address 从起始到结束的区间内 FLASH_ProgramWord 逐字写入,每次 Address += 4;随后 FLASH_Lock,再以 *(__IO uint32_t*)Address 回读比较,最后按 MemoryProgramStatus 打印结果。 |
| 主要 API | FLASH_Unlock、FLASH_EraseOnePage、FLASH_ProgramWord、FLASH_Lock |
| 验证方法 | 运行后串口依次打印擦除与编程结果; 校验通过时打印 Flash Program Test PASSED!,不一致时打印失败的地址、实际值与期望值。 |
| 使用提示 | 编程前必须先擦除对应页; 编程区域为 0x08008000~0x08008200,须确保不与应用代码重叠; 每页 512 字节。 开发环境为 Keil MDK-ARM V5.34 或 IAR EWARM V8.50。 |
核心代码解读与移植
示例演示 Flash 的最小写入流程:解锁控制器、擦除目标页、按字编程、锁定后再回读校验。
数据路径: FLASH_Unlock -> FLASH_EraseOnePage 擦除起始页 -> FLASH_ProgramWord 逐字写入 -> FLASH_Lock -> 以 *(__IO uint32_t*) 回读比较
1. 用宏固定擦写区间。 示例把页大小、写入起始与结束地址定义为宏,擦除与编程都以这组宏为准,结果状态用 MemoryProgramStatus 记录。
#define FLASH_PAGE_SIZE ((uint32_t)0x200) /* 512 bytes per page */
#define FLASH_WRITE_START_ADDR ((uint32_t)0x08008000)
#define FLASH_WRITE_END_ADDR ((uint32_t)0x08008200)
volatile TestStatus MemoryProgramStatus = PASSED;
移植到其它板卡或存储布局时,只改这三个地址宏与页大小即可,后续代码不用改动。
2. 解锁控制器后擦除目标页。 擦除前先解锁 Flash 编程擦除控制器,再调用 FLASH_EraseOnePage,返回值不等于 FLASH_EOP 时进入死循环报错。
/* Unlock the Flash Program Erase controller */
FLASH_Unlock();
/* Erase one page */
printf("Erasing page at 0x%08X...\r\n", (uint32_t)FLASH_WRITE_START_ADDR);
if (FLASH_EraseOnePage(FLASH_WRITE_START_ADDR) != FLASH_EOP)
{
printf("Erase FAILED!\r\n");
while (1) {}
}
printf("Erase OK\r\n");
3. 按字循环编程并锁定。 循环从起始地址步进 4 字节,每次调用 FLASH_ProgramWord 写入同一个字,写完 0x08008000~0x08008200 后调用 FLASH_Lock 重新锁定控制器。
/* Program Flash word by word */
printf("Programming...\r\n");
Address = FLASH_WRITE_START_ADDR;
while (Address < FLASH_WRITE_END_ADDR)
{
if (FLASH_ProgramWord(Address, Data) != FLASH_EOP)
{
printf("Program FAILED at 0x%08X!\r\n", (uint32_t)Address);
while (1) {}
}
Address += 4;
}
printf("Program OK\r\n");
/* Lock the Flash */
FLASH_Lock();
4. 回读校验写入结果。 锁定后不再经驱动,直接用 *(__IO uint32_t*)Address 读回每个字与写入值比较,任一不符即置 MemoryProgramStatus 为 FAILED 并跳出。
/* Verify programmed data */
printf("Verifying...\r\n");
Address = FLASH_WRITE_START_ADDR;
MemoryProgramStatus = PASSED;
while (Address < FLASH_WRITE_END_ADDR)
{
if ((*(__IO uint32_t*)Address) != Data)
{
MemoryProgramStatus = FAILED;
printf("Verify FAILED at 0x%08X! (read 0x%08X, expected 0x%08X)\r\n",
(uint32_t)Address,
(uint32_t)(*(__IO uint32_t*)Address),
(uint32_t)Data);
break;
}
Address += 4;
}
对应源码: src/main.c
5.8.3.2 Option_Byte_Configure 选项字节配置
工程目录: projects/n32g41x_EVAL/examples/Flash/Option_Byte_Configure/
初始化打印串口→解锁 Flash 与选项字节控制器→擦除选项字节→依次编程 RDP1、USER0、USER1、USER3_USER2、USER5_USER4、USER6、USER7 与用户数据→解除全部页写保护→关闭读保护 L2→按返回值打印结果
| 项目 | 源码解读 |
|---|---|
| 功能 | 解锁并擦除选项字节区,依次关闭读保护 L1、选择软件看门狗、把 PD7 用作 NRST、配置启动位与 IWDG 冻结项、选择 BOOT 串口与 I2C/CAN 引脚、使能 BOOT 写保护并写入 2 字节用户数据,最后解除全部页写保护并关闭读保护 L2。 |
| 源码组成 | src/main.c:main 中按 FLASH_EOP 串接的选项字节编程流程;inc/main.h:包含 n32g41x.h、log.h;src/n32g41x_it.c、inc/n32g41x_it.h:中断服务函数;readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程。 |
| 关键配置 | 读保护 L1、L2 均关闭; USER0: FLASH_OB_IWDG_SOFTWARE;USER1: FLASH_OB_PD7_NRST;USER3_USER2: FLASH_OB_NBOOT0_SET、FLASH_OB_NBOOT1_SET、FLASH_OB_NSWBOOT0_SET、FLASH_OB_BOOT0_CFG_HIGH、FLASH_OB_IWDG_SLEEP_NOFRZ、FLASH_OB_IWDG_STOP_NOFRZ、POR 延时 0xFF;USER5_USER4: BOOT_UARTPIN_PA9PA10、BOOT_I2C_PIN_PA4PA5;USER6: BOOT_CAN_PIN_PA12PA11;USER7: BOOT_WRP_ENABLE;用户数据:0x55、0xAA; 系统时钟:HSI+PLL,N32G412 80 MHz,N32G415 96 MHz。 |
| 程序与数据路径 | main 先 FLASH_Unlock 与 Option_Bytes_Unlock,再 FLASH_EraseOB;state_value 为 FLASH_EOP 时依次调用 8 个选项字节编程函数,每个函数返回 FLASH_EOP 才会进入下一步;最后 FLASH_EnWriteProtection(~FLASH_WRP_AllPages) 解除全部页写保护、FLASH_ProgramOptionBytes_RDP2(FLASH_OB_RDP2_DISABLE) 关闭读保护 L2,并按最终 state_value 打印结果。 |
| 主要 API | FLASH_Unlock、Option_Bytes_Unlock、FLASH_EraseOB、FLASH_ProgramOptionBytes_RDP1、FLASH_ProgramOptionBytes_USER0~FLASH_ProgramOptionBytes_USER7、FLASH_ProgramOptionBytes_DATA1_DATA0、FLASH_EnWriteProtection、FLASH_ProgramOptionBytes_RDP2 |
| 验证方法 | 运行后串口打印 Option Byte configure Test start!;全部步骤成功时打印 Option Byte configure OK!,任一步失败则打印 Option Byte configure failed!,选项字节擦除失败时打印 Option Byte erase failed!。 |
| 使用提示 | 修改选项字节后可能需要系统复位才能生效; 开启 RDP2 后无法撤销,请勿在调试阶段使用; 写保护配置后相关页无法编程和擦除。 开发环境为 Keil MDK-ARM V5.x 或 IAR EWARM V8.50。 |
核心代码解读与移植
示例把选项字节的整段配置串成一条链:每一步都以上一步的返回值是否为 FLASH_EOP 来决定是否继续。
数据路径: FLASH_Unlock 与 Option_Bytes_Unlock -> FLASH_EraseOB 擦除选项字节 -> FLASH_ProgramOptionBytes_* 逐项写入 -> state_value == FLASH_EOP 串接各步 -> 串口打印结果
1. 先解锁两个控制器并擦除选项字节。 擦除选项字节前需要分别解锁 Flash 与选项字节两个控制器,FLASH_EraseOB 的结果存入 state_value 作为后续各步的闸门。
/* Unlocks the FLASH Program Erase Controller */
FLASH_Unlock();
/* Unlocks the Option Byte Program Erase Controller */
Option_Bytes_Unlock();
/* Erase Option Byte page */
state_value = FLASH_EraseOB();
2. 以返回值为闸门逐项编程。 每个编程步骤都先判断 state_value 是否为 FLASH_EOP,满足才执行并把新返回值写回 state_value,任一失败即跳过后续所有步骤。
/* Disable read protection L1 */
state_value = FLASH_ProgramOptionBytes_RDP1(FLASH_OB_RDP1_DISABLE);
/* Programs USER0~USER6 */
if(state_value == FLASH_EOP)
{
state_value = FLASH_ProgramOptionBytes_USER0(FLASH_OB_IWDG_SOFTWARE);
}
else
{
/* no process*/
}
if(state_value == FLASH_EOP)
{
state_value = FLASH_ProgramOptionBytes_USER1(FLASH_OB_PD7_NRST);
}
3. 一次配置多位与 BOOT 引脚。 FLASH_ProgramOptionBytes_USER3_USER2 一次接收 7 个参数,把启动位、看门狗冻结位与 POR 延时一次写入;FLASH_ProgramOptionBytes_USER5_USER4 与 FLASH_ProgramOptionBytes_USER6 分别选择 BOOT 串口、I2C 与 CAN 引脚。
if(state_value == FLASH_EOP)
{
state_value = FLASH_ProgramOptionBytes_USER3_USER2(FLASH_OB_NBOOT0_SET,FLASH_OB_NBOOT1_SET,FLASH_OB_NSWBOOT0_SET,\
FLASH_OB_BOOT0_CFG_HIGH,FLASH_OB_IWDG_SLEEP_NOFRZ,FLASH_OB_IWDG_STOP_NOFRZ,0xFF);
}
else
{
/* no process*/
}
if(state_value == FLASH_EOP)
{
state_value = FLASH_ProgramOptionBytes_USER5_USER4(BOOT_UARTPIN_PA9PA10,BOOT_I2C_PIN_PA4PA5);
}
else
{
/* no process*/
}
if(state_value == FLASH_EOP)
{
state_value = FLASH_ProgramOptionBytes_USER6(BOOT_CAN_PIN_PA12PA11);
}
4. 解除写保护并关闭读保护 L2。 链路末尾用取反的 ~FLASH_WRP_AllPages 解除全部页写保护,再关闭读保护 L2,最后按 state_value 打印成功或失败。
/* Disable write protection */
if(state_value == FLASH_EOP)
{
state_value = FLASH_EnWriteProtection(~FLASH_WRP_AllPages);
}
else
{
/* no process*/
}
/* Disable read protection L2 */
if(state_value == FLASH_EOP)
{
state_value = FLASH_ProgramOptionBytes_RDP2(FLASH_OB_RDP2_DISABLE);
}
对应源码: src/main.c
5.8.4 注意与排障
| 场景 | 检查与建议 | 来源 |
|---|---|---|
| 编程前未擦除 | 主存储区只能按页擦除,编程只能把存储位由 1 改为 0; 向未擦除的页写入新数据得到的是按位与的结果。 改写前先擦除目标页。 |
用户手册 |
| 控制器处于锁定状态 | 擦除与编程前必须先执行解锁序列打开 FLASH_CTRL 的 LOCK 位;密钥顺序或数值错误会使控制器保持锁定直到下次复位。 |
用户手册 |
| 页大小与地址对齐 | 主存储区以页为单位管理,N32G412、N32G415 的页大小为 0.5 KB(512 字节),主存储区最大 128 KB、共 256 页; 编程地址须按字(4 字节)对齐。 |
用户手册 |
| 写保护生效后仍要擦写 | 写保护以 8 页为一组,被保护的页无法编程和擦除; 解除写保护需要改写对应的 WRP 选项字节。 |
用户手册 |
| 读保护的不可逆性 | 使能读保护 L1 后通过调试口无法读出主存储区; 使能读保护 L2 后无法再撤销,且不能再擦写选项字节,配置前须确认。 |
用户手册 |
| 选项字节何时生效 | 选项字节的修改通常需要一次系统复位后才生效。 | 用户手册 |
N32G412、N32G415 系列勘误手册 V1.0.0(2026.6.30)的勘误列表只列出一行 A 版芯片条目,勘误链接列为空,未列出 Flash 相关条目。