1 /***************COPYRIGHT(C)  2019 WCH. A11 rights reserved*********************
2  * File Name          : ch32f10x_flash.h
3  * Author             : WCH
4  * Version            : V1.0.0
5  * Date               : 2019/10/15
6  * Description        : This file contains all the functions prototypes for the FLASH
7  *                      firmware library.
8  *******************************************************************************/
9 #ifndef __CH32F10x_FLASH_H
10 #define __CH32F10x_FLASH_H
11 
12 #ifdef __cplusplus
13  extern "C" {
14 #endif
15 
16 #include "ch32f10x.h"
17 
18 /* FLASH Status */
19 typedef enum
20 {
21   FLASH_BUSY = 1,
22   FLASH_ERROR_PG,
23   FLASH_ERROR_WRP,
24   FLASH_COMPLETE,
25   FLASH_TIMEOUT
26 }FLASH_Status;
27 
28 
29 /* Flash_Latency */
30 #define FLASH_Latency_0                ((uint32_t)0x00000000)  /*!< FLASH Zero Latency cycle */
31 #define FLASH_Latency_1                ((uint32_t)0x00000001)  /*!< FLASH One Latency cycle */
32 #define FLASH_Latency_2                ((uint32_t)0x00000002)  /*!< FLASH Two Latency cycles */
33 
34 /* Half_Cycle_Enable_Disable */
35 #define FLASH_HalfCycleAccess_Enable   ((uint32_t)0x00000008)  /*!< FLASH Half Cycle Enable */
36 #define FLASH_HalfCycleAccess_Disable  ((uint32_t)0x00000000)  /*!< FLASH Half Cycle Disable */
37 
38 /* Prefetch_Buffer_Enable_Disable */
39 #define FLASH_PrefetchBuffer_Enable    ((uint32_t)0x00000010)  /*!< FLASH Prefetch Buffer Enable */
40 #define FLASH_PrefetchBuffer_Disable   ((uint32_t)0x00000000)  /*!< FLASH Prefetch Buffer Disable */
41 
42 /* Values to be used with CH32F10x Low and Medium density devices */
43 #define FLASH_WRProt_Pages0to3         ((uint32_t)0x00000001) /*!< CH32 Low and Medium density devices: Write protection of page 0 to 3 */
44 #define FLASH_WRProt_Pages4to7         ((uint32_t)0x00000002) /*!< CH32 Low and Medium density devices: Write protection of page 4 to 7 */
45 #define FLASH_WRProt_Pages8to11        ((uint32_t)0x00000004) /*!< CH32 Low and Medium density devices: Write protection of page 8 to 11 */
46 #define FLASH_WRProt_Pages12to15       ((uint32_t)0x00000008) /*!< CH32 Low and Medium density devices: Write protection of page 12 to 15 */
47 #define FLASH_WRProt_Pages16to19       ((uint32_t)0x00000010) /*!< CH32 Low and Medium density devices: Write protection of page 16 to 19 */
48 #define FLASH_WRProt_Pages20to23       ((uint32_t)0x00000020) /*!< CH32 Low and Medium density devices: Write protection of page 20 to 23 */
49 #define FLASH_WRProt_Pages24to27       ((uint32_t)0x00000040) /*!< CH32 Low and Medium density devices: Write protection of page 24 to 27 */
50 #define FLASH_WRProt_Pages28to31       ((uint32_t)0x00000080) /*!< CH32 Low and Medium density devices: Write protection of page 28 to 31 */
51 
52 /* Values to be used with CH32F10x Medium-density devices */
53 #define FLASH_WRProt_Pages32to35       ((uint32_t)0x00000100) /*!< CH32 Medium-density devices: Write protection of page 32 to 35 */
54 #define FLASH_WRProt_Pages36to39       ((uint32_t)0x00000200) /*!< CH32 Medium-density devices: Write protection of page 36 to 39 */
55 #define FLASH_WRProt_Pages40to43       ((uint32_t)0x00000400) /*!< CH32 Medium-density devices: Write protection of page 40 to 43 */
56 #define FLASH_WRProt_Pages44to47       ((uint32_t)0x00000800) /*!< CH32 Medium-density devices: Write protection of page 44 to 47 */
57 #define FLASH_WRProt_Pages48to51       ((uint32_t)0x00001000) /*!< CH32 Medium-density devices: Write protection of page 48 to 51 */
58 #define FLASH_WRProt_Pages52to55       ((uint32_t)0x00002000) /*!< CH32 Medium-density devices: Write protection of page 52 to 55 */
59 #define FLASH_WRProt_Pages56to59       ((uint32_t)0x00004000) /*!< CH32 Medium-density devices: Write protection of page 56 to 59 */
60 #define FLASH_WRProt_Pages60to63       ((uint32_t)0x00008000) /*!< CH32 Medium-density devices: Write protection of page 60 to 63 */
61 #define FLASH_WRProt_Pages64to67       ((uint32_t)0x00010000) /*!< CH32 Medium-density devices: Write protection of page 64 to 67 */
62 #define FLASH_WRProt_Pages68to71       ((uint32_t)0x00020000) /*!< CH32 Medium-density devices: Write protection of page 68 to 71 */
63 #define FLASH_WRProt_Pages72to75       ((uint32_t)0x00040000) /*!< CH32 Medium-density devices: Write protection of page 72 to 75 */
64 #define FLASH_WRProt_Pages76to79       ((uint32_t)0x00080000) /*!< CH32 Medium-density devices: Write protection of page 76 to 79 */
65 #define FLASH_WRProt_Pages80to83       ((uint32_t)0x00100000) /*!< CH32 Medium-density devices: Write protection of page 80 to 83 */
66 #define FLASH_WRProt_Pages84to87       ((uint32_t)0x00200000) /*!< CH32 Medium-density devices: Write protection of page 84 to 87 */
67 #define FLASH_WRProt_Pages88to91       ((uint32_t)0x00400000) /*!< CH32 Medium-density devices: Write protection of page 88 to 91 */
68 #define FLASH_WRProt_Pages92to95       ((uint32_t)0x00800000) /*!< CH32 Medium-density devices: Write protection of page 92 to 95 */
69 #define FLASH_WRProt_Pages96to99       ((uint32_t)0x01000000) /*!< CH32 Medium-density devices: Write protection of page 96 to 99 */
70 #define FLASH_WRProt_Pages100to103     ((uint32_t)0x02000000) /*!< CH32 Medium-density devices: Write protection of page 100 to 103 */
71 #define FLASH_WRProt_Pages104to107     ((uint32_t)0x04000000) /*!< CH32 Medium-density devices: Write protection of page 104 to 107 */
72 #define FLASH_WRProt_Pages108to111     ((uint32_t)0x08000000) /*!< CH32 Medium-density devices: Write protection of page 108 to 111 */
73 #define FLASH_WRProt_Pages112to115     ((uint32_t)0x10000000) /*!< CH32 Medium-density devices: Write protection of page 112 to 115 */
74 #define FLASH_WRProt_Pages116to119     ((uint32_t)0x20000000) /*!< CH32 Medium-density devices: Write protection of page 115 to 119 */
75 #define FLASH_WRProt_Pages120to123     ((uint32_t)0x40000000) /*!< CH32 Medium-density devices: Write protection of page 120 to 123 */
76 #define FLASH_WRProt_Pages124to127     ((uint32_t)0x80000000) /*!< CH32 Medium-density devices: Write protection of page 124 to 127 */
77 
78 #define FLASH_WRProt_Pages62to255      ((uint32_t)0x80000000) /*!< CH32 Medium-density devices: Write protection of page 62 to 255 */
79 
80 #define FLASH_WRProt_AllPages          ((uint32_t)0xFFFFFFFF) /*!< Write protection of all Pages */
81 
82 /* Option_Bytes_IWatchdog */
83 #define OB_IWDG_SW                     ((uint16_t)0x0001)  /*!< Software IWDG selected */
84 #define OB_IWDG_HW                     ((uint16_t)0x0000)  /*!< Hardware IWDG selected */
85 
86 /* Option_Bytes_nRST_STOP */
87 #define OB_STOP_NoRST                  ((uint16_t)0x0002) /*!< No reset generated when entering in STOP */
88 #define OB_STOP_RST                    ((uint16_t)0x0000) /*!< Reset generated when entering in STOP */
89 
90 /* Option_Bytes_nRST_STDBY  */
91 #define OB_STDBY_NoRST                 ((uint16_t)0x0004) /*!< No reset generated when entering in STANDBY */
92 #define OB_STDBY_RST                   ((uint16_t)0x0000) /*!< Reset generated when entering in STANDBY */
93 
94 /* FLASH_Interrupts */
95 #define FLASH_IT_ERROR                 ((uint32_t)0x00000400)  /*!< FPEC error interrupt source */
96 #define FLASH_IT_EOP                   ((uint32_t)0x00001000)  /*!< End of FLASH Operation Interrupt source */
97 #define FLASH_IT_BANK1_ERROR           FLASH_IT_ERROR          /*!< FPEC BANK1 error interrupt source */
98 #define FLASH_IT_BANK1_EOP             FLASH_IT_EOP            /*!< End of FLASH BANK1 Operation Interrupt source */
99 
100 /* FLASH_Flags */
101 #define FLASH_FLAG_BSY                 ((uint32_t)0x00000001)  /*!< FLASH Busy flag */
102 #define FLASH_FLAG_EOP                 ((uint32_t)0x00000020)  /*!< FLASH End of Operation flag */
103 #define FLASH_FLAG_PGERR               ((uint32_t)0x00000004)  /*!< FLASH Program error flag */
104 #define FLASH_FLAG_WRPRTERR            ((uint32_t)0x00000010)  /*!< FLASH Write protected error flag */
105 #define FLASH_FLAG_OPTERR              ((uint32_t)0x00000001)  /*!< FLASH Option Byte error flag */
106 
107 #define FLASH_FLAG_BANK1_BSY                 FLASH_FLAG_BSY       /*!< FLASH BANK1 Busy flag*/
108 #define FLASH_FLAG_BANK1_EOP                 FLASH_FLAG_EOP       /*!< FLASH BANK1 End of Operation flag */
109 #define FLASH_FLAG_BANK1_PGERR               FLASH_FLAG_PGERR     /*!< FLASH BANK1 Program error flag */
110 #define FLASH_FLAG_BANK1_WRPRTERR            FLASH_FLAG_WRPRTERR  /*!< FLASH BANK1 Write protected error flag */
111 
112 
113 /*Functions used for all CH32F10x 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 
138 /* New function used for all CH32F10x devices */
139 void FLASH_UnlockBank1(void);
140 void FLASH_LockBank1(void);
141 FLASH_Status FLASH_EraseAllBank1Pages(void);
142 FLASH_Status FLASH_GetBank1Status(void);
143 FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout);
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 
150 #endif /* __CH32F10x_FLASH_H */
151 
152