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 STM32G4xx FLASH</b>
6 *
7 * @version 1.0.0
8 *
9 * Benjamin Levine <benjamin@jesco.karoo.co.uk>
10 * Ben Brewer <ben.brewer@codethink.co.uk>
11 *
12 * @date 30 July 2020
13 *
14 * This library supports the FLASH memory controller in the STM32G4
15 * series of ARM Cortex Microcontrollers by ST Microelectronics.
16 *
17 * For the STM32G4xx, accessing FLASH memory is described briefly in
18 * section 3 of the STM32G4 Reference Manual.
19 *
20 * LGPL License Terms @ref lgpl_license
21 */
22
23/*
24 * This file is part of the libopencm3 project.
25 *
26 * Copyright (C) 2016 Benjamin Levine <benjamin@jesco.karoo.co.uk>
27 * Copyright (C) 2020 Ben Brewer <ben.brewer@codethink.co.uk>
28 *
29 * This library is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU Lesser General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
33 *
34 * This library is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU Lesser General Public License for more details.
38 *
39 * You should have received a copy of the GNU Lesser General Public License
40 * along with this library. If not, see <http://www.gnu.org/licenses/>.
41 */
42
43/**@{*/
44
46
47/** @brief Wait until Last Operation has Ended
48 * This loops indefinitely until an operation (write or erase) has completed
49 * by testing the busy flag.
50 */
52{
53 while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
54}
55
56/** @brief Clear the Programming Sequence Error Flag
57 * This flag is set when incorrect programming configuration has been made.
58 */
60{
62}
63
64/** Clear programming size error flag */
66{
68}
69
70/** @brief Clear the Programming Alignment Error Flag
71 */
73{
75}
76
77/** @brief Clear the Write Protect Error Flag
78 */
80{
82}
83
84/** @brief Clear the Programming Error Status Flag
85 */
87{
89}
90
91/** @brief Clear All Status Flags
92 * Program error, end of operation, write protect error, busy.
93 */
95{
102}
103
104/** @brief Lock the Option Byte Access
105 * This disables write access to the option bytes. It is locked by default on
106 * reset.
107 */
109{
111}
112
113/** @brief Program a 64 bit word to FLASH
114 *
115 * This performs all operations necessary to program a 64 bit word to FLASH memory.
116 * The program error flag should be checked separately for the event that memory
117 * was not properly erased.
118 *
119 * @param[in] address Starting address in Flash.
120 * @param[in] data Double word to write
121 */
122void flash_program_double_word(uint32_t address, uint64_t data)
123{
124 /* Ensure that all flash operations are complete. */
126
127 /* Enable writes to flash. */
129
130 /* Program the each word separately. */
131 MMIO32(address) = (uint32_t)data;
132 MMIO32(address+4) = (uint32_t)(data >> 32);
133
134 /* Wait for the write to complete. */
136
137 /* Disable writes to flash. */
138 FLASH_CR &= ~FLASH_CR_PG;
139}
140
141/** @brief Program a Data Block to FLASH
142 * This programs an arbitrary length data block to FLASH memory.
143 * The program error flag should be checked separately for the event that
144 * memory was not properly erased.
145 * @param[in] address Starting address in Flash.
146 * @param[in] data Pointer to start of data block.
147 * @param[in] len Length of data block in bytes (multiple of 8).
148 */
149void flash_program(uint32_t address, uint8_t *data, uint32_t len)
150{
151 for (uint32_t i = 0; i < len; i += 8) {
152 flash_program_double_word(address+i, *(uint64_t*)(data + i));
153 }
154}
155
156/** @brief Erase a page of FLASH
157 * @param[in] page (0 - 255 for bank 1, 256-511 for bank 2)
158 */
159void flash_erase_page(uint32_t page)
160{
162
163 /* page and bank are contiguous bits */
165 if (page > 255) {
167 }
168 FLASH_CR |= page << FLASH_CR_PNB_SHIFT;
171
173 FLASH_CR &= ~FLASH_CR_PER;
174}
175
176/** @brief Erase All FLASH
177 * This performs all operations necessary to erase all sectors in the FLASH
178 * memory.
179 */
181{
183
186
188 FLASH_CR &= ~FLASH_CR_MER1 & ~FLASH_CR_MER2;
189}
190
191/** @brief Program the Option Bytes
192 * This performs all operations necessary to program the option bytes.
193 * The option bytes do not need to be erased first.
194 * @param[in] data value to be programmed.
195 */
196void flash_program_option_bytes(uint32_t data)
197{
199
202 }
203
204 FLASH_OPTR = data;
207}
208/**@}*/
209
#define MMIO32(addr)
Definition: common.h:69
#define FLASH_CR_PNB_SHIFT
Definition: g4/flash.h:115
#define FLASH_CR_MER1
Definition: g4/flash.h:111
#define FLASH_CR_OPTLOCK
Definition: g4/flash.h:99
#define FLASH_SR_SIZERR
Definition: g4/flash.h:89
#define FLASH_SR
Definition: g4/flash.h:56
#define FLASH_CR_MER2
Definition: g4/flash.h:109
#define FLASH_CR_PG
Definition: g4/flash.h:113
#define FLASH_SR_BSY
Definition: g4/flash.h:83
#define FLASH_CR_BKER
Definition: g4/flash.h:110
#define FLASH_SR_PGSERR
Definition: g4/flash.h:88
#define FLASH_CR_PNB_MASK
Definition: g4/flash.h:116
#define FLASH_OPTR
Definition: g4/flash.h:59
#define FLASH_CR
Definition: g4/flash.h:57
#define FLASH_SR_PROGERR
Definition: g4/flash.h:92
#define FLASH_CR_START
Definition: g4/flash.h:108
#define FLASH_SR_WRPERR
Definition: g4/flash.h:91
#define FLASH_SR_PGAERR
Definition: g4/flash.h:90
#define FLASH_CR_PER
Definition: g4/flash.h:112
#define FLASH_CR_OPTSTRT
Definition: g4/flash.h:107
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_unlock_option_bytes(void)
Unlock the Option Byte Access.
void flash_wait_for_last_operation(void)
Wait until Last Operation has Ended This loops indefinitely until an operation (write or erase) has c...
Definition: flash.c:51
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_clear_eop_flag(void)
Clear the End of OPeration flag.
void flash_clear_status_flags(void)
Clear All Status Flags Program error, end of operation, write protect error, busy.
Definition: flash.c:94
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