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