libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
flash_common_f24.c
Go to the documentation of this file.
1/** @addtogroup flash_file
2 *
3 */
4
5/*
6 * This file is part of the libopencm3 project.
7 *
8 * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
9 * Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
10 *
11 * This library is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25/**@{*/
26
28
29/*---------------------------------------------------------------------------*/
30/** @brief Set the Program Parallelism Size
31
32Set the programming word width. Note carefully the power supply voltage
33restrictions under which the different word sizes may be used. See the
34programming manual for more information.
35@param[in] psize The programming word width one of: @ref flash_cr_program_width
36*/
37
38static inline void flash_set_program_size(uint32_t psize)
39{
42}
43
44/*---------------------------------------------------------------------------*/
45/** @brief Clear the Programming Alignment Error Flag
46
47*/
48
50{
52}
53
54/** Clear programming parallelism error flag
55 */
57{
59}
60
61/*---------------------------------------------------------------------------*/
62/** @brief Clear the Write Protect Error Flag
63
64*/
65
67{
69}
70
71/*---------------------------------------------------------------------------*/
72/** @brief Lock the Option Byte Access
73
74This disables write access to the option bytes. It is locked by default on
75reset.
76*/
77
79{
81}
82
83/*---------------------------------------------------------------------------*/
84/** @brief Program a 64 bit Word to FLASH
85
86This performs all operations necessary to program a 64 bit word to FLASH memory.
87The program error flag should be checked separately for the event that memory
88was not properly erased.
89
90@param[in] address Starting address in Flash.
91@param[in] data Double word to write
92*/
93
94void flash_program_double_word(uint32_t address, uint64_t data)
95{
96 /* Ensure that all flash operations are complete. */
99
100 /* Enable writes to flash. */
102
103 /* Program the double_word. */
104 MMIO64(address) = data;
105
106 /* Wait for the write to complete. */
108
109 /* Disable writes to flash. */
110 FLASH_CR &= ~FLASH_CR_PG;
111}
112
113/*---------------------------------------------------------------------------*/
114/** @brief Program a 32 bit Word to FLASH
115
116This performs all operations necessary to program a 32 bit word to FLASH memory.
117The program error flag should be checked separately for the event that memory
118was not properly erased.
119
120@param[in] address Starting address in Flash.
121@param[in] data word to write
122*/
123
124void flash_program_word(uint32_t address, uint32_t data)
125{
126 /* Ensure that all flash operations are complete. */
129
130 /* Enable writes to flash. */
132
133 /* Program the word. */
134 MMIO32(address) = data;
135
136 /* Wait for the write to complete. */
138
139 /* Disable writes to flash. */
140 FLASH_CR &= ~FLASH_CR_PG;
141}
142
143/*---------------------------------------------------------------------------*/
144/** @brief Program a Half Word to FLASH
145
146This performs all operations necessary to program a 16 bit word to FLASH memory.
147The program error flag should be checked separately for the event that memory
148was not properly erased.
149
150@param[in] address Starting address in Flash.
151@param[in] data half word to write
152*/
153
154void flash_program_half_word(uint32_t address, uint16_t data)
155{
158
160
161 MMIO16(address) = data;
162
164
165 FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */
166}
167
168/*---------------------------------------------------------------------------*/
169/** @brief Program an 8 bit Byte to FLASH
170
171This performs all operations necessary to program an 8 bit byte to FLASH memory.
172The program error flag should be checked separately for the event that memory
173was not properly erased.
174
175@param[in] address Starting address in Flash.
176@param[in] data byte to write
177*/
178
179void flash_program_byte(uint32_t address, uint8_t data)
180{
183
185
186 MMIO8(address) = data;
187
189
190 FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */
191}
192
193/*---------------------------------------------------------------------------*/
194/** @brief Program a Data Block to FLASH
195
196This programs an arbitrary length data block to FLASH memory.
197The program error flag should be checked separately for the event that memory
198was not properly erased.
199
200@param[in] address Starting address in Flash.
201@param[in] data Pointer to start of data block.
202@param[in] len Length of data block.
203*/
204
205void flash_program(uint32_t address, const uint8_t *data, uint32_t len)
206{
207 /* TODO: Use dword and word size program operations where possible for
208 * turbo speed.
209 */
210 uint32_t i;
211 for (i = 0; i < len; i++) {
212 flash_program_byte(address+i, data[i]);
213 }
214}
215
216/*---------------------------------------------------------------------------*/
217/** @brief Erase a Sector of FLASH
218
219This performs all operations necessary to erase a sector in FLASH memory.
220The page should be checked to ensure that it was properly erased. A sector must
221first be fully erased before attempting to program it.
222
223See the reference manual or the FLASH programming manual for details.
224
225@param[in] sector (0 - 11 for some parts, 0-23 on others)
226@param program_size: 0 (8-bit), 1 (16-bit), 2 (32-bit), 3 (64-bit)
227*/
228
229void flash_erase_sector(uint8_t sector, uint32_t program_size)
230{
232 flash_set_program_size(program_size);
233
234 /* Sector numbering is not contiguous internally! */
235 if (sector >= 12) {
236 sector += 4;
237 }
238
243
245 FLASH_CR &= ~FLASH_CR_SER;
247}
248
249/*---------------------------------------------------------------------------*/
250/** @brief Erase All FLASH
251
252This performs all operations necessary to erase all sectors in the FLASH
253memory.
254
255@param program_size: 0 (8-bit), 1 (16-bit), 2 (32-bit), 3 (64-bit)
256*/
257
258void flash_erase_all_sectors(uint32_t program_size)
259{
261 flash_set_program_size(program_size);
262
263 FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */
264 FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */
265
267 FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
268}
269
270/*---------------------------------------------------------------------------*/
271/** @brief Program the Option Bytes
272
273This performs all operations necessary to program the option bytes.
274The option bytes do not need to be erased first.
275
276@param[in] data value to be programmed.
277*/
278
279void flash_program_option_bytes(uint32_t data)
280{
282
285 }
286
287 FLASH_OPTCR = data & ~0x3;
288 FLASH_OPTCR |= FLASH_OPTCR_OPTSTRT; /* Enable option byte prog. */
290}
291/**@}*/
#define MMIO8(addr)
Definition: common.h:67
#define MMIO32(addr)
Definition: common.h:69
#define MMIO16(addr)
Definition: common.h:68
#define MMIO64(addr)
Definition: common.h:70
#define FLASH_CR_PROGRAM_X64
#define FLASH_CR_PROGRAM_X16
#define FLASH_CR_PROGRAM_X32
#define FLASH_CR_PROGRAM_X8
#define FLASH_OPTCR_OPTSTRT
#define FLASH_CR_PROGRAM_MASK
#define FLASH_CR_PROGRAM_SHIFT
#define FLASH_CR_SNB_MASK
#define FLASH_CR_PG
#define FLASH_CR_MER
#define FLASH_OPTCR_OPTLOCK
#define FLASH_SR_PGPERR
#define FLASH_SR_WRPERR
#define FLASH_SR_PGAERR
#define FLASH_CR_SER
#define FLASH_CR_SNB_SHIFT
#define FLASH_CR_STRT
void flash_clear_pgperr_flag(void)
Clear programming parallelism error flag.
void flash_program_option_bytes(uint32_t data)
Program the Option Bytes.
void flash_lock_option_bytes(void)
Lock the Option Byte Access.
void flash_unlock_option_bytes(void)
Unlock the Option Byte Access.
void flash_erase_all_sectors(uint32_t program_size)
Erase All FLASH.
void flash_wait_for_last_operation(void)
Wait until Last Operation has Ended.
Definition: flash.c:52
void flash_program_word(uint32_t address, uint32_t data)
Program a 32 bit Word to FLASH.
void flash_clear_pgaerr_flag(void)
Clear the Programming Alignment Error Flag.
void flash_program_double_word(uint32_t address, uint64_t data)
Program a 64 bit Word to FLASH.
static void flash_set_program_size(uint32_t psize)
Set the Program Parallelism Size.
void flash_erase_sector(uint8_t sector, uint32_t program_size)
Erase a Sector of FLASH.
void flash_clear_wrperr_flag(void)
Clear the Write Protect Error Flag.
void flash_program(uint32_t address, const uint8_t *data, uint32_t len)
Program a Data Block to FLASH.
void flash_program_byte(uint32_t address, uint8_t data)
Program an 8 bit Byte to FLASH.
void flash_program_half_word(uint32_t address, uint16_t data)
Program a Half Word to FLASH.
#define FLASH_SR
Flash Status register.
#define FLASH_CR
Flash Control register.
#define FLASH_OPTCR
Flash Option Control register.