1 /*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2018-12-5 SummerGift first version
9 * 2019-3-2 jinsheng add Macro judgment
10 * 2020-1-6 duminmin support single bank mode
11 * 2021-8-11 CX fix the sector calculation error bug
12 */
13
14 #include <rtconfig.h>
15 #include <rtdef.h>
16
17 #ifdef BSP_USING_ON_CHIP_FLASH
18 #include "drv_config.h"
19 #include "drv_flash.h"
20 #include <board.h>
21
22 #if defined(RT_USING_FAL)
23 #include "fal.h"
24 #endif
25
26 //#define DRV_DEBUG
27 #define LOG_TAG "drv.flash"
28 #include <drv_log.h>
29 #define ADDR_FLASH_SECTOR_0 ((rt_uint32_t)0x08000000) /* Base address of Sector 0, 32 Kbytes */
30 #define ADDR_FLASH_SECTOR_1 ((rt_uint32_t)0x08008000) /* Base address of Sector 1, 32 Kbytes */
31 #define ADDR_FLASH_SECTOR_2 ((rt_uint32_t)0x08010000) /* Base address of Sector 2, 32 Kbytes */
32 #define ADDR_FLASH_SECTOR_3 ((rt_uint32_t)0x08018000) /* Base address of Sector 3, 32 Kbytes */
33 #define ADDR_FLASH_SECTOR_4 ((rt_uint32_t)0x08020000) /* Base address of Sector 4, 128 Kbytes */
34 #define ADDR_FLASH_SECTOR_5 ((rt_uint32_t)0x08040000) /* Base address of Sector 5, 256 Kbytes */
35 #define ADDR_FLASH_SECTOR_6 ((rt_uint32_t)0x08080000) /* Base address of Sector 6, 256 Kbytes */
36 #define ADDR_FLASH_SECTOR_7 ((rt_uint32_t)0x080C0000) /* Base address of Sector 7, 256 Kbytes */
37 #define ADDR_FLASH_SECTOR_8 ((rt_uint32_t)0x08100000) /* Base address of Sector 8, 256 Kbytes */
38 #define ADDR_FLASH_SECTOR_9 ((rt_uint32_t)0x08140000) /* Base address of Sector 9, 256 Kbytes */
39 #define ADDR_FLASH_SECTOR_10 ((rt_uint32_t)0x08180000) /* Base address of Sector 10, 256 Kbytes */
40 #define ADDR_FLASH_SECTOR_11 ((rt_uint32_t)0x081C0000) /* Base address of Sector 11, 256 Kbytes */
41 /**
42 * @brief Gets the sector of a given address
43 * @param None
44 * @retval The sector of a given address
45 */
GetSector(rt_uint32_t Address)46 static rt_uint32_t GetSector(rt_uint32_t Address)
47 {
48 uint32_t sector = 0;
49
50 #if defined (FLASH_OPTCR_nDBANK)
51 FLASH_OBProgramInitTypeDef OBInit;
52 uint32_t nbank = 0;
53
54 //get duel bank ability:nDBANK(Bit29)
55 HAL_FLASHEx_OBGetConfig(&OBInit);
56 nbank = ((OBInit.USERConfig & 0x20000000U) >> 29);
57 //1:single bank mode
58 if (1 == nbank)
59 {
60 if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
61 {
62 sector = FLASH_SECTOR_0;
63 }
64 else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
65 {
66 sector = FLASH_SECTOR_1;
67 }
68 else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
69 {
70 sector = FLASH_SECTOR_2;
71 }
72 else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
73 {
74 sector = FLASH_SECTOR_3;
75 }
76 else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
77 {
78 sector = FLASH_SECTOR_4;
79 }
80 else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
81 {
82 sector = FLASH_SECTOR_5;
83 }
84 else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
85 {
86 sector = FLASH_SECTOR_6;
87 }
88 else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
89 {
90 sector = FLASH_SECTOR_7;
91 }
92 else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
93 {
94 sector = FLASH_SECTOR_8;
95 }
96 else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
97 {
98 sector = FLASH_SECTOR_9;
99 }
100 else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
101 {
102 sector = FLASH_SECTOR_10;
103 }
104 else
105 {
106 sector = FLASH_SECTOR_11;
107 }
108 }
109 else //0:dual bank mode
110 {
111 LOG_E("rtthread doesn't support duel bank mode yet!");
112 RT_ASSERT(0);
113 }
114 #else //no dual bank ability
115 if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
116 {
117 sector = FLASH_SECTOR_0;
118 }
119 else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
120 {
121 sector = FLASH_SECTOR_1;
122 }
123 else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
124 {
125 sector = FLASH_SECTOR_2;
126 }
127 else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
128 {
129 sector = FLASH_SECTOR_3;
130 }
131 else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
132 {
133 sector = FLASH_SECTOR_4;
134 }
135 else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
136 {
137 sector = FLASH_SECTOR_5;
138 }
139 else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
140 {
141 sector = FLASH_SECTOR_6;
142 }
143 else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
144 {
145 sector = FLASH_SECTOR_7;
146 }
147 else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
148 {
149 sector = FLASH_SECTOR_8;
150 }
151 else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
152 {
153 sector = FLASH_SECTOR_9;
154 }
155 else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
156 {
157 sector = FLASH_SECTOR_10;
158 }
159 else
160 {
161 sector = FLASH_SECTOR_11;
162 }
163 #endif
164 return sector;
165 }
166
167
168 /**
169 * Read data from flash.
170 * @note This operation's units is word.
171 *
172 * @param addr flash address
173 * @param buf buffer to store read data
174 * @param size read bytes size
175 *
176 * @return result
177 */
stm32_flash_read(rt_uint32_t addr,rt_uint8_t * buf,size_t size)178 int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
179 {
180 size_t i;
181
182 if ((addr + size) > STM32_FLASH_END_ADDRESS)
183 {
184 LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
185 return -1;
186 }
187
188 for (i = 0; i < size; i++, buf++, addr++)
189 {
190 *buf = *(rt_uint8_t *) addr;
191 }
192
193 return size;
194 }
195
196 /**
197 * Write data to flash.
198 * @note This operation's units is word.
199 * @note This operation must after erase. @see flash_erase.
200 *
201 * @param addr flash address
202 * @param buf the write data buffer
203 * @param size write bytes size
204 *
205 * @return result
206 */
stm32_flash_write(rt_uint32_t addr,const rt_uint8_t * buf,size_t size)207 int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
208 {
209 rt_err_t result = RT_EOK;
210 rt_uint32_t end_addr = addr + size;
211
212 if ((end_addr) > STM32_FLASH_END_ADDRESS)
213 {
214 LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
215 return -RT_EINVAL;
216 }
217
218 if (size < 1)
219 {
220 return -RT_EINVAL;
221 }
222
223 /* Unlock the Flash to enable the flash control register access */
224 HAL_FLASH_Unlock();
225 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR);
226
227 for (size_t i = 0; i < size; i++, addr++, buf++)
228 {
229 /* write data to flash */
230 if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (rt_uint64_t)(*buf)) == HAL_OK)
231 {
232 if (*(rt_uint8_t *)addr != *buf)
233 {
234 result = -RT_ERROR;
235 break;
236 }
237 }
238 else
239 {
240 result = -RT_ERROR;
241 break;
242 }
243 }
244
245 HAL_FLASH_Lock();
246
247 if (result != RT_EOK)
248 {
249 return result;
250 }
251
252 return size;
253 }
254
255 /**
256 * Erase data on flash.
257 * @note This operation is irreversible.
258 * @note This operation's units is different which on many chips.
259 *
260 * @param addr flash address
261 * @param size erase bytes size
262 *
263 * @return result
264 */
stm32_flash_erase(rt_uint32_t addr,size_t size)265 int stm32_flash_erase(rt_uint32_t addr, size_t size)
266 {
267 rt_err_t result = RT_EOK;
268 rt_uint32_t FirstSector = 0, NbOfSectors = 0;
269 rt_uint32_t SECTORError = 0;
270
271 if ((addr + size) > STM32_FLASH_END_ADDRESS)
272 {
273 LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
274 return -RT_EINVAL;
275 }
276
277 /*Variable used for Erase procedure*/
278 FLASH_EraseInitTypeDef EraseInitStruct;
279
280 /* Unlock the Flash to enable the flash control register access */
281 HAL_FLASH_Unlock();
282
283 /* Get the 1st sector to erase */
284 FirstSector = GetSector(addr);
285 /* Get the number of sector to erase from 1st sector*/
286 NbOfSectors = GetSector(addr + size - 1) - FirstSector + 1;
287 /* Fill EraseInit structure*/
288 EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
289 EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
290 EraseInitStruct.Sector = FirstSector;
291 EraseInitStruct.NbSectors = NbOfSectors;
292
293 if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
294 {
295 result = -RT_ERROR;
296 goto __exit;
297 }
298
299 __exit:
300
301 HAL_FLASH_Lock();
302
303 if (result != RT_EOK)
304 {
305 return result;
306 }
307
308 LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
309 return size;
310 }
311
312 #if defined(RT_USING_FAL)
313 #define FLASH_SIZE_GRANULARITY_32K (4 * 32 * 1024)
314 #define FLASH_SIZE_GRANULARITY_128K (128 * 1024)
315 #define FLASH_SIZE_GRANULARITY_256K (7 * 256 *1024)
316
317 #define STM32_FLASH_START_ADRESS_32K (STM32_FLASH_START_ADRESS)
318 #define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_32K + FLASH_SIZE_GRANULARITY_32K)
319 #define STM32_FLASH_START_ADRESS_256K (STM32_FLASH_START_ADRESS_128K + FLASH_SIZE_GRANULARITY_128K)
320
321 static int fal_flash_read_32k(long offset, rt_uint8_t *buf, size_t size);
322 static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
323 static int fal_flash_read_256k(long offset, rt_uint8_t *buf, size_t size);
324
325 static int fal_flash_write_32k(long offset, const rt_uint8_t *buf, size_t size);
326 static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
327 static int fal_flash_write_256k(long offset, const rt_uint8_t *buf, size_t size);
328
329 static int fal_flash_erase_32k(long offset, size_t size);
330 static int fal_flash_erase_128k(long offset, size_t size);
331 static int fal_flash_erase_256k(long offset, size_t size);
332
333 const struct fal_flash_dev stm32_onchip_flash_32k = { "onchip_flash_32k", STM32_FLASH_START_ADRESS_32K, FLASH_SIZE_GRANULARITY_32K, (32 * 1024), {NULL, fal_flash_read_32k, fal_flash_write_32k, fal_flash_erase_32k} };
334 const struct fal_flash_dev stm32_onchip_flash_128k = { "onchip_flash_128k", STM32_FLASH_START_ADRESS_128K, FLASH_SIZE_GRANULARITY_128K, (128 * 1024), {NULL, fal_flash_read_128k, fal_flash_write_128k, fal_flash_erase_128k} };
335 const struct fal_flash_dev stm32_onchip_flash_256k = { "onchip_flash_256k", STM32_FLASH_START_ADRESS_256K, FLASH_SIZE_GRANULARITY_256K, (256 * 1024), {NULL, fal_flash_read_256k, fal_flash_write_256k, fal_flash_erase_256k} };
336
fal_flash_read_32k(long offset,rt_uint8_t * buf,size_t size)337 static int fal_flash_read_32k(long offset, rt_uint8_t *buf, size_t size)
338 {
339 return stm32_flash_read(stm32_onchip_flash_32k.addr + offset, buf, size);
340 }
fal_flash_read_128k(long offset,rt_uint8_t * buf,size_t size)341 static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
342 {
343 return stm32_flash_read(stm32_onchip_flash_128k.addr + offset, buf, size);
344 }
fal_flash_read_256k(long offset,rt_uint8_t * buf,size_t size)345 static int fal_flash_read_256k(long offset, rt_uint8_t *buf, size_t size)
346 {
347 return stm32_flash_read(stm32_onchip_flash_256k.addr + offset, buf, size);
348 }
349
fal_flash_write_32k(long offset,const rt_uint8_t * buf,size_t size)350 static int fal_flash_write_32k(long offset, const rt_uint8_t *buf, size_t size)
351 {
352 return stm32_flash_write(stm32_onchip_flash_32k.addr + offset, buf, size);
353 }
fal_flash_write_128k(long offset,const rt_uint8_t * buf,size_t size)354 static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
355 {
356 return stm32_flash_write(stm32_onchip_flash_128k.addr + offset, buf, size);
357 }
fal_flash_write_256k(long offset,const rt_uint8_t * buf,size_t size)358 static int fal_flash_write_256k(long offset, const rt_uint8_t *buf, size_t size)
359 {
360 return stm32_flash_write(stm32_onchip_flash_256k.addr + offset, buf, size);
361 }
362
fal_flash_erase_32k(long offset,size_t size)363 static int fal_flash_erase_32k(long offset, size_t size)
364 {
365 return stm32_flash_erase(stm32_onchip_flash_32k.addr + offset, size);
366 }
fal_flash_erase_128k(long offset,size_t size)367 static int fal_flash_erase_128k(long offset, size_t size)
368 {
369 return stm32_flash_erase(stm32_onchip_flash_128k.addr + offset, size);
370 }
fal_flash_erase_256k(long offset,size_t size)371 static int fal_flash_erase_256k(long offset, size_t size)
372 {
373 return stm32_flash_erase(stm32_onchip_flash_256k.addr + offset, size);
374 }
375
376 #endif
377 #endif /* BSP_USING_ON_CHIP_FLASH */
378