Trailing whitespace
[bertos.git] / bertos / cpu / cortex-m3 / io / stm32_flash.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief STM32F103xx Embedded flash definition.
34  */
35
36 #ifndef STM32_FLASH_H
37 #define STM32_FLASH_H
38
39 #include <cfg/compiler.h>
40
41 #include <cpu/types.h>
42
43 /** Return the embedded flash size in kB */
44 #define F_SIZE                   ((*(reg32_t *) 0x1FFFF7E0) & 0xFFFF)
45
46
47 /* Flash Access Control Register bits */
48 #define ACR_LATENCY_MASK         ((uint32_t)0x00000038)
49 #define ACR_HLFCYA_MASK          ((uint32_t)0xFFFFFFF7)
50 #define ACR_PRFTBE_MASK          ((uint32_t)0xFFFFFFEF)
51
52 /* Flash Access Control Register bits */
53 #define ACR_PRFTBS_MASK          ((uint32_t)0x00000020)
54
55 /* Flash Control Register bits */
56 #define CR_PG_SET                ((uint32_t)0x00000001)
57 #define CR_PG_RESET              ((uint32_t)0x00001FFE)
58
59 #define CR_PER_SET               ((uint32_t)0x00000002)
60 #define CR_PER_RESET             ((uint32_t)0x00001FFD)
61
62 #define CR_MER_SET               ((uint32_t)0x00000004)
63 #define CR_MER_RESET             ((uint32_t)0x00001FFB)
64
65 #define CR_OPTPG_SET             ((uint32_t)0x00000010)
66 #define CR_OPTPG_RESET           ((uint32_t)0x00001FEF)
67
68 #define CR_OPTER_SET             ((uint32_t)0x00000020)
69 #define CR_OPTER_RESET           ((uint32_t)0x00001FDF)
70
71 #define CR_STRT_SET              ((uint32_t)0x00000040)
72
73 #define CR_LOCK_SET              ((uint32_t)0x00000080)
74
75 /* FLASH Mask */
76 #define RDPRT_MASK               ((uint32_t)0x00000002)
77 #define WRP0_MASK                ((uint32_t)0x000000FF)
78 #define WRP1_MASK                ((uint32_t)0x0000FF00)
79 #define WRP2_MASK                ((uint32_t)0x00FF0000)
80 #define WRP3_MASK                ((uint32_t)0xFF000000)
81
82 /* FLASH Keys */
83 #define RDP_KEY                  ((uint16_t)0x00A5)
84 #define FLASH_KEY1               ((uint32_t)0x45670123)
85 #define FLASH_KEY2               ((uint32_t)0xCDEF89AB)
86
87 /* Flash Latency */
88 #define FLASH_LATENCY_0                ((uint32_t)0x00000000)  /* FLASH Zero Latency cycle */
89 #define FLASH_LATENCY_1                ((uint32_t)0x00000001)  /* FLASH One Latency cycle */
90 #define FLASH_LATENCY_2                ((uint32_t)0x00000002)  /* FLASH Two Latency cycles */
91
92 /* Half Cycle Enable/Disable */
93 #define FLASH_HALFCYCLEACCESS_ENABLE   ((uint32_t)0x00000008)  /* FLASH Half Cycle Enable */
94 #define FLASH_HALFCYCLEACCESS_DISABLE  ((uint32_t)0x00000000)  /* FLASH Half Cycle Disable */
95
96 /* Prefetch Buffer Enable/Disable */
97 #define FLASH_PREFETCHBUFFER_ENABLE    ((uint32_t)0x00000010)  /* FLASH Prefetch Buffer Enable */
98 #define FLASH_PREFETCHBUFFER_DISABLE   ((uint32_t)0x00000000)  /* FLASH Prefetch Buffer Disable */
99
100 /* Option Bytes Write Protection */
101 #define FLASH_WRPROT_PAGES0TO3      ((uint32_t)0x00000001) /* Write protection of page 0 to 3 */
102 #define FLASH_WRPROT_PAGES4TO7      ((uint32_t)0x00000002) /* Write protection of page 4 to 7 */
103 #define FLASH_WRPROT_PAGES8TO11     ((uint32_t)0x00000004) /* Write protection of page 8 to 11 */
104 #define FLASH_WRPROT_PAGES12TO15    ((uint32_t)0x00000008) /* Write protection of page 12 to 15 */
105 #define FLASH_WRPROT_PAGES16TO19    ((uint32_t)0x00000010) /* Write protection of page 16 to 19 */
106 #define FLASH_WRPROT_PAGES20TO23    ((uint32_t)0x00000020) /* Write protection of page 20 to 23 */
107 #define FLASH_WRPROT_PAGES24TO27    ((uint32_t)0x00000040) /* Write protection of page 24 to 27 */
108 #define FLASH_WRPROT_PAGES28TO31    ((uint32_t)0x00000080) /* Write protection of page 28 to 31 */
109 #define FLASH_WRPROT_PAGES32TO35    ((uint32_t)0x00000100) /* Write protection of page 32 to 35 */
110 #define FLASH_WRPROT_PAGES36TO39    ((uint32_t)0x00000200) /* Write protection of page 36 to 39 */
111 #define FLASH_WRPROT_PAGES40TO43    ((uint32_t)0x00000400) /* Write protection of page 40 to 43 */
112 #define FLASH_WRPROT_PAGES44TO47    ((uint32_t)0x00000800) /* Write protection of page 44 to 47 */
113 #define FLASH_WRPROT_PAGES48TO51    ((uint32_t)0x00001000) /* Write protection of page 48 to 51 */
114 #define FLASH_WRPROT_PAGES52TO55    ((uint32_t)0x00002000) /* Write protection of page 52 to 55 */
115 #define FLASH_WRPROT_PAGES56TO59    ((uint32_t)0x00004000) /* Write protection of page 56 to 59 */
116 #define FLASH_WRPROT_PAGES60TO63    ((uint32_t)0x00008000) /* Write protection of page 60 to 63 */
117 #define FLASH_WRPROT_PAGES64TO67    ((uint32_t)0x00010000) /* Write protection of page 64 to 67 */
118 #define FLASH_WRPROT_PAGES68TO71    ((uint32_t)0x00020000) /* Write protection of page 68 to 71 */
119 #define FLASH_WRPROT_PAGES72TO75    ((uint32_t)0x00040000) /* Write protection of page 72 to 75 */
120 #define FLASH_WRPROT_PAGES76TO79    ((uint32_t)0x00080000) /* Write protection of page 76 to 79 */
121 #define FLASH_WRPROT_PAGES80TO83    ((uint32_t)0x00100000) /* Write protection of page 80 to 83 */
122 #define FLASH_WRPROT_PAGES84TO87    ((uint32_t)0x00200000) /* Write protection of page 84 to 87 */
123 #define FLASH_WRPROT_PAGES88TO91    ((uint32_t)0x00400000) /* Write protection of page 88 to 91 */
124 #define FLASH_WRPROT_PAGES92TO95    ((uint32_t)0x00800000) /* Write protection of page 92 to 95 */
125 #define FLASH_WRPROT_PAGES96TO99    ((uint32_t)0x01000000) /* Write protection of page 96 to 99 */
126 #define FLASH_WRPROT_PAGES100TO103  ((uint32_t)0x02000000) /* Write protection of page 100 to 103 */
127 #define FLASH_WRPROT_PAGES104TO107  ((uint32_t)0x04000000) /* Write protection of page 104 to 107 */
128 #define FLASH_WRPROT_PAGES108TO111  ((uint32_t)0x08000000) /* Write protection of page 108 to 111 */
129 #define FLASH_WRPROT_PAGES112TO115  ((uint32_t)0x10000000) /* Write protection of page 112 to 115 */
130 #define FLASH_WRPROT_PAGES116TO119  ((uint32_t)0x20000000) /* Write protection of page 115 to 119 */
131 #define FLASH_WRPROT_PAGES120TO123  ((uint32_t)0x40000000) /* Write protection of page 120 to 123 */
132 #define FLASH_WRPROT_PAGES124TO127  ((uint32_t)0x80000000) /* Write protection of page 124 to 127 */
133 #define FLASH_WRPROT_ALLPAGES       ((uint32_t)0xFFFFFFFF) /* Write protection All Pages */
134
135 /* Option Bytes IWatchdog */
136 #define OB_IWDG_SW                  ((uint16_t)0x0001)  /* Software IWDG selected */
137 #define OB_IWDG_HW                  ((uint16_t)0x0000)  /* Hardware IWDG selected */
138
139 /* Option Bytes nRST_STOP */
140 #define OB_STOP_NORST               ((uint16_t)0x0002) /* No reset generated when entering in STOP */
141 #define OB_STOP_RST                 ((uint16_t)0x0000) /* Reset generated when entering in STOP */
142
143 /* Option Bytes nRST_STDBY */
144 #define OB_STDBY_NORST              ((uint16_t)0x0004) /* No reset generated when entering in STANDBY */
145 #define OB_STDBY_RST                ((uint16_t)0x0000) /* Reset generated when entering in STANDBY */
146
147 /* FLASH Interrupts */
148 #define FLASH_IT_ERROR           ((uint32_t)0x00000400)  /* FPEC error interrupt source */
149 #define FLASH_IT_EOP             ((uint32_t)0x00001000)  /* End of FLASH Operation Interrupt source */
150
151 /* FLASH Flags */
152 #define FLASH_FLAG_BSY           ((uint32_t)0x00000001)  /* FLASH Busy flag */
153 #define FLASH_FLAG_EOP           ((uint32_t)0x00000020)  /* FLASH End of Operation flag */
154 #define FLASH_FLAG_PGERR         ((uint32_t)0x00000004)  /* FLASH Program error flag */
155 #define FLASH_FLAG_WRPRTERR      ((uint32_t)0x00000010)  /* FLASH Write protected error flag */
156 #define FLASH_FLAG_OPTERR        ((uint32_t)0x00000001)  /* FLASH Option Byte error flag */
157
158
159
160 /**
161  * Embbeded flash configuration registers structure
162  */
163 struct stm32_flash
164 {
165   reg32_t ACR;
166   reg32_t KEYR;
167   reg32_t OPTKEYR;
168   reg32_t SR;
169   reg32_t CR;
170   reg32_t AR;
171   reg32_t RESERVED;
172   reg32_t OBR;
173   reg32_t WRPR;
174 };
175
176 #endif /* STM32_FLASH_H */