APM:Libraries
flash.c
Go to the documentation of this file.
1 /************************************************************************************
2  * Copyright (C) 2011 Uros Platise. All rights reserved.
3  * Author: Uros Platise <uros.platise@isotel.eu>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name NuttX nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ************************************************************************************/
33 
34 /*
35  * This file is free software: you can redistribute it and/or modify it
36  * under the terms of the GNU General Public License as published by the
37  * Free Software Foundation, either version 3 of the License, or
38  * (at your option) any later version.
39  *
40  * This file is distributed in the hope that it will be useful, but
41  * WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
43  * See the GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License along
46  * with this program. If not, see <http://www.gnu.org/licenses/>.
47  *
48  * Modified for use in AP_HAL by Andrew Tridgell and Siddharth Bharat Purohit
49  */
50 
51 #include "flash.h"
52 #include "hal.h"
53 
54 // #pragma GCC optimize("O0")
55 
56 /*
57  this driver has been tested with STM32F427 and STM32F412
58  */
59 
60 #ifndef HAL_USE_EMPTY_STORAGE
61 
62 #ifndef BOARD_FLASH_SIZE
63 #error "You must define BOARD_FLASH_SIZE in kbyte"
64 #endif
65 
66 #define KB(x) ((x*1024))
67 // Refer Flash memory map in the User Manual to fill the following fields per microcontroller
68 #define STM32_FLASH_BASE 0x08000000
69 #define STM32_FLASH_SIZE KB(BOARD_FLASH_SIZE)
70 
71 // optionally disable interrupts during flash writes
72 #define STM32_FLASH_DISABLE_ISR 0
73 
74 // the 2nd bank of flash needs to be handled differently
75 #define STM32_FLASH_BANK2_START (STM32_FLASH_BASE+0x00080000)
76 
77 
78 #if BOARD_FLASH_SIZE == 512
79 #define STM32_FLASH_NPAGES 7
80 static const uint32_t flash_memmap[STM32_FLASH_NPAGES] = { KB(16), KB(16), KB(16), KB(16), KB(64),
81  KB(128), KB(128), KB(128) };
82 
83 #elif BOARD_FLASH_SIZE == 1024
84 #define STM32_FLASH_NPAGES 12
85 static const uint32_t flash_memmap[STM32_FLASH_NPAGES] = { KB(16), KB(16), KB(16), KB(16), KB(64),
86  KB(128), KB(128), KB(128), KB(128), KB(128), KB(128), KB(128) };
87 
88 #elif BOARD_FLASH_SIZE == 2048
89 #define STM32_FLASH_NPAGES 24
90 static const uint32_t flash_memmap[STM32_FLASH_NPAGES] = { KB(16), KB(16), KB(16), KB(16), KB(64),
91  KB(128), KB(128), KB(128), KB(128), KB(128), KB(128), KB(128),
92  KB(16), KB(16), KB(16), KB(16), KB(64),
93  KB(128), KB(128), KB(128), KB(128), KB(128), KB(128), KB(128)};
94 #endif
95 
96 // keep a cache of the page addresses
97 static uint32_t flash_pageaddr[STM32_FLASH_NPAGES];
99 
100 
101 #define FLASH_KEY1 0x45670123
102 #define FLASH_KEY2 0xCDEF89AB
103 /* Some compiler options will convert short loads and stores into byte loads
104  * and stores. We don't want this to happen for IO reads and writes!
105  */
106 /* # define getreg16(a) (*(volatile uint16_t *)(a)) */
107 static inline uint16_t getreg16(unsigned int addr)
108 {
109  uint16_t retval;
110  __asm__ __volatile__("\tldrh %0, [%1]\n\t" : "=r"(retval) : "r"(addr));
111  return retval;
112 }
113 
114 /* define putreg16(v,a) (*(volatile uint16_t *)(a) = (v)) */
115 static inline void putreg16(uint16_t val, unsigned int addr)
116 {
117  __asm__ __volatile__("\tstrh %0, [%1]\n\t": : "r"(val), "r"(addr));
118 }
119 
120 static void stm32_flash_wait_idle(void)
121 {
122  while (FLASH->SR & FLASH_SR_BSY) {
123  // nop
124  }
125 }
126 
127 static void stm32_flash_unlock(void)
128 {
130 
131  if (FLASH->CR & FLASH_CR_LOCK) {
132  /* Unlock sequence */
133  FLASH->KEYR = FLASH_KEY1;
134  FLASH->KEYR = FLASH_KEY2;
135  }
136 
137  // disable the data cache - see stm32 errata 2.1.11
138  FLASH->ACR &= ~FLASH_ACR_DCEN;
139 }
140 
142 {
144  FLASH->CR |= FLASH_CR_LOCK;
145 
146  // reset and re-enable the data cache - see stm32 errata 2.1.11
147  FLASH->ACR |= FLASH_ACR_DCRST;
148  FLASH->ACR &= ~FLASH_ACR_DCRST;
149  FLASH->ACR |= FLASH_ACR_DCEN;
150 }
151 
152 
153 
154 /*
155  get the memory address of a page
156  */
157 uint32_t stm32_flash_getpageaddr(uint32_t page)
158 {
159  if (page >= STM32_FLASH_NPAGES) {
160  return 0;
161  }
162 
164  uint32_t address = STM32_FLASH_BASE;
165  uint8_t i;
166 
167  for (i = 0; i < STM32_FLASH_NPAGES; i++) {
168  flash_pageaddr[i] = address;
169  address += stm32_flash_getpagesize(i);
170  }
172  }
173 
174  return flash_pageaddr[page];
175 }
176 
177 /*
178  get size in bytes of a page
179  */
180 uint32_t stm32_flash_getpagesize(uint32_t page)
181 {
182  return flash_memmap[page];
183 }
184 
185 /*
186  return total number of pages
187  */
189 {
190  return STM32_FLASH_NPAGES;
191 }
192 
193 static bool stm32_flash_ispageerased(uint32_t page)
194 {
195  uint32_t addr;
196  uint32_t count;
197 
198  if (page >= STM32_FLASH_NPAGES) {
199  return false;
200  }
201 
202  for (addr = stm32_flash_getpageaddr(page), count = stm32_flash_getpagesize(page);
203  count; count--, addr++) {
204  if ((*(volatile uint8_t *)(addr)) != 0xff) {
205  return false;
206  }
207  }
208 
209  return true;
210 }
211 
212 /*
213  erase a page
214  */
215 bool stm32_flash_erasepage(uint32_t page)
216 {
217  if (page >= STM32_FLASH_NPAGES) {
218  return false;
219  }
220 
221 #if STM32_FLASH_DISABLE_ISR
222  syssts_t sts = chSysGetStatusAndLockX();
223 #endif
226 
227  // clear any previous errors
228  FLASH->SR = 0xF3;
229 
231 
232  // the snb mask is not contiguous, calculate the mask for the page
233  uint8_t snb = (((page % 12) << 3) | ((page / 12) << 7));
234 
235  FLASH->CR = FLASH_CR_PSIZE_1 | snb | FLASH_CR_SER;
236  FLASH->CR |= FLASH_CR_STRT;
237 
239 
240  if (FLASH->SR) {
241  // an error occurred
242  FLASH->SR = 0xF3;
244 #if STM32_FLASH_DISABLE_ISR
245  chSysRestoreStatusX(sts);
246 #endif
247  return false;
248  }
249 
251 #if STM32_FLASH_DISABLE_ISR
252  chSysRestoreStatusX(sts);
253 #endif
254 
255  return stm32_flash_ispageerased(page);
256 }
257 
258 
259 int32_t stm32_flash_write(uint32_t addr, const void *buf, uint32_t count)
260 {
261  uint16_t *hword = (uint16_t *)buf;
262  uint32_t written = count;
263 
264  /* STM32 requires half-word access */
265  if (count & 1) {
266  return -1;
267  }
268 
269  if ((addr+count) >= STM32_FLASH_BASE+STM32_FLASH_SIZE) {
270  return -1;
271  }
272 
273  /* Get flash ready and begin flashing */
274 
275  if (!(RCC->CR & RCC_CR_HSION)) {
276  return -1;
277  }
278 
279 #if STM32_FLASH_DISABLE_ISR
280  syssts_t sts = chSysGetStatusAndLockX();
281 #endif
282 
284 
285  // clear previous errors
286  FLASH->SR = 0xF3;
287 
288  /* TODO: implement up_progmem_write() to support other sizes than 16-bits */
289  FLASH->CR &= ~(FLASH_CR_PSIZE);
290  FLASH->CR |= FLASH_CR_PSIZE_0 | FLASH_CR_PG;
291 
292  for (;count; count -= 2, hword++, addr += 2) {
293  /* Write half-word and wait to complete */
294 
295  putreg16(*hword, addr);
296 
298 
299  if (FLASH->SR) {
300  // we got an error
301  FLASH->SR = 0xF3;
302  FLASH->CR &= ~(FLASH_CR_PG);
303  goto failed;
304  }
305 
306  if (getreg16(addr) != *hword) {
307  FLASH->CR &= ~(FLASH_CR_PG);
308  goto failed;
309  }
310  }
311 
312  FLASH->CR &= ~(FLASH_CR_PG);
313 
315 #if STM32_FLASH_DISABLE_ISR
316  chSysRestoreStatusX(sts);
317 #endif
318  return written;
319 
320 failed:
322 #if STM32_FLASH_DISABLE_ISR
323  chSysRestoreStatusX(sts);
324 #endif
325  return -1;
326 }
327 
328 #endif // HAL_USE_EMPTY_STORAGE
static void stm32_flash_wait_idle(void)
Definition: flash.c:120
uint32_t stm32_flash_getnumpages()
Definition: flash.c:188
static void stm32_flash_unlock(void)
Definition: flash.c:127
static uint16_t getreg16(unsigned int addr)
Definition: flash.c:107
static bool flash_pageaddr_initialised
Definition: flash.c:98
#define FLASH_KEY2
Definition: flash.c:102
static void putreg16(uint16_t val, unsigned int addr)
Definition: flash.c:115
uint32_t stm32_flash_getpagesize(uint32_t page)
Definition: flash.c:180
static bool stm32_flash_ispageerased(uint32_t page)
Definition: flash.c:193
bool stm32_flash_erasepage(uint32_t page)
Definition: flash.c:215
#define STM32_FLASH_SIZE
Definition: flash.c:69
#define KB(x)
Definition: flash.c:66
static uint32_t flash_pageaddr[STM32_FLASH_NPAGES]
Definition: flash.c:97
#define STM32_FLASH_BASE
Definition: flash.c:68
#define FLASH_KEY1
Definition: flash.c:101
uint32_t stm32_flash_getpageaddr(uint32_t page)
Definition: flash.c:157
int32_t stm32_flash_write(uint32_t addr, const void *buf, uint32_t count)
Definition: flash.c:259
void stm32_flash_lock(void)
Definition: flash.c:141