5.6 FLASH Guide
This chapter describes the file layout and the 38 public functions of the FLASH driver in the N32G033 standard peripheral library, together with the 2 examples (Flash_Program and Option_Byte_Configure). Read the FLASH sections of the user manual for the erase granularity, wait states, option bytes, and read-out protection, and the errata sheet for known chip issues.
5.6.1 Related files
| Relative path | Purpose |
|---|---|
firmware/n32g033_std_periph_driver/inc/n32g033_flash.h |
Defines the FLASH_STS type and the macros for write protection, option bytes, and interrupt flags, and declares 38 public functions in three blocks |
firmware/n32g033_std_periph_driver/src/n32g033_flash.c |
Implements those 38 functions: wait states, lock and unlock, main-memory erase and program, option-byte program and read-out, interrupts and flags, and the vector table address |
projects/n32g033_EVAL/examples/FLASH/ |
FLASH example root directory, with 2 examples: Flash_Program and Option_Byte_Configure |
projects/n32g033_EVAL/examples/FLASH/Flash_Program/ |
Project layout of this example: MDK-ARM/, EWARM/, inc/, src/, and readme.txt |
projects/n32g033_EVAL/bsp/ |
Board support files shared by the examples: the serial log interface in log.h |
5.6.2 API reference
The following 38 public functions are listed in the declaration order of n32g033_flash.h in 3 groups; each entry gives a summary and the original comment above the function in n32g033_flash.c.
Wait states, locking, and main-memory operations
FLASH_SetLatency, FLASH_GetLatency, FLASH_Unlock, FLASH_Lock, Flash_GetLockStatus, Option_Bytes_Unlock, Option_Bytes_Lock, OB_GetLockStatus, FLASH_EraseOnePage, FLASH_MassErase, FLASH_ProgramWord
Option-byte program and read-out
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
Interrupts, flags, and the vector table address
FLASH_INTConfig, FLASH_GetFlagSTS, FLASH_GetOBFlagSTS, FLASH_ClearFlag, FLASH_GetSTS, FLASH_WaitForLastOpt, FLASH_SetVTORAddress
void FLASH_SetLatency(uint32_t FLASH_Latency)Sets the code latency value.
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)Get the code latency value.
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)Unlocks the FLASH Program Erase Controller.
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)Locks the FLASH Program Erase Controller.
name FLASH_Lock
fun Locks the FLASH Program Erase Controller.
param none
return none
FlagStatus Flash_GetLockStatus(void)Get the Flash lock status.
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)Unlocks the Option_Bytes Program Erase Controller.
name Option_Bytes_Unlock
fun Unlocks the Option_Bytes Program Erase Controller.
param none
return none
void Option_Bytes_Lock(void)Locks the Option_Bytes Program Erase Controller.
name Option_Bytes_Lock
fun Locks the Option_Bytes Program Erase Controller.
param none
return none
FlagStatus OB_GetLockStatus(void)Get the Option Bytes lock status.
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)Erases a specified main FLASH page.
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)Erases all main FLASH pages.
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)Programs one word at a specified address.
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)Erases the FLASH option bytes.
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)Programs the Option Byte: USER4 and 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)Programs the Option Byte: 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)Programs the Option Byte: 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)Programs the Option Byte: USER3 and 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)Programs 2 half words at a specified Option Byte Data1 and 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)Write protects the desired 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)Programs the Option Byte: 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)Enables the read out protection.
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)Enables the read out protection 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)Returns the FLASH User Option Bytes values.
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)Returns the FLASH User Option Bytes values.
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)Returns the FLASH Option Bytes PORCNT values.
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)Returns the FLASH User Option Bytes values.
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)Returns the serial communication pins for boot code.
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)Returns the FLASH User Option Bytes values.
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)Returns the FLASH User Option Bytes values.
name FLASH_GetOptionBytes_Data1
fun Returns the FLASH User Option Bytes values.
param none
return data1:
- 0x00 to 0xFF.
FlagStatus FLASH_GetReadOutProtectionSTS(void)Checks whether the FLASH Read Out Protection L1 status_value is set or not.
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)Checks whether the FLASH Read Out Protection L2 status_value is set or not.
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)Returns the FLASH Write Protection Option Bytes Register value.
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)Enables the specified FLASH interrupts.
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)Checks whether the specified FLASH flag is set or not.
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)Checks whether the specified FLASH flag is set or not.
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)Clears the FLASH’s status flags.
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)Returns the 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_STS FLASH_WaitForLastOpt(uint32_t Timeout)Waits for a Flash operation to complete or a timeout to occur.
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)Set interrupt vector offset address.
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 descriptions
This section describes the 2 FLASH examples: Flash_Program reads and writes the main FLASH directly with a verification pass, and Option_Byte_Configure erases and programs the option bytes item by item.
5.6.3.1 Flash_Program FLASH read and write
Example directory: projects/n32g033_EVAL/examples/FLASH/Flash_Program/
Unlock the FLASH program/erase controller→Erase the page that holds the write start address→Program the whole page word by word→Lock the controller→Read back and compare word by word→Print the result over the serial port
| Item | Source analysis |
|---|---|
| Function | Reads and writes the main FLASH directly: unlock and erase the page at the start address, program a fixed word across the page, then lock and read back word by word to compare, printing the result over the serial port. |
| Source files | main in src/main.c performs the unlock, erase, program, and check;src/n32g033_it.c keeps only empty exception handlers;inc/main.h and inc/n32g033_it.h give the declarations;readme.txt gives the test steps;the project files are in MDK-ARM/ and EWARM/. |
| Key configuration | Page size 0x200, write range 0x08008000 to 0x08010000;every word holds the fixed data 0xCDEF89AB;the system clock is 64 MHz and the UART is 115200 with TX on PA9 and RX on PA10. |
| Program and data path | main -> log_init starts the serial port -> FLASH_Unlock unlocks -> FLASH_EraseOnePage erases one page -> a loop calls FLASH_ProgramWord for each word -> FLASH_Lock locks -> a loop reads back and compares word by word -> the test result is printed. |
| Main APIs | FLASH_Unlock, FLASH_EraseOnePage, FLASH_ProgramWord, FLASH_Lock |
| Verification | The hardware is the N32G033K8Q7-1_STB V1.0 evaluation board with the UART at 115200 baud; after downloading, resetting, and running, the serial port prints Flash Program Test Start and Flash Program Test End, and prints a failure message if the read-back data differs. |
| Use note | A page is 0x200 bytes; the target page must be erased before programming, and the write address must be word-aligned. |
Core-code walkthrough and porting
The example runs the full unlock, erase, word programming, and read-back check flow on the main FLASH.
Data path: macros set the page size and write range → FLASH_Unlock unlocks → FLASH_EraseOnePage erases one page → a loop calls FLASH_ProgramWord for each word → FLASH_Lock locks → the data is read back and compared word by word
1. Set the page size and write address with macros first. The page size sets the erase granularity and the write range decides which pages are touched, so both macros should be multiples of the page size and the word width.
#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 set to 0x200 makes one page 512 bytes; the write start address must be page-aligned, otherwise the erase reaches into the neighbouring page.
2. Unlock before erasing the whole page. The program/erase controller must be unlocked before erasing, and the erase result is judged by the returned status, which enters a dead loop with a message on error.
/* 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 can only clear bits from 1 to 0, so the target page must be erased before new data is written.
3. Program the whole page word by word. Each step writes one word and the address advances by 4 bytes until the page is full; every step is checked by the returned status as well.
/* 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");
}
}
}
The write address must be word-aligned, and the example uses the fixed data 0xCDEF89AB so it is easy to recognize on read-back.
4. Lock, then read back and check. After programming, the controller is locked first, and then the same page is read back word by word and compared with the written data; only a full match passes.
/* 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;
}
}
Read-back is a plain dereference of the address; with write protection or read-out protection enabled the read-back value may differ, so judge it against the protection settings.
Corresponding source files: src/main.c, firmware/n32g033_std_periph_driver/inc/n32g033_flash.h
5.6.3.2 Option_Byte_Configure Option-byte configuration
Example directory: projects/n32g033_EVAL/examples/FLASH/Option_Byte_Configure/
Unlock the FLASH and option-byte controllers→Erase the option bytes→Program USER4/RDP1 and USER0 in turn→Program USER1, USER3/USER2, and the DATA bytes→Set write protection and program RDP2→Judge by the return value and print the result
| Item | Source analysis |
|---|---|
| Function | Erases and programs the option bytes item by item: it writes USER4/RDP1, USER0, USER1, USER3/USER2, DATA1/DATA0, the write protection, and RDP2 in turn, judging success by the return value of each step. |
| Source files | main in src/main.c performs the option-byte erase and chained programming;src/n32g033_it.c keeps only empty exception handlers;inc/main.h and inc/n32g033_it.h give the declarations;readme.txt gives the test steps;the project files are in MDK-ARM/ and EWARM/. |
| Key configuration | FLASH_Unlock first and then Option_Bytes_Unlock;the programmed content: the BOOT UART pins are PA9/PA10, read protection L1 is disabled, IWDG is software, PF3 is NRST, nBOOT0/nBOOT1/nSWBOOT0 are set, BOOT0 is active high, USER2 is 0xFF, DATA1/DATA0 are 0x55/0xAA, and all-page write protection is turned off before RDP2 is disabled. |
| Program and data path | main -> log_init -> FLASH_Unlock and Option_Bytes_Unlock -> FLASH_EraseOB erases the option bytes -> the option-byte program functions and FLASH_EnWriteProtection are called in turn, each compared against FLASH_EOP -> the result is printed when every step succeeds. |
| Main APIs | FLASH_Unlock, Option_Bytes_Unlock, FLASH_EraseOB, FLASH_ProgramOptionBytes_USER4_RDP1, FLASH_ProgramOptionBytes_USER0, FLASH_EnWriteProtection, FLASH_ProgramOptionBytes_RDP2 |
| Verification | The hardware is the N32G033K8Q7-1_STB V1.0 evaluation board with the UART at 115200 baud; after downloading, resetting, and running, the serial port prints Option Byte configure OK! when every option byte is programmed successfully, otherwise it prints a failure message. |
| Use note | The system clock is 64 MHz; the option bytes change chip behaviour such as boot, watchdog, and read-out protection, so confirm each value before volume production. |
Core-code walkthrough and porting
The example erases the option bytes and programs them item by item, chaining each step on the returned status and printing success only when all pass.
Data path: FLASH_Unlock and Option_Bytes_Unlock unlock → FLASH_EraseOB erases the option bytes → USER4/RDP1 and USER0 and the rest are programmed in turn → FLASH_EnWriteProtection sets the write protection → FLASH_ProgramOptionBytes_RDP2 disables read protection L2 → the return value drives the result message
1. Unlock and erase the option bytes. The option bytes have their own unlock function, and the erase result is judged against FLASH_EOP.
/* 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)
{
The option bytes and the main memory share the FLASH controller, but each has its own unlock and erase entry point, so neither step can be skipped.
2. Chain the program steps on the return value. Each program call returns a FLASH_STS, and the example only proceeds when the previous step reported success.
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, and DATA1/DATA0 are programmed in the same chained style, and any failure stops the chain.
3. Set the write protection and the read-out protection separately. The write protection is set in one call through a page mask, while read protection L2 is programmed by a dedicated function whose value decides whether it is enabled.
if(state_value == FLASH_EOP)
{
state_value = FLASH_EnWriteProtection(~FLASH_WRP_AllPages);
}
if(state_value == FLASH_EOP)
{
state_value = FLASH_ProgramOptionBytes_RDP2(FLASH_OB_RDP2_DISABLE);
}
Inverting the write-protection mask disables protection on all pages; once enabled, RDP2 is usually irreversible, so confirm the production policy before changing it.
4. Decide and print on the final return value. After all steps, the last returned status drives the pass or fail message.
if(state_value == FLASH_EOP)
{
log_info("Option Byte configure OK!\r\n");
}
else
{
log_info("Option Byte configure failed!\r\n");
}
The serial message is a one-shot output; to double-check, read the option bytes back and compare after a reset.
Corresponding source files: src/main.c, firmware/n32g033_std_periph_driver/inc/n32g033_flash.h
5.6.4 Notes and troubleshooting
| Scenario | Check & action | Source |
|---|---|---|
| Erase before programming | The FLASH section of the user manual states that bits can only be cleared from 1 to 0, so the target page or the whole main memory must be erased before programming; erase the target page before changing its data. |
User manual |
| Code wait state | The FLASH section of the user manual states that one wait state is required when the frequency is above 32 MHz; use one wait state at a 64 MHz system clock. |
User manual |
| Option-byte programming | The FLASH section of the user manual states that the option bytes need the option-byte controller unlocked before erasing; confirm each step by the return value after programming. |
User manual |
| Read-out protection L2 | The FLASH section of the user manual states that read protection L2 is irreversible once enabled; confirm whether it is needed before volume production. |
User manual |
| Vector table offset | The FLASH section of the user manual states that the interrupt vector table offset address can be set in a register; set the same offset when you jump from a bootloader. |
User manual |
Errata sheet V1.0.0 (2025.9.25) covers revision A silicon and lists no FLASH entries.