5.6 FLASH 使用说明

本章说明 N32G033 标准外设库中 FLASH 驱动的文件组成、38 个公开函数,以及 Flash_Program、Option_Byte_Configure 2 个示例。FLASH 的擦除粒度、等待周期、选项字节与读出保护请配合用户手册的相关章节阅读,芯片已知问题以勘误手册为准。

5.6.1 相关文件

相对路径 作用
firmware/n32g033_std_periph_driver/inc/n32g033_flash.h 定义 FLASH_STS 等类型与写保护、选项字节、中断标志等宏,并按三段声明 38 个公开函数
firmware/n32g033_std_periph_driver/src/n32g033_flash.c 实现上述 38 个函数:等待周期、锁定与解锁、主存储区擦除与编程、选项字节编程与读出、中断与标志、向量表地址
projects/n32g033_EVAL/examples/FLASH/ FLASH 示例根目录,含 Flash_Program、Option_Byte_Configure 2 个示例
projects/n32g033_EVAL/examples/FLASH/Flash_Program/ 该示例的工程目录组成:MDK-ARM/、EWARM/、inc/、src/、readme.txt
projects/n32g033_EVAL/bsp/ 示例共用的板级支持文件:log.h 提供串口日志接口

5.6.2 API 参考

搜索 API共 38 个函数

以下 38 个公开函数按 n32g033_flash.h 的声明顺序分为 3 组;每个条目给出功能摘要与 n32g033_flash.c 中该函数正上方的注释原文。

等待周期、锁定与主存储区操作

FLASH_SetLatency、FLASH_GetLatency、FLASH_Unlock、FLASH_Lock、Flash_GetLockStatus、Option_Bytes_Unlock、Option_Bytes_Lock、OB_GetLockStatus、FLASH_EraseOnePage、FLASH_MassErase、FLASH_ProgramWord

选项字节编程与读出

FLASH_EraseOB、FLASH_ProgramOptionBytes_USER4_RDP1、FLASH_ProgramOptionBytes_USER0、FLASH_ProgramOptionBytes_USER1、FLASH_ProgramOptionBytes_USER3_USER2、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_GetOptionBytes_Data0、FLASH_GetOptionBytes_Data1、FLASH_GetReadOutProtectionSTS、FLASH_GetReadOutProtectionL2STS、FLASH_GetWriteProtectionSTS

中断、标志与向量表地址

FLASH_INTConfig、FLASH_GetFlagSTS、FLASH_GetOBFlagSTS、FLASH_ClearFlag、FLASH_GetSTS、FLASH_WaitForLastOpt、FLASH_SetVTORAddress

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 < HCLK <= 32MHz
            - FLASH_LATENCY_1    FLASH One Latency cycle, 32MHz < HCLK<= 64MHz
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.
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 < HCLK <= 32MHz
            - FLASH_LATENCY_1    FLASH One Latency cycle, 32MHz < HCLK<= 64MHz
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.
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
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)擦除指定的主 FLASH 页。
源码注释
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 0x0800FFFF, 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_ERR_ADD  FLASH address error
FLASH_STS FLASH_MassErase(void)擦除全部主 FLASH 页。
源码注释
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_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 0x0800FFFF, 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
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
FLASH_STS FLASH_ProgramOptionBytes_USER4_RDP1(uint32_t option_byte_user4, uint8_t option_byte_rpd1)编程选项字节 USER4 与 RDP1。
源码注释
name   FLASH_ProgramOptionBytes_USER4_RDP1
fun    Programs the Option Byte: USER4 and RDP1.
param  option_byte_user4 :(The input parameters must be the following values)
        - BOOT_UARTPIN_PA9PA10   BOOT uart pins are PA9 and PA10
        - BOOT_UARTPIN_PA13PA14  BOOT uart pins are PA13 and PA14
        - BOOT_UARTPIN_PF0PF1    BOOT uart pins are PF0 and PF1
        - BOOT_UARTPIN_PA2PA3    BOOT uart pins are PA2 and PA3
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
FLASH_STS FLASH_ProgramOptionBytes_USER0(uint32_t option_byte_iwdg)编程选项字节 USER0(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_PF3)编程选项字节 USER1(NRST_PF3)。
源码注释
name   FLASH_ProgramOptionBytes_USER1
fun    Programs the Option Byte: NRST_PF3.
param  option_byte_iwdg :(The input parameters must be the following values)
            - FLASH_OB_PF3_GPIO PF3 set GPIO
            - FLASH_OB_PF3_NRST PF3 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_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_user2:(The input parameters must be the following values)
            - 0x00 to 0xFF  PDR/POR filter control count value,8192~30us
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,各写入一个半字。
源码注释
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_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_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      PF3 used as NRST
            - RESET    PF3 used as GPIO
uint32_t FLASH_GetOptionBytes_User2(void)返回 FLASH 选项字节的 PORCNT 取值。
源码注释
name   FLASH_GetOptionBytes_User2
fun    Returns the FLASH Option Bytes PORCNT values.
param  none
return PORCNT:
            - 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
return FlagStatus :
            - SET      Enter this mode without resetting
            - RESET    Enter this mode reset
uint32_t FLASH_GetBOOTUartPIN(void)返回 BOOT 引导代码使用的串口通信引脚。
源码注释
name   FLASH_GetBOOTUartPIN
fun    Returns the serial communication pins for boot code.
param  none
return option_byte_user4:
         - BOOT_UARTPIN_PA9PA10    BOOT uart pins are PA9 and PA10
         - BOOT_UARTPIN_PB10PB11   BOOT uart pins are PB10 and PB11
         - BOOT_UARTPIN_PD10PD11   BOOT uart pins are PD10 and PD11
         - BOOT_UARTPIN_PA2PA3     BOOT uart pins are PA2 and PA3
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 :
            - Bit15 - Bit0 write-protects pages (120~127) - page (0~7)
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
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 状态。
源码注释
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_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
void FLASH_SetVTORAddress(uint32_t VTOR_address, uint32_t VTOR_cmd)设置中断向量表偏移地址。
源码注释
name   FLASH_SetVTORAddress
fun    Set interrupt vector offset address.
param  VTOR_address :(The input parameters must be the following values)
            - 0x00000000~0x7FFFFFFF
param  VTOR_cmd :(The input parameters must be the following values)
            - FLASH_VTOR_ENABLE
            - FLASH_VTOR_DISABLE
return none

5.6.3 Demo 示例说明

本节说明 FLASH 的 2 个示例:Flash_Program 直接对主 FLASH 读写并校验,Option_Byte_Configure 擦除并逐项编程选项字节。

5.6.3.1 Flash_Program FLASH 读写

工程目录: projects/n32g033_EVAL/examples/FLASH/Flash_Program/

解锁 FLASH 编程擦除控制器→擦除写入起始地址所在的一页→按字循环编程整页数据→锁定控制器→回读并逐字比较→串口打印结果

项目 源码解读
功能 直接对主 FLASH 读写:先解锁并擦除起始地址所在的一页,再按字循环写入固定数据,最后锁定并回读逐字比较,结果打印到串口。
源码组成 src/main.c 中的 main 完成解锁、擦除、编程与校验;
src/n32g033_it.c 只保留异常处理的空函数;
inc/main.h、inc/n32g033_it.h 给出声明;
readme.txt 给出测试步骤;
工程文件在 MDK-ARM/ 与 EWARM/。
关键配置 页大小 0x200,写入区间 0x08008000~0x08010000;
每个字写入固定数据 0xCDEF89AB;
系统时钟 64 MHz,串口 115200,TX 为 PA9、RX 为 PA10。
程序与数据路径 main → log_init 初始化串口 → FLASH_Unlock 解锁 → FLASH_EraseOnePage 擦除一页 → 循环调用 FLASH_ProgramWord 按字写入 → FLASH_Lock 锁定 → 循环回读并逐字比较 → 打印测试结果。
主要 API FLASH_Unlock、FLASH_EraseOnePage、FLASH_ProgramWord、FLASH_Lock
验证方法 硬件基于评估板 N32G033K8Q7-1_STB V1.0,串口波特率 115200;
编译下载复位运行后,串口打印 Flash Program Test Start 与 Flash Program Test End,回读数据不一致时会打印失败提示。
使用提示 单页为 0x200 字节;
写入前必须先擦除对应页,且写入地址要对齐到字。
核心代码解读与移植

示例在主 FLASH 上完整走一遍解锁、擦除、按字编程与回读校验的流程。

数据路径: 宏定义页大小与写入区间 → FLASH_Unlock 解锁 → FLASH_EraseOnePage 擦除一页 → 循环 FLASH_ProgramWord 按字写入 → FLASH_Lock 锁定 → 回读并逐字比较

1. 页大小与写入地址先用宏定好。 页大小决定擦除粒度,写入区间决定落在哪几个页上,两个宏都应是页大小与字宽度的整数倍。

src/main.csrc/main.c
#define FLASH_PAGE_SIZE        ((uint16_t)0x200)
#define FLASH_WRITE_START_ADDR ((uint32_t)0x08008000)
#define FLASH_WRITE_END_ADDR   ((uint32_t)0x08010000)

FLASH_PAGE_SIZE 取 0x200 表示一页 512 字节;写入起始地址要按页对齐,否则擦除会波及相邻页。

2. 先解锁再做整页擦除。 擦除前必须先解锁编程擦除控制器,擦除结果用返回状态判断,出错时进入死循环并打印提示。

src/main.csrc/main.c · main
    /* Unlocks the FLASH Program Erase Controller */
    FLASH_Unlock();

    /* Erase */
    if (FLASH_EOP != FLASH_EraseOnePage(FLASH_WRITE_START_ADDR))
    {
        while(1)
        {
            log_info("Flash EraseOnePage Error. Please Deal With This Error Promptly\r\n");
        }
    }

FLASH 只能把位由 1 写为 0,写入新数据前必须先擦除对应页。

3. 按字循环编程整页。 每次写入一个字,地址按 4 字节递增直到写满一页;每一步同样用返回状态判断是否成功。

src/main.csrc/main.c · main
    /* Program */
    for (Counter_Num = 0; Counter_Num < FLASH_PAGE_SIZE; Counter_Num += 4)
    {
        if (FLASH_EOP != FLASH_ProgramWord(FLASH_WRITE_START_ADDR + Counter_Num, Erase_Data))
        {
            while(1)
            {
                log_info("Flash ProgramWord Error. Please Deal With This Error Promptly\r\n");
            }
        }
    }

写入地址要与字宽度对齐,示例用固定数据 0xCDEF89AB 便于回读时辨认。

4. 锁定后回读校验。 编程结束先锁定控制器,再把同一页逐字读回与写入数据比较,全部一致才算通过。

src/main.csrc/main.c · main
    /* Locks the FLASH Program Erase Controller */
    FLASH_Lock();

    /* Check */
    for (Counter_Num = 0; Counter_Num < FLASH_PAGE_SIZE; Counter_Num += 4)
    {
        if (Erase_Data != (*(__IO uint32_t*)(FLASH_WRITE_START_ADDR + Counter_Num)))
        {
            log_info("Flash Program Test Failed\r\n");
            break;
        }
    }

回读时按地址直接解引用即可;写保护或读出保护打开后回读结果可能不同,要按保护配置判断。

对应源码: src/main.c、firmware/n32g033_std_periph_driver/inc/n32g033_flash.h

5.6.3.2 Option_Byte_Configure 选项字节配置

工程目录: projects/n32g033_EVAL/examples/FLASH/Option_Byte_Configure/

解锁 FLASH 与选项字节控制器→擦除选项字节→依次编程 USER4/RDP1 与 USER0→依次编程 USER1、USER3/USER2 与 DATA→设置写保护并编程 RDP2→按返回值判定并打印结果

项目 源码解读
功能 擦除并逐项编程选项字节:依次写入 USER4/RDP1、USER0、USER1、USER3/USER2、DATA1/DATA0、写保护与 RDP2,并按返回值判断每一步是否成功。
源码组成 src/main.c 中的 main 完成选项字节的擦除与链式编程;
src/n32g033_it.c 只保留异常处理的空函数;
inc/main.h、inc/n32g033_it.h 给出声明;
readme.txt 给出测试步骤;
工程文件在 MDK-ARM/ 与 EWARM/。
关键配置 先 FLASH_Unlock 再 Option_Bytes_Unlock;
编程内容:BOOT 串口引脚取 PA9/PA10、关闭读出保护 L1、IWDG 用软件、PF3 作 NRST、nBOOT0/nBOOT1/nSWBOOT0 置位、BOOT0 高有效、USER2 取 0xFF、DATA1/DATA0 取 0x55/0xAA、关闭全部页写保护后再关闭 RDP2。
程序与数据路径 main → log_init → FLASH_Unlock 与 Option_Bytes_Unlock → FLASH_EraseOB 擦除选项字节 → 依次调用各选项字节编程函数与 FLASH_EnWriteProtection,每步比对 FLASH_EOP → 全部成功后打印结果。
主要 API FLASH_Unlock、Option_Bytes_Unlock、FLASH_EraseOB、FLASH_ProgramOptionBytes_USER4_RDP1、FLASH_ProgramOptionBytes_USER0、FLASH_EnWriteProtection、FLASH_ProgramOptionBytes_RDP2
验证方法 硬件基于评估板 N32G033K8Q7-1_STB V1.0,串口波特率 115200;
编译下载复位运行后,串口打印 Option Byte configure OK! 表示全部选项字节编程成功,否则打印失败提示。
使用提示 系统时钟 64 MHz;
选项字节会改变启动、看门狗与读出保护等芯片行为,量产前逐项确认取值。
核心代码解读与移植

示例擦除选项字节后逐项编程,每一步都用返回状态串起来,全部成功才打印通过。

数据路径: FLASH_Unlock 与 Option_Bytes_Unlock 解锁 → FLASH_EraseOB 擦除选项字节 → 依次编程 USER4/RDP1 与 USER0 等各项 → FLASH_EnWriteProtection 设置写保护 → FLASH_ProgramOptionBytes_RDP2 关闭读出保护 L2 → 按返回值打印结果

1. 解锁并擦除选项字节。 选项字节有独立的解锁函数,擦除后返回状态用 FLASH_EOP 判断是否成功。

src/main.csrc/main.c · main
    /* 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();

    if(state_value == FLASH_EOP)
    {

选项字节与主存储区共用 FLASH 控制器,但各有独立的解锁与擦除入口,两步都不能省。

2. 用返回值把各项编程串成链。 每项编程都返回 FLASH_STS,示例判断上一步成功后再做下一步。

src/main.csrc/main.c · main
        state_value = FLASH_ProgramOptionBytes_USER4_RDP1(BOOT_UARTPIN_PA9PA10,FLASH_OB_RDP1_DISABLE);
        /* Programs USER0 */
        if(state_value == FLASH_EOP)
        {
            state_value = FLASH_ProgramOptionBytes_USER0(FLASH_OB_IWDG_SOFTWARE);
        }

USER1、USER3/USER2、DATA1/DATA0 也用同样的链式写法依次编程,任一步失败即停止。

3. 写保护与读出保护单独设置。 写保护用页掩码一次配置,读出保护 L2 由专门函数编程,取值决定是否使能。

src/main.csrc/main.c · main
        if(state_value == FLASH_EOP)
        {
            state_value = FLASH_EnWriteProtection(~FLASH_WRP_AllPages);
        }
src/main.csrc/main.c · main
        if(state_value == FLASH_EOP)
        {
            state_value = FLASH_ProgramOptionBytes_RDP2(FLASH_OB_RDP2_DISABLE);
        }

写保护掩码取反表示关闭全部页保护;RDP2 一旦使能通常不可逆,修改前要确认量产策略。

4. 用最终返回值判定并打印。 全部步骤完成后按最后一次的返回状态打印通过或失败。

src/main.csrc/main.c · main
        if(state_value == FLASH_EOP)
        {
            log_info("Option Byte configure OK!\r\n");
        }
        else
        {
            log_info("Option Byte configure failed!\r\n");
        }

串口提示是一次性输出;要复核结果可在复位后读回各选项字节再比较。

对应源码: src/main.c、firmware/n32g033_std_periph_driver/inc/n32g033_flash.h

5.6.4 注意与排障

场景 检查与建议 来源
编程前必须擦除 用户手册 FLASH 章节写明位只能由 1 写为 0,编程前必须先擦除对应页或整个主存储区;
改数据前先擦除目标页。
用户手册
代码等待周期 用户手册 FLASH 章节写明主频高于 32 MHz 时需要设置一个等待周期;
系统时钟 64 MHz 下使用 1 个等待周期。
用户手册
选项字节编程 用户手册 FLASH 章节写明选项字节需先解锁选项字节控制器再擦除;
编程后按返回状态确认每一步成功。
用户手册
读出保护 L2 用户手册 FLASH 章节写明读出保护 L2 使能后不可逆;
量产前确认是否需要使能。
用户手册
中断向量表偏移 用户手册 FLASH 章节写明可用寄存器设置中断向量表偏移地址;
做 Bootloader 跳转时同步设置该偏移。
用户手册

勘误手册 V1.0.0(2025.9.25)针对 A 版芯片,未列出 FLASH 相关条目。

国民技术开发套件文档中心

按芯片系列在线查阅国民技术 MCU 开发套件的芯片资料、SDK、驱动 API 与 Demo 示例说明。

© 2026 国民技术股份有限公司 版权所有粤ICP备11069485号本站使用不含 Cookie 的匿名访问统计