5.8 Flash Guide

This chapter describes the file composition and the 50 public functions of the Flash driver in the N32G41x standard peripheral library: the latency and prefetch buffer settings, ICache and cache locking, the unlock and lock sequences for programming and erasing, page and mass erase, word programming, programming and readback of the option bytes including write and read protection, and interrupt and status flag handling, together with the Flash_Program and Option_Byte_Configure examples. Read the FLASH address space, the page layout, the erase and program flow and the option byte definitions together with the “Memory And Bus Architecture” chapter of the user manual, and take the errata sheet as the authority on known chip issues.

5.8.1 Related files

Relative path Purpose
firmware/n32g41x_std_periph_driver/inc/n32g41x_flash.h Defines FLASH_STS and the configuration, cache, key and option byte macros, and declares 50 public functions in the eleven sections configuration, cache management, unlock and lock, erase, program, option byte programming, write protection, read protection, option byte readback, status and protection status, and interrupt and flag
firmware/n32g41x_std_periph_driver/src/n32g41x_flash.c Implements those 50 functions: latency and prefetch settings, ICache reset and locking, controller unlock sequences, page and mass erase, word programming, option byte erase, program and readback, write and read protection configuration, and status flag reads and timeout waits
firmware/CMSIS/device/n32g41x.h Defines the FLASH, FLASH_OB and FLASH_OB2 register structures and their base addresses, and field macros such as FLASH_KEY1 and FLASH_KEY2
projects/n32g41x_EVAL/examples/Flash/ Flash example root directory with the Flash_Program and Option_Byte_Configure examples
projects/n32g41x_EVAL/examples/Flash/Flash_Program/ Composition of one example project: MDK-ARM/, EWARM/, inc/, src/ and readme.txt
projects/n32g41x_EVAL/bsp/ Board support files shared by both examples: the UART log initialisation and the millisecond delay function

5.8.2 API reference

Search API50 functions

The following 50 public functions are listed in the declaration order of n32g41x_flash.h in 11 groups; each entry gives a summary and the original comment above the function in n32g41x_flash.c.

Configuration

FLASH_SetLatency, FLASH_GetLatency, FLASH_PrefetchBufSet, Flash_GetPrefetchBufferStatus

Cache management

FLASH_iCacheRST, FLASH_iCacheCmd, FLASH_StartCacheLock, FLASH_StopCacheLock, FLASH_CancelCacheLock

Unlock and lock

FLASH_Unlock, FLASH_Lock, Flash_GetLockStatus, Option_Bytes_Unlock, Option_Bytes_Lock, OB_GetLockStatus

Erase

FLASH_EraseOnePage, FLASH_MassErase, FLASH_EraseOB

Program

FLASH_ProgramWord

Option byte programming

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

Write protection

FLASH_EnWriteProtection

Read protection

FLASH_ProgramOptionBytes_RDP2, FLASH_ReadOutProtectionL1, FLASH_ReadOutProtectionL2_ENABLE

Option byte readback

FLASH_GetOptionBytes_User0, FLASH_GetOptionBytes_User1, FLASH_GetOptionBytes_User2, FLASH_GetOptionBytes_User3, FLASH_GetBOOTUartPIN, FLASH_GetBOOTI2cPIN, FLASH_GetBOOTCanPIN, FLASH_GetOptionBytes_Data0, FLASH_GetOptionBytes_Data1

Status and protection status

FLASH_GetReadOutProtectionSTS, FLASH_GetReadOutProtectionL2STS, FLASH_GetWriteProtectionSTS, FLASH_GetBOOTWriteProtectionSTS

Interrupt and flag

FLASH_INTConfig, FLASH_GetFlagSTS, FLASH_GetOBFlagSTS, FLASH_ClearFlag, FLASH_GetSTS, FLASH_WaitForLastOpt

void FLASH_SetLatency(uint32_t FLASH_Latency)Sets the code latency value.
Source comment
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.

Use note: The implementation reads FLASH_AC, clears the LATENCY field and writes the register back, leaving the prefetch buffer and ICache bits unchanged.

uint8_t FLASH_GetLatency(void)Get the code latency value.
Source comment
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)Enables or disables the Prefetch Buffer.
Source comment
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)Get the Prefetch Buffer status.
Source comment
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 Reset.
Source comment
name    FLASH_iCacheRST
fun     ICache Reset.
param   none
return  none
void FLASH_iCacheCmd(uint32_t FLASH_iCache)Enables or disables the iCache.
Source comment
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)Start cache lock.
Source comment
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)Stop cache lock.
Source comment
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)Cancel cache lock.
Source comment
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)Unlocks the FLASH Program Erase Controller.
Source comment
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.

Use note: The implementation writes FLASH_KEY1 (0x45670123) and then FLASH_KEY2 (0xCDEF89AB) to FLASH_KEY.

void FLASH_Lock(void)Locks the FLASH Program Erase Controller.
Source comment
name   FLASH_Lock
fun    Locks the FLASH Program Erase Controller.
param  none
return none
FlagStatus Flash_GetLockStatus(void)Get the Flash lock status.
Source comment
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.
Source comment
name   Option_Bytes_Unlock
fun    Unlocks the Option_Bytes Program Erase Controller.
param  none
return none

Use note: The implementation writes FLASH_KEY1 and then FLASH_KEY2 to FLASH_OPTKEY, the same key pair used by FLASH_Unlock.

void Option_Bytes_Lock(void)Locks the Option_Bytes Program Erase Controller.
Source comment
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.
Source comment
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.
Source comment
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)Erases all main FLASH pages.
Source comment
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)Erases the FLASH option bytes.
Source comment
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

Use note: The implementation reads the RDP2 status first and returns FLASH_ERR_RDP2 directly when read protection L2 is enabled; it calls Option_Bytes_Unlock itself before erasing.

FLASH_STS FLASH_ProgramWord(uint32_t address, uint32_t data)Programs one word at a specified address.
Source comment
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

Use note: The implementation checks that address is word aligned (4 bytes) first and returns FLASH_ERR_ADD without starting the program when it is not.

FLASH_STS FLASH_ProgramOptionBytes_RDP1(uint8_t option_byte_rpd1)Programs the Option Byte: RDP1.
Source comment
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

Use note: The implementation checks the RDP2 status and calls Option_Bytes_Unlock itself before programming, returning FLASH_ERR_RDP2 when L2 is already enabled.

FLASH_STS FLASH_ProgramOptionBytes_USER0(uint32_t option_byte_iwdg)Programs the Option Byte: IWDG_SW.
Source comment
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)Programs the Option Byte: NRST_PD7.
Source comment
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)Programs the Option Byte: USER3 and USER2.
Source comment
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)Programs the Option Byte: USER5 and USER4.
Source comment
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)Programs the Option Byte: USER6.
Source comment
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)Programs the Option Byte: BOOT write protection Enable or disable.
Source comment
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)Programs 2 half words at a specified Option Byte Data1 and Data0.
Source comment
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
Source comment
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

Use note: The implementation inverts FLASH_Pages, splits it into WRP0 to WRP3 and writes it into the option bytes, calling Option_Bytes_Unlock itself; passing ~FLASH_WRP_AllPages releases the write protection of all pages.

FLASH_STS FLASH_ProgramOptionBytes_RDP2(uint8_t option_byte_rpd2)Programs the Option Byte: RDP2.
Source comment
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.
Source comment
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.
Source comment
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.
Source comment
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.
Source comment
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)Returns the FLASH Option Bytes POR_DELAY values.
Source comment
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)Returns the FLASH User Option Bytes values.
Source comment
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

Use note: The implementation shifts option_byte_bit right by 4 bits and ANDs it with FLASH_OB, so the macro passed in must be aligned with the bits in FLASH_OB.

uint32_t FLASH_GetBOOTUartPIN(void)Returns the FLASH User Option Bytes values.
Source comment
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)Returns the FLASH User Option Bytes values.
Source comment
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

Use note: The implementation shifts the I2C pin selection field of FLASH_OB2 left by 16 bits and returns it.

uint32_t FLASH_GetBOOTCanPIN(void)Returns the FLASH User Option Bytes values.
Source comment
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)Returns the FLASH User Option Bytes values.
Source comment
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.
Source comment
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.
Source comment
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.
Source comment
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.
Source comment
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)Checks whether the BOOT Protection status_value is set or not.
Source comment
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)Enables the specified FLASH interrupts.
Source comment
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.
Source comment
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)Checks whether the specified FLASH flag is set or not.
Source comment
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.
Source comment
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.
Source comment
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

Use note: The implementation tests FLASH_BUSY, then FLASH_ERR_PG and then FLASH_ERR_WRP, and returns FLASH_EOP when none of them is set.

FLASH_STS FLASH_WaitForLastOpt(uint32_t Timeout)Waits for a Flash operation to complete or a timeout to occur.
Source comment
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

Use note: The implementation decrements the timeout counter and returns FLASH_TIMEOUT when the counter reaches 0 while the BUSY bit of FLASH_STS is still set.

5.8.3 Demo descriptions

This section describes the 2 Flash examples: Flash_Program performs a page erase, word programming and a readback check, and Option_Byte_Configure erases and configures the option bytes item by item.

5.8.3.1 Flash_Program Flash programming

Example directory: projects/n32g41x_EVAL/examples/Flash/Flash_Program/

Initialise the log UART→Unlock the Flash program erase controller→Erase the page holding the start address→Write one word every 4 bytes from the start address→Lock the controller→Read every word back and compare it with the written value

Item Source analysis
Function Erases the page starting at 0x08008000, writes 0x12345678 word by word from 0x08008000 to 0x08008200, locks the controller and then reads the data back;
when every word matches, the test passes.
Source files src/main.c: the main flow and the page size, start and end address macros;
inc/main.h: includes n32g41x.h and log.h and defines the TestStatus enum;
src/n32g41x_it.c and inc/n32g41x_it.h: interrupt service routines;
readme.txt: function, environment and usage;
MDK-ARM/ and EWARM/: the Keil and IAR projects.
Key configuration FLASH_PAGE_SIZE is 0x200 (512 bytes), FLASH_WRITE_START_ADDR is 0x08008000, FLASH_WRITE_END_ADDR is 0x08008200, and the written data is 0x12345678.
The system clock is HSI plus PLL, 80 MHz for the N32G412 and 96 MHz for the N32G415, and USART1 logs at 115200 baud.
Program and data path main calls log_init and then FLASH_Unlock;
FLASH_EraseOnePage erases the start page and the code checks that the return value is FLASH_EOP;
a while loop calls FLASH_ProgramWord for every 4-byte step of Address between the start and end addresses;
then FLASH_Lock is called and every word is read back through *(__IO uint32_t*)Address and compared, and MemoryProgramStatus is printed at the end.
Main APIs FLASH_Unlock, FLASH_EraseOnePage, FLASH_ProgramWord, FLASH_Lock
Verification After the reset the UART prints the erase and program results in turn;
when the check passes it prints Flash Program Test PASSED!, otherwise it prints the failing address, the value read and the expected value.
Use note The target page must be erased before programming;
the programming range is 0x08008000 to 0x08008200 and must not overlap the application code;
one page is 512 bytes.
The development environment is Keil MDK-ARM V5.34 or IAR EWARM V8.50.
Core-code walkthrough and porting

The example shows the minimum Flash write flow: unlock the controller, erase the target page, program word by word, then lock and read back for verification.

Data path: FLASH_Unlock -> FLASH_EraseOnePage erases the start page -> FLASH_ProgramWord writes word by word -> FLASH_Lock -> read back through *(__IO uint32_t*) and compare

1. Pin the erase and program range with macros. The example defines the page size and the start and end addresses as macros, and records the result in MemoryProgramStatus.

src/main.csrc/main.c
#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;

When porting to another board or memory layout, only these three address macros and the page size change; the rest of the code stays the same.

2. Unlock the controller and erase the target page. The Flash program erase controller is unlocked first, then FLASH_EraseOnePage is called and the code loops forever on error when the return value is not FLASH_EOP.

src/main.csrc/main.c · main
    /* 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. Program word by word in a loop and lock. The loop steps 4 bytes at a time from the start address and calls FLASH_ProgramWord with the same word each time; after 0x08008000 to 0x08008200 is written, FLASH_Lock locks the controller again.

src/main.csrc/main.c · main
    /* 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. Read the data back for verification. After locking, the code no longer goes through the driver: it reads each word with *(__IO uint32_t*)Address and compares it with the written value, and on the first mismatch it sets MemoryProgramStatus to FAILED and breaks out.

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

Corresponding source files: src/main.c

5.8.3.2 Option_Byte_Configure Option byte configuration

Example directory: projects/n32g41x_EVAL/examples/Flash/Option_Byte_Configure/

Initialise the log UART→Unlock the Flash and option byte controllers→Erase the option bytes→Program RDP1, USER0, USER1, USER3_USER2, USER5_USER4, USER6, USER7 and the user data in turn→Release the write protection of all pages→Disable read protection L2→Print the result

Item Source analysis
Function Unlocks and erases the option byte area, then disables read protection L1, selects the software IWDG, uses PD7 as NRST, configures the boot bits and the IWDG freeze options, selects the BOOT UART and I2C/CAN pins, enables BOOT write protection and writes 2 user data bytes, then releases the write protection of all pages and disables read protection L2.
Source files src/main.c: the option byte programming flow chained on FLASH_EOP inside main;
inc/main.h: includes n32g41x.h and log.h;
src/n32g41x_it.c and inc/n32g41x_it.h: interrupt service routines;
readme.txt: function, environment and usage;
MDK-ARM/ and EWARM/: the Keil and IAR projects.
Key configuration Read protection L1 and L2 are both disabled;
USER0 selects FLASH_OB_IWDG_SOFTWARE;
USER1 selects FLASH_OB_PD7_NRST;
USER3_USER2 receives 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 and a POR delay of 0xFF;
USER5_USER4 selects BOOT_UARTPIN_PA9PA10 and BOOT_I2C_PIN_PA4PA5;
USER6 selects BOOT_CAN_PIN_PA12PA11;
USER7 selects BOOT_WRP_ENABLE;
the user data is 0x55 and 0xAA.
The system clock is HSI plus PLL, 80 MHz for the N32G412 and 96 MHz for the N32G415.
Program and data path main calls FLASH_Unlock and Option_Bytes_Unlock first and then FLASH_EraseOB;
while state_value is FLASH_EOP the code calls 8 option byte programming functions in turn and only moves on when the call returns FLASH_EOP;
finally FLASH_EnWriteProtection(~FLASH_WRP_AllPages) releases the write protection of all pages and FLASH_ProgramOptionBytes_RDP2(FLASH_OB_RDP2_DISABLE) disables read protection L2, and the result is printed from the final state_value.
Main APIs FLASH_Unlock, Option_Bytes_Unlock, FLASH_EraseOB, FLASH_ProgramOptionBytes_RDP1, FLASH_ProgramOptionBytes_USER0 to FLASH_ProgramOptionBytes_USER7, FLASH_ProgramOptionBytes_DATA1_DATA0, FLASH_EnWriteProtection, FLASH_ProgramOptionBytes_RDP2
Verification After the reset the UART prints Option Byte configure Test start!;
when every step succeeds it prints Option Byte configure OK!, when any step fails it prints Option Byte configure failed!, and when the option byte erase fails it prints Option Byte erase failed!.
Use note A system reset may be required after the option bytes are changed;
read protection L2 cannot be reverted once enabled and must not be used during debugging;
pages under write protection cannot be programmed or erased.
The development environment is Keil MDK-ARM V5.x or IAR EWARM V8.50.
Core-code walkthrough and porting

The example chains the whole option byte configuration into one sequence: each step decides whether to continue from the return value of the previous step.

Data path: FLASH_Unlock and Option_Bytes_Unlock -> FLASH_EraseOB erases the option bytes -> FLASH_ProgramOptionBytes_* writes item by item -> state_value == FLASH_EOP chains the steps -> the UART prints the result

1. Unlock both controllers and erase the option bytes. Erasing the option bytes needs both the Flash and the option byte controllers unlocked, and the result of FLASH_EraseOB is stored in state_value as the gate for the following steps.

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();

2. Program item by item with the return value as a gate. Every programming step first checks whether state_value is FLASH_EOP, runs only when it is and writes the new return value back into state_value, so any failure skips all the remaining steps.

src/main.csrc/main.c · main
        /* 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. Configure several bits and the BOOT pins at once. FLASH_ProgramOptionBytes_USER3_USER2 takes 7 parameters and writes the boot bits, the watchdog freeze bits and the POR delay in one call; FLASH_ProgramOptionBytes_USER5_USER4 and FLASH_ProgramOptionBytes_USER6 select the BOOT UART, I2C and CAN pins.

src/main.csrc/main.c · main
        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*/
        }
src/main.csrc/main.c · main
        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. Release the write protection and disable read protection L2. At the end of the chain the inverted ~FLASH_WRP_AllPages releases the write protection of all pages, read protection L2 is disabled, and the code prints success or failure from state_value.

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

Corresponding source files: src/main.c

5.8.4 Notes and troubleshooting

Scenario Check & action Source
Programming without erasing first The main memory can only be erased page by page, and programming can only change a bit from 1 to 0;
writing new data into a page that has not been erased gives the bitwise AND of the two values.
Erase the target page before rewriting it.
User manual
The controller is locked Before erasing or programming the unlock sequence must open the LOCK bit of FLASH_CTRL;
a wrong key order or value keeps the controller locked until the next reset.
User manual
Page size and address alignment The main memory is managed in pages of 0.5 KB (512 bytes) on the N32G412 and N32G415, up to 128 KB and 256 pages in total, and the programming address must be word aligned (4 bytes). User manual
Writing to a write-protected page Write protection covers 8 pages per group and protected pages cannot be programmed or erased;
releasing the protection requires rewriting the matching WRP option bytes.
User manual
Read protection is irreversible Read protection L1 keeps the main memory from being read through the debug port;
read protection L2 cannot be reverted once enabled and blocks any further option byte erase or program, so confirm before configuring it.
User manual
When option byte changes take effect A change to the option bytes usually takes effect only after a system reset. User manual

The N32G412 and N32G415 series errata sheet V1.0.0 (2026.6.30) lists a single row for revision A chips with an empty errata link column, and lists no Flash entries.

Nations Technologies DevKit Docs

Datasheets, SDK, driver APIs and demo guides for Nations Technologies MCU development kits, organized by chip series.

© 2026 Nations Technologies Inc. All rights reserved.粤ICP备11069485号This site uses cookie-free anonymous analytics.