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 STM32G0xx FLASH</b>
6 *
7 * @version 1.0.0
8 *
9 * LGPL License Terms @ref lgpl_license
10 */
11
12/*
13 * This file is part of the libopencm3 project.
14 *
15 * This library is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this library. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29/**@{*/
30
32
33/** @brief Wait until Last Flash Operation has Ended */
35{
36 while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
37}
38
39/** @brief Program a 64bits word to FLASH
40 *
41 * Program a 64bit word to FLASH memory.
42 * Flash programing error must be checked and cleared if needed
43 * before starting programming.
44 *
45 * @param[in] address Address in FLASH
46 * @param[in] data Double word to write
47 */
48void flash_program_double_word(uint32_t address, uint64_t data)
49{
51
53
54 MMIO32(address) = (uint32_t)data;
55 MMIO32(address+4) = (uint32_t)(data >> 32);
56
58
59 FLASH_CR &= ~FLASH_CR_PG;
60}
61
62/** @brief Program a Data Block to FLASH
63 *
64 * This programs an arbitrary length data block to FLASH memory.
65 * The program error flag should be checked separately for the event that memory
66 * was not properly erased.
67 *
68 * @param[in] address Starting address in Flash.
69 * @param[in] data Pointer to start of data block.
70 * @param[in] len Length of data block in bytes (multiple of 8).
71 **/
72void flash_program(uint32_t address, uint8_t *data, uint32_t len)
73{
74 for (uint32_t i = 0; i < len; i += 8) {
75 flash_program_double_word(address+i, *(uint64_t*)(data + i));
76 }
77}
78
79/** @brief Erase a page of FLASH
80 * @param[in] page
81 */
82void flash_erase_page(uint32_t page)
83{
85
86 uint32_t reg = FLASH_CR;
88 reg |= (page & FLASH_CR_PNB_MASK) << FLASH_CR_PNB_SHIFT;
89 reg |= FLASH_CR_PER;
90 reg |= FLASH_CR_STRT;
91
92 FLASH_CR = reg;
93
95
96 FLASH_CR &= ~FLASH_CR_PER;
97}
98
99/** @brief Erase All FLASH
100 * This performs all operations necessary to erase all sectors in the FLASH
101 * memory.
102 */
104{
106
109
111 FLASH_CR &= ~FLASH_CR_MER;
112}
113
114/** @brief Clear the Programming Sequence Error Flag */
116{
118}
119
120/** @brief Clear the End of Operation Flag */
122{
124}
125
126/** @brief Clear programming size error flag */
128{
130}
131
132/** @brief Clear the Programming Alignment Error Flag
133 */
135{
137}
138
139/** @brief Clear the Write Protected Error Flag
140 */
142{
144}
145
146/** @brief Clear the Programming Error Status Flag
147 */
149{
151}
152
153/** @brief Clear the Operation Error Status Flag
154 */
156{
158}
159
160/** @brief Clear All Status Flags */
162{
170}
171
173{
175}
176
178{
179 FLASH_ACR &= ~FLASH_ACR_ICEN;
180}
181
183{
185}
186
188{
191}
192
194{
196}
197
199{
201}
202
203void flash_unlock(void)
204{
207}
208
209void flash_lock(void)
210{
213}
214
215/**@}*/
#define MMIO32(addr)
Definition: common.h:69
#define FLASH_ACR_ICEN
FLASH_ACR_ICEN Instruction cache enable.
Definition: g0/flash.h:63
#define FLASH_ACR_ICRST
FLASH_ACR_ICRST Instruction cache reset.
Definition: g0/flash.h:61
#define FLASH_CR_PNB_SHIFT
Definition: g0/flash.h:155
#define FLASH_CR_OPTLOCK
FLASH_CR_OPTLOCK Options Lock.
Definition: g0/flash.h:137
#define FLASH_CR_PG
FLASH_CR_PG Programming.
Definition: g0/flash.h:163
#define FLASH_CR_MER
FLASH_CR_MER Mass erase.
Definition: g0/flash.h:159
#define FLASH_CR_PNB_MASK
Definition: g0/flash.h:156
#define FLASH_CR_LOCK
FLASH_CR_LOCK FLASH_CR Lock.
Definition: g0/flash.h:135
#define FLASH_CR_PER
FLASH_CR_PER Page erase.
Definition: g0/flash.h:161
#define FLASH_CR_STRT
FLASH_CR_STRT Start.
Definition: g0/flash.h:153
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_unlock(void)
Unlock the Flash Program and Erase Controller This enables write access to the Flash memory.
Definition: flash.c:203
void flash_unlock_option_bytes(void)
Unlock the Option Byte Access.
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_lock(void)
Lock the Flash Program and Erase Controller Used to prevent spurious writes to FLASH.
Definition: flash.c:209
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
#define FLASH_KEYR_KEY2
FLASH_KEYR_KEY2 Flash key 2.
Definition: g0/flash.h:85
#define FLASH_KEYR_KEY1
FLASH_KEYR_KEY1 Flash key 1.
Definition: g0/flash.h:83
#define FLASH_ACR
Definition: g0/flash.h:36
#define FLASH_SR
Definition: g0/flash.h:39
#define FLASH_KEYR
Definition: g0/flash.h:37
#define FLASH_CR
Definition: g0/flash.h:40
#define FLASH_SR_SIZERR
FLASH_SR_SIZERR Size error.
Definition: g0/flash.h:117
#define FLASH_SR_BSY
FLASH_SR_BSY Busy.
Definition: g0/flash.h:105
#define FLASH_SR_OPERR
FLASH_SR_OPERR Operation error.
Definition: g0/flash.h:125
#define FLASH_SR_PGSERR
FLASH_SR_PGSERR Programming sequence error.
Definition: g0/flash.h:115
#define FLASH_SR_PROGERR
FLASH_SR_PROGERR Programming error.
Definition: g0/flash.h:123
#define FLASH_SR_WRPERR
FLASH_SR_WRPERR Write protected error.
Definition: g0/flash.h:121
#define FLASH_SR_PGAERR
FLASH_SR_PGAERR Programming alignment error.
Definition: g0/flash.h:119
#define FLASH_SR_EOP
FLASH_SR_EOP End of operation.
Definition: g0/flash.h:127