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>
20 #include <cfg/macros.h> // MIN()
21 #include <cfg/compiler.h>
22 #include <cfg/debug.h>
26 #include <avr/pgmspace.h>
28 typedef uint16_t avr_page_addr_t;
29 typedef uint16_t avr_page_t;
32 * Temporary buffer cointaing data block to
35 static uint8_t page_buf[SPM_PAGESIZE];
37 bool page_modified; /// Flag for checking if current page is modified.
40 * Current buffered page.
42 static avr_page_t curr_page = 0;
45 * Write current buffered page in flash memory (if modified).
46 * This function erase flash memory page before writing.
48 static void prog_flush(void)
52 kprintf("Flushing page %d\n", curr_page);
54 boot_spm_busy_wait(); // Wait while the SPM instruction is busy.
56 kprintf("Filling temparary page buffer...");
57 /* Fill the temporary buffer of the AVR */
58 for (avr_page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2)
60 uint16_t word = ((uint16_t)page_buf[page_addr + 1] << 8) | page_buf[page_addr];
62 ATOMIC(boot_page_fill(page_addr, word));
68 kprintf("Erasing page, addr %d...", curr_page * SPM_PAGESIZE);
71 ATOMIC(boot_page_erase(curr_page * SPM_PAGESIZE));
73 /* Wait until the memory is erased. */
77 kprintf("Writing page, addr %d...", curr_page * SPM_PAGESIZE);
79 /* Store buffer in flash page. */
80 ATOMIC(boot_page_write(curr_page * SPM_PAGESIZE));
81 boot_spm_busy_wait(); // Wait while the SPM instruction is busy.
84 * Reenable RWW-section again. We need this if we want to jump back
85 * to the application after bootloading.
87 ATOMIC(boot_rww_enable());
89 page_modified = false;
96 * Check current page and if \param page is different, load it in
99 static void prog_loadPage(avr_page_t page)
101 if (page != curr_page)
105 memcpy_P(page_buf, (const char *)(page * SPM_PAGESIZE), SPM_PAGESIZE);
107 kprintf("Loaded page %d\n", curr_page);
112 * Write program memory.
113 * Write \param size bytes from buffer \param buf to file \param *fd
114 * \note Write operations are buffered.
116 size_t prog_write(struct _KFile *fd, const void *_buf, size_t size)
118 const uint8_t *buf =(const uint8_t *)_buf;
121 avr_page_addr_t page_addr;
122 size_t total_write = 0;
124 ASSERT(fd->seek_pos + size <= fd->size);
125 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
127 kprintf("Writing at pos[%d]\n", fd->seek_pos);
130 page = fd->seek_pos / SPM_PAGESIZE;
131 page_addr = fd->seek_pos % SPM_PAGESIZE;
135 size_t wr_len = MIN(size, SPM_PAGESIZE - page_addr);
136 memcpy(page_buf + page_addr, buf, wr_len);
137 page_modified = true;
140 fd->seek_pos += wr_len;
142 total_write += wr_len;
144 kprintf("written %d bytes\n", total_write);
149 * Open flash file \param *fd.
150 * \param name and \param mode are unused, cause flash memory is
151 * threated like one file.
153 bool prog_open(struct _KFile *fd, UNUSED_ARG(const char *, name), UNUSED_ARG(int, mode))
156 memcpy_P(page_buf, (const char *)(curr_page * SPM_PAGESIZE), SPM_PAGESIZE);
159 fd->size = (uint16_t)(FLASHEND - CONFIG_BOOT_SIZE + 1);
160 page_modified = false;
162 kprintf("Flash file opened\n");
167 * Close file \param *fd
169 bool prog_close(UNUSED_ARG(struct _KFile *,fd))
172 kprintf("Flash file closed\n");
177 * Move \param *fd file seek position of \param offset bytes
178 * from current position.
180 bool prog_seek(struct _KFile *fd, int32_t offset)
182 ASSERT(fd->seek_pos + offset <= fd->size);
185 if (fd->seek_pos + offset > fd->size)
188 fd->seek_pos += offset;
189 kprintf("Flash seek to [%d]\n", fd->seek_pos);
195 * Read from file \param *fd \param size bytes and put it in buffer \param *buf
196 * \return the number of bytes read.
198 size_t prog_read(struct _KFile *fd, void *buf, size_t size)
200 ASSERT(fd->seek_pos + size <= fd->size);
201 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
203 kprintf("Reading at pos[%d]\n", fd->seek_pos);
204 // Flush current buffered page (if modified).
208 * AVR pointers are 16 bits wide, this hack is needed to avoid
209 * compiler warning, cause fd->seek_pos is a 32bit offset.
211 const uint8_t *pgm_addr = (const uint8_t *)0;
212 pgm_addr += fd->seek_pos;
214 memcpy_P(buf, pgm_addr, size);
215 fd->seek_pos += size;
216 kprintf("Read %d bytes\n", size);