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 STM32F0xx FLASH</b>
6 *
7 * @version 1.0.0
8 *
9 * @author @htmlonly &copy; @endhtmlonly 2013
10 * Frantisek Burian <BuFran@seznam.cz>
11 *
12 * @date 14 January 2014
13 *
14 * For the STM32F0xx, accessing FLASH memory is described in
15 * section 3 of the STM32F0xx Reference Manual.
16 *
17 * FLASH memory may be used for data storage as well as code, and may be
18 * programmatically modified. Note that for firmware upload the STM32F1xx
19 * provides a built-in bootloader in system memory that can be entered from a
20 * running program.
21 *
22 * FLASH must first be unlocked before programming. In this module a write to
23 * FLASH is a blocking operation until the end-of-operation flag is asserted.
24 *
25 * @note: don't forget to lock it again when all operations are complete.
26 *
27 * LGPL License Terms @ref lgpl_license
28 */
29
30/*
31 * This file is part of the libopencm3 project.
32 *
33 * Copyright (C) 2013 Frantisek Burian <BuFran@seznam.cz>
34 *
35 * This library is free software: you can redistribute it and/or modify
36 * it under the terms of the GNU Lesser General Public License as published by
37 * the Free Software Foundation, either version 3 of the License, or
38 * (at your option) any later version.
39 *
40 * This library is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU Lesser General Public License for more details.
44 *
45 * You should have received a copy of the GNU Lesser General Public License
46 * along with this library. If not, see <http://www.gnu.org/licenses/>.
47 */
48
49/**@{*/
50
52
53/*---------------------------------------------------------------------------*/
54/** @brief Clear All Status Flags
55
56Program error, end of operation, write protect error, busy.
57*/
58
60{
64}
65
66/*---------------------------------------------------------------------------*/
67/** @brief Read All Status Flags
68
69The programming error, end of operation, write protect error and busy flags
70are returned in the order of appearance in the status register.
71
72@returns uint32_t. bit 0: busy, bit 2: programming error, bit 4: write protect
73error, bit 5: end of operation.
74*/
75
77{
78 return FLASH_SR & (FLASH_SR_PGERR |
82}
83
84/*---------------------------------------------------------------------------*/
85/** @brief Program a Half Word to FLASH
86
87This performs all operations necessary to program a 16 bit word to FLASH memory.
88The program error flag should be checked separately for the event that memory
89was not properly erased.
90
91Status bit polling is used to detect end of operation.
92
93@param[in] address Full address of flash half word to be programmed.
94@param[in] data half word to write
95*/
96
97void flash_program_half_word(uint32_t address, uint16_t data)
98{
100
102
103 MMIO16(address) = data;
104
106
107 FLASH_CR &= ~FLASH_CR_PG;
108}
109
110/*---------------------------------------------------------------------------*/
111/** @brief Erase a Page of FLASH
112
113This performs all operations necessary to erase a page in FLASH memory.
114The page should be checked to ensure that it was properly erased. A page must
115first be fully erased before attempting to program it.
116
117Note that the page sizes differ between devices. See the reference manual or
118the FLASH programming manual for details.
119
120@param[in] page_address Full address of flash page to be erased.
121*/
122
123void flash_erase_page(uint32_t page_address)
124{
126
128 FLASH_AR = page_address;
130
132
133 FLASH_CR &= ~FLASH_CR_PER;
134}
135
136/*---------------------------------------------------------------------------*/
137/** @brief Erase All FLASH
138
139This performs all operations necessary to erase all user pages in the FLASH
140memory. The information block is unaffected.
141*/
142
144{
146
147 FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */
148 FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */
149
151 FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
152
153}
154
155/**@}*/
156
#define MMIO16(addr)
Definition: common.h:68
#define FLASH_AR
#define FLASH_SR
#define FLASH_SR_WRPRTERR
Definition: f0/flash.h:74
#define FLASH_CR_PG
#define FLASH_CR_MER
#define FLASH_SR_BSY
Definition: f0/flash.h:76
#define FLASH_SR_PGERR
Definition: f0/flash.h:75
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_PER
#define FLASH_SR_EOP
Definition: f0/flash.h:73
#define FLASH_CR_STRT
void flash_erase_page(uint32_t page_address)
Erase a Page of FLASH.
Definition: flash.c:123
uint32_t flash_get_status_flags(void)
Read All Status Flags.
Definition: flash.c:76
void flash_wait_for_last_operation(void)
Wait until Last Operation has Ended.
void flash_clear_eop_flag(void)
Clear the End of OPeration flag.
void flash_clear_status_flags(void)
Clear All Status Flags.
Definition: flash.c:59
void flash_erase_all_pages(void)
Erase All FLASH.
Definition: flash.c:143
void flash_program_half_word(uint32_t address, uint16_t data)
Program a Half Word to FLASH.
Definition: flash.c:97