libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
g4/flash.h
Go to the documentation of this file.
1/** @defgroup flash_defines FLASH Defines
2 *
3 * @ingroup STM32G4xx_defines
4 *
5 * @brief <b>Defined Constants and Types for the STM32G4xx Flash Control</b>
6 *
7 * @version 1.0.0
8 *
9 * @author @htmlonly &copy; @endhtmlonly 2016 Benjamin Levine <benjamin@jesco.karoo.co.uk>
10 * @author @htmlonly &copy; @endhtmlonly 2020 Ben Brewer <ben.brewer@codethink.co.uk>
11 *
12 * @date 30 July 2020
13 *
14 * LGPL License Terms @ref lgpl_license
15 * */
16/*
17 * This file is part of the libopencm3 project.
18 *
19 * Copyright (C) 2016 Benjamin Levine <benjamin@jesco.karoo.co.uk>
20 * Copyright (C) 2020 Ben Brewer <ben.brewer@codethink.co.uk>
21 *
22 * This library is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU Lesser General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26 *
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public License
33 * along with this library. If not, see <http://www.gnu.org/licenses/>.
34 */
35
36/*
37 * For details see:
38 * RM0440 Reference manual: STM32G4 Series advanced ARMĀ®-based 32-bit MCUs
39 * 15 April 2020, Doc ID 00355726 Rev 4
40 */
41
42/**@{*/
43#ifndef LIBOPENCM3_FLASH_H
44#define LIBOPENCM3_FLASH_H
45
49
50/* --- FLASH registers ----------------------------------------------------- */
51
52#define FLASH_ACR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x00)
53#define FLASH_PDKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x04)
54#define FLASH_KEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x08)
55#define FLASH_OPTKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x0C)
56#define FLASH_SR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x10)
57#define FLASH_CR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14)
58#define FLASH_ECCR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x18)
59#define FLASH_OPTR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x20)
60#define FLASH_PCROP1SR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x24)
61#define FLASH_PCROP1ER MMIO32(FLASH_MEM_INTERFACE_BASE + 0x28)
62#define FLASH_WRP1AR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x2C)
63#define FLASH_WRP1BR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x30)
64#define FLASH_PCROP2SR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x44)
65#define FLASH_PCROP2ER MMIO32(FLASH_MEM_INTERFACE_BASE + 0x48)
66#define FLASH_WRP2AR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x4C)
67#define FLASH_WRP2BR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x50)
68#define FLASH_SEC1R MMIO32(FLASH_MEM_INTERFACE_BASE + 0x70)
69#define FLASH_SEC2R MMIO32(FLASH_MEM_INTERFACE_BASE + 0x74)
70
71/* --- FLASH_ACR values ---------------------------------------------------- */
72
73#define FLASH_ACR_DBG_SWEN (1 << 18)
74#define FLASH_ACR_SLEEP_PD (1 << 14)
75#define FLASH_ACR_RUN_PD (1 << 13)
76#define FLASH_ACR_PRFTEN (1 << 8)
77
78#define FLASH_ACR_LATENCY_SHIFT 0
79#define FLASH_ACR_LATENCY_MASK 0xf
80
81/* --- FLASH_SR values ----------------------------------------------------- */
82
83#define FLASH_SR_BSY (1 << 16)
84#define FLASH_SR_OPTVERR (1 << 15)
85#define FLASH_SR_RDERR (1 << 14)
86#define FLASH_SR_FASTERR (1 << 9)
87#define FLASH_SR_MISERR (1 << 8)
88#define FLASH_SR_PGSERR (1 << 7)
89#define FLASH_SR_SIZERR (1 << 6)
90#define FLASH_SR_PGAERR (1 << 5)
91#define FLASH_SR_WRPERR (1 << 4)
92#define FLASH_SR_PROGERR (1 << 3)
93#define FLASH_SR_OPERR (1 << 1)
94#define FLASH_SR_EOP (1 << 0)
95
96/* --- FLASH_CR values ----------------------------------------------------- */
97
98#define FLASH_CR_LOCK (1 << 31)
99#define FLASH_CR_OPTLOCK (1 << 30)
100#define FLASH_CR_SEC_PROT2 (1 << 29)
101#define FLASH_CR_SEC_PROT1 (1 << 28)
102#define FLASH_CR_OBL_LAUNCH (1 << 27)
103#define FLASH_CR_RDERRIE (1 << 26)
104#define FLASH_CR_ERRIE (1 << 25)
105#define FLASH_CR_EOPIE (1 << 24)
106#define FLASH_CR_FSTPG (1 << 18)
107#define FLASH_CR_OPTSTRT (1 << 17)
108#define FLASH_CR_START (1 << 16)
109#define FLASH_CR_MER2 (1 << 15)
110#define FLASH_CR_BKER (1 << 11)
111#define FLASH_CR_MER1 (1 << 2)
112#define FLASH_CR_PER (1 << 1)
113#define FLASH_CR_PG (1 << 0)
114
115#define FLASH_CR_PNB_SHIFT 3
116#define FLASH_CR_PNB_MASK 0x7f
117
118/* --- FLASH_ECCR values -------------------------------------------------- */
119
120#define FLASH_ECCR_ECCD (1 << 31)
121#define FLASH_ECCR_ECCC (1 << 30)
122#define FLASH_ECCR_ECCD2 (1 << 31)
123#define FLASH_ECCR_ECCC2 (1 << 30)
124#define FLASH_ECCR_ECCIE (1 << 24)
125#define FLASH_ECCR_SYSF_ECC (1 << 22)
126#define FLASH_ECCR_BK_ECC (1 << 21)
127
128#define FLASH_ECCR_ADDR_ECC_SHIFT 0
129#define FLASH_ECCR_ADDR_ECC_MASK 0x7ffff
130
131/* --- FLASH_OPTR values -------------------------------------------------- */
132
133#define FLASH_OPTR_IRHEN (1 << 30)
134
135#define FLASH_OPTR_NRST_MODE_SHIFT 28
136#define FLASH_OPTR_NRST_MODE_MASK 0x3
137#define FLASH_OPTR_NRST_MODE_RESET 1
138#define FLASH_OPTR_NRST_MODE_GPIO 2
139#define FLASH_OPTR_NRST_MODE_BIDIR 3
140
141#define FLASH_OPTR_nBOOT0 (1 << 27)
142#define FLASH_OPTR_nSWBOOT0 (1 << 26)
143#define FLASH_OPTR_SRAM_RST (1 << 25)
144#define FLASH_OPTR_SRAM_PE (1 << 24)
145#define FLASH_OPTR_nBOOT1 (1 << 23)
146#define FLASH_OPTR_DUALBANK (1 << 21)
147#define FLASH_OPTR_BFB2 (1 << 20)
148#define FLASH_OPTR_WWDG_SW (1 << 19)
149#define FLASH_OPTR_IWDG_STDBY (1 << 18)
150#define FLASH_OPTR_IWDG_STOP (1 << 17)
151#define FLASH_OPTR_IDWG_SW (1 << 16)
152#define FLASH_OPTR_nRST_SHDW (1 << 14)
153#define FLASH_OPTR_nRST_STDBY (1 << 13)
154#define FLASH_OPTR_nRST_STOP (1 << 12)
155
156#define FLASH_OPTR_BOR_SHIFT 8
157#define FLASH_OPTR_BOR_MASK 0x7
158#define FLASH_OPTR_BOR_LEVEL_0 0
159#define FLASH_OPTR_BOR_LEVEL_1 1
160#define FLASH_OPTR_BOR_LEVEL_2 2
161#define FLASH_OPTR_BOR_LEVEL_3 3
162#define FLASH_OPTR_BOR_LEVEL_4 4
163
164#define FLASH_OPTR_RDP_SHIFT 0
165#define FLASH_OPTR_RDP_MASK 0xff
166#define FLASH_OPTR_RDP_LEVEL_0 0xAA
167#define FLASH_OPTR_RDP_LEVEL_1 0xBB
168#define FLASH_OPTR_RDP_LEVEL_2 0xCC
169
170/* --- FLASH_PCROP1SR values -------------------------------------------------- */
171
172#define FLASH_PCROP1SR_PCROP1_STRT_SHIFT 0
173#define FLASH_PCROP1SR_PCROP1_STRT_MASK 0x7fff
174
175/* --- FLASH_PCROP1ER values -------------------------------------------------- */
176
177#define FLASH_PCROP1ER_PCROP_RDP (1 << 31)
178#define FLASH_PCROP1ER_PCROP1_END_SHIFT 0
179#define FLASH_PCROP1ER_PCROP1_END_MASK 0x7fff
180
181/* --- FLASH_WRP1AR values -------------------------------------------------- */
182
183#define FLASH_WRP1AR_WRP1A_END_SHIFT 16
184#define FLASH_WRP1AR_WRP1A_END_MASK 0x7f
185
186#define FLASH_WRP1AR_WRP1A_STRT_SHIFT 0
187#define FLASH_WRP1AR_WRP1A_STRT_MASK 0x7f
188
189/* --- FLASH_WRP1BR values -------------------------------------------------- */
190
191#define FLASH_WRP1BR_WRP1B_END_SHIFT 16
192#define FLASH_WRP1BR_WRP1B_END_MASK 0x7f
193
194#define FLASH_WRP1BR_WRP1B_STRT_SHIFT 0
195#define FLASH_WRP1BR_WRP1B_STRT_MASK 0x7f
196
197/* --- FLASH_PCROP2SR values -------------------------------------------------- */
198
199#define FLASH_PCROP2SR_PCROP2_STRT_SHIFT 0
200#define FLASH_PCROP2SR_PCROP2_STRT_MASK 0x7fff
201
202/* --- FLASH_PCROP2ER values -------------------------------------------------- */
203
204#define FLASH_PCROP2ER_PCROP2_END_SHIFT 0
205#define FLASH_PCROP2ER_PCROP2_END_MASK 0x7fff
206
207/* --- FLASH_WRP2AR values -------------------------------------------------- */
208
209#define FLASH_WRP2AR_WRP2A_END_SHIFT 16
210#define FLASH_WRP2AR_WRP2A_END_MASK 0x7f
211
212#define FLASH_WRP2AR_WRP2A_STRT_SHIFT 0
213#define FLASH_WRP2AR_WRP2A_STRT_MASK 0x7f
214
215/* --- FLASH_WRP2BR values -------------------------------------------------- */
216
217#define FLASH_WRP2BR_WRP2B_END_SHIFT 16
218#define FLASH_WRP2BR_WRP2B_END_MASK 0xff
219
220#define FLASH_WRP2BR_WRP2B_STRT_SHIFT 0
221#define FLASH_WRP2BR_WRP2B_STRT_MASK 0xff
222
223/* --- FLASH_SEC1R values -------------------------------------------------- */
224
225#define FLASH_SEC1R_BOOT_LOCK (1 << 16)
226
227#define FLASH_SEC1R_SEC_SIZE1_SHIFT 0
228#define FLASH_SEC1R_SEC_SIZE1_MASK 0xff
229
230/* --- FLASH_SEC2R values -------------------------------------------------- */
231
232#define FLASH_SEC2R_SEC_SIZE2_SHIFT 0
233#define FLASH_SEC2R_SEC_SIZE2_MASK 0xff
234
235/* --- FLASH Keys -----------------------------------------------------------*/
236
237#define FLASH_PDKEYR_PDKEY1 ((uint32_t)0x04152637)
238#define FLASH_PDKEYR_PDKEY2 ((uint32_t)0xfafbfcfd)
239
240#define FLASH_KEYR_KEY1 ((uint32_t)0x45670123)
241#define FLASH_KEYR_KEY2 ((uint32_t)0xcdef89ab)
242
243#define FLASH_OPTKEYR_KEY1 ((uint32_t)0x08192a3b)
244#define FLASH_OPTKEYR_KEY2 ((uint32_t)0x4c5d6e7f)
245
246/* --- Function prototypes ------------------------------------------------- */
247
249
250void flash_clear_progerr_flag(void);
251void flash_clear_pgserr_flag(void);
252void flash_clear_size_flag(void);
253void flash_clear_pgaerr_flag(void);
254void flash_clear_wrperr_flag(void);
255void flash_lock_option_bytes(void);
256void flash_program_double_word(uint32_t address, uint64_t data);
257void flash_program(uint32_t address, uint8_t *data, uint32_t len);
258void flash_erase_page(uint32_t page);
259void flash_erase_all_pages(void);
260void flash_program_option_bytes(uint32_t data);
261
263
264#endif
265/**@}*/
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
void flash_program_option_bytes(uint32_t data)
Program the Option Bytes This performs all operations necessary to program the option bytes.
Definition: flash.c:196
void flash_erase_page(uint32_t page)
Erase a page of FLASH.
Definition: flash.c:159
void flash_lock_option_bytes(void)
Lock the Option Byte Access This disables write access to the option bytes.
Definition: flash.c:108
void flash_clear_pgaerr_flag(void)
Clear the Programming Alignment Error Flag.
Definition: flash.c:72
void flash_clear_pgserr_flag(void)
Clear the Programming Sequence Error Flag This flag is set when incorrect programming configuration h...
Definition: flash.c:59
void flash_program(uint32_t address, uint8_t *data, uint32_t len)
Program a Data Block to FLASH This programs an arbitrary length data block to FLASH memory.
Definition: flash.c:149
void flash_clear_size_flag(void)
Clear programming size error flag.
Definition: flash.c:65
void flash_program_double_word(uint32_t address, uint64_t data)
Program a 64 bit word to FLASH.
Definition: flash.c:122
void flash_clear_progerr_flag(void)
Clear the Programming Error Status Flag.
Definition: flash.c:86
void flash_clear_wrperr_flag(void)
Clear the Write Protect Error Flag.
Definition: flash.c:79
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:180