libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
flash_common_f24.h
Go to the documentation of this file.
1/** @addtogroup flash_defines
2 *
3 * @author @htmlonly © @endhtmlonly 2010
4 * Thomas Otto <tommi@viadmin.org>
5 * @author @htmlonly &copy; @endhtmlonly 2010
6 * Mark Butler <mbutler@physics.otago.ac.nz>
7 *
8 */
9/*
10 * This file is part of the libopencm3 project.
11 *
12 * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
13 * Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
14 *
15 * This library is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this library. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29/*
30 * For details see:
31 * PM0081 Programming manual: STM32F40xxx and STM32F41xxx Flash programming
32 * September 2011, Doc ID 018520 Rev 1
33 * https://github.com/libopencm3/libopencm3-archive/blob/master/st_micro/DM00023388.pdf
34 */
35
36/** @cond */
37#ifdef LIBOPENCM3_FLASH_H
38/** @endcond */
39#ifndef LIBOPENCM3_FLASH_COMMON_F24_H
40#define LIBOPENCM3_FLASH_COMMON_F24_H
41/**@{*/
42
44
45/** @defgroup flash_registers Flash Registers
46 * @ingroup flash_defines
47@{*/
48/** Flash Access Control register */
49#define FLASH_ACR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x00)
50/** Flash Key register */
51#define FLASH_KEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x04)
52/** Flash Option bytes key register */
53#define FLASH_OPTKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x08)
54/** Flash Status register*/
55#define FLASH_SR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x0C)
56/** Flash Control register */
57#define FLASH_CR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x10)
58/** Flash Option Control register */
59#define FLASH_OPTCR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14)
60/** Flash Option Control register 1 (bank 2) */
61#define FLASH_OPTCR1 MMIO32(FLASH_MEM_INTERFACE_BASE + 0x18)
62/**@}*/
63
64/** @defgroup flash_latency FLASH Wait States
65@ingroup flash_defines
66@{*/
67#define FLASH_ACR_LATENCY(w) ((w) & FLASH_ACR_LATENCY_MASK)
68#define FLASH_ACR_LATENCY_0WS 0x00
69#define FLASH_ACR_LATENCY_1WS 0x01
70#define FLASH_ACR_LATENCY_2WS 0x02
71#define FLASH_ACR_LATENCY_3WS 0x03
72#define FLASH_ACR_LATENCY_4WS 0x04
73#define FLASH_ACR_LATENCY_5WS 0x05
74#define FLASH_ACR_LATENCY_6WS 0x06
75#define FLASH_ACR_LATENCY_7WS 0x07
76/**@}*/
77#define FLASH_ACR_LATENCY_SHIFT 0
78#define FLASH_ACR_LATENCY_MASK 0x0f
79
80/** @defgroup flash_acr_values FLASH_ACR values
81 * @ingroup flash_registers
82 * @brief Access Control register values
83 * @{*/
84#define FLASH_ACR_PRFTEN (1 << 8)
85/**@}*/
86
87/* --- FLASH_SR values ----------------------------------------------------- */
88
89#define FLASH_SR_BSY (1 << 16)
90#define FLASH_SR_PGPERR (1 << 6)
91#define FLASH_SR_PGAERR (1 << 5)
92#define FLASH_SR_WRPERR (1 << 4)
93#define FLASH_SR_OPERR (1 << 1)
94#define FLASH_SR_EOP (1 << 0)
95
96/* --- FLASH_CR values ----------------------------------------------------- */
97
98#define FLASH_CR_LOCK (1 << 31)
99#define FLASH_CR_ERRIE (1 << 25)
100#define FLASH_CR_EOPIE (1 << 24)
101#define FLASH_CR_STRT (1 << 16)
102#define FLASH_CR_MER (1 << 2)
103#define FLASH_CR_SER (1 << 1)
104#define FLASH_CR_PG (1 << 0)
105#define FLASH_CR_SNB_SHIFT 3
106#define FLASH_CR_SNB_MASK 0x1f
107#define FLASH_CR_PROGRAM_MASK 0x3
108#define FLASH_CR_PROGRAM_SHIFT 8
109/** @defgroup flash_cr_program_width Flash programming width
110@ingroup flash_group
111
112@{*/
113#define FLASH_CR_PROGRAM_X8 0
114#define FLASH_CR_PROGRAM_X16 1
115#define FLASH_CR_PROGRAM_X32 2
116#define FLASH_CR_PROGRAM_X64 3
117/**@}*/
118
119/* --- FLASH_OPTCR values -------------------------------------------------- */
120
121/* FLASH_OPTCR[27:16]: nWRP */
122/* FLASH_OBR[15:8]: RDP */
123#define FLASH_OPTCR_NRST_STDBY (1 << 7)
124#define FLASH_OPTCR_NRST_STOP (1 << 6)
125#define FLASH_OPTCR_OPTSTRT (1 << 1)
126#define FLASH_OPTCR_OPTLOCK (1 << 0)
127#define FLASH_OPTCR_BOR_LEVEL_3 (0x00 << 2)
128#define FLASH_OPTCR_BOR_LEVEL_2 (0x01 << 2)
129#define FLASH_OPTCR_BOR_LEVEL_1 (0x02 << 2)
130#define FLASH_OPTCR_BOR_OFF (0x03 << 2)
131
132/* --- FLASH_OPTCR1 values ------------------------------------------------- */
133/* Only on some devices */
134/* FLASH_OPTCR1[27:16]: nWRP bank 2 */
135
136/* --- FLASH Keys -----------------------------------------------------------*/
137
138#define FLASH_KEYR_KEY1 ((uint32_t)0x45670123)
139#define FLASH_KEYR_KEY2 ((uint32_t)0xcdef89ab)
140
141#define FLASH_OPTKEYR_KEY1 ((uint32_t)0x08192a3b)
142#define FLASH_OPTKEYR_KEY2 ((uint32_t)0x4c5d6e7f)
143
144/* --- Function prototypes ------------------------------------------------- */
145
147
148void flash_lock_option_bytes(void);
149void flash_clear_pgperr_flag(void);
150void flash_clear_wrperr_flag(void);
151void flash_clear_pgaerr_flag(void);
152void flash_erase_all_sectors(uint32_t program_size);
153void flash_erase_sector(uint8_t sector, uint32_t program_size);
154void flash_program_double_word(uint32_t address, uint64_t data);
155void flash_program_word(uint32_t address, uint32_t data);
156void flash_program_half_word(uint32_t address, uint16_t data);
157void flash_program_byte(uint32_t address, uint8_t data);
158void flash_program(uint32_t address, const uint8_t *data, uint32_t len);
159void flash_program_option_bytes(uint32_t data);
160
162/**@}*/
163
164#endif
165/** @cond */
166#else
167#warning "flash_common_f24.h should not be included direcitly,"
168#warning "only via flash.h"
169#endif
170/** @endcond */
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
void flash_clear_pgperr_flag(void)
Clear programming parallelism error flag.
void flash_program_option_bytes(uint32_t data)
Program the Option Bytes.
void flash_lock_option_bytes(void)
Lock the Option Byte Access.
void flash_erase_all_sectors(uint32_t program_size)
Erase All FLASH.
void flash_program_word(uint32_t address, uint32_t data)
Program a 32 bit Word to FLASH.
void flash_clear_pgaerr_flag(void)
Clear the Programming Alignment Error Flag.
void flash_program_double_word(uint32_t address, uint64_t data)
Program a 64 bit Word to FLASH.
void flash_erase_sector(uint8_t sector, uint32_t program_size)
Erase a Sector of FLASH.
void flash_clear_wrperr_flag(void)
Clear the Write Protect Error Flag.
void flash_program(uint32_t address, const uint8_t *data, uint32_t len)
Program a Data Block to FLASH.
void flash_program_byte(uint32_t address, uint8_t data)
Program an 8 bit Byte to FLASH.
void flash_program_half_word(uint32_t address, uint16_t data)
Program a Half Word to FLASH.