Updated flash_avr module to support KFileFlashAvr structure
[bertos.git] / bertos / cpu / avr / drv / flash_avr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Self programming routines.
34  *
35  * \version $Id$
36  * \author Francesco Sacchi <batt@develer.com>
37  * \author Daniele Basile <asterix@develer.com>
38  *
39  * This module implements a kfile-like access for Atmel avr
40  * internal flash.
41  * Internal flash writing access is controlled by BOOTSZ fuses, check
42  * datasheet for details.
43  */
44
45 #include "flash_avr.h"
46
47 #include "cfg/cfg_flash_avr.h"
48 #include <cfg/macros.h> // MIN()
49 #include <cfg/compiler.h>
50 #include <cfg/debug.h>
51 #include <cpu/irq.h>
52
53 #include <drv/wdt.h>
54
55 #include <kern/kfile.h>
56
57 #include <avr/io.h>
58 #include <avr/boot.h>
59 #include <avr/pgmspace.h>
60
61 #include <string.h>
62
63
64
65 /**
66  * Definition of type for avr flash module.
67  */
68 typedef uint16_t avr_page_addr_t;
69 typedef uint16_t avr_page_t;
70
71 /**
72  * Temporary buffer cointaing data block to
73  * write on flash.
74  */
75 static uint8_t page_buf[SPM_PAGESIZE];
76
77 /**
78  * Flag for checking if current page is modified.
79  */
80 bool page_modified;
81
82 /**
83  * Current buffered page.
84  */
85 static avr_page_t curr_page = 0;
86
87 /*
88  * Private avr flush funtion.
89  *
90  * Write current buffered page in flash memory (if modified).
91  * This function erase flash memory page before writing.
92  *
93  * This function is only use internaly in this module.
94  */
95 static void flash_avr_flush(void)
96 {
97         if (page_modified)
98         {
99                 kprintf("Flushing page %d\n", curr_page);
100
101                 // Wait while the SPM instruction is busy.
102                 boot_spm_busy_wait();
103
104                 kprintf("Filling temparary page buffer...");
105
106                 // Fill the temporary buffer of the AVR
107                 for (avr_page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2)
108                 {
109                         uint16_t word = ((uint16_t)page_buf[page_addr + 1] << 8) | page_buf[page_addr];
110
111                         ATOMIC(boot_page_fill(page_addr, word));
112                 }
113                 kprintf("Done.\n");
114
115                 wdt_reset();
116
117                 kprintf("Erasing page, addr %u...", curr_page * SPM_PAGESIZE);
118
119                 /* Page erase */
120                 ATOMIC(boot_page_erase(curr_page * SPM_PAGESIZE));
121
122                 /* Wait until the memory is erased. */
123                 boot_spm_busy_wait();
124
125                 kprintf("Done.\n");
126                 kprintf("Writing page, addr %u...", curr_page * SPM_PAGESIZE);
127
128                 /* Store buffer in flash page. */
129                 ATOMIC(boot_page_write(curr_page * SPM_PAGESIZE));
130                 boot_spm_busy_wait();  // Wait while the SPM instruction is busy.
131
132                 /*
133                 * Reenable RWW-section again. We need this if we want to jump back
134                 * to the application after bootloading.
135                 */
136                 ATOMIC(boot_rww_enable());
137
138                 page_modified = false;
139                 kprintf("Done.\n");
140         }
141 }
142
143
144 /**
145  * Flush avr flash function.
146  *
147  * Write current buffered page in flash memory (if modified).
148  * This function erase flash memory page before writing.
149  */
150 static int flash_avr_kfileFlush(UNUSED_ARG(struct KFile *,fd))
151 {
152
153         KFILEFLASHAVR(fd);
154         flash_avr_flush();
155         return 0;
156 }
157
158
159 /**
160  * Check current page and if \a page is different, load it in
161  * temporary buffer.
162  */
163 static void flash_avr_loadPage(avr_page_t page)
164 {
165         if (page != curr_page)
166         {
167                 flash_avr_flush();
168                 // Load page
169                 memcpy_P(page_buf, (const char *)(page * SPM_PAGESIZE), SPM_PAGESIZE);
170                 curr_page = page;
171                 kprintf("Loaded page %d\n", curr_page);
172         }
173 }
174
175 /**
176  * Write program memory.
177  * Write \a size bytes from buffer \a _buf to file \a fd
178  * \note Write operations are buffered.
179  */
180 static size_t flash_avr_write(struct KFile *fd, const void *_buf, size_t size)
181 {
182         KFILEFLASHAVR(fd);
183         const uint8_t *buf =(const uint8_t *)_buf;
184
185         avr_page_t page;
186         avr_page_addr_t page_addr;
187         size_t total_write = 0;
188
189
190         ASSERT(fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)fd->size);
191         size = MIN((uint32_t)size, fd->size - fd->seek_pos);
192
193         kprintf("Writing at pos[%u]\n", fd->seek_pos);
194         while (size)
195         {
196                 page = fd->seek_pos / SPM_PAGESIZE;
197                 page_addr = fd->seek_pos % SPM_PAGESIZE;
198
199                 flash_avr_loadPage(page);
200
201                 size_t wr_len = MIN(size, SPM_PAGESIZE - page_addr);
202                 memcpy(page_buf + page_addr, buf, wr_len);
203                 page_modified = true;
204
205                 buf += wr_len;
206                 fd->seek_pos += wr_len;
207                 size -= wr_len;
208                 total_write += wr_len;
209         }
210         kprintf("written %u bytes\n", total_write);
211         return total_write;
212 }
213
214 /**
215  * Open flash file \a fd
216  * \a name and \a mode are unused, cause flash memory is
217  * threated like one file.
218  */
219 static void flash_avr_open(struct KFileFlashAvr *fd)
220 {
221         curr_page = 0;
222         memcpy_P(page_buf, (const char *)(curr_page * SPM_PAGESIZE), SPM_PAGESIZE);
223
224         fd->fd.seek_pos = 0;
225         fd->fd.size = (uint16_t)(FLASHEND - CONFIG_FLASH_AVR_BOOTSIZE + 1);
226         page_modified = false;
227
228         kprintf("Flash file opened\n");
229 }
230
231 /**
232  * Close file \a fd
233  */
234 static int flash_avr_close(UNUSED_ARG(struct KFile *,fd))
235 {
236         KFILEFLASHAVR(fd);
237         flash_avr_flush();
238         kprintf("Flash file closed\n");
239         return 0;
240 }
241
242 /**
243  * Reopen file \a fd
244  */
245 static struct KFile *flash_avr_reopen(struct KFile *fd)
246 {
247         KFileFlashAvr *_fd = KFILEFLASHAVR(fd);
248         flash_avr_close(fd);
249         flash_avr_open(_fd);
250         return fd;
251 }
252
253
254 /**
255  * Read from file \a fd \a size bytes and put it in buffer \a buf
256  * \return the number of bytes read.
257  */
258 static size_t flash_avr_read(struct KFile *fd, void *buf, size_t size)
259 {
260         KFILEFLASHAVR(fd);
261         ASSERT(fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)fd->size);
262         size = MIN((uint32_t)size, fd->size - fd->seek_pos);
263
264         kprintf("Reading at pos[%u]\n", fd->seek_pos);
265         // Flush current buffered page (if modified).
266         flash_avr_flush();
267
268         /*
269          * AVR pointers are 16 bits wide, this hack is needed to avoid
270          * compiler warning, cause fd->seek_pos is a 32bit offset.
271          */
272         const uint8_t *pgm_addr = (const uint8_t *)0;
273         pgm_addr += fd->seek_pos;
274
275         memcpy_P(buf, pgm_addr, size);
276         fd->seek_pos += size;
277         kprintf("Read %u bytes\n", size);
278         return size;
279 }
280
281 /**
282  * Init AVR flash read/write file.
283  */
284 void flash_avr_init(struct KFileFlashAvr *fd)
285 {
286         memset(fd, 0, sizeof(*fd));
287         DB(fd->fd._type = KFT_FLASHAVR);
288
289         // Set up flash programming functions.
290         fd->fd.reopen = flash_avr_reopen;
291         fd->fd.close = flash_avr_close;
292         fd->fd.read = flash_avr_read;
293         fd->fd.write = flash_avr_write;
294         fd->fd.seek = kfile_genericSeek;
295         fd->fd.flush = flash_avr_kfileFlush;
296
297         flash_avr_open(fd);
298 }
299
300