From 2302be36b228a46292a3f4eae649f5e29bd9999c Mon Sep 17 00:00:00 2001 From: asterix Date: Tue, 11 May 2010 10:13:35 +0000 Subject: [PATCH] Add flash module. Refactor the cpu implementation. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3646 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cfg/compiler.h | 1 - bertos/cpu/arm/drv/flash_arm.h | 46 ++++++++++++++++++++++++++ bertos/cpu/arm/drv/flash_at91.c | 31 +++++++++--------- bertos/cpu/arm/drv/flash_at91.h | 37 +++++++++++---------- bertos/cpu/avr/drv/flash_avr.c | 34 +++++++++----------- bertos/cpu/avr/drv/flash_avr.h | 57 +++++++++++++++++---------------- 6 files changed, 128 insertions(+), 78 deletions(-) create mode 100644 bertos/cpu/arm/drv/flash_arm.h diff --git a/bertos/cfg/compiler.h b/bertos/cfg/compiler.h index 4d0c33be..82bd6dbb 100644 --- a/bertos/cfg/compiler.h +++ b/bertos/cfg/compiler.h @@ -423,7 +423,6 @@ typedef const void * const_iptr_t; typedef unsigned char sigbit_t; /**< Type for signal bits. */ typedef unsigned char sigmask_t; /**< Type for signal masks. */ -typedef unsigned char page_t; /**< Type for banked memory pages. */ /** diff --git a/bertos/cpu/arm/drv/flash_arm.h b/bertos/cpu/arm/drv/flash_arm.h new file mode 100644 index 00000000..50b84b91 --- /dev/null +++ b/bertos/cpu/arm/drv/flash_arm.h @@ -0,0 +1,46 @@ +/** + * \file + * + * + * \brief Low-level flash module for ARM (interface). + * + * \author Daniele Basile + * + */ + +#include + +#if CPU_ARM_AT91 + #include "flash_at91.h" +/*#elif Add other ARM families here */ +#else + #error Unknown CPU +#endif diff --git a/bertos/cpu/arm/drv/flash_at91.c b/bertos/cpu/arm/drv/flash_at91.c index 778cb11a..06ff71e4 100644 --- a/bertos/cpu/arm/drv/flash_at91.c +++ b/bertos/cpu/arm/drv/flash_at91.c @@ -59,6 +59,7 @@ #include #include +#include #include @@ -130,15 +131,15 @@ RAM_FUNC static int flash_at91_getStatus(struct KFile *_fd) * Write modified page on internal latch, and then send write command to * flush page to internal flash. */ -RAM_FUNC static void flash_at91_flush(FlashAt91 *fd) +RAM_FUNC static void flash_at91_flush(Flash *fd) { if (fd->page_dirty) { //Compute page address of current page. - arm_page_addr_t *addr = (arm_page_addr_t *)((fd->curr_page * FLASH_PAGE_SIZE_BYTES) + FLASH_BASE); + page_addr_t *addr = (page_addr_t *)((fd->curr_page * FLASH_PAGE_SIZE_BYTES) + FLASH_BASE); //Copy modified page into internal latch. - for (arm_page_addr_t page_addr = 0; page_addr < FLASH_PAGE_SIZE_BYTES; page_addr += 4) + for (page_addr_t page_addr = 0; page_addr < FLASH_PAGE_SIZE_BYTES; page_addr += 4) { uint32_t data; memcpy(&data, &fd->page_buf[page_addr], sizeof(data)); @@ -159,7 +160,7 @@ RAM_FUNC static void flash_at91_flush(FlashAt91 *fd) */ static int flash_at91_kfileFlush(struct KFile *_fd) { - FlashAt91 *fd = FLASHAT91_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); flash_at91_flush(fd); return 0; } @@ -169,7 +170,7 @@ static int flash_at91_kfileFlush(struct KFile *_fd) * Check current page and if \a page is different, load it in * temporary buffer. */ -static void flash_at91_loadPage(FlashAt91 *fd, arm_page_t page) +static void flash_at91_loadPage(Flash *fd, page_t page) { if (page != fd->curr_page) { @@ -189,11 +190,11 @@ static void flash_at91_loadPage(FlashAt91 *fd, arm_page_t page) */ static size_t flash_at91_write(struct KFile *_fd, const void *_buf, size_t size) { - FlashAt91 *fd = FLASHAT91_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); const uint8_t *buf =(const uint8_t *)_buf; - arm_page_t page; - arm_page_addr_t page_addr; + page_t page; + page_addr_t page_addr; size_t total_write = 0; size = MIN((kfile_off_t)size, (kfile_off_t)(fd->fd.size - (fd->fd.seek_pos - FLASH_BASE))); @@ -225,7 +226,7 @@ static size_t flash_at91_write(struct KFile *_fd, const void *_buf, size_t size) */ static int flash_at91_close(struct KFile *_fd) { - FlashAt91 *fd = FLASHAT91_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); flash_at91_flush(fd); LOG_INFO("Flash file closed\n"); @@ -237,7 +238,7 @@ static int flash_at91_close(struct KFile *_fd) * \a name and \a mode are unused, cause flash memory is * threated like one file. */ -static void flash_at91_open(struct FlashAt91 *fd) +static void flash_at91_open(struct Flash *fd) { fd->fd.size = FLASH_BASE + FLASH_MEM_SIZE; fd->fd.seek_pos = FLASH_BASE + FLASH_BOOT_SIZE; @@ -256,7 +257,7 @@ static void flash_at91_open(struct FlashAt91 *fd) */ static kfile_off_t flash_at91_seek(struct KFile *_fd, kfile_off_t offset, KSeekMode whence) { - FlashAt91 *fd = FLASHAT91_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); kfile_off_t seek_pos; switch (whence) @@ -293,7 +294,7 @@ static kfile_off_t flash_at91_seek(struct KFile *_fd, kfile_off_t offset, KSeekM */ static struct KFile *flash_at91_reopen(struct KFile *_fd) { - FlashAt91 *fd = FLASHAT91_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); flash_at91_close(_fd); flash_at91_open(fd); @@ -306,7 +307,7 @@ static struct KFile *flash_at91_reopen(struct KFile *_fd) */ static size_t flash_at91_read(struct KFile *_fd, void *_buf, size_t size) { - FlashAt91 *fd = FLASHAT91_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); uint8_t *buf =(uint8_t *)_buf; size = MIN((kfile_off_t)size, fd->fd.size - fd->fd.seek_pos); @@ -330,10 +331,10 @@ static size_t flash_at91_read(struct KFile *_fd, void *_buf, size_t size) * Init module to perform write and read operation on internal * flash memory. */ -void flash_at91_init(FlashAt91 *fd) +void flash_hw_init(struct Flash *fd) { memset(fd, 0, sizeof(*fd)); - DB(fd->fd._type = KFT_FLASHAT91); + DB(fd->fd._type = KFT_FLASH); // Set up flash programming functions. fd->fd.reopen = flash_at91_reopen; diff --git a/bertos/cpu/arm/drv/flash_at91.h b/bertos/cpu/arm/drv/flash_at91.h index 1dcd9829..0b6c8d66 100644 --- a/bertos/cpu/arm/drv/flash_at91.h +++ b/bertos/cpu/arm/drv/flash_at91.h @@ -47,14 +47,23 @@ #include +#define FLASH_PAGE_SIZE FLASH_PAGE_SIZE_BYTES + /** * Define data type to manage page and memory address. */ -typedef uint32_t arm_page_t; -typedef uint32_t arm_page_addr_t; +typedef uint32_t page_t; +typedef uint32_t page_addr_t; + +struct Flash; /** * FlashAt91 KFile context structure. + * + * DEPREACTED STRUCTURE! + * Use EmbFlash instead + * + * \{ */ typedef struct FlashAt91 { @@ -71,32 +80,26 @@ typedef struct FlashAt91 /** * Current buffered page. */ - arm_page_t curr_page; + page_t curr_page; /** * Temporary buffer cointaing data block to * write on flash. */ uint8_t page_buf[FLASH_PAGE_SIZE_BYTES]; - - } FlashAt91; +/* \} */ -/** - * ID for FlashAt91 - */ -#define KFT_FLASHAT91 MAKE_ID('F', 'A', '9', '1') +void flash_hw_init(struct Flash *fd); /** - * Convert + ASSERT from generic KFile to FlashAt91. + * WARNING! + * This function is DEPRECADED! + * use the emb_flash module instead. */ -INLINE FlashAt91 * FLASHAT91_CAST(KFile *fd) +INLINE void flash_at91_init(struct FlashAt91 *fd) { - ASSERT(fd->_type == KFT_FLASHAT91); - return (FlashAt91 *)fd; + flash_hw_init((struct Flash *)fd); } - -void flash_at91_init(FlashAt91 *fd); - -#endif +#endif /* DRV_FLASH_ARM_H */ diff --git a/bertos/cpu/avr/drv/flash_avr.c b/bertos/cpu/avr/drv/flash_avr.c index 03740dd7..2c3d1840 100644 --- a/bertos/cpu/avr/drv/flash_avr.c +++ b/bertos/cpu/avr/drv/flash_avr.c @@ -32,7 +32,6 @@ * * \brief Self programming routines. * - * \version $Id$ * \author Francesco Sacchi * \author Daniele Basile * @@ -56,6 +55,7 @@ #include #include +#include #include @@ -69,9 +69,7 @@ /** * Definition of type for avr flash module. */ -typedef uint16_t avr_page_addr_t; - - +typedef uint16_t page_addr_t; /** @@ -82,7 +80,7 @@ typedef uint16_t avr_page_addr_t; * * This function is only use internally in this module. */ -static void flash_avr_flush(FlashAvr *fd) +static void flash_avr_flush(Flash *fd) { if (fd->page_dirty) { @@ -95,7 +93,7 @@ static void flash_avr_flush(FlashAvr *fd) LOG_INFO("Filling temparary page buffer..."); // Fill the temporary buffer of the AVR - for (avr_page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2) + for (page_addr_t page_addr = 0; page_addr < SPM_PAGESIZE; page_addr += 2) { uint16_t word = ((uint16_t)fd->page_buf[page_addr + 1] << 8) | fd->page_buf[page_addr]; @@ -140,7 +138,7 @@ static void flash_avr_flush(FlashAvr *fd) */ static int flash_avr_kfileFlush(struct KFile *_fd) { - FlashAvr *fd = FLASHAVR_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); flash_avr_flush(fd); return 0; } @@ -150,7 +148,7 @@ static int flash_avr_kfileFlush(struct KFile *_fd) * Check current page and if \a page is different, load it in * temporary buffer. */ -static void flash_avr_loadPage(FlashAvr *fd, avr_page_t page) +static void flash_avr_loadPage(Flash *fd, page_t page) { if (page != fd->curr_page) { @@ -169,11 +167,11 @@ static void flash_avr_loadPage(FlashAvr *fd, avr_page_t page) */ static size_t flash_avr_write(struct KFile *_fd, const void *_buf, size_t size) { - FlashAvr *fd = FLASHAVR_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); const uint8_t *buf =(const uint8_t *)_buf; - avr_page_t page; - avr_page_addr_t page_addr; + page_t page; + page_addr_t page_addr; size_t total_write = 0; @@ -206,7 +204,7 @@ static size_t flash_avr_write(struct KFile *_fd, const void *_buf, size_t size) * \a name and \a mode are unused, cause flash memory is * threated like one file. */ -static void flash_avr_open(struct FlashAvr *fd) +static void flash_avr_open(struct Flash *fd) { fd->curr_page = 0; memcpy_P(fd->page_buf, (const char *)(fd->curr_page * SPM_PAGESIZE), SPM_PAGESIZE); @@ -223,7 +221,7 @@ static void flash_avr_open(struct FlashAvr *fd) */ static int flash_avr_close(struct KFile *_fd) { - FlashAvr *fd = FLASHAVR_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); flash_avr_flush(fd); LOG_INFO("Flash file closed\n"); return 0; @@ -234,9 +232,9 @@ static int flash_avr_close(struct KFile *_fd) */ static struct KFile *flash_avr_reopen(struct KFile *fd) { - FlashAvr *_fd = FLASHAVR_CAST(fd); + Flash *_fd = FLASH_CAST(fd); flash_avr_close(fd); - flash_avr_open(_fd); + flash_avr_open((struct Flash *)_fd); return fd; } @@ -247,7 +245,7 @@ static struct KFile *flash_avr_reopen(struct KFile *fd) */ static size_t flash_avr_read(struct KFile *_fd, void *buf, size_t size) { - FlashAvr *fd = FLASHAVR_CAST(_fd); + Flash *fd = FLASH_CAST(_fd); ASSERT(fd->fd.seek_pos + (kfile_off_t)size <= (kfile_off_t)fd->fd.size); size = MIN((kfile_off_t)size, fd->fd.size - fd->fd.seek_pos); @@ -271,10 +269,10 @@ static size_t flash_avr_read(struct KFile *_fd, void *buf, size_t size) /** * Init AVR flash read/write file. */ -void flash_avr_init(struct FlashAvr *fd) +void flash_hw_init(struct Flash *fd) { memset(fd, 0, sizeof(*fd)); - DB(fd->fd._type = KFT_FLASHAVR); + DB(fd->fd._type = KFT_FLASH); // Set up flash programming functions. fd->fd.reopen = flash_avr_reopen; diff --git a/bertos/cpu/avr/drv/flash_avr.h b/bertos/cpu/avr/drv/flash_avr.h index 0897132c..6c06eaf3 100644 --- a/bertos/cpu/avr/drv/flash_avr.h +++ b/bertos/cpu/avr/drv/flash_avr.h @@ -30,29 +30,42 @@ * * --> * - * \brief Self programming routines (interface). - * - * \version $Id$ * \author Francesco Sacchi * \author Daniele Basile + * + * \brief AVR Internal flash read/write driver. + * + * */ -#ifndef DRV_FLASH_AVR_H -#define DRV_FLASH_AVR_H +#ifndef FLASH_AT91_H +#define FLASH_AT91_H + +#include #include + #include + #include +#define FLASH_PAGE_SIZE SPM_PAGESIZE + /** * Definition of type for avr flash module. */ -typedef uint16_t avr_page_t; +typedef uint16_t page_t; +/* Forward declaration */ +struct Flash; /** * FlashAvr KFile context structure. + * DEPREACTED STRUCTURE! + * Use Flash instead + * + * \{ */ typedef struct FlashAvr { @@ -62,43 +75,33 @@ typedef struct FlashAvr KFile fd; /** - * Current buffered page. + * Flag for checking if current page is modified. */ - avr_page_t curr_page; + bool page_dirty; /** - * Flag for checking if current page is modified. + * Current buffered page. */ - bool page_dirty; + page_t curr_page; /** * Temporary buffer cointaing data block to * write on flash. */ uint8_t page_buf[SPM_PAGESIZE]; - - } FlashAvr; +/* \} */ - - -/** - * ID for FlashAvr - */ -#define KFT_FLASHAVR MAKE_ID('F', 'L', 'A', 'V') +void flash_hw_init(struct Flash *fd); /** - * Convert + ASSERT from generic KFile to FlashAvr. + * WARNING! + * This function is DEPRECADED! + * use the flash module instead. */ -INLINE FlashAvr * FLASHAVR_CAST(KFile *fd) +INLINE void flash_avr_init(struct FlashAvr *fd) { - ASSERT(fd->_type == KFT_FLASHAVR); - return (FlashAvr *)fd; + flash_hw_init((struct Flash *)fd); } - -void flash_avr_init(struct FlashAvr *fd); - - - #endif /* DRV_FLASH_AVR_H */ -- 2.25.1