1 /********************************** (C) COPYRIGHT ******************************* 2 * File Name : ch32v10x_flash.h 3 * Author : WCH 4 * Version : V1.0.0 5 * Date : 2020/04/30 6 * Description : This file contains all the functions prototypes for the FLASH 7 * firmware library. 8 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. 9 * SPDX-License-Identifier: Apache-2.0 10 *******************************************************************************/ 11 #ifndef __CH32V10x_FLASH_H 12 #define __CH32V10x_FLASH_H 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 #include "ch32v10x.h" 19 20 /* FLASH Status */ 21 typedef enum 22 { 23 FLASH_BUSY = 1, 24 FLASH_ERROR_PG, 25 FLASH_ERROR_WRP, 26 FLASH_COMPLETE, 27 FLASH_TIMEOUT 28 } FLASH_Status; 29 30 /* Flash_Latency */ 31 #define FLASH_Latency_0 ((uint32_t)0x00000000) /* FLASH Zero Latency cycle */ 32 #define FLASH_Latency_1 ((uint32_t)0x00000001) /* FLASH One Latency cycle */ 33 #define FLASH_Latency_2 ((uint32_t)0x00000002) /* FLASH Two Latency cycles */ 34 35 /* Half_Cycle_Enable_Disable */ 36 #define FLASH_HalfCycleAccess_Enable ((uint32_t)0x00000008) /* FLASH Half Cycle Enable */ 37 #define FLASH_HalfCycleAccess_Disable ((uint32_t)0x00000000) /* FLASH Half Cycle Disable */ 38 39 /* Prefetch_Buffer_Enable_Disable */ 40 #define FLASH_PrefetchBuffer_Enable ((uint32_t)0x00000010) /* FLASH Prefetch Buffer Enable */ 41 #define FLASH_PrefetchBuffer_Disable ((uint32_t)0x00000000) /* FLASH Prefetch Buffer Disable */ 42 43 /* Values to be used with CH32V10x Low and Medium density devices */ 44 #define FLASH_WRProt_Pages0to3 ((uint32_t)0x00000001) /* CH32 Low and Medium density devices: Write protection of page 0 to 3 */ 45 #define FLASH_WRProt_Pages4to7 ((uint32_t)0x00000002) /* CH32 Low and Medium density devices: Write protection of page 4 to 7 */ 46 #define FLASH_WRProt_Pages8to11 ((uint32_t)0x00000004) /* CH32 Low and Medium density devices: Write protection of page 8 to 11 */ 47 #define FLASH_WRProt_Pages12to15 ((uint32_t)0x00000008) /* CH32 Low and Medium density devices: Write protection of page 12 to 15 */ 48 #define FLASH_WRProt_Pages16to19 ((uint32_t)0x00000010) /* CH32 Low and Medium density devices: Write protection of page 16 to 19 */ 49 #define FLASH_WRProt_Pages20to23 ((uint32_t)0x00000020) /* CH32 Low and Medium density devices: Write protection of page 20 to 23 */ 50 #define FLASH_WRProt_Pages24to27 ((uint32_t)0x00000040) /* CH32 Low and Medium density devices: Write protection of page 24 to 27 */ 51 #define FLASH_WRProt_Pages28to31 ((uint32_t)0x00000080) /* CH32 Low and Medium density devices: Write protection of page 28 to 31 */ 52 53 /* Values to be used with CH32V10x Medium-density devices */ 54 #define FLASH_WRProt_Pages32to35 ((uint32_t)0x00000100) /* CH32 Medium-density devices: Write protection of page 32 to 35 */ 55 #define FLASH_WRProt_Pages36to39 ((uint32_t)0x00000200) /* CH32 Medium-density devices: Write protection of page 36 to 39 */ 56 #define FLASH_WRProt_Pages40to43 ((uint32_t)0x00000400) /* CH32 Medium-density devices: Write protection of page 40 to 43 */ 57 #define FLASH_WRProt_Pages44to47 ((uint32_t)0x00000800) /* CH32 Medium-density devices: Write protection of page 44 to 47 */ 58 #define FLASH_WRProt_Pages48to51 ((uint32_t)0x00001000) /* CH32 Medium-density devices: Write protection of page 48 to 51 */ 59 #define FLASH_WRProt_Pages52to55 ((uint32_t)0x00002000) /* CH32 Medium-density devices: Write protection of page 52 to 55 */ 60 #define FLASH_WRProt_Pages56to59 ((uint32_t)0x00004000) /* CH32 Medium-density devices: Write protection of page 56 to 59 */ 61 #define FLASH_WRProt_Pages60to63 ((uint32_t)0x00008000) /* CH32 Medium-density devices: Write protection of page 60 to 63 */ 62 #define FLASH_WRProt_Pages64to67 ((uint32_t)0x00010000) /* CH32 Medium-density devices: Write protection of page 64 to 67 */ 63 #define FLASH_WRProt_Pages68to71 ((uint32_t)0x00020000) /* CH32 Medium-density devices: Write protection of page 68 to 71 */ 64 #define FLASH_WRProt_Pages72to75 ((uint32_t)0x00040000) /* CH32 Medium-density devices: Write protection of page 72 to 75 */ 65 #define FLASH_WRProt_Pages76to79 ((uint32_t)0x00080000) /* CH32 Medium-density devices: Write protection of page 76 to 79 */ 66 #define FLASH_WRProt_Pages80to83 ((uint32_t)0x00100000) /* CH32 Medium-density devices: Write protection of page 80 to 83 */ 67 #define FLASH_WRProt_Pages84to87 ((uint32_t)0x00200000) /* CH32 Medium-density devices: Write protection of page 84 to 87 */ 68 #define FLASH_WRProt_Pages88to91 ((uint32_t)0x00400000) /* CH32 Medium-density devices: Write protection of page 88 to 91 */ 69 #define FLASH_WRProt_Pages92to95 ((uint32_t)0x00800000) /* CH32 Medium-density devices: Write protection of page 92 to 95 */ 70 #define FLASH_WRProt_Pages96to99 ((uint32_t)0x01000000) /* CH32 Medium-density devices: Write protection of page 96 to 99 */ 71 #define FLASH_WRProt_Pages100to103 ((uint32_t)0x02000000) /* CH32 Medium-density devices: Write protection of page 100 to 103 */ 72 #define FLASH_WRProt_Pages104to107 ((uint32_t)0x04000000) /* CH32 Medium-density devices: Write protection of page 104 to 107 */ 73 #define FLASH_WRProt_Pages108to111 ((uint32_t)0x08000000) /* CH32 Medium-density devices: Write protection of page 108 to 111 */ 74 #define FLASH_WRProt_Pages112to115 ((uint32_t)0x10000000) /* CH32 Medium-density devices: Write protection of page 112 to 115 */ 75 #define FLASH_WRProt_Pages116to119 ((uint32_t)0x20000000) /* CH32 Medium-density devices: Write protection of page 115 to 119 */ 76 #define FLASH_WRProt_Pages120to123 ((uint32_t)0x40000000) /* CH32 Medium-density devices: Write protection of page 120 to 123 */ 77 #define FLASH_WRProt_Pages124to127 ((uint32_t)0x80000000) /* CH32 Medium-density devices: Write protection of page 124 to 127 */ 78 79 #define FLASH_WRProt_Pages62to255 ((uint32_t)0x80000000) /* CH32 Medium-density devices: Write protection of page 62 to 255 */ 80 81 #define FLASH_WRProt_AllPages ((uint32_t)0xFFFFFFFF) /* Write protection of all Pages */ 82 83 /* Option_Bytes_IWatchdog */ 84 #define OB_IWDG_SW ((uint16_t)0x0001) /* Software IWDG selected */ 85 #define OB_IWDG_HW ((uint16_t)0x0000) /* Hardware IWDG selected */ 86 87 /* Option_Bytes_nRST_STOP */ 88 #define OB_STOP_NoRST ((uint16_t)0x0002) /* No reset generated when entering in STOP */ 89 #define OB_STOP_RST ((uint16_t)0x0000) /* Reset generated when entering in STOP */ 90 91 /* Option_Bytes_nRST_STDBY */ 92 #define OB_STDBY_NoRST ((uint16_t)0x0004) /* No reset generated when entering in STANDBY */ 93 #define OB_STDBY_RST ((uint16_t)0x0000) /* Reset generated when entering in STANDBY */ 94 95 /* FLASH_Interrupts */ 96 #define FLASH_IT_ERROR ((uint32_t)0x00000400) /* FPEC error interrupt source */ 97 #define FLASH_IT_EOP ((uint32_t)0x00001000) /* End of FLASH Operation Interrupt source */ 98 #define FLASH_IT_BANK1_ERROR FLASH_IT_ERROR /* FPEC BANK1 error interrupt source */ 99 #define FLASH_IT_BANK1_EOP FLASH_IT_EOP /* End of FLASH BANK1 Operation Interrupt source */ 100 101 /* FLASH_Flags */ 102 #define FLASH_FLAG_BSY ((uint32_t)0x00000001) /* FLASH Busy flag */ 103 #define FLASH_FLAG_EOP ((uint32_t)0x00000020) /* FLASH End of Operation flag */ 104 #define FLASH_FLAG_PGERR ((uint32_t)0x00000004) /* FLASH Program error flag */ 105 #define FLASH_FLAG_WRPRTERR ((uint32_t)0x00000010) /* FLASH Write protected error flag */ 106 #define FLASH_FLAG_OPTERR ((uint32_t)0x00000001) /* FLASH Option Byte error flag */ 107 108 #define FLASH_FLAG_BANK1_BSY FLASH_FLAG_BSY /* FLASH BANK1 Busy flag*/ 109 #define FLASH_FLAG_BANK1_EOP FLASH_FLAG_EOP /* FLASH BANK1 End of Operation flag */ 110 #define FLASH_FLAG_BANK1_PGERR FLASH_FLAG_PGERR /* FLASH BANK1 Program error flag */ 111 #define FLASH_FLAG_BANK1_WRPRTERR FLASH_FLAG_WRPRTERR /* FLASH BANK1 Write protected error flag */ 112 113 /*Functions used for all CH32V10x devices*/ 114 void FLASH_SetLatency(uint32_t FLASH_Latency); 115 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess); 116 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer); 117 void FLASH_Unlock(void); 118 void FLASH_Lock(void); 119 FLASH_Status FLASH_ErasePage(uint32_t Page_Address); 120 FLASH_Status FLASH_EraseAllPages(void); 121 FLASH_Status FLASH_EraseOptionBytes(void); 122 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data); 123 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data); 124 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data); 125 FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages); 126 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState); 127 FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY); 128 uint32_t FLASH_GetUserOptionByte(void); 129 uint32_t FLASH_GetWriteProtectionOptionByte(void); 130 FlagStatus FLASH_GetReadOutProtectionStatus(void); 131 FlagStatus FLASH_GetPrefetchBufferStatus(void); 132 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState); 133 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG); 134 void FLASH_ClearFlag(uint32_t FLASH_FLAG); 135 FLASH_Status FLASH_GetStatus(void); 136 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout); 137 void FLASH_Unlock_Fast(void); 138 void FLASH_Lock_Fast(void); 139 void FLASH_BufReset(void); 140 void FLASH_BufLoad(uint32_t Address, uint32_t Data0, uint32_t Data1, uint32_t Data2, uint32_t Data3); 141 void FLASH_ErasePage_Fast(uint32_t Page_Address); 142 void FLASH_ProgramPage_Fast(uint32_t Page_Address); 143 144 /* New function used for all CH32V10x devices */ 145 void FLASH_UnlockBank1(void); 146 void FLASH_LockBank1(void); 147 FLASH_Status FLASH_EraseAllBank1Pages(void); 148 FLASH_Status FLASH_GetBank1Status(void); 149 FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout); 150 151 #ifdef __cplusplus 152 } 153 #endif 154 155 #endif /* __CH32V10x_FLASH_H */ 156