libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
flash_common_l01.h
Go to the documentation of this file.
1/** @addtogroup flash_defines
2 *
3 */
4/*
5 * This file is part of the libopencm3 project.
6 *
7 * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
8 * Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
9 * Copyright (C) 2012 Karl Palsson <karlp@tweak.net.au>
10 *
11 * This library is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25/** @cond */
26#ifdef LIBOPENCM3_FLASH_H
27/** @endcond */
28#ifndef LIBOPENCM3_FLASH_COMMON_L01_H
29#define LIBOPENCM3_FLASH_COMMON_L01_H
30/**@{*/
31
33
34/* --- FLASH registers ----------------------------------------------------- */
35
36#define FLASH_ACR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x00)
37#define FLASH_PECR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x04)
38#define FLASH_PDKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x08)
39#define FLASH_PEKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x0C)
40#define FLASH_PRGKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x10)
41#define FLASH_OPTKEYR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x14)
42#define FLASH_SR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x18)
43#define FLASH_OBR MMIO32(FLASH_MEM_INTERFACE_BASE + 0x1c)
44#define FLASH_WRPR1 MMIO32(FLASH_MEM_INTERFACE_BASE + 0x20)
45#define FLASH_WRPR2 MMIO32(FLASH_MEM_INTERFACE_BASE + 0x80)
46
47/* --- FLASH_ACR values ---------------------------------------------------- */
48#define FLASH_ACR_RUNPD (1 << 4)
49#define FLASH_ACR_SLEEPPD (1 << 3)
50#define FLASH_ACR_PRFTEN (1 << 1)
51#define FLASH_ACR_LATENCY_SHIFT 0
52#define FLASH_ACR_LATENCY_MASK 1
53/** @defgroup flash_latency FLASH Wait States
54@ingroup flash_defines
55@{*/
56#define FLASH_ACR_LATENCY_0WS 0x00
57#define FLASH_ACR_LATENCY_1WS 0x01
58/**@}*/
59
60/* --- FLASH_PECR values. Program/erase control register */
61#define FLASH_PECR_OBL_LAUNCH (1 << 18)
62#define FLASH_PECR_ERRIE (1 << 17)
63#define FLASH_PECR_EOPIE (1 << 16)
64#define FLASH_PECR_PARALLBANK (1 << 15)
65#define FLASH_PECR_FPRG (1 << 10)
66#define FLASH_PECR_ERASE (1 << 9)
67#define FLASH_PECR_FTDW (1 << 8)
68#define FLASH_PECR_DATA (1 << 4)
69#define FLASH_PECR_PROG (1 << 3)
70#define FLASH_PECR_OPTLOCK (1 << 2)
71#define FLASH_PECR_PRGLOCK (1 << 1)
72#define FLASH_PECR_PELOCK (1 << 0)
73
74/* Power down key register (FLASH_PDKEYR) */
75#define FLASH_PDKEYR_PDKEY1 ((uint32_t)0x04152637)
76#define FLASH_PDKEYR_PDKEY2 ((uint32_t)0xFAFBFCFD)
77
78/* Program/erase key register (FLASH_PEKEYR) */
79#define FLASH_PEKEYR_PEKEY1 ((uint32_t)0x89ABCDEF)
80#define FLASH_PEKEYR_PEKEY2 ((uint32_t)0x02030405)
81
82/* Program memory key register (FLASH_PRGKEYR) */
83#define FLASH_PRGKEYR_PRGKEY1 ((uint32_t)0x8C9DAEBF)
84#define FLASH_PRGKEYR_PRGKEY2 ((uint32_t)0x13141516)
85
86/* Option byte key register (FLASH_OPTKEYR) */
87#define FLASH_OPTKEYR_KEY1 ((uint32_t)0xFBEAD9C8)
88#define FLASH_OPTKEYR_KEY2 ((uint32_t)0x24252627)
89
90/* --- FLASH_SR values ----------------------------------------------------- */
91#define FLASH_SR_OPTVERR (1 << 11)
92#define FLASH_SR_SIZEERR (1 << 10)
93#define FLASH_SR_PGAERR (1 << 9)
94#define FLASH_SR_WRPERR (1 << 8)
95#define FLASH_SR_READY (1 << 3)
96#define FLASH_SR_ENDHV (1 << 2)
97#define FLASH_SR_EOP (1 << 1)
98#define FLASH_SR_BSY (1 << 0)
99
100/* --- FLASH_OBR values ----------------------------------------------------- */
101#define FLASH_OBR_BFB2 (1 << 23)
102#define FLASH_OBR_NRST_STDBY (1 << 22)
103#define FLASH_OBR_NRST_STOP (1 << 21)
104#define FLASH_OBR_IWDG_SW (1 << 20)
105#define FLASH_OBR_BOR_OFF (0x0 << 16)
106#define FLASH_OBR_BOR_LEVEL_1 (0x8 << 16)
107#define FLASH_OBR_BOR_LEVEL_2 (0x9 << 16)
108#define FLASH_OBR_BOR_LEVEL_3 (0xa << 16)
109#define FLASH_OBR_BOR_LEVEL_4 (0xb << 16)
110#define FLASH_OBR_BOR_LEVEL_5 (0xc << 16)
111#define FLASH_OBR_RDPRT_LEVEL_0 (0xaa)
112#define FLASH_OBR_RDPRT_LEVEL_1 (0x00)
113#define FLASH_OBR_RDPRT_LEVEL_2 (0xcc)
114
116
117void flash_unlock_pecr(void);
118void flash_lock_pecr(void);
119void flash_unlock_progmem(void);
120void flash_lock_progmem(void);
121void flash_lock_option_bytes(void);
122void flash_unlock_acr(void);
123/** Erase a page in flash.
124 * @param page_address For L1, must be first word in page, L0 doesn't care
125 * Takes 1 tprog. Flash must already be unlocked!
126 */
127void flash_erase_page(uint32_t page_address);
128
129/**
130 * Write a half page from buf to dst.
131 * This function _must_ be in ram! (See the Ref Man for more details)
132 * @param dst where to write to, expected to be aligned and erased.
133 * @param buf the half page to write, size required depends on target
134 */
135void flash_program_half_page(uint32_t *dst, void *buf);
136
137void eeprom_program_word(uint32_t address, uint32_t data);
138void eeprom_program_words(uint32_t address, uint32_t *data, int length_in_words);
139
141/**@}*/
142
143#endif
144/** @cond */
145#else
146#error "flash_common_l01.h should not be included directly, only via flash.h"
147#endif
148/** @endcond */
149
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
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_lock_progmem(void)
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.