1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-05-16     shelton      first version
9  */
10 
11 #include <rtthread.h>
12 #include "drv_common.h"
13 
14 #ifdef BSP_USING_ON_CHIP_FLASH
15 #include "drv_flash.h"
16 
17 #if defined(RT_USING_FAL)
18 #include "fal.h"
19 #endif
20 
21 //#define DRV_DEBUG
22 #define LOG_TAG                "drv.flash"
23 #include <drv_log.h>
24 
25 /**
26   * @brief  gets the page of a given address
27   * @param  addr: address of the flash memory
28   * @retval the page of a given address
29   */
get_page(uint32_t addr)30 static rt_uint32_t get_page(uint32_t addr)
31 {
32     rt_uint32_t page = 0;
33 
34     page = RT_ALIGN_DOWN(addr, FLASH_PAGE_SIZE);
35 
36     return page;
37 }
38 
39 /**
40  * @brief 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  */
at32_flash_read(rt_uint32_t addr,rt_uint8_t * buf,rt_uint32_t size)49 int at32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, rt_uint32_t size)
50 {
51     rt_uint32_t i;
52 
53     if ((addr + size) > AT32_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  * @brief 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  */
at32_flash_write(rt_uint32_t addr,const rt_uint8_t * buf,rt_uint32_t size)78 int at32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, rt_uint32_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) > AT32_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     flash_unlock();
96 
97     while (addr < end_addr)
98     {
99         if (flash_word_program(addr, *((rt_uint32_t *)buf)) == FLASH_OPERATE_DONE)
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     flash_lock();
117 
118     if (result != RT_EOK)
119     {
120         return result;
121     }
122 
123     return size;
124 }
125 
126 /**
127  * @brief 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  */
at32_flash_erase(rt_uint32_t addr,rt_uint32_t size)136 int at32_flash_erase(rt_uint32_t addr, rt_uint32_t size)
137 {
138     rt_err_t result = RT_EOK;
139     rt_uint32_t end_addr = addr + size;
140     rt_uint32_t page_addr = 0;
141 
142     flash_unlock();
143 
144     if ((end_addr) > AT32_FLASH_END_ADDRESS)
145     {
146         LOG_E("erase outrange flash size! addr is (0x%p)", (void *)(addr + size));
147         return -RT_EINVAL;
148     }
149 
150     while(addr < end_addr)
151     {
152         page_addr = get_page(addr);
153 
154         if(flash_sector_erase(page_addr) != FLASH_OPERATE_DONE)
155         {
156             result = -RT_ERROR;
157             goto __exit;
158         }
159 
160         addr += FLASH_PAGE_SIZE;
161     }
162 
163     flash_lock();
164 
165 __exit:
166     if(result != RT_EOK)
167     {
168         return result;
169     }
170 
171     return size;
172 }
173 
174 #if defined(RT_USING_FAL)
175 
176 static int fal_flash_read(long offset, rt_uint8_t *buf, rt_uint32_t size);
177 static int fal_flash_write(long offset, const rt_uint8_t *buf, rt_uint32_t size);
178 static int fal_flash_erase(long offset, rt_uint32_t size);
179 
180 const struct fal_flash_dev at32_onchip_flash =
181 {
182     "onchip_flash",
183     AT32_FLASH_START_ADRESS,
184     AT32_FLASH_SIZE,
185     FLASH_PAGE_SIZE,
186     {
187         NULL,
188         fal_flash_read,
189         fal_flash_write,
190         fal_flash_erase
191     }
192 };
193 
fal_flash_read(long offset,rt_uint8_t * buf,rt_uint32_t size)194 static int fal_flash_read(long offset, rt_uint8_t *buf, rt_uint32_t size)
195 {
196     return at32_flash_read(at32_onchip_flash.addr + offset, buf, size);
197 }
198 
fal_flash_write(long offset,const rt_uint8_t * buf,rt_uint32_t size)199 static int fal_flash_write(long offset, const rt_uint8_t *buf, rt_uint32_t size)
200 {
201     return at32_flash_write(at32_onchip_flash.addr + offset, buf, size);
202 }
203 
fal_flash_erase(long offset,rt_uint32_t size)204 static int fal_flash_erase(long offset, rt_uint32_t size)
205 {
206     return at32_flash_erase(at32_onchip_flash.addr + offset, size);
207 }
208 
209 #endif
210 #endif /* BSP_USING_ON_CHIP_FLASH */
211