4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
8 * \brief Self programming routines.
11 * \author Francesco Sacchi <batt@develer.com>
12 * \author Daniele Basile <asterix@develer.com>
14 * This module implements a kfile-like access for Atmel avr
16 * Internal flash writing access is controlled by BOOTSZ fuses, check
17 * datasheet for details.
20 #include "flash_avr.h"
25 #include <cfg/macros.h> // MIN()
26 #include <cfg/compiler.h>
27 #include <cfg/debug.h>
31 #include <avr/pgmspace.h>
33 typedef uint16_t avr_page_addr_t;
34 typedef uint16_t avr_page_t;
37 * Temporary buffer cointaing data block to
40 static uint8_t page_buf[SPM_PAGESIZE];
42 bool page_modified; /// Flag for checking if current page is modified.
45 * Current buffered page.
47 static avr_page_t curr_page = 0;
50 * Write current buffered page in flash memory (if modified).
51 * This function erase flash memory page before writing.
53 static void avrflash_flush(void)
57 kprintf("Flushing page %d\n", curr_page);
59 boot_spm_busy_wait(); // Wait while the SPM instruction is busy.
61 kprintf("Filling temparary page buffer...");
62 /* Fill the temporary buffer of the AVR */
63 for (avr_page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2)
65 uint16_t word = ((uint16_t)page_buf[page_addr + 1] << 8) | page_buf[page_addr];
67 ATOMIC(boot_page_fill(page_addr, word));
73 kprintf("Erasing page, addr %d...", curr_page * SPM_PAGESIZE);
76 ATOMIC(boot_page_erase(curr_page * SPM_PAGESIZE));
78 /* Wait until the memory is erased. */
82 kprintf("Writing page, addr %d...", curr_page * SPM_PAGESIZE);
84 /* Store buffer in flash page. */
85 ATOMIC(boot_page_write(curr_page * SPM_PAGESIZE));
86 boot_spm_busy_wait(); // Wait while the SPM instruction is busy.
89 * Reenable RWW-section again. We need this if we want to jump back
90 * to the application after bootloading.
92 ATOMIC(boot_rww_enable());
94 page_modified = false;
101 * Check current page and if \a page is different, load it in
104 static void avrflash_loadPage(avr_page_t page)
106 if (page != curr_page)
110 memcpy_P(page_buf, (const char *)(page * SPM_PAGESIZE), SPM_PAGESIZE);
112 kprintf("Loaded page %d\n", curr_page);
117 * Write program memory.
118 * Write \a size bytes from buffer \a _buf to file \a *fd
119 * \note Write operations are buffered.
121 static size_t avrflash_write(struct _KFile *fd, const void *_buf, size_t size)
123 const uint8_t *buf =(const uint8_t *)_buf;
126 avr_page_addr_t page_addr;
127 size_t total_write = 0;
129 ASSERT(fd->seek_pos + size <= fd->size);
130 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
132 kprintf("Writing at pos[%d]\n", fd->seek_pos);
135 page = fd->seek_pos / SPM_PAGESIZE;
136 page_addr = fd->seek_pos % SPM_PAGESIZE;
138 avrflash_loadPage(page);
140 size_t wr_len = MIN(size, SPM_PAGESIZE - page_addr);
141 memcpy(page_buf + page_addr, buf, wr_len);
142 page_modified = true;
145 fd->seek_pos += wr_len;
147 total_write += wr_len;
149 kprintf("written %d bytes\n", total_write);
154 * Open flash file \a *fd
155 * \a name and \a mode are unused, cause flash memory is
156 * threated like one file.
158 static bool avrflash_open(struct _KFile *fd, UNUSED_ARG(const char *, name), UNUSED_ARG(int, mode))
161 memcpy_P(page_buf, (const char *)(curr_page * SPM_PAGESIZE), SPM_PAGESIZE);
164 fd->size = (uint16_t)(FLASHEND - CONFIG_BOOT_SIZE + 1);
165 page_modified = false;
167 kprintf("Flash file opened\n");
174 static bool avrflash_close(UNUSED_ARG(struct _KFile *,fd))
177 kprintf("Flash file closed\n");
182 * Move \a *fd file seek position of \a offset bytes
183 * from current position.
185 static bool avrflash_seek(struct _KFile *fd, int32_t offset)
187 ASSERT(fd->seek_pos + offset <= fd->size);
190 if (fd->seek_pos + offset > fd->size)
193 fd->seek_pos += offset;
194 kprintf("Flash seek to [%d]\n", fd->seek_pos);
200 * Read from file \a *fd \a size bytes and put it in buffer \a *buf
201 * \return the number of bytes read.
203 static size_t avrflash_read(struct _KFile *fd, void *buf, size_t size)
205 ASSERT(fd->seek_pos + size <= fd->size);
206 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
208 kprintf("Reading at pos[%d]\n", fd->seek_pos);
209 // Flush current buffered page (if modified).
213 * AVR pointers are 16 bits wide, this hack is needed to avoid
214 * compiler warning, cause fd->seek_pos is a 32bit offset.
216 const uint8_t *pgm_addr = (const uint8_t *)0;
217 pgm_addr += fd->seek_pos;
219 memcpy_P(buf, pgm_addr, size);
220 fd->seek_pos += size;
221 kprintf("Read %d bytes\n", size);
226 * Init AVR flash read/write file.
228 void avrflash_init(struct _KFile *fd)
230 // Set up flash programming functions.
231 fd.open = avrflash_open;
232 fd.close = avrflash_close;
233 fd.read = avrflash_read;
234 fd.write = avrflash_write;
235 fd.seek = avrflash_seek;
240 #define TEST_SIZE 683
241 #define ONE_BYTE_TEST_ADDRESS 347
243 uint8_t test_buf[TEST_SIZE];
244 uint8_t save_buf[TEST_SIZE];
247 * Program memory read/write subtest.
248 * Try to write/read in the same \param f file location \param _size bytes.
249 * \return true if all is ok, false otherwise
250 * \note Restore file position at exit (if no error)
251 * \note Test buffer \param buf must be filled with
252 * the following statement:
257 static bool avrflash_rwTest(KFile *f, uint8_t *buf, size_t _size)
259 int32_t size = _size;
261 if (f->write(f, buf, size) != size)
266 memset(buf, 0, size);
268 // Read flash in test buffer
269 if (f->read(f, buf, size) != size)
274 for (size_t i = 0; i < size; i++)
275 if (buf[i] != (i & 0xff))
282 * Test for program memory read/write.
284 bool avrflash_test(void)
288 // Set up flash programming functions.
291 // Fill in test buffer
292 for (int i = 0; i < TEST_SIZE; i++)
293 test_buf[i] = (i & 0xff);
296 fd.open(&fd, NULL, 0);
297 // Save flash content (for later restore).
298 fd.read(&fd, save_buf, sizeof(save_buf));
299 fd.seek(&fd, -TEST_SIZE);
301 // Test flash read/write to address 0..TEST_SIZE
302 if (!avrflash_rwTest(&fd, test_buf, TEST_SIZE))
303 goto avrflash_test_end;
305 // One byte read/write test
306 fd.seek(&fd, ONE_BYTE_TEST_ADDRESS); // Random address
307 if (!avrflash_rwTest(&fd, test_buf, 1))
308 goto avrflash_test_end;
309 fd.seek(&fd, -(int32_t)ONE_BYTE_TEST_ADDRESS);
311 // Restore old flash data
312 if (fd.write(&fd, save_buf, sizeof(test_buf)) != TEST_SIZE)
313 goto avrflash_test_end;
314 fd.seek(&fd, -TEST_SIZE);
316 // Go to the Flash end
317 fd.seek(&fd, fd.size - TEST_SIZE);
318 // Save flash content (for later restore).
319 fd.read(&fd, save_buf, sizeof(save_buf));
320 fd.seek(&fd, -TEST_SIZE);
322 // Test flash read/write to address (FLASHEND - TEST_SIZE) ... FLASHEND
323 if (!avrflash_rwTest(&fd, test_buf, TEST_SIZE))
324 goto avrflash_test_end;
326 // Go to half test size.
327 fd.seek(&fd, (TEST_SIZE / 2));
329 // This test should FAIL, cause we try to write over file end.
330 if (avrflash_rwTest(&fd, test_buf, TEST_SIZE))
331 goto avrflash_test_end;
333 fd.seek(&fd, -TEST_SIZE);
334 // Restore old flash data
335 fd.write(&fd, save_buf, TEST_SIZE);