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-22     zylx         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, 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 rt_uint8_t * buf,size_t size)78 int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
79 {
80     rt_err_t result        = RT_EOK;
81     rt_uint32_t end_addr   = addr + size;
82 
83     if (addr % 4 != 0)
84     {
85         LOG_E("write addr must be 4-byte alignment");
86         return -RT_EINVAL;
87     }
88 
89     if ((end_addr) > STM32_FLASH_END_ADDRESS)
90     {
91         LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
92         return -RT_EINVAL;
93     }
94 
95     HAL_FLASH_Unlock();
96 
97     while (addr < end_addr)
98     {
99         if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, *((rt_uint32_t *)buf)) == HAL_OK)
100         {
101             if (*(rt_uint32_t *)addr != *(rt_uint32_t *)buf)
102             {
103                 result = -RT_ERROR;
104                 break;
105             }
106             addr += 4;
107             buf  += 4;
108         }
109         else
110         {
111             result = -RT_ERROR;
112             break;
113         }
114     }
115 
116     HAL_FLASH_Lock();
117 
118     if (result != RT_EOK)
119     {
120         return result;
121     }
122 
123     return size;
124 }
125 
126 /**
127  * Erase data on flash.
128  * @note This operation is irreversible.
129  * @note This operation's units is different which on many chips.
130  *
131  * @param addr flash address
132  * @param size erase bytes size
133  *
134  * @return result
135  */
stm32_flash_erase(rt_uint32_t addr,size_t size)136 int stm32_flash_erase(rt_uint32_t addr, size_t size)
137 {
138     rt_err_t result = RT_EOK;
139     uint32_t PAGEError = 0;
140 
141     /*Variable used for Erase procedure*/
142     FLASH_EraseInitTypeDef EraseInitStruct;
143 
144     if ((addr + size) > STM32_FLASH_END_ADDRESS)
145     {
146         LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
147         return -RT_EINVAL;
148     }
149 
150     HAL_FLASH_Unlock();
151 
152     /* Fill EraseInit structure*/
153     EraseInitStruct.TypeErase   = FLASH_TYPEERASE_PAGES;
154     EraseInitStruct.PageAddress = GetPage(addr);
155     EraseInitStruct.NbPages     = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
156 
157     if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
158     {
159         result = -RT_ERROR;
160         goto __exit;
161     }
162 
163 __exit:
164     HAL_FLASH_Lock();
165 
166     if (result != RT_EOK)
167     {
168         return result;
169     }
170 
171     LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
172     return size;
173 }
174 
175 #if defined(RT_USING_FAL)
176 
177 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
178 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
179 static int fal_flash_erase(long offset, size_t size);
180 
181 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} };
182 
fal_flash_read(long offset,rt_uint8_t * buf,size_t size)183 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
184 {
185     return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size);
186 }
187 
fal_flash_write(long offset,const rt_uint8_t * buf,size_t size)188 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
189 {
190     return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size);
191 }
192 
fal_flash_erase(long offset,size_t size)193 static int fal_flash_erase(long offset, size_t size)
194 {
195     return stm32_flash_erase(stm32_onchip_flash.addr + offset, size);
196 }
197 
198 #endif
199 #endif /* BSP_USING_ON_CHIP_FLASH */
200