1 /**
2 * \file
3 *
4 * \brief Flash functionality implementation.
5 *
6 * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Subject to your compliance with these terms, you may use Microchip
13 * software and any derivatives exclusively with Microchip products.
14 * It is your responsibility to comply with third party license terms applicable
15 * to your use of third party software (including open source software) that
16 * may accompany Microchip software.
17 *
18 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
26 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29 *
30 * \asf_license_stop
31 *
32 */
33
34 #include "hal_flash.h"
35 #include <utils_assert.h>
36 #include <utils.h>
37 #include <hal_atomic.h>
38
39 /**
40 * \brief Driver version
41 */
42 #define DRIVER_VERSION 0x00000001u
43
44 static void flash_ready(struct _flash_device *device);
45 static void flash_error(struct _flash_device *device);
46
47 static int32_t flash_is_address_aligned(struct flash_descriptor *flash, const uint32_t flash_addr);
48
49 /**
50 * \brief Initialize the FLASH HAL instance and hardware for callback mode.
51 */
flash_init(struct flash_descriptor * flash,void * const hw)52 int32_t flash_init(struct flash_descriptor *flash, void *const hw)
53 {
54 int32_t rc;
55
56 ASSERT(flash && hw);
57
58 rc = _flash_init(&flash->dev, hw);
59 if (rc) {
60 return rc;
61 }
62
63 flash->dev.flash_cb.ready_cb = flash_ready;
64 flash->dev.flash_cb.error_cb = flash_error;
65
66 return ERR_NONE;
67 }
68
69 /**
70 * \brief Deinitialize the FLASH HAL instance.
71 */
flash_deinit(struct flash_descriptor * flash)72 int32_t flash_deinit(struct flash_descriptor *flash)
73 {
74 ASSERT(flash);
75
76 _flash_deinit(&flash->dev);
77
78 return ERR_NONE;
79 }
80
81 /**
82 * \brief Reads a number of bytes to a page in the internal Flash
83 */
flash_read(struct flash_descriptor * flash,uint32_t src_addr,uint8_t * buffer,uint32_t length)84 int32_t flash_read(struct flash_descriptor *flash, uint32_t src_addr, uint8_t *buffer, uint32_t length)
85 {
86 ASSERT(flash && buffer && length);
87
88 uint32_t page_size = _flash_get_page_size(&flash->dev);
89 uint32_t total_pages = _flash_get_total_pages(&flash->dev);
90
91 /* Check if the address is valid */
92 if ((src_addr > page_size * total_pages) || (src_addr + length > page_size * total_pages)) {
93 return ERR_BAD_ADDRESS;
94 }
95
96 _flash_read(&flash->dev, src_addr, buffer, length);
97
98 return ERR_NONE;
99 }
100
101 /**
102 * \brief Updates several bytes to the internal Flash
103 */
flash_write(struct flash_descriptor * flash,uint32_t dst_addr,uint8_t * buffer,uint32_t length)104 int32_t flash_write(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length)
105 {
106 ASSERT(flash && buffer && length);
107
108 uint32_t page_size = _flash_get_page_size(&flash->dev);
109 uint32_t total_pages = _flash_get_total_pages(&flash->dev);
110
111 /* Check if the address is valid */
112 if ((dst_addr > page_size * total_pages) || (dst_addr + length > page_size * total_pages)) {
113 return ERR_BAD_ADDRESS;
114 }
115
116 if (_flash_is_locked(&flash->dev, dst_addr)) {
117 return ERR_DENIED;
118 }
119
120 _flash_write(&flash->dev, dst_addr, buffer, length);
121
122 return ERR_NONE;
123 }
124
125 /**
126 * \brief Appends a number of bytes to a page in the internal Flash
127 */
flash_append(struct flash_descriptor * flash,uint32_t dst_addr,uint8_t * buffer,uint32_t length)128 int32_t flash_append(struct flash_descriptor *flash, uint32_t dst_addr, uint8_t *buffer, uint32_t length)
129 {
130 ASSERT(flash && buffer && length);
131
132 uint32_t page_size = _flash_get_page_size(&flash->dev);
133 uint32_t total_pages = _flash_get_total_pages(&flash->dev);
134
135 /* Check if the address is valid */
136 if ((dst_addr > page_size * total_pages) || (dst_addr + length > page_size * total_pages)) {
137 return ERR_BAD_ADDRESS;
138 }
139
140 if (_flash_is_locked(&flash->dev, dst_addr)) {
141 return ERR_DENIED;
142 }
143
144 _flash_append(&flash->dev, dst_addr, buffer, length);
145
146 return ERR_NONE;
147 }
148
149 /**
150 * \brief Execute erase in the internal flash
151 */
flash_erase(struct flash_descriptor * flash,const uint32_t dst_addr,const uint32_t page_nums)152 int32_t flash_erase(struct flash_descriptor *flash, const uint32_t dst_addr, const uint32_t page_nums)
153 {
154 ASSERT(flash && page_nums);
155 uint32_t page_size = _flash_get_page_size(&flash->dev);
156 uint32_t total_pages = _flash_get_total_pages(&flash->dev);
157 int32_t rc;
158
159 rc = flash_is_address_aligned(flash, dst_addr);
160 if (rc) {
161 return rc;
162 }
163
164 if ((page_nums > total_pages) || (dst_addr / page_size + page_nums > total_pages)) {
165 return ERR_INVALID_ARG;
166 }
167
168 _flash_erase(&flash->dev, dst_addr, page_nums);
169
170 return ERR_NONE;
171 }
172
173 /**
174 * \brief Register a function as FLASH transfer completion callback
175 */
flash_register_callback(struct flash_descriptor * flash,const enum flash_cb_type type,flash_cb_t func)176 int32_t flash_register_callback(struct flash_descriptor *flash, const enum flash_cb_type type, flash_cb_t func)
177 {
178 ASSERT(flash);
179
180 switch (type) {
181 case FLASH_CB_READY:
182 flash->callbacks.cb_ready = func;
183 break;
184
185 case FLASH_CB_ERROR:
186 flash->callbacks.cb_error = func;
187 break;
188
189 default:
190 return ERR_INVALID_ARG;
191 }
192
193 _flash_set_irq_state(&flash->dev, (enum _flash_cb_type)type, NULL != func);
194
195 return ERR_NONE;
196 }
197
198 /**
199 * \brief Execute lock in the internal flash
200 */
flash_lock(struct flash_descriptor * flash,const uint32_t dst_addr,const uint32_t page_nums)201 int32_t flash_lock(struct flash_descriptor *flash, const uint32_t dst_addr, const uint32_t page_nums)
202 {
203 ASSERT(flash && page_nums);
204 uint32_t page_size = _flash_get_page_size(&flash->dev);
205 uint32_t total_pages = _flash_get_total_pages(&flash->dev);
206 int32_t rc;
207
208 rc = flash_is_address_aligned(flash, dst_addr);
209 if (rc) {
210 return rc;
211 }
212
213 if ((page_nums > total_pages) || (dst_addr / page_size + page_nums > total_pages)) {
214 return ERR_INVALID_ARG;
215 }
216
217 return _flash_lock(&flash->dev, dst_addr, page_nums);
218 }
219
220 /**
221 * \brief Execute unlock in the internal flash
222 */
flash_unlock(struct flash_descriptor * flash,const uint32_t dst_addr,const uint32_t page_nums)223 int32_t flash_unlock(struct flash_descriptor *flash, const uint32_t dst_addr, const uint32_t page_nums)
224 {
225 ASSERT(flash && page_nums);
226 uint32_t page_size = _flash_get_page_size(&flash->dev);
227 uint32_t total_pages = _flash_get_total_pages(&flash->dev);
228 int32_t rc;
229
230 rc = flash_is_address_aligned(flash, dst_addr);
231 if (rc) {
232 return rc;
233 }
234
235 if ((page_nums > total_pages) || (dst_addr / page_size + page_nums > total_pages)) {
236 return ERR_INVALID_ARG;
237 }
238
239 return _flash_unlock(&flash->dev, dst_addr, page_nums);
240 }
241
242 /**
243 * \brief Get the flash page size.
244 */
flash_get_page_size(struct flash_descriptor * flash)245 uint32_t flash_get_page_size(struct flash_descriptor *flash)
246 {
247 ASSERT(flash);
248 return _flash_get_page_size(&flash->dev);
249 }
250
251 /**
252 * \brief Get the numbers of flash page.
253 */
flash_get_total_pages(struct flash_descriptor * flash)254 uint32_t flash_get_total_pages(struct flash_descriptor *flash)
255 {
256 ASSERT(flash);
257 return _flash_get_total_pages(&flash->dev);
258 }
259
260 /**
261 * \brief Retrieve the current driver version
262 */
flash_get_version(void)263 uint32_t flash_get_version(void)
264 {
265 return DRIVER_VERSION;
266 }
267
268 /**
269 * \internal check the address whether it is aligned
270 * \param[in, out] flash Pointer to the HAL FLASH instance.
271 * \param[in] flash_addr address to be check in flash
272 *
273 * \return whether it is valid
274 * \retval 0 Valid.
275 * \retval -1 Error, invalid.
276 */
flash_is_address_aligned(struct flash_descriptor * flash,const uint32_t flash_addr)277 static int32_t flash_is_address_aligned(struct flash_descriptor *flash, const uint32_t flash_addr)
278 {
279 ASSERT(flash);
280
281 uint32_t page_size = _flash_get_page_size(&flash->dev);
282
283 /* Check if the read address not aligned to the start of a page */
284 if (flash_addr & (page_size - 1)) {
285 return ERR_BAD_ADDRESS;
286 }
287 return ERR_NONE;
288 }
289
290 /**
291 * \internal Ready for a new flash command
292 *
293 * \param[in] device The pointer to flash device structure
294 */
flash_ready(struct _flash_device * device)295 static void flash_ready(struct _flash_device *device)
296 {
297 struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev);
298 if (descr->callbacks.cb_ready) {
299 descr->callbacks.cb_ready(descr);
300 }
301 }
302
303 /**
304 * \internal Error occurs in flash command
305 *
306 * \param[in] device The pointer to flash device structure
307 */
flash_error(struct _flash_device * device)308 static void flash_error(struct _flash_device *device)
309 {
310 struct flash_descriptor *const descr = CONTAINER_OF(device, struct flash_descriptor, dev);
311 if (descr->callbacks.cb_error) {
312 descr->callbacks.cb_error(descr);
313 }
314 }
315