libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
g0/flash.h
Go to the documentation of this file.
1/** @defgroup flash_defines FLASH Defines
2 *
3 * @ingroup STM32G0xx_defines
4 *
5 * @brief <b>Defined Constants and Types for the STM32G0xx Flash Control</b>
6 *
7 * @version 1.0.0
8 *
9 * LGPL License Terms @ref lgpl_license
10 * */
11/*
12 * This file is part of the libopencm3 project.
13 *
14 * This library is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28/**@{*/
29#ifndef LIBOPENCM3_FLASH_H
30#define LIBOPENCM3_FLASH_H
31
33
34/** @defgroup flash_registers FLASH Registers
35@{*/
36#define FLASH_ACR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x00)
37#define FLASH_KEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x08)
38#define FLASH_OPTKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x0c)
39#define FLASH_SR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x10)
40#define FLASH_CR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14)
41#define FLASH_ECCR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x18)
42#define FLASH_OPTR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x20)
43#define FLASH_PCROP1ASR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x24)
44#define FLASH_PCROP1AER MMIO32(FLASH_MEM_INTERFACE_BASE + 0x28)
45#define FLASH_WRP1AR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x2c)
46#define FLASH_WRP1BR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x30)
47#define FLASH_PCROP1BSR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x34)
48#define FLASH_PCROP1BER MMIO32(FLASH_MEM_INTERFACE_BASE + 0x38)
49#define FLASH_SECR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x80)
50/**@}*/
51
52
53/** @defgroup flash_acr ACR Access control register
54@{*/
55
56/** FLASH_ACR_DBG_SWEN Debug access software enable **/
57#define FLASH_ACR_DBG_SWEN (1 << 18)
58/** FLASH_ACR_EMPTY Flash User area empty **/
59#define FLASH_ACR_EMPTY (1 << 16)
60/** FLASH_ACR_ICRST Instruction cache reset **/
61#define FLASH_ACR_ICRST (1 << 11)
62/** FLASH_ACR_ICEN Instruction cache enable **/
63#define FLASH_ACR_ICEN (1 << 9)
64/** FLASH_ACR_PRFTEN Prefetch enable **/
65#define FLASH_ACR_PRFTEN (1 << 8)
66
67#define FLASH_ACR_LATENCY_SHIFT 0
68#define FLASH_ACR_LATENCY_MASK 0x7
69/** @defgroup flash_latency FLASH Wait States
70 * @brief Flash memory access latency. flash HCLK max freq for 0ws is 24mhz (range 1 voltage) / 8mhz (range 0), 48mhz/16mhz for 1ws and 64mhz for 2ws.
71@{*/
72#define FLASH_ACR_LATENCY_0WS 0x00
73#define FLASH_ACR_LATENCY_1WS 0x01
74#define FLASH_ACR_LATENCY_2WS 0x02
75/**@}*/
76
77/**@}*/
78
79/** @defgroup flash_keyr KEYR Flash key register
80@{*/
81
82/** FLASH_KEYR_KEY1 Flash key 1 **/
83#define FLASH_KEYR_KEY1 ((uint32_t)0x45670123)
84/** FLASH_KEYR_KEY2 Flash key 2 **/
85#define FLASH_KEYR_KEY2 ((uint32_t)0xcdef89ab)
86
87/**@}*/
88
89/** @defgroup flash_optkeyr OPTKEYR Option byte key register
90@{*/
91
92/** FLASH_OPTKEYR_KEY1 Option key 1 **/
93#define FLASH_OPTKEYR_KEY1 ((uint32_t)0x08192a3b)
94/** FLASH_OPTKEYR_KEY2 Option key 2 **/
95#define FLASH_OPTKEYR_KEY2 ((uint32_t)0x4c5d6e7f)
96
97/**@}*/
98
99/** @defgroup flash_sr SR Status register
100@{*/
101
102/** FLASH_SR_CFGBSY Programming or erase configuration busy. **/
103#define FLASH_SR_CFGBSY (1 << 18)
104/** FLASH_SR_BSY Busy **/
105#define FLASH_SR_BSY (1 << 16)
106/** FLASH_SR_OPTVERR Option and Engineering bits loading validity error **/
107#define FLASH_SR_OPTVERR (1 << 15)
108/** FLASH_SR_RDERR PCROP read error **/
109#define FLASH_SR_RDERR (1 << 14)
110/** FLASH_SR_FASTERR Fast programming error **/
111#define FLASH_SR_FASTERR (1 << 9)
112/** FLASH_SR_MISERR Fast programming data miss error **/
113#define FLASH_SR_MISERR (1 << 8)
114/** FLASH_SR_PGSERR Programming sequence error **/
115#define FLASH_SR_PGSERR (1 << 7)
116/** FLASH_SR_SIZERR Size error **/
117#define FLASH_SR_SIZERR (1 << 6)
118/** FLASH_SR_PGAERR Programming alignment error **/
119#define FLASH_SR_PGAERR (1 << 5)
120/** FLASH_SR_WRPERR Write protected error **/
121#define FLASH_SR_WRPERR (1 << 4)
122/** FLASH_SR_PROGERR Programming error **/
123#define FLASH_SR_PROGERR (1 << 3)
124/** FLASH_SR_OPERR Operation error **/
125#define FLASH_SR_OPERR (1 << 1)
126/** FLASH_SR_EOP End of operation **/
127#define FLASH_SR_EOP (1 << 0)
128
129/**@}*/
130
131/** @defgroup flash_cr CR Flash control register
132@{*/
133
134/** FLASH_CR_LOCK FLASH_CR Lock **/
135#define FLASH_CR_LOCK (1 << 31)
136/** FLASH_CR_OPTLOCK Options Lock **/
137#define FLASH_CR_OPTLOCK (1 << 30)
138/** FLASH_CR_SEC_PROT Securable memory area protection enable **/
139#define FLASH_CR_SEC_PROT (1 << 28)
140/** FLASH_CR_OBL_LAUNCH Force the option byte loading **/
141#define FLASH_CR_OBL_LAUNCH (1 << 27)
142/** FLASH_CR_RDERRIE PCROP read error interrupt enable **/
143#define FLASH_CR_RDERRIE (1 << 26)
144/** FLASH_CR_ERRIE Error interrupt enable **/
145#define FLASH_CR_ERRIE (1 << 25)
146/** FLASH_CR_EOPIE End of operation interrupt enable **/
147#define FLASH_CR_EOPIE (1 << 24)
148/** FLASH_CR_FSTPG Fast programming **/
149#define FLASH_CR_FSTPG (1 << 18)
150/** FLASH_CR_OPTSTRT Options modification start **/
151#define FLASH_CR_OPTSTRT (1 << 17)
152/** FLASH_CR_STRT Start **/
153#define FLASH_CR_STRT (1 << 16)
154
155#define FLASH_CR_PNB_SHIFT 3
156#define FLASH_CR_PNB_MASK 0x3f
157
158/** FLASH_CR_MER Mass erase **/
159#define FLASH_CR_MER (1 << 2)
160/** FLASH_CR_PER Page erase **/
161#define FLASH_CR_PER (1 << 1)
162/** FLASH_CR_PG Programming **/
163#define FLASH_CR_PG (1 << 0)
164
165/**@}*/
166
167/** @defgroup flash_eccr ECCR Flash ECC register
168@{*/
169/** FLASH_ECCR_ECCD ECC detection **/
170#define FLASH_ECCR_ECCD (1 << 31)
171/** FLASH_ECCR_ECCC ECC correction **/
172#define FLASH_ECCR_ECCC (1 << 30)
173/** FLASH_ECCR_ECCIE ECC correction interrupt enable **/
174#define FLASH_ECCR_ECCIE (1 << 24)
175/** FLASH_ECCR_SYSF_ECC ECC fail for Corrected ECC Error or Double ECC Error in info block **/
176#define FLASH_ECCR_SYSF_ECC (1 << 20)
177
178#define FLASH_ECCR_ADDR_ECC_SHIFT 0
179#define FLASH_ECCR_ADDR_ECC_MASK 0x3fff
180
181/**@}*/
182
183/** @defgroup flash_optr OPTR Flash option register
184@{*/
185
186/** FLASH_OPTR_IRHEN Internal reset holder enable bit **/
187#define FLASH_OPTR_IRHEN (1 << 29)
188
189#define FLASH_OPTR_NRST_MODE_SHIFT 27
190#define FLASH_OPTR_NRST_MODE_MASK 0x03
191/** @defgroup flash_optr_nrst_mode NRST MODE
192* @brief NRST_MODE
193@{*/
194#define FLASH_OPTR_NRST_MODE_RESET 1
195#define FLASH_OPTR_NRST_MODE_GPIO 2
196#define FLASH_OPTR_NRST_MODE_BIDIR 3
197/**@}*/
198
199/** FLASH_OPTR_nBOOT0 nBOOT0 option bit **/
200#define FLASH_OPTR_nBOOT0 (1 << 26)
201/** FLASH_OPTR_nBOOT1 Boot configuration **/
202#define FLASH_OPTR_nBOOT1 (1 << 25)
203/** FLASH_OPTR_nBOOT_SEL nBOOT_SEL **/
204#define FLASH_OPTR_nBOOT_SEL (1 << 24)
205/** FLASH_OPTR_RAM_PARITY_CHECK SRAM parity check control **/
206#define FLASH_OPTR_RAM_PARITY_CHECK (1 << 22)
207/** FLASH_OPTR_WWDG_SW Window watchdog selection **/
208#define FLASH_OPTR_WWDG_SW (1 << 19)
209/** FLASH_OPTR_IWDG_STDBY Independent watchdog counter freeze in Standby mode **/
210#define FLASH_OPTR_IWDG_STDBY (1 << 18)
211/** FLASH_OPTR_IWDG_STOP Independent watchdog counter freeze in Stop mode **/
212#define FLASH_OPTR_IWDG_STOP (1 << 17)
213/** FLASH_OPTR_IDWG_SW Independent watchdog selection **/
214#define FLASH_OPTR_IDWG_SW (1 << 16)
215/** FLASH_OPTR_nRSTS_HDW nRSTS_HDW **/
216#define FLASH_OPTR_nRSTS_HDW (1 << 15)
217/** FLASH_OPTR_nRST_STDBY nRST_STDBY **/
218#define FLASH_OPTR_nRST_STDBY (1 << 14)
219/** FLASH_OPTR_nRST_STOP nRST_STOP **/
220#define FLASH_OPTR_nRST_STOP (1 << 13)
221
222#define FLASH_OPTR_BORR_LEV_SHIFT 11
223#define FLASH_OPTR_BORR_LEV_MASK 0x03
224/** @defgroup flash_optr_borr_lev BORR LEV
225* @brief These bits contain the VDD supply level threshold that releases the reset.
226@{*/
227#define FLASH_OPTR_BORR_LEV_2V1 0
228#define FLASH_OPTR_BORR_LEV_2V3 1
229#define FLASH_OPTR_BORR_LEV_2V6 2
230#define FLASH_OPTR_BORR_LEV_2V9 3
231/**@}*/
232
233#define FLASH_OPTR_BORF_LEV_SHIFT 9
234#define FLASH_OPTR_BORF_LEV_MASK 0x03
235/** @defgroup flash_optr_borf_lev BOR FLEV
236* @brief These bits contain the VDD supply level threshold that activates the reset
237@{*/
238#define FLASH_OPTR_BORF_LEV_2V0 0
239#define FLASH_OPTR_BORF_LEV_2V2 1
240#define FLASH_OPTR_BORF_LEV_2V5 2
241#define FLASH_OPTR_BORF_LEV_2V8 3
242/**@}*/
243
244/** FLASH_OPTR_BOREN BOR reset Level **/
245#define FLASH_OPTR_BOREN (1 << 8)
246
247#define FLASH_OPTR_RDP_SHIFT 0
248#define FLASH_OPTR_RDP_MASK 0xff
249/** @defgroup flash_optr_rdp RDP
250* @brief Read protection level
251@{*/
252#define FLASH_OPTR_RDP_LEVEL_0 0xAA
253#define FLASH_OPTR_RDP_LEVEL_1 0xBB /* or any other value. */
254#define FLASH_OPTR_RDP_LEVEL_2 0xCC
255/**@}*/
256
257/**@}*/
258
260
261void flash_clear_progerr_flag(void);
262void flash_clear_pgserr_flag(void);
263void flash_clear_size_flag(void);
264void flash_clear_pgaerr_flag(void);
265void flash_clear_wrperr_flag(void);
266void flash_clear_operr_flag(void);
267void flash_clear_eop_flag(void);
268
269void flash_clear_status_flags(void);
270
272
273void flash_program_double_word(uint32_t address, uint64_t data);
274void flash_program(uint32_t address, uint8_t *data, uint32_t len);
275
276void flash_erase_page(uint32_t page);
277void flash_erase_all_pages(void);
278
279/** Enable instruction cache */
280void flash_icache_enable(void);
281/** Disable instruction cache */
282void flash_icache_disable(void);
283/** Reset instruction cache */
284void flash_icache_reset(void);
285
286/** Unlock program memory */
287void flash_unlock_progmem(void);
288/** lock program memory */
289void flash_lock_progmem(void);
290
291/** Lock Option Byte Access */
292void flash_lock_option_bytes(void);
293
295
296#endif
297/**@}*/
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
void flash_unlock_progmem(void)
Unlock program memory.
Definition: flash.c:187
void flash_icache_disable(void)
Disable instruction cache.
Definition: flash.c:177
void flash_erase_page(uint32_t page)
Erase a page of FLASH.
Definition: flash.c:82
void flash_lock_option_bytes(void)
Lock Option Byte Access.
Definition: flash.c:198
void flash_lock_progmem(void)
lock program memory
Definition: flash.c:193
void flash_wait_for_last_operation(void)
Wait until Last Flash Operation has Ended.
Definition: flash.c:34
void flash_clear_pgaerr_flag(void)
Clear the Programming Alignment Error Flag.
Definition: flash.c:134
void flash_clear_pgserr_flag(void)
Clear the Programming Sequence Error Flag.
Definition: flash.c:115
void flash_icache_reset(void)
Reset instruction cache.
Definition: flash.c:182
void flash_clear_eop_flag(void)
Clear the End of Operation Flag.
Definition: flash.c:121
void flash_clear_status_flags(void)
Clear All Status Flags.
Definition: flash.c:161
void flash_program(uint32_t address, uint8_t *data, uint32_t len)
Program a Data Block to FLASH.
Definition: flash.c:72
void flash_icache_enable(void)
Enable instruction cache.
Definition: flash.c:172
void flash_clear_size_flag(void)
Clear programming size error flag.
Definition: flash.c:127
void flash_program_double_word(uint32_t address, uint64_t data)
Program a 64bits word to FLASH.
Definition: flash.c:48
void flash_clear_progerr_flag(void)
Clear the Programming Error Status Flag.
Definition: flash.c:148
void flash_clear_operr_flag(void)
Clear the Operation Error Status Flag.
Definition: flash.c:155
void flash_clear_wrperr_flag(void)
Clear the Write Protected Error Flag.
Definition: flash.c:141
void flash_erase_all_pages(void)
Erase All FLASH This performs all operations necessary to erase all sectors in the FLASH memory.
Definition: flash.c:103