libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
flash_common_l01.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) 2010 Thomas Otto <tommi@viadmin.org>
9 * Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
10 * Copyright (C) 2012-13 Karl Palsson <karlp@tweak.net.au>
11 *
12 * This library is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this library. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26/* Flash routines shared by L0/L1. L4 has a different flash controller */
27
28/**@{*/
29
31
32
33/**
34 * Unlock primary access to the flash control/erase block
35 * You must call this before using any of the low level routines
36 * yourself.
37 * @sa flash_unlock
38 */
40{
43}
44
46{
48}
49
50/**
51 * Unlock program memory itself.
52 * Writes the magic sequence to unlock the program memory
53 * you must have already unlocked access to this register!
54 * @sa flash_unlock_pecr
55 */
57{
60}
61
63{
65}
66
68{
70}
71
72/** @brief Unlock all segments of flash
73 *
74 */
75void flash_unlock(void)
76{
80}
81
82/** @brief Lock all segments of flash
83 *
84 */
85void flash_lock(void)
86{
90}
91
92/** @brief Unlock RUN_PD bit from FLASH_ACR register.
93 */
95{
98}
99
100void flash_erase_page(uint32_t page_address)
101{
103 MMIO32(page_address) = 0;
104 while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
106}
107
108/* Must be run from RAM (per ref manual), and because it's in ram, more
109 * than 64MB away from flash address space, must be a long_call. */
110__attribute__ ((long_call, section (".ramtext")))
111void flash_program_half_page(uint32_t *dst, void *buf)
112{
113 uint32_t *src = buf;
114
115 /* Enable half page writes to program memory */
117 while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
118 for (int i = 0; i < FLASH_HALF_PAGE_SIZE; i++) {
119 *dst++ = *src++;
120 }
121 while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
123}
124
125
126/** @brief Write a word to eeprom
127 *
128 * @param address assumed to be in the eeprom space, no checking
129 * @param data word to write
130 */
131void eeprom_program_word(uint32_t address, uint32_t data)
132{
134 /* erase only if needed */
135 FLASH_PECR &= ~FLASH_PECR_FTDW;
136 MMIO32(address) = data;
138}
139
140/** @brief Write a block of words to eeprom
141 *
142 * Writes a block of words to EEPROM at the requested address, erasing if
143 * necessary, and locking afterwards. Only wordwise writing is safe for
144 * writing any value
145 *
146 * @param[in] address must point to EEPROM space, no checking!
147 * @param[in] data pointer to data to write
148 * @param[in] length_in_words size of of data in WORDS!
149 */
150void eeprom_program_words(uint32_t address, uint32_t *data, int length_in_words)
151{
152 int i;
154 while (FLASH_SR & FLASH_SR_BSY);
155 /* erase only if needed */
156 FLASH_PECR &= ~FLASH_PECR_FTDW;
157 for (i = 0; i < length_in_words; i++) {
158 MMIO32(address + (i * sizeof(uint32_t))) = *(data+i);
159 while (FLASH_SR & FLASH_SR_BSY);
160 }
162}
163
164/**@}*/
#define MMIO32(addr)
Definition: common.h:69
#define FLASH_PRGKEYR_PRGKEY2
#define FLASH_PDKEYR_PDKEY2
#define FLASH_PECR_ERASE
#define FLASH_PDKEYR_PDKEY1
#define FLASH_SR
#define FLASH_PECR
#define FLASH_PECR_OPTLOCK
#define FLASH_PEKEYR
#define FLASH_PEKEYR_PEKEY1
#define FLASH_SR_BSY
#define FLASH_PECR_PELOCK
#define FLASH_PECR_PROG
#define FLASH_PRGKEYR
#define FLASH_PRGKEYR_PRGKEY1
#define FLASH_PDKEYR
#define FLASH_PECR_FPRG
#define FLASH_PEKEYR_PEKEY2
#define FLASH_PECR_PRGLOCK
#define FLASH_HALF_PAGE_SIZE
Definition: l1/flash.h:59
void flash_erase_page(uint32_t page_address)
Erase a page in flash.
void flash_lock_pecr(void)
void eeprom_program_words(uint32_t address, uint32_t *data, int length_in_words)
Write a block of words to eeprom.
void flash_unlock_progmem(void)
Unlock program memory itself.
void flash_unlock_acr(void)
Unlock RUN_PD bit from FLASH_ACR register.
void flash_unlock_pecr(void)
Unlock primary access to the flash control/erase block You must call this before using any of the low...
void flash_lock_option_bytes(void)
void flash_unlock(void)
Unlock all segments of flash.
void flash_unlock_option_bytes(void)
Unlock the Option Byte Access.
void flash_lock_progmem(void)
void flash_lock(void)
Lock all segments of flash.
void eeprom_program_word(uint32_t address, uint32_t data)
Write a word to eeprom.
void flash_program_half_page(uint32_t *dst, void *buf)
Write a half page from buf to dst.