libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
flash_common_f01.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) 2014 Ken Sarkies <ksarkies@internode.on.net>
9 *
10 * This library is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/**@{*/
25
27
28
29/*---------------------------------------------------------------------------*/
30/** @brief Unlock the Flash Program and Erase Controller
31
32This enables write access to the Flash memory. It is locked by default on
33reset.
34*/
35
36
37/*---------------------------------------------------------------------------*/
38/** @brief Clear the Programming Error Status Flag
39
40*/
41
43{
45}
46
47/*---------------------------------------------------------------------------*/
48/** @brief Clear the Write Protect Error Status Flag
49
50*/
51
53{
55}
56
57/*---------------------------------------------------------------------------*/
58/** @brief Wait until Last Operation has Ended
59
60This loops indefinitely until an operation (write or erase) has completed by
61testing the busy flag.
62*/
63
65{
67}
68
69/*---------------------------------------------------------------------------*/
70/** @brief Program a 32 bit Word to FLASH
71
72This performs all operations necessary to program a 32 bit word to FLASH memory.
73The program error flag should be checked separately for the event that memory
74was not properly erased.
75
76Status bit polling is used to detect end of operation.
77
78@param[in] address Full address of flash word to be programmed.
79@param[in] data word to write
80*/
81
82void flash_program_word(uint32_t address, uint32_t data)
83{
84 flash_program_half_word(address, (uint16_t)data);
85 flash_program_half_word(address+2, (uint16_t)(data>>16));
86}
87
88/*---------------------------------------------------------------------------*/
89/** @brief Erase All Option Bytes
90
91This performs all operations necessary to erase the option bytes. These must
92first be fully erased before attempting to program them, therefore it is
93recommended to check these after an erase attempt.
94*/
95
97{
99
100 if ((FLASH_CR & FLASH_CR_OPTWRE) == 0) {
102 }
103
104 FLASH_CR |= FLASH_CR_OPTER; /* Enable option byte erase. */
107 FLASH_CR &= ~FLASH_CR_OPTER; /* Disable option byte erase. */
108}
109
110/*---------------------------------------------------------------------------*/
111/** @brief Program the Option Bytes
112
113This performs all operations necessary to program the option bytes.
114The write protect error flag should be checked separately for the event that
115an option byte had not been properly erased before calling this function.
116
117Only the lower 8 bits of the data is significant.
118
119@param[in] address Address of option byte from @ref flash_options.
120@param[in] data value to write
121*/
122
123void flash_program_option_bytes(uint32_t address, uint16_t data)
124{
126
127 if ((FLASH_CR & FLASH_CR_OPTWRE) == 0) {
129 }
130
131 FLASH_CR |= FLASH_CR_OPTPG; /* Enable option byte programming. */
132 MMIO16(address) = data;
134 FLASH_CR &= ~FLASH_CR_OPTPG; /* Disable option byte programming. */
135}
136
137/**@}*/
138
#define MMIO16(addr)
Definition: common.h:68
#define FLASH_CR_OPTER
#define FLASH_CR_OPTWRE
#define FLASH_SR
#define FLASH_SR_WRPRTERR
Definition: f0/flash.h:74
#define FLASH_SR_BSY
Definition: f0/flash.h:76
uint32_t flash_get_status_flags(void)
Read All Status Flags.
Definition: flash.c:76
#define FLASH_SR_PGERR
Definition: f0/flash.h:75
#define FLASH_CR_OPTPG
#define FLASH_CR
void flash_program_half_word(uint32_t address, uint16_t data)
Program a Half Word to FLASH.
Definition: flash.c:97
#define FLASH_CR_STRT
void flash_unlock_option_bytes(void)
Unlock the Option Byte Access.
void flash_erase_option_bytes(void)
Erase All Option Bytes.
void flash_wait_for_last_operation(void)
Wait until Last Operation has Ended.
void flash_program_word(uint32_t address, uint32_t data)
Program a 32 bit Word to FLASH.
void flash_clear_pgerr_flag(void)
Unlock the Flash Program and Erase Controller.
void flash_clear_wrprterr_flag(void)
Clear the Write Protect Error Status Flag.
void flash_program_option_bytes(uint32_t address, uint16_t data)
Program the Option Bytes.