From: asterix Date: Thu, 5 Jun 2008 17:25:57 +0000 (+0000) Subject: Add test module for dataflash. Add cfg and use log macros instead kprintf. X-Git-Tag: 2.0.0~517 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=a625fd2523a0c960351f124baa7cd5c3c69c3272;p=bertos.git Add test module for dataflash. Add cfg and use log macros instead kprintf. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1417 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/cfg/cfg_dataflash.h b/bertos/cfg/cfg_dataflash.h new file mode 100644 index 00000000..d0ff90e4 --- /dev/null +++ b/bertos/cfg/cfg_dataflash.h @@ -0,0 +1,62 @@ +/** + * \file + * + * + * \brief Configuration file for data flash memory module. + * + * \version $Id$ + * + * \author Daniele Basile + */ + +#ifndef CFG_DATAFLASH_H +#define CFG_DATAFLASH_H + +/** + * Logging level definition. + * + * Use 0 to log only the error messages + * Use 1 to log the error and warning messages + * Use 2 to log all messages + */ +#define DATAFLASH_LOG_LEVEL 2 + +/** + * Set logging verbosity. + * + * If verbosity is zero print short log messages. + */ +#define DATAFLASH_LOG_VERBOSITY 0 + + +#endif /* CFG_DATAFLASH_H */ + + diff --git a/bertos/drv/dataflash.c b/bertos/drv/dataflash.c index 20844aa9..c8af4265 100644 --- a/bertos/drv/dataflash.c +++ b/bertos/drv/dataflash.c @@ -30,20 +30,26 @@ * * --> * - * \brief Function library for AT45DBXX Data Flash memory. + * \brief Function library for dataflash AT45DB family (implementation). * * - * \version $Id: dataflash.c 20677 2008-02-19 14:29:52Z batt $ + * \version $Id: dataflash.c 21658 2008-06-05 16:42:54Z asterix $ * \author Daniele Basile * \author Francesco Sacchi */ #include "dataflash.h" +#include "cfg/cfg_dataflash.h" #include #include #include +// Define logging setting (for cfg/log.h module). +#define LOG_LEVEL DATAFLASH_LOG_LEVEL +#define LOG_VERBOSITY DATAFLASH_LOG_VERBOSITY +#include + #include #include @@ -184,9 +190,7 @@ static uint8_t dataflash_stat(KFileDataflash *fd) * and reset dataflash command decoder. */ CS_TOGGLE(fd); - kfile_putc(DFO_READ_STATUS, fd->channel); - return kfile_getc(fd->channel); } @@ -290,7 +294,7 @@ static int dataflash_flush(KFile *_fd) fd->page_dirty = false; - kprintf("Flushing page <%ld>\n", fd->current_page); + LOG_INFO("Flushing page {%ld}\n", fd->current_page); } return 0; } @@ -303,7 +307,7 @@ static int dataflash_flush(KFile *_fd) static int dataflash_close(struct KFile *_fd) { dataflash_flush(_fd); - TRACE; + LOG_INFO("Close.\n"); return 0; } @@ -321,7 +325,7 @@ static KFile *dataflash_reopen(KFile *_fd) /* Load selected page from dataflash memory */ dataflash_loadPage(fd, fd->current_page); - TRACE; + LOG_INFO("Reopen.\n"); return &fd->fd; } @@ -350,7 +354,7 @@ static size_t dataflash_read(struct KFile *_fd, void *buf, size_t size) ASSERT(fd->fd.seek_pos + size <= fd->fd.size); size = MIN((kfile_size_t)size, fd->fd.size - fd->fd.seek_pos); - kprintf("Reading at pos[%lu]\n", fd->fd.seek_pos); + LOG_INFO("Reading at pos[%lu]\n", fd->fd.seek_pos); /* * We select page and offest from absolute address. @@ -358,7 +362,7 @@ static size_t dataflash_read(struct KFile *_fd, void *buf, size_t size) page_addr = fd->fd.seek_pos / mem_info[fd->dev].page_size; byte_addr = fd->fd.seek_pos % mem_info[fd->dev].page_size; - kprintf("[page-<%ld>, byte-<%ld>]", page_addr, byte_addr); + LOG_INFO("[page-{%ld}, byte-{%ld}]\n", page_addr, byte_addr); /* * Flush current page in main memory if @@ -372,7 +376,7 @@ static size_t dataflash_read(struct KFile *_fd, void *buf, size_t size) dataflash_readBlock(fd, page_addr, byte_addr, mem_info[fd->dev].read_cmd, data, size); fd->fd.seek_pos += size; - kprintf("Read %ld bytes\n", size); + LOG_INFO("Read %ld bytes\n", size); return size; } @@ -401,7 +405,7 @@ static size_t dataflash_write(struct KFile *_fd, const void *_buf, size_t size) ASSERT(fd->fd.seek_pos + size <= fd->fd.size); size = MIN((kfile_size_t)size, fd->fd.size - fd->fd.seek_pos); - kprintf("Writing at pos[%lu]\n", fd->fd.seek_pos); + LOG_INFO("Writing at pos[%lu]\n", fd->fd.seek_pos); while (size) { @@ -414,7 +418,7 @@ static size_t dataflash_write(struct KFile *_fd, const void *_buf, size_t size) size_t wr_len = MIN((dataflash_size_t)size, mem_info[fd->dev].page_size - offset); - kprintf(" [page-<%ld>, byte-<%ld>]",new_page, offset); + LOG_INFO("[page-{%ld}, byte-{%ld}]\n",new_page, offset); if (new_page != fd->current_page) { @@ -424,7 +428,7 @@ static size_t dataflash_write(struct KFile *_fd, const void *_buf, size_t size) dataflash_loadPage(fd, new_page); fd->current_page = new_page; - kprintf(" >> Load page: <%ld> ", new_page); + LOG_INFO(" >> Load page: {%ld}\n", new_page); } /* * Write byte in current page, and set true @@ -439,7 +443,7 @@ static size_t dataflash_write(struct KFile *_fd, const void *_buf, size_t size) total_write += wr_len; } - kprintf("written %lu bytes\n", total_write); + LOG_INFO("written %lu bytes\n", total_write); return total_write; } @@ -481,7 +485,6 @@ bool dataflash_init(KFileDataflash *fd, KFile *ch, DataflashType dev, dataflash_ fd->fd.flush = dataflash_flush; dataflash_reset(fd); - stat = dataflash_stat(fd); /* diff --git a/bertos/drv/dataflash.h b/bertos/drv/dataflash.h index e9ef00af..923b9ec3 100644 --- a/bertos/drv/dataflash.h +++ b/bertos/drv/dataflash.h @@ -30,9 +30,9 @@ * * --> * - * \brief Function library for AT45DB081D Flash memory. + * \brief Function library for dataflash AT45DB family (interface). * - * \version $Id: dataflash.h 20677 2008-02-19 14:29:52Z batt $ + * \version $Id: dataflash.h 21658 2008-06-05 16:42:54Z asterix $ * * \author Daniele Basile */ @@ -183,4 +183,15 @@ typedef struct DataflashInfo bool dataflash_init(KFileDataflash *fd, KFile *ch, DataflashType type, dataflash_setCS_t *setCS, dataflash_setReset_t *setReset); +/** + * To test data falsh drive you could use + * this functions. To use these functions make sure to include in your make file the + * drv/datafalsh_test.c source. + * + * (see drv/datafalsh_test.c for more detail) + */ +int dataflash_testSetup(void); +int dataflash_testRun(void); +int dataflash_testTearDown(void); + #endif /* DRV_DATAFLASH_H */ diff --git a/bertos/drv/dataflash_test.c b/bertos/drv/dataflash_test.c new file mode 100644 index 00000000..e8147758 --- /dev/null +++ b/bertos/drv/dataflash_test.c @@ -0,0 +1,218 @@ +/** + * \file + * + * + * \brief DataFlash test. + * + * This module test the dataflash memory among the supported memory (see drv/dataflash.h + * for more detail). + * To test memory we fill one buffer with casual char, and write it in different + * part of memory. After every write we read the data that we have been write + * and compare this with test buffer, checking if write and read command work + * correclty. We also check if driver work properly when we make a write out the + * limit of memory size. + * + * Note: dataflash driver use a kfile interface, so for write/read test + * we use a kfile_test module that perform some generic test. + * + * + * \version $Id$ + * + * \author Daniele Basile + */ + +#include "hw/hw_dataflash.h" +#include "cfg/cfg_dataflash.h" +#include +#include + +// Define logging setting (for cfg/log.h module). +#define LOG_LEVEL DATAFLASH_LOG_LEVEL +#define LOG_VERBOSITY DATAFLASH_LOG_VERBOSITY +#include // for logging system + +#include +#include +#include + + +#include + +#include + +/* + * Settings for dataflash test + * + * \{ + */ +// Datafalsh type memory to test (see drv/dataflash.h for supported memory types) +#define DATAFLASH_MEM_MODEL DFT_AT45DB642D + +// Function to set CS, this is typically implement in hw/hw_dataflash.{c, h} +#define DATAFLASH_FUNC_CS_SET &dataflash_hw_setCS + +// Function to reset memery, this is typically implement in hw/hw_dataflash.{c, h} +#define DATAFLASH_FUNC_RESET NULL + +// Buffer len to test dataflash +#define DATAFLASH_TEST_STR_LEN 12307 + +// If you want use a rand function of standard library set to 1. +#define DATAFLASH_USE_RAND_FUNC 1 +/* \} */ + +/* + * Kfile structure to test a dataflash. + */ +static KFileSerial spi_fd; +static KFileDataflash dflash_fd; + +/* + * Define tmp buffer to stora data for + * write and read flash memory test. + */ +static uint8_t test_buf[DATAFLASH_TEST_STR_LEN]; +static uint8_t save_buf[DATAFLASH_TEST_STR_LEN]; + +/** + * Setup all needed to test dataflash memory + * + */ +int dataflash_testSetup(void) +{ + /* + * This test use a kfile_test module, + * so should include source in your makefile. + */ + MOD_CHECK(kfile_test); + + timer_init(); + LOG_INFO("Timer init..ok\n"); + + /* + * Init SPI module and dataflash driver. + */ + // Open SPI comunication channel + spimaster_init(&spi_fd, SER_SPI0); + LOG_INFO("SPI0 init..ok\n"); + + ser_setbaudrate(&spi_fd, 5000000UL); + LOG_INFO("SPI0 set baudrate..ok\n"); + + //Init dataflash memory + dataflash_hw_init(); + LOG_INFO("DATAFLASH HW..ok\n"); + + if (dataflash_init(&dflash_fd, &spi_fd.fd, DATAFLASH_MEM_MODEL, DATAFLASH_FUNC_CS_SET, DATAFLASH_FUNC_RESET)) + LOG_INFO("DATAFLASH init..ok\n"); + else + LOG_ERR("DATAFLASH init..fail\n"); + + + //Fill tmp buffer with rand chars. + for (int i = 0; i < DATAFLASH_TEST_STR_LEN; i++) + { + #if DATAFLASH_USE_RAND_FUNC + #include //Rand() + + test_buf[i] = (uint8_t)rand(); + #else + test_buf[i] = (i & 0xff); + #endif + } + + LOG_INFO("Fill tmp buff..ok\n"); + + return 0; +} + + +/** + * Run dataflash test memory + * + */ +int dataflash_testRun(void) +{ + if (!dataflash_testSetup()) + { + LOG_INFO("DATAFLASH setup..ok\n"); + } + else + { + LOG_ERR("DATAFLASH setup..fail!\n"); + return EOF; + } + + LOG_INFO("Run KFILE test.\n"); + + if (kfile_testRun(&dflash_fd.fd, test_buf, save_buf, sizeof(test_buf))) + { + LOG_INFO("KFILE test..ok\n"); + } + else + { + LOG_ERR("KFILE test..fail!\n"); + return EOF; + } + + return 0; +} + +/** + * End a dataflash Test. + * (Unused) + */ +int dataflash_testTearDown(void) +{ + /* */ + return 0; +} + +/* + * Empty main. + * + * Look it as exmple, or use it if + * you want test a data flash driver stand alone. + */ +#if 0 +int main(void) +{ + IRQ_ENABLE; + kdbg_init(); + + dataflash_testRun(); + + for(;;) + { + } + +} +#endif