*/
static uint8_t page_buf[PAGEBUF];
+/**
+ * Store current flash page memory in use.
+ */
static page_t curr_pag_num = 0;
*/
RAMPZ = 0x00;
- for (flash_addr = 0; (flash_addr < (uint16_t)(APP_END & 0xFFFF)) | (RAMPZ == 0x00);
+ for (flash_addr = 0; (flash_addr < (uint16_t)(APP_END & 0xFFFF)) | (RAMPZ == 0x00));
{
wdt_reset();
}
+/**
+ * Flush temporary buffer into flash memory.
+ */
+static void prog_flush(void)
+{
+
+ /* Fill the temporary buffer of the AVR */
+ for (page_addr_t page_addr = 0; page_addr < PAGEBUF; page_addr += 2)
+ fill_temp_buffer(page_buf[page_addr + 1] | (uint16_t)page_buf[page_addr] << 8, page_addr);
+
+
+ wdt_reset();
+
+ /* Page write */
+ prog_pagewrite(curr_page_num * PAGEBUF);
+}
+
/**
* Write program memory.
+ * This function to write on flash memory load a selected page from
+ * flash memory and save it in temporary buffer. Them update temporary buffer
+ * with \param buf data. We write in flash memory everery time we
+ * change current page memory.
+ *
*/
size_t prog_write(struct _KFile *fd, const char *buf, size_t size)
{
page_t page;
page_addr_t page_addr;
size_t total_write = 0;
-
+ size_t wr_len;
+
while (size)
{
+ /* Current page memory */
page = fd->SeekPos / PAGEBUF;
+
+ /* Address in page memory */
page_addr = fd->SeekPos % PAGEBUF;
-
+
prog_loadPage(page);
-
- size_t wr_len = MIN(size, PAGEBUF - page_addr);
+
+ wr_len = MIN(size, PAGEBUF - page_addr);
memcpy(page_buf + page_addr, buf, wr_len);
buf += wr_len;
size -= wr_len;
total_write += wr_len;
}
+ /* Return total byte write on flash memory */
return total_write;
}
-void prog_flush(void)
-{
-
- /* Fill the temporary buffer of the AVR */
- for (page_addr_t page_addr = 0; page_addr < PAGEBUF; page_addr += 2)
- fill_temp_buffer(page_buf[page_addr + 1] | (uint16_t)page_buf[page_addr] << 8, page_addr);
-
-
- wdt_reset();
- /* Page write */
- prog_pagewrite(curr_page_num * PAGEBUF);
-}
+/**
+ * Load select \param page memory buffer from AVR flash memory.
+ * If select page is not current page, we flush it, and then we load
+ * select page memory flash.
+ */
void prog_loadPage(page_t page)
{
if (page != curr_page_num)
{
prog_flush();
- // Load page
+ /* Load selet page in temporary buffer store into RAM */
memcpy_P(page_buf, (const char *)(page * PAGEBUF), PAGEBUF);
+ /* Update current page */
curr_page_num = page;
}
-}
\ No newline at end of file
+}