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