Refactor code, "page_buf" moved into KFileFlashAvr struct
[bertos.git] / bertos / cpu / avr / drv / flash_avr.c
index a0792e3cf3ddb592e517b0a6025b0af7eff46796..76023a29af1dd655760c85d6ea3b933d64506df7 100644 (file)
 typedef uint16_t avr_page_addr_t;
 
 
-/**
- * Temporary buffer cointaing data block to
- * write on flash.
- */
-static uint8_t page_buf[SPM_PAGESIZE];
-
-/**
- * Flag for checking if current page is modified.
- */
-bool page_modified;
-
 
 
 /*
@@ -91,7 +80,7 @@ bool page_modified;
  */
 static void flash_avr_flush(KFileFlashAvr *fd)
 {
-       if (page_modified)
+       if (fd->page_dirty)
        {
                kprintf("Flushing page %d\n", fd->curr_page);
 
@@ -103,7 +92,7 @@ static void flash_avr_flush(KFileFlashAvr *fd)
                // Fill the temporary buffer of the AVR
                for (avr_page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2)
                {
-                       uint16_t word = ((uint16_t)page_buf[page_addr + 1] << 8) | page_buf[page_addr];
+                       uint16_t word = ((uint16_t)fd->page_buf[page_addr + 1] << 8) | fd->page_buf[page_addr];
 
                        ATOMIC(boot_page_fill(page_addr, word));
                }
@@ -132,7 +121,7 @@ static void flash_avr_flush(KFileFlashAvr *fd)
                */
                ATOMIC(boot_rww_enable());
 
-               page_modified = false;
+               fd->page_dirty = false;
                kprintf("Done.\n");
        }
 }
@@ -162,7 +151,7 @@ static void flash_avr_loadPage(KFileFlashAvr *fd, avr_page_t page)
        {
                flash_avr_flush(fd);
                // Load page
-               memcpy_P(page_buf, (const char *)(page * SPM_PAGESIZE), SPM_PAGESIZE);
+               memcpy_P(fd->page_buf, (const char *)(page * SPM_PAGESIZE), SPM_PAGESIZE);
                fd->curr_page = page;
                kprintf("Loaded page %d\n", fd->curr_page);
        }
@@ -195,8 +184,8 @@ static size_t flash_avr_write(struct KFile *_fd, const void *_buf, size_t size)
                flash_avr_loadPage(fd, page);
 
                size_t wr_len = MIN(size, SPM_PAGESIZE - page_addr);
-               memcpy(page_buf + page_addr, buf, wr_len);
-               page_modified = true;
+               memcpy(fd->page_buf + page_addr, buf, wr_len);
+               fd->page_dirty = true;
 
                buf += wr_len;
                fd->fd.seek_pos += wr_len;
@@ -215,11 +204,11 @@ static size_t flash_avr_write(struct KFile *_fd, const void *_buf, size_t size)
 static void flash_avr_open(struct KFileFlashAvr *fd)
 {
        fd->curr_page = 0;
-       memcpy_P(page_buf, (const char *)(fd->curr_page * SPM_PAGESIZE), SPM_PAGESIZE);
+       memcpy_P(fd->page_buf, (const char *)(fd->curr_page * SPM_PAGESIZE), SPM_PAGESIZE);
 
        fd->fd.seek_pos = 0;
        fd->fd.size = (uint16_t)(FLASHEND - CONFIG_FLASH_AVR_BOOTSIZE + 1);
-       page_modified = false;
+       fd->page_dirty = false;
 
        kprintf("Flash file opened\n");
 }