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-06-27 NU-LL 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 */
stm32_flash_write(rt_uint32_t addr,const uint8_t * buf,size_t size)78 int stm32_flash_write(rt_uint32_t addr, const uint8_t *buf, size_t size)
79 {
80 size_t i, j;
81 rt_err_t result = 0;
82 rt_uint64_t write_data = 0, temp_data = 0;
83
84 if ((addr + size) > STM32_FLASH_END_ADDRESS)
85 {
86 LOG_E("ERROR: write outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
87 return -RT_EINVAL;
88 }
89
90 if(addr % 8 != 0)
91 {
92 LOG_E("write addr must be 8-byte alignment");
93 return -RT_EINVAL;
94 }
95
96 HAL_FLASH_Unlock();
97
98 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);
99
100 if (size < 1)
101 {
102 return -RT_ERROR;
103 }
104
105 for (i = 0; i < size;)
106 {
107 if ((size - i) < 8)
108 {
109 for (j = 0; (size - i) > 0; i++, j++)
110 {
111 temp_data = *buf;
112 write_data = (write_data) | (temp_data << 8 * j);
113 buf ++;
114 }
115 }
116 else
117 {
118 for (j = 0; j < 8; j++, i++)
119 {
120 temp_data = *buf;
121 write_data = (write_data) | (temp_data << 8 * j);
122 buf ++;
123 }
124 }
125
126 /* write data */
127 if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, write_data) == HAL_OK)
128 {
129 /* Check the written value */
130 if (*(uint64_t*)addr != write_data)
131 {
132 LOG_E("ERROR: write data != read data\n");
133 result = -RT_ERROR;
134 goto __exit;
135 }
136 }
137 else
138 {
139 result = -RT_ERROR;
140 goto __exit;
141 }
142
143 temp_data = 0;
144 write_data = 0;
145
146 addr += 8;
147 }
148
149 __exit:
150 HAL_FLASH_Lock();
151 if (result != 0)
152 {
153 return result;
154 }
155
156 return size;
157 }
158
159 /**
160 * Erase data on flash.
161 * @note This operation is irreversible.
162 * @note This operation's units is different which on many chips.
163 *
164 * @param addr flash address
165 * @param size erase bytes size
166 *
167 * @return result
168 */
stm32_flash_erase(rt_uint32_t addr,size_t size)169 int stm32_flash_erase(rt_uint32_t addr, size_t size)
170 {
171 rt_err_t result = RT_EOK;
172 uint32_t PAGEError = 0;
173
174 /*Variable used for Erase procedure*/
175 FLASH_EraseInitTypeDef EraseInitStruct;
176
177 if ((addr + size) > STM32_FLASH_END_ADDRESS)
178 {
179 LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
180 return -RT_EINVAL;
181 }
182
183 HAL_FLASH_Unlock();
184
185 /* Fill EraseInit structure*/
186 EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
187 EraseInitStruct.Page = GetPage(addr);
188 EraseInitStruct.NbPages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
189
190 if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
191 {
192 result = -RT_ERROR;
193 goto __exit;
194 }
195
196 __exit:
197 HAL_FLASH_Lock();
198
199 if (result != RT_EOK)
200 {
201 return result;
202 }
203
204 LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
205 return size;
206 }
207
208 #if defined(RT_USING_FAL)
209
210 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
211 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
212 static int fal_flash_erase(long offset, size_t size);
213
214 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} };
215
fal_flash_read(long offset,rt_uint8_t * buf,size_t size)216 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
217 {
218 return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size);
219 }
220
fal_flash_write(long offset,const rt_uint8_t * buf,size_t size)221 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
222 {
223 return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size);
224 }
225
fal_flash_erase(long offset,size_t size)226 static int fal_flash_erase(long offset, size_t size)
227 {
228 return stm32_flash_erase(stm32_onchip_flash.addr + offset, size);
229 }
230
231 #endif
232 #endif /* BSP_USING_ON_CHIP_FLASH */
233