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  * 2020-10-14     Dozingfiretruck   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 /**
28   * @brief  Gets the page of a given address
29   * @param  Addr: Address of the FLASH Memory
30   * @retval The page of a given address
31   */
GetPage(uint32_t addr)32 static uint32_t GetPage(uint32_t addr)
33 {
34     uint32_t page = 0;
35     page = RT_ALIGN_DOWN(addr-STM32_FLASH_START_ADRESS, FLASH_PAGE_SIZE)/FLASH_PAGE_SIZE;
36     return page;
37 }
38 
39 /**
40  * Read data from flash.
41  * @note This operation's units is word.
42  *
43  * @param addr flash address
44  * @param buf buffer to store read data
45  * @param size read bytes size
46  *
47  * @return result
48  */
stm32_flash_read(rt_uint32_t addr,rt_uint8_t * buf,size_t size)49 int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
50 {
51     size_t i;
52 
53     if ((addr + size) > STM32_FLASH_END_ADDRESS)
54     {
55         LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size));
56         return -RT_EINVAL;
57     }
58 
59     for (i = 0; i < size; i++, buf++, addr++)
60     {
61         *buf = *(rt_uint8_t *) addr;
62     }
63 
64     return size;
65 }
66 
67 /**
68  * Write data to flash.
69  * @note This operation's units is word.
70  * @note This operation must after erase. @see flash_erase.
71  *
72  * @param addr flash address
73  * @param buf the write data buffer
74  * @param size write bytes size
75  *
76  * @return result
77  */
78 
stm32_flash_write(rt_uint32_t addr,const uint8_t * buf,size_t size)79 int stm32_flash_write(rt_uint32_t addr, const uint8_t *buf, size_t size)
80 {
81     size_t i, j;
82     rt_err_t result = 0;
83     rt_uint64_t write_data = 0, temp_data = 0;
84 
85     if ((addr + size) > STM32_FLASH_END_ADDRESS)
86     {
87         LOG_E("ERROR: write outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
88         return -RT_EINVAL;
89     }
90 
91     if(addr % 8 != 0)
92     {
93         LOG_E("write addr must be 8-byte alignment");
94         return -RT_EINVAL;
95     }
96 
97     HAL_FLASH_Unlock();
98 
99     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);
100 
101     if (size < 1)
102     {
103         return -RT_ERROR;
104     }
105 
106     for (i = 0; i < size;)
107     {
108         if ((size - i) < 8)
109         {
110             for (j = 0; (size - i) > 0; i++, j++)
111             {
112                 temp_data = *buf;
113                 write_data = (write_data) | (temp_data << 8 * j);
114                 buf ++;
115             }
116         }
117         else
118         {
119             for (j = 0; j < 8; j++, i++)
120             {
121                 temp_data = *buf;
122                 write_data = (write_data) | (temp_data << 8 * j);
123                 buf ++;
124             }
125         }
126 
127         /* write data */
128         if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, write_data) == HAL_OK)
129         {
130             /* Check the written value */
131             if (*(uint64_t*)addr != write_data)
132             {
133                 LOG_E("ERROR: write data != read data\n");
134                 result = -RT_ERROR;
135                 goto __exit;
136             }
137         }
138         else
139         {
140             result = -RT_ERROR;
141             goto __exit;
142         }
143 
144         temp_data = 0;
145         write_data = 0;
146 
147         addr += 8;
148     }
149 
150 __exit:
151     HAL_FLASH_Lock();
152     if (result != 0)
153     {
154         return result;
155     }
156 
157     return size;
158 }
159 
160 /**
161  * Erase data on flash.
162  * @note This operation is irreversible.
163  * @note This operation's units is different which on many chips.
164  *
165  * @param addr flash address
166  * @param size erase bytes size
167  *
168  * @return result
169  */
stm32_flash_erase(rt_uint32_t addr,size_t size)170 int stm32_flash_erase(rt_uint32_t addr, size_t size)
171 {
172     rt_err_t result = RT_EOK;
173     uint32_t PAGEError = 0;
174 
175     /*Variable used for Erase procedure*/
176     FLASH_EraseInitTypeDef EraseInitStruct;
177 
178     if ((addr + size) > STM32_FLASH_END_ADDRESS)
179     {
180         LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
181         return -RT_EINVAL;
182     }
183 
184     HAL_FLASH_Unlock();
185 
186     /* Fill EraseInit structure*/
187     EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
188     EraseInitStruct.Page                = GetPage(addr);
189     EraseInitStruct.NbPages     = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
190 
191     if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
192     {
193         result = -RT_ERROR;
194         goto __exit;
195     }
196 
197 __exit:
198     HAL_FLASH_Lock();
199 
200     if (result != RT_EOK)
201     {
202         return result;
203     }
204 
205     LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size);
206     return size;
207 }
208 
209 #if defined(RT_USING_FAL)
210 
211 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
212 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
213 static int fal_flash_erase(long offset, size_t size);
214 
215 const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE, FLASH_PAGE_SIZE, {NULL, fal_flash_read, fal_flash_write, fal_flash_erase} };
216 
fal_flash_read(long offset,rt_uint8_t * buf,size_t size)217 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
218 {
219     return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size);
220 }
221 
fal_flash_write(long offset,const rt_uint8_t * buf,size_t size)222 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
223 {
224     return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size);
225 }
226 
fal_flash_erase(long offset,size_t size)227 static int fal_flash_erase(long offset, size_t size)
228 {
229     return stm32_flash_erase(stm32_onchip_flash.addr + offset, size);
230 }
231 
232 #endif
233 #endif /* BSP_USING_ON_CHIP_FLASH */
234