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  */
10 
11 #include <rtconfig.h>
12 #include <rtdef.h>
13 
14 #ifdef BSP_USING_ON_CHIP_FLASH
15 #include "drv_config.h"
16 #include "drv_flash.h"
17 #include <board.h>
18 
19 #if defined(RT_USING_FAL)
20 #include "fal.h"
21 #endif
22 
23 //#define DRV_DEBUG
24 #define LOG_TAG                "drv.flash"
25 #include <drv_log.h>
26 
27 /* Base address of the Flash sectors Bank 1 */
28 #define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
29 #define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
30 #define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
31 #define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
32 #define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
33 #define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
34 #define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
35 #define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
36 #define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
37 #define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
38 #define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
39 #define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
40 
41 /* Base address of the Flash sectors Bank 2 */
42 #define ADDR_FLASH_SECTOR_12     ((uint32_t)0x08100000) /* Base @ of Sector 0, 16 Kbytes */
43 #define ADDR_FLASH_SECTOR_13     ((uint32_t)0x08104000) /* Base @ of Sector 1, 16 Kbytes */
44 #define ADDR_FLASH_SECTOR_14     ((uint32_t)0x08108000) /* Base @ of Sector 2, 16 Kbytes */
45 #define ADDR_FLASH_SECTOR_15     ((uint32_t)0x0810C000) /* Base @ of Sector 3, 16 Kbytes */
46 #define ADDR_FLASH_SECTOR_16     ((uint32_t)0x08110000) /* Base @ of Sector 4, 64 Kbytes */
47 #define ADDR_FLASH_SECTOR_17     ((uint32_t)0x08120000) /* Base @ of Sector 5, 128 Kbytes */
48 #define ADDR_FLASH_SECTOR_18     ((uint32_t)0x08140000) /* Base @ of Sector 6, 128 Kbytes */
49 #define ADDR_FLASH_SECTOR_19     ((uint32_t)0x08160000) /* Base @ of Sector 7, 128 Kbytes */
50 #define ADDR_FLASH_SECTOR_20     ((uint32_t)0x08180000) /* Base @ of Sector 8, 128 Kbytes  */
51 #define ADDR_FLASH_SECTOR_21     ((uint32_t)0x081A0000) /* Base @ of Sector 9, 128 Kbytes  */
52 #define ADDR_FLASH_SECTOR_22     ((uint32_t)0x081C0000) /* Base @ of Sector 10, 128 Kbytes */
53 #define ADDR_FLASH_SECTOR_23     ((uint32_t)0x081E0000) /* Base @ of Sector 11, 128 Kbytes */
54 
55 /**
56   * @brief  Gets the sector of a given address
57   * @param  None
58   * @retval The sector of a given address
59   */
GetSector(rt_uint32_t Address)60 static rt_uint32_t GetSector(rt_uint32_t Address)
61 {
62     rt_uint32_t sector = 0;
63 
64     if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
65     {
66         sector = FLASH_SECTOR_0;
67     }
68     else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
69     {
70         sector = FLASH_SECTOR_1;
71     }
72     else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
73     {
74         sector = FLASH_SECTOR_2;
75     }
76     else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
77     {
78         sector = FLASH_SECTOR_3;
79     }
80     else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
81     {
82         sector = FLASH_SECTOR_4;
83     }
84     else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
85     {
86         sector = FLASH_SECTOR_5;
87     }
88     else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
89     {
90         sector = FLASH_SECTOR_6;
91     }
92     else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
93     {
94         sector = FLASH_SECTOR_7;
95     }
96 #if defined(FLASH_SECTOR_8)
97     else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
98     {
99         sector = FLASH_SECTOR_8;
100     }
101 #endif
102 #if defined(FLASH_SECTOR_9)
103     else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
104     {
105         sector = FLASH_SECTOR_9;
106     }
107 #endif
108 #if defined(FLASH_SECTOR_10)
109     else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
110     {
111         sector = FLASH_SECTOR_10;
112     }
113 #endif
114 #if defined(FLASH_SECTOR_11)
115     else if((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11))
116     {
117         sector = FLASH_SECTOR_11;
118     }
119 #endif
120 #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
121     else if((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12))
122     {
123         sector = FLASH_SECTOR_12;
124     }
125     else if((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13))
126     {
127         sector = FLASH_SECTOR_13;
128     }
129     else if((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14))
130     {
131         sector = FLASH_SECTOR_14;
132     }
133     else if((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15))
134     {
135         sector = FLASH_SECTOR_15;
136     }
137     else if((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16))
138     {
139         sector = FLASH_SECTOR_16;
140     }
141     else if((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17))
142     {
143         sector = FLASH_SECTOR_17;
144     }
145     else if((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18))
146     {
147         sector = FLASH_SECTOR_18;
148     }
149     else if((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19))
150     {
151         sector = FLASH_SECTOR_19;
152     }
153     else if((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20))
154     {
155         sector = FLASH_SECTOR_20;
156     }
157     else if((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21))
158     {
159         sector = FLASH_SECTOR_21;
160     }
161     else if((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22))
162     {
163         sector = FLASH_SECTOR_22;
164     }
165     else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_23) */
166     {
167         sector = FLASH_SECTOR_23;
168     }
169 #endif
170     return sector;
171 }
172 
173 /**
174  * Read data from flash.
175  * @note This operation's units is word.
176  *
177  * @param addr flash address
178  * @param buf buffer to store read data
179  * @param size read bytes size
180  *
181  * @return result
182  */
stm32_flash_read(rt_uint32_t addr,rt_uint8_t * buf,size_t size)183 int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
184 {
185     size_t i;
186 
187     if ((addr + size) > STM32_FLASH_END_ADDRESS)
188     {
189         LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size));
190         return -1;
191     }
192 
193     for (i = 0; i < size; i++, buf++, addr++)
194     {
195         *buf = *(rt_uint8_t *) addr;
196     }
197 
198     return size;
199 }
200 
201 /**
202  * Write data to flash.
203  * @note This operation's units is word.
204  * @note This operation must after erase. @see flash_erase.
205  *
206  * @param addr flash address
207  * @param buf the write data buffer
208  * @param size write bytes size
209  *
210  * @return result
211  */
stm32_flash_write(rt_uint32_t addr,const rt_uint8_t * buf,size_t size)212 int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
213 {
214     rt_err_t result      = RT_EOK;
215     rt_uint32_t end_addr = addr + size;
216     rt_uint32_t written_size = 0;
217     rt_uint32_t write_size = 0;
218 
219     if ((end_addr) > STM32_FLASH_END_ADDRESS)
220     {
221         LOG_E("write outrange flash size! addr is (0x%p)", (void*)(addr + size));
222         return -RT_EINVAL;
223     }
224 
225     if (size < 1)
226     {
227         return -RT_EINVAL;
228     }
229 
230     HAL_FLASH_Unlock();
231 
232     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
233 
234     while (written_size < size)
235     {
236         if (((addr + written_size) % 4 == 0) && (size - written_size >= 4))
237         {
238             if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr + written_size, *((rt_uint32_t *)(buf + written_size))) == HAL_OK)
239             {
240                 if (*(rt_uint32_t *)(addr + written_size) != *(rt_uint32_t *)(buf + written_size))
241                 {
242                     result = -RT_ERROR;
243                     break;
244                 }
245             }
246             else
247             {
248                 result = -RT_ERROR;
249                 break;
250             }
251             write_size = 4;
252         }
253         else if (((addr + written_size) % 2 == 0) && (size - written_size >= 2))
254         {
255             if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr + written_size, *((rt_uint16_t *)(buf + written_size))) == HAL_OK)
256             {
257                 if (*(rt_uint16_t *)(addr + written_size) != *(rt_uint16_t *)(buf + written_size))
258                 {
259                     result = -RT_ERROR;
260                     break;
261                 }
262             }
263             else
264             {
265                 result = -RT_ERROR;
266                 break;
267             }
268             write_size = 2;
269         }
270         else
271         {
272             if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr + written_size, *((rt_uint8_t *)(buf + written_size))) == HAL_OK)
273             {
274                 if (*(rt_uint8_t *)(addr + written_size) != *(rt_uint8_t *)(buf + written_size))
275                 {
276                     result = -RT_ERROR;
277                     break;
278                 }
279             }
280             else
281             {
282                 result = -RT_ERROR;
283                 break;
284             }
285             write_size = 1;
286         }
287 
288         written_size += write_size;
289     }
290 
291     HAL_FLASH_Lock();
292 
293     if (result != RT_EOK)
294     {
295         return result;
296     }
297 
298     return size;
299 }
300 
301 /**
302  * Erase data on flash.
303  * @note This operation is irreversible.
304  * @note This operation's units is different which on many chips.
305  *
306  * @param addr flash address
307  * @param size erase bytes size
308  *
309  * @return result
310  */
stm32_flash_erase(rt_uint32_t addr,size_t size)311 int stm32_flash_erase(rt_uint32_t addr, size_t size)
312 {
313     rt_err_t result = RT_EOK;
314     rt_uint32_t FirstSector = 0, NbOfSectors = 0;
315     rt_uint32_t SECTORError = 0;
316 
317     if ((addr + size) > STM32_FLASH_END_ADDRESS)
318     {
319         LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
320         return -RT_EINVAL;
321     }
322 
323     if (size < 1)
324     {
325         return -RT_EINVAL;
326     }
327 
328     /*Variable used for Erase procedure*/
329     FLASH_EraseInitTypeDef EraseInitStruct;
330 
331     /* Unlock the Flash to enable the flash control register access */
332     HAL_FLASH_Unlock();
333 
334     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
335 
336     /* Get the 1st sector to erase */
337     FirstSector = GetSector(addr);
338     /* Get the number of sector to erase from 1st sector*/
339     NbOfSectors = GetSector(addr + size - 1) - FirstSector + 1;
340     /* Fill EraseInit structure*/
341     EraseInitStruct.TypeErase     = FLASH_TYPEERASE_SECTORS;
342     EraseInitStruct.VoltageRange  = FLASH_VOLTAGE_RANGE_3;
343     EraseInitStruct.Sector        = FirstSector;
344     EraseInitStruct.NbSectors     = NbOfSectors;
345 
346     if (HAL_FLASHEx_Erase(&EraseInitStruct, (uint32_t *)&SECTORError) != HAL_OK)
347     {
348         result = -RT_ERROR;
349         goto __exit;
350     }
351 
352 __exit:
353     HAL_FLASH_Lock();
354 
355     if (result != RT_EOK)
356     {
357         return result;
358     }
359 
360     LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size);
361     return size;
362 }
363 
364 #if defined(RT_USING_FAL)
365 
366 static int fal_flash_read_16k(long offset, rt_uint8_t *buf, size_t size);
367 static int fal_flash_read_64k(long offset, rt_uint8_t *buf, size_t size);
368 static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
369 
370 static int fal_flash_write_16k(long offset, const rt_uint8_t *buf, size_t size);
371 static int fal_flash_write_64k(long offset, const rt_uint8_t *buf, size_t size);
372 static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
373 
374 static int fal_flash_erase_16k(long offset, size_t size);
375 static int fal_flash_erase_64k(long offset, size_t size);
376 static int fal_flash_erase_128k(long offset, size_t size);
377 
378 const struct fal_flash_dev stm32_onchip_flash_16k =
379 {
380     "onchip_flash_16k",
381     STM32_FLASH_START_ADRESS_16K,
382     FLASH_SIZE_GRANULARITY_16K,
383     (16 * 1024),
384     {
385         NULL,
386         fal_flash_read_16k,
387         fal_flash_write_16k,
388         fal_flash_erase_16k,
389     },
390     8,
391     {},
392 };
393 const struct fal_flash_dev stm32_onchip_flash_64k =
394 {
395     "onchip_flash_64k",
396     STM32_FLASH_START_ADRESS_64K,
397     FLASH_SIZE_GRANULARITY_64K,
398     (64 * 1024),
399     {
400         NULL,
401         fal_flash_read_64k,
402         fal_flash_write_64k,
403         fal_flash_erase_64k,
404     },
405     8,
406     {},
407 };
408 const struct fal_flash_dev stm32_onchip_flash_128k =
409 {
410     "onchip_flash_128k",
411     STM32_FLASH_START_ADRESS_128K,
412     FLASH_SIZE_GRANULARITY_128K,
413     (128 * 1024),
414     {
415         NULL,
416         fal_flash_read_128k,
417         fal_flash_write_128k,
418         fal_flash_erase_128k,
419     },
420     8,
421     {},
422 };
423 
fal_flash_read_16k(long offset,rt_uint8_t * buf,size_t size)424 static int fal_flash_read_16k(long offset, rt_uint8_t *buf, size_t size)
425 {
426     return stm32_flash_read(stm32_onchip_flash_16k.addr + offset, buf, size);
427 }
fal_flash_read_64k(long offset,rt_uint8_t * buf,size_t size)428 static int fal_flash_read_64k(long offset, rt_uint8_t *buf, size_t size)
429 {
430     return stm32_flash_read(stm32_onchip_flash_64k.addr + offset, buf, size);
431 }
fal_flash_read_128k(long offset,rt_uint8_t * buf,size_t size)432 static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
433 {
434     return stm32_flash_read(stm32_onchip_flash_128k.addr + offset, buf, size);
435 }
436 
fal_flash_write_16k(long offset,const rt_uint8_t * buf,size_t size)437 static int fal_flash_write_16k(long offset, const rt_uint8_t *buf, size_t size)
438 {
439     return stm32_flash_write(stm32_onchip_flash_16k.addr + offset, buf, size);
440 }
fal_flash_write_64k(long offset,const rt_uint8_t * buf,size_t size)441 static int fal_flash_write_64k(long offset, const rt_uint8_t *buf, size_t size)
442 {
443     return stm32_flash_write(stm32_onchip_flash_64k.addr + offset, buf, size);
444 }
fal_flash_write_128k(long offset,const rt_uint8_t * buf,size_t size)445 static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
446 {
447     return stm32_flash_write(stm32_onchip_flash_128k.addr + offset, buf, size);
448 }
449 
fal_flash_erase_16k(long offset,size_t size)450 static int fal_flash_erase_16k(long offset, size_t size)
451 {
452     return stm32_flash_erase(stm32_onchip_flash_16k.addr + offset, size);
453 }
fal_flash_erase_64k(long offset,size_t size)454 static int fal_flash_erase_64k(long offset, size_t size)
455 {
456     return stm32_flash_erase(stm32_onchip_flash_64k.addr + offset, size);
457 }
fal_flash_erase_128k(long offset,size_t size)458 static int fal_flash_erase_128k(long offset, size_t size)
459 {
460     return stm32_flash_erase(stm32_onchip_flash_128k.addr + offset, size);
461 }
462 
463 #endif
464 #endif /* BSP_USING_ON_CHIP_FLASH */
465