4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
33 * \brief Self programming routines.
36 * \author Francesco Sacchi <batt@develer.com>
37 * \author Daniele Basile <asterix@develer.com>
39 * This module implements a kfile-like access for Atmel avr
41 * Internal flash writing access is controlled by BOOTSZ fuses, check
42 * datasheet for details.
45 #include "flash_avr.h"
49 #include <avr/pgmspace.h>
51 #include <cfg/macros.h> // MIN()
52 #include <cfg/compiler.h>
53 #include <cfg/debug.h>
58 #include <kern/kfile.h>
63 * Definition of type for avr flash module.
65 typedef uint16_t avr_page_addr_t;
66 typedef uint16_t avr_page_t;
69 * Temporary buffer cointaing data block to
72 static uint8_t page_buf[SPM_PAGESIZE];
75 * Flag for checking if current page is modified.
80 * Current buffered page.
82 static avr_page_t curr_page = 0;
85 * Private avr flush funtion.
87 * Write current buffered page in flash memory (if modified).
88 * This function erase flash memory page before writing.
90 * This function is only use internaly in this module.
92 static void flash_avr_flush(void)
96 kprintf("Flushing page %d\n", curr_page);
98 // Wait while the SPM instruction is busy.
101 kprintf("Filling temparary page buffer...");
103 // Fill the temporary buffer of the AVR
104 for (avr_page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2)
106 uint16_t word = ((uint16_t)page_buf[page_addr + 1] << 8) | page_buf[page_addr];
108 ATOMIC(boot_page_fill(page_addr, word));
114 kprintf("Erasing page, addr %u...", curr_page * SPM_PAGESIZE);
117 ATOMIC(boot_page_erase(curr_page * SPM_PAGESIZE));
119 /* Wait until the memory is erased. */
120 boot_spm_busy_wait();
123 kprintf("Writing page, addr %u...", curr_page * SPM_PAGESIZE);
125 /* Store buffer in flash page. */
126 ATOMIC(boot_page_write(curr_page * SPM_PAGESIZE));
127 boot_spm_busy_wait(); // Wait while the SPM instruction is busy.
130 * Reenable RWW-section again. We need this if we want to jump back
131 * to the application after bootloading.
133 ATOMIC(boot_rww_enable());
135 page_modified = false;
142 * Flush avr flash function.
144 * Write current buffered page in flash memory (if modified).
145 * This function erase flash memory page before writing.
147 static int flash_avr_kfileFlush(struct KFile * fd)
149 KFILE_ASSERT_GENERIC(fd);
157 * Check current page and if \a page is different, load it in
160 static void flash_avr_loadPage(avr_page_t page)
162 if (page != curr_page)
166 memcpy_P(page_buf, (const char *)(page * SPM_PAGESIZE), SPM_PAGESIZE);
168 kprintf("Loaded page %d\n", curr_page);
173 * Write program memory.
174 * Write \a size bytes from buffer \a _buf to file \a fd
175 * \note Write operations are buffered.
177 static size_t flash_avr_write(struct KFile *fd, const void *_buf, size_t size)
179 KFILE_ASSERT_GENERIC(fd);
180 const uint8_t *buf =(const uint8_t *)_buf;
183 avr_page_addr_t page_addr;
184 size_t total_write = 0;
186 ASSERT(fd->seek_pos + size <= fd->size);
187 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
189 kprintf("Writing at pos[%u]\n", fd->seek_pos);
192 page = fd->seek_pos / SPM_PAGESIZE;
193 page_addr = fd->seek_pos % SPM_PAGESIZE;
195 flash_avr_loadPage(page);
197 size_t wr_len = MIN(size, SPM_PAGESIZE - page_addr);
198 memcpy(page_buf + page_addr, buf, wr_len);
199 page_modified = true;
202 fd->seek_pos += wr_len;
204 total_write += wr_len;
206 kprintf("written %u bytes\n", total_write);
211 * Open flash file \a fd
212 * \a name and \a mode are unused, cause flash memory is
213 * threated like one file.
215 static void flash_avr_open(struct KFile *fd)
217 KFILE_ASSERT_GENERIC(fd);
219 memcpy_P(page_buf, (const char *)(curr_page * SPM_PAGESIZE), SPM_PAGESIZE);
222 fd->size = (uint16_t)(FLASHEND - CONFIG_BOOT_SIZE + 1);
223 page_modified = false;
225 kprintf("Flash file opened\n");
231 static int flash_avr_close(UNUSED_ARG(struct KFile *,fd))
233 KFILE_ASSERT_GENERIC(fd);
235 kprintf("Flash file closed\n");
242 static struct KFile *flash_avr_reopen(struct KFile *fd)
244 KFILE_ASSERT_GENERIC(fd);
252 * Read from file \a fd \a size bytes and put it in buffer \a buf
253 * \return the number of bytes read.
255 static size_t flash_avr_read(struct KFile *fd, void *buf, size_t size)
257 KFILE_ASSERT_GENERIC(fd);
258 ASSERT(fd->seek_pos + size <= fd->size);
259 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
261 kprintf("Reading at pos[%u]\n", fd->seek_pos);
262 // Flush current buffered page (if modified).
266 * AVR pointers are 16 bits wide, this hack is needed to avoid
267 * compiler warning, cause fd->seek_pos is a 32bit offset.
269 const uint8_t *pgm_addr = (const uint8_t *)0;
270 pgm_addr += fd->seek_pos;
272 memcpy_P(buf, pgm_addr, size);
273 fd->seek_pos += size;
274 kprintf("Read %u bytes\n", size);
279 * Init AVR flash read/write file.
281 void flash_avr_init(struct KFile *fd)
283 memset(fd, 0, sizeof(*fd));
284 DB(fd->_type = KFT_GENERIC);
286 // Set up flash programming functions.
287 fd->reopen = flash_avr_reopen;
288 fd->close = flash_avr_close;
289 fd->read = flash_avr_read;
290 fd->write = flash_avr_write;
291 fd->seek = kfile_genericSeek;
292 fd->flush = flash_avr_kfileFlush;