libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
flash.c
Go to the documentation of this file.
1/** @defgroup flash_file FLASH peripheral API
2 *
3 * @ingroup peripheral_apis
4 *
5 * @brief <b>libopencm3 STM32F1xx FLASH Memory</b>
6 *
7 * @version 1.0.0
8 *
9 * @author @htmlonly &copy; @endhtmlonly 2010
10 * Thomas Otto <tommi@viadmin.org>
11 * @author @htmlonly &copy; @endhtmlonly 2010
12 * Mark Butler <mbutler@physics.otago.ac.nz>
13 *
14 * @date 14 January 2014
15 *
16 * For the STM32F1xx, accessing FLASH memory is described briefly in
17 * section 3.3.3 of the STM32F10x Reference Manual.
18 * For detailed programming information see:
19 * PM0075 programming manual: STM32F10xxx Flash programming
20 * August 2010, Doc ID 17863 Rev 1
21 * https://github.com/libopencm3/libopencm3-archive/blob/master/st_micro/CD00283419.pdf
22 *
23 * FLASH memory may be used for data storage as well as code, and may be
24 * programmatically modified. Note that for firmware upload the STM32F1xx
25 * provides a built-in bootloader in system memory that can be entered from a
26 * running program.
27 *
28 * FLASH must first be unlocked before programming. In this module a write to
29 * FLASH is a blocking operation until the end-of-operation flag is asserted.
30 *
31 * @note: don't forget to lock it again when all operations are complete.
32 *
33 * For the large memory XL series, with two banks of FLASH, the upper bank is
34 * accessed with a second set of registers. In principle both banks can be
35 * written simultaneously, or one read while the other is written. This module
36 * does not support the simultaneous write feature.
37 *
38 * LGPL License Terms @ref lgpl_license
39 */
40/*
41 * This file is part of the libopencm3 project.
42 *
43 * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
44 * Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
45 *
46 * This library is free software: you can redistribute it and/or modify
47 * it under the terms of the GNU Lesser General Public License as published by
48 * the Free Software Foundation, either version 3 of the License, or
49 * (at your option) any later version.
50 *
51 * This library is distributed in the hope that it will be useful,
52 * but WITHOUT ANY WARRANTY; without even the implied warranty of
53 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54 * GNU Lesser General Public License for more details.
55 *
56 * You should have received a copy of the GNU Lesser General Public License
57 * along with this library. If not, see <http://www.gnu.org/licenses/>.
58 */
59
60/**@{*/
61
64
65/*---------------------------------------------------------------------------*/
66/** @brief Enable the FLASH Half Cycle Mode
67
68This mode is used for power saving during read access. It is disabled by default
69on reset.
70
71Note carefully the clock restrictions under which the half cycle mode may be
72enabled or disabled. This mode may only be used while the clock is running at
738MHz. See the reference manual for details.
74*/
75
77{
79}
80
81/*---------------------------------------------------------------------------*/
82/** @brief Disable the FLASH Half Cycle Mode
83
84*/
85
87{
88 FLASH_ACR &= ~FLASH_ACR_HLFCYA;
89}
90
91/*---------------------------------------------------------------------------*/
92/** @brief Unlock the Flash Program and Erase Controller, upper Bank
93
94This enables write access to the upper bank of the Flash memory in XL devices.
95It is locked by default on reset.
96*/
97
99{
100 if (desig_get_flash_size() > 512) {
101
102 /* Clear the unlock state. */
104
105 /* Authorize the FPEC access. */
108 }
109}
110
111/*---------------------------------------------------------------------------*/
112/** @brief Lock the Flash Program and Erase Controller, upper Bank
113
114Used to prevent spurious writes to FLASH.
115*/
116
118{
120}
121
122/*---------------------------------------------------------------------------*/
123/** @brief Clear the Programming Error Status Flag, upper Bank
124
125*/
126
128{
129 if (desig_get_flash_size() > 512) {
131 }
132}
133
134/*---------------------------------------------------------------------------*/
135/** @brief Clear the End of Operation Status Flag, upper Bank
136
137*/
138
140{
141 if (desig_get_flash_size() > 512) {
143 }
144}
145
146/*---------------------------------------------------------------------------*/
147/** @brief Clear the Write Protect Error Status Flag, upper Bank
148
149*/
150
152{
153 if (desig_get_flash_size() > 512) {
155 }
156}
157
158/*---------------------------------------------------------------------------*/
159/** @brief Clear All Status Flags
160
161Program error, end of operation, write protect error, busy. Both banks cleared.
162*/
163
165{
169 if (desig_get_flash_size() > 512) {
173 }
174}
175
176/*---------------------------------------------------------------------------*/
177/** @brief Read All Status Flags
178
179The programming error, end of operation, write protect error and busy flags
180are returned in the order of appearance in the status register.
181
182Flags for the upper bank, where appropriate, are combined with those for
183the lower bank using bitwise OR, without distinction.
184
185@returns uint32_t. bit 0: busy, bit 2: programming error, bit 4: write protect
186error, bit 5: end of operation.
187*/
188
190{
191 uint32_t flags = (FLASH_SR & (FLASH_SR_PGERR |
194 FLASH_SR_BSY));
195 if (desig_get_flash_size() > 512) {
196 flags |= (FLASH_SR2 & (FLASH_SR_PGERR |
199 FLASH_SR_BSY));
200 }
201
202 return flags;
203}
204
205/*---------------------------------------------------------------------------*/
206/** @brief Program a Half Word to FLASH
207
208This performs all operations necessary to program a 16 bit word to FLASH memory.
209The program error flag should be checked separately for the event that memory
210was not properly erased.
211
212Status bit polling is used to detect end of operation.
213
214@param[in] address Full address of flash half word to be programmed.
215@param[in] data half word to write
216*/
217
218void flash_program_half_word(uint32_t address, uint16_t data)
219{
221
222 if ((desig_get_flash_size() > 512) && (address >= FLASH_BASE+0x00080000)) {
224 } else {
226 }
227
228 MMIO16(address) = data;
229
231
232 if ((desig_get_flash_size() > 512) && (address >= FLASH_BASE+0x00080000)) {
233 FLASH_CR2 &= ~FLASH_CR_PG;
234 } else {
235 FLASH_CR &= ~FLASH_CR_PG;
236 }
237}
238
239/*---------------------------------------------------------------------------*/
240/** @brief Erase a Page of FLASH
241
242This performs all operations necessary to erase a page in FLASH memory.
243The page should be checked to ensure that it was properly erased. A page must
244first be fully erased before attempting to program it.
245
246Note that the page sizes differ between devices. See the reference manual or
247the FLASH programming manual for details.
248
249@param[in] page_address Full address of flash page to be erased.
250*/
251
252void flash_erase_page(uint32_t page_address)
253{
255
256 if ((desig_get_flash_size() > 512)
257 && (page_address >= FLASH_BASE+0x00080000)) {
259 FLASH_AR2 = page_address;
261 } else {
263 FLASH_AR = page_address;
265 }
266
268
269 if ((desig_get_flash_size() > 512)
270 && (page_address >= FLASH_BASE+0x00080000)) {
271 FLASH_CR2 &= ~FLASH_CR_PER;
272 } else {
273 FLASH_CR &= ~FLASH_CR_PER;
274 }
275}
276
277/*---------------------------------------------------------------------------*/
278/** @brief Erase All FLASH
279
280This performs all operations necessary to erase all user pages in the FLASH
281memory. The information block is unaffected.
282*/
283
285{
287
288 FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */
289 FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */
290
292 FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
293
294/* Repeat for bank 2 */
297
299 FLASH_CR2 &= ~FLASH_CR_MER;
300}
301
302/**@}*/
303
#define MMIO16(addr)
Definition: common.h:68
uint16_t desig_get_flash_size(void)
Read the on board flash size.
#define FLASH_AR
#define FLASH_ACR
#define FLASH_CR2
#define FLASH_SR
#define FLASH_SR_WRPRTERR
Definition: f1/flash.h:83
#define FLASH_CR_PG
#define FLASH_CR_MER
#define FLASH_SR_BSY
Definition: f1/flash.h:85
#define FLASH_SR_PGERR
Definition: f1/flash.h:84
#define FLASH_ACR_HLFCYA
Definition: f1/flash.h:78
void flash_clear_pgerr_flag(void)
Unlock the Flash Program and Erase Controller.
#define FLASH_CR
void flash_clear_wrprterr_flag(void)
Clear the Write Protect Error Status Flag.
#define FLASH_CR_LOCK
#define FLASH_KEYR_KEY2
#define FLASH_CR_PER
#define FLASH_SR_EOP
Definition: f1/flash.h:82
#define FLASH_SR2
#define FLASH_KEYR2
#define FLASH_AR2
#define FLASH_KEYR_KEY1
#define FLASH_CR_STRT
void flash_clear_wrprterr_flag_upper(void)
Clear the Write Protect Error Status Flag, upper Bank.
Definition: flash.c:151
void flash_lock_upper(void)
Lock the Flash Program and Erase Controller, upper Bank.
Definition: flash.c:117
void flash_erase_page(uint32_t page_address)
Erase a Page of FLASH.
Definition: flash.c:252
uint32_t flash_get_status_flags(void)
Read All Status Flags.
Definition: flash.c:189
void flash_wait_for_last_operation(void)
Wait until Last Operation has Ended.
void flash_clear_pgerr_flag_upper(void)
Clear the Programming Error Status Flag, upper Bank.
Definition: flash.c:127
void flash_clear_eop_flag(void)
Clear the End of OPeration flag.
void flash_halfcycle_disable(void)
Disable the FLASH Half Cycle Mode.
Definition: flash.c:86
void flash_clear_status_flags(void)
Clear All Status Flags.
Definition: flash.c:164
void flash_unlock_upper(void)
Unlock the Flash Program and Erase Controller, upper Bank.
Definition: flash.c:98
void flash_halfcycle_enable(void)
Enable the FLASH Half Cycle Mode.
Definition: flash.c:76
void flash_erase_all_pages(void)
Erase All FLASH.
Definition: flash.c:284
void flash_clear_eop_flag_upper(void)
Clear the End of Operation Status Flag, upper Bank.
Definition: flash.c:139
void flash_program_half_word(uint32_t address, uint16_t data)
Program a Half Word to FLASH.
Definition: flash.c:218
#define FLASH_BASE