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