Refactor code, moved page_modified variable in KFileFlashAvr struct
[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
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
79
80
81 /*
82  * Private avr flush funtion.
83  *
84  * Write current buffered page in flash memory (if modified).
85  * This function erase flash memory page before writing.
86  *
87  * This function is only use internaly in this module.
88  */
89 static void flash_avr_flush(KFileFlashAvr *fd)
90 {
91         if (fd->page_modified)
92         {
93                 kprintf("Flushing page %d\n", fd->curr_page);
94
95                 // Wait while the SPM instruction is busy.
96                 boot_spm_busy_wait();
97
98                 kprintf("Filling temparary page buffer...");
99
100                 // Fill the temporary buffer of the AVR
101                 for (avr_page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2)
102                 {
103                         uint16_t word = ((uint16_t)page_buf[page_addr + 1] << 8) | page_buf[page_addr];
104
105                         ATOMIC(boot_page_fill(page_addr, word));
106                 }
107                 kprintf("Done.\n");
108
109                 wdt_reset();
110
111                 kprintf("Erasing page, addr %u...", fd->curr_page * SPM_PAGESIZE);
112
113                 /* Page erase */
114                 ATOMIC(boot_page_erase(fd->curr_page * SPM_PAGESIZE));
115
116                 /* Wait until the memory is erased. */
117                 boot_spm_busy_wait();
118
119                 kprintf("Done.\n");
120                 kprintf("Writing page, addr %u...", fd->curr_page * SPM_PAGESIZE);
121
122                 /* Store buffer in flash page. */
123                 ATOMIC(boot_page_write(fd->curr_page * SPM_PAGESIZE));
124                 boot_spm_busy_wait();  // Wait while the SPM instruction is busy.
125
126                 /*
127                 * Reenable RWW-section again. We need this if we want to jump back
128                 * to the application after bootloading.
129                 */
130                 ATOMIC(boot_rww_enable());
131
132                 fd->page_modified = false;
133                 kprintf("Done.\n");
134         }
135 }
136
137
138 /**
139  * Flush avr flash function.
140  *
141  * Write current buffered page in flash memory (if modified).
142  * This function erase flash memory page before writing.
143  */
144 static int flash_avr_kfileFlush(struct KFile *_fd)
145 {
146         KFileFlashAvr *fd = KFILEFLASHAVR(_fd);
147         flash_avr_flush(fd);
148         return 0;
149 }
150
151
152 /**
153  * Check current page and if \a page is different, load it in
154  * temporary buffer.
155  */
156 static void flash_avr_loadPage(KFileFlashAvr *fd, avr_page_t page)
157 {
158         if (page != fd->curr_page)
159         {
160                 flash_avr_flush(fd);
161                 // Load page
162                 memcpy_P(page_buf, (const char *)(page * SPM_PAGESIZE), SPM_PAGESIZE);
163                 fd->curr_page = page;
164                 kprintf("Loaded page %d\n", fd->curr_page);
165         }
166 }
167
168 /**
169  * Write program memory.
170  * Write \a size bytes from buffer \a _buf to file \a fd
171  * \note Write operations are buffered.
172  */
173 static size_t flash_avr_write(struct KFile *_fd, const void *_buf, size_t size)
174 {
175         KFileFlashAvr *fd = KFILEFLASHAVR(_fd);
176         const uint8_t *buf =(const uint8_t *)_buf;
177
178         avr_page_t page;
179         avr_page_addr_t page_addr;
180         size_t total_write = 0;
181
182
183         ASSERT(fd->fd.seek_pos + (kfile_off_t)size <= (kfile_off_t)fd->fd.size);
184         size = MIN((uint32_t)size, fd->fd.size - fd->fd.seek_pos);
185
186         kprintf("Writing at pos[%u]\n", fd->fd.seek_pos);
187         while (size)
188         {
189                 page = fd->fd.seek_pos / SPM_PAGESIZE;
190                 page_addr = fd->fd.seek_pos % SPM_PAGESIZE;
191
192                 flash_avr_loadPage(fd, page);
193
194                 size_t wr_len = MIN(size, SPM_PAGESIZE - page_addr);
195                 memcpy(page_buf + page_addr, buf, wr_len);
196                 fd->page_modified = true;
197
198                 buf += wr_len;
199                 fd->fd.seek_pos += wr_len;
200                 size -= wr_len;
201                 total_write += wr_len;
202         }
203         kprintf("written %u bytes\n", total_write);
204         return total_write;
205 }
206
207 /**
208  * Open flash file \a fd
209  * \a name and \a mode are unused, cause flash memory is
210  * threated like one file.
211  */
212 static void flash_avr_open(struct KFileFlashAvr *fd)
213 {
214         fd->curr_page = 0;
215         memcpy_P(page_buf, (const char *)(fd->curr_page * SPM_PAGESIZE), SPM_PAGESIZE);
216
217         fd->fd.seek_pos = 0;
218         fd->fd.size = (uint16_t)(FLASHEND - CONFIG_FLASH_AVR_BOOTSIZE + 1);
219         fd->page_modified = false;
220
221         kprintf("Flash file opened\n");
222 }
223
224 /**
225  * Close file \a fd
226  */
227 static int flash_avr_close(struct KFile *_fd)
228 {
229         KFileFlashAvr *fd = KFILEFLASHAVR(_fd);
230         flash_avr_flush(fd);
231         kprintf("Flash file closed\n");
232         return 0;
233 }
234
235 /**
236  * Reopen file \a fd
237  */
238 static struct KFile *flash_avr_reopen(struct KFile *fd)
239 {
240         KFileFlashAvr *_fd = KFILEFLASHAVR(fd);
241         flash_avr_close(fd);
242         flash_avr_open(_fd);
243         return fd;
244 }
245
246
247 /**
248  * Read from file \a fd \a size bytes and put it in buffer \a buf
249  * \return the number of bytes read.
250  */
251 static size_t flash_avr_read(struct KFile *_fd, void *buf, size_t size)
252 {
253         KFileFlashAvr *fd = KFILEFLASHAVR(_fd);
254         ASSERT(fd->fd.seek_pos + (kfile_off_t)size <= (kfile_off_t)fd->fd.size);
255         size = MIN((uint32_t)size, fd->fd.size - fd->fd.seek_pos);
256
257         kprintf("Reading at pos[%u]\n", fd->fd.seek_pos);
258         // Flush current buffered page (if modified).
259         flash_avr_flush(fd);
260
261         /*
262          * AVR pointers are 16 bits wide, this hack is needed to avoid
263          * compiler warning, cause fd->seek_pos is a 32bit offset.
264          */
265         const uint8_t *pgm_addr = (const uint8_t *)0;
266         pgm_addr += fd->fd.seek_pos;
267
268         memcpy_P(buf, pgm_addr, size);
269         fd->fd.seek_pos += size;
270         kprintf("Read %u bytes\n", size);
271         return size;
272 }
273
274 /**
275  * Init AVR flash read/write file.
276  */
277 void flash_avr_init(struct KFileFlashAvr *fd)
278 {
279         memset(fd, 0, sizeof(*fd));
280         DB(fd->fd._type = KFT_FLASHAVR);
281
282         // Set up flash programming functions.
283         fd->fd.reopen = flash_avr_reopen;
284         fd->fd.close = flash_avr_close;
285         fd->fd.read = flash_avr_read;
286         fd->fd.write = flash_avr_write;
287         fd->fd.seek = kfile_genericSeek;
288         fd->fd.flush = flash_avr_kfileFlush;
289         fd->curr_page = 0;
290
291         flash_avr_open(fd);
292 }
293
294