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