X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=drv%2Fdataflash.c;h=9089fa978d588556537f5306b67b735f86608cab;hb=6b03e5a1d04f119f778b19d9754e5e1d5e78deec;hp=9742e557a7823f4c319bc41eefa774beabf3d734;hpb=4e59a3961d59e6aef45e50630bf9e66a6e19e9d4;p=bertos.git diff --git a/drv/dataflash.c b/drv/dataflash.c index 9742e557..9089fa97 100644 --- a/drv/dataflash.c +++ b/drv/dataflash.c @@ -55,8 +55,8 @@ * Global variable for store current and previous data * flash memory page address during operation of writing. */ -dataflash_t previous_page = 0; -bool page_modified = false; +static dataflash_t previous_page = 0; +static bool page_modified = false; /** @@ -88,7 +88,6 @@ static void send_cmd(dataflash_t page_addr, dataflash_t byte_addr, DataFlashOpco * hight part of address byte in third byte togheter low par of page * address. * - * \{ */ /* @@ -98,18 +97,15 @@ static void send_cmd(dataflash_t page_addr, dataflash_t byte_addr, DataFlashOpco /* * Send page address. - * \{ */ spi_sendRecv((uint8_t)(page_addr >> (16 - DATAFLASH_PAGE_ADDRESS_BIT))); spi_sendRecv((uint8_t)((page_addr << (DATAFLASH_PAGE_ADDRESS_BIT - 8)) + (byte_addr >> 8))); - /*\}*/ /* * Send byte page address. */ spi_sendRecv((uint8_t)byte_addr); - /* \} */ } @@ -181,7 +177,6 @@ static uint8_t dataflash_stat(void) /* * Make sure to toggle CS signal in order, * and reset dataflash command decoder. - * \{ */ CS_TOGGLE(); @@ -202,8 +197,7 @@ static uint8_t dataflash_cmd(dataflash_t page_addr, dataflash_t byte_addr, DataF send_cmd(page_addr, byte_addr, opcode); - CS_DISABLE(); - CS_ENABLE(); + CS_TOGGLE(); /* * We chech data flash memory state, and wait until busy-flag @@ -230,12 +224,10 @@ static uint8_t dataflash_read_byte(dataflash_t page_addr, dataflash_t byte_addr, { /* * Send 24 don't care bit. - * \{ */ spi_sendRecv(0x00); spi_sendRecv(0x00); spi_sendRecv(0x00); - /* \} */ } #endif @@ -260,12 +252,11 @@ static void dataflash_read_block(dataflash_t page_addr, dataflash_t byte_addr, D { /* * Send 24 don't care bit. - * \{ */ spi_sendRecv(0x00); spi_sendRecv(0x00); spi_sendRecv(0x00); - /* \} */ + } spi_sendRecv(0x00); //Send 8 don't care bit. @@ -411,11 +402,10 @@ static size_t dataflash_read(struct _KFile *fd, void *buf, size_t size) /* * We select from absolute address page address * and byte address in page. - * \{ */ page_addr = fd->seek_pos / (dataflashAddr_t)DATAFLASH_PAGE_SIZE; byte_addr = fd->seek_pos % (dataflashAddr_t)DATAFLASH_PAGE_SIZE; - /* \} */ + kprintf(" [page-<%ld>, byte-<%ld>]", page_addr, byte_addr); @@ -459,11 +449,10 @@ static size_t dataflash_write(struct _KFile *fd, const void *_buf, size_t size) /* * We select from absolute address page address * and byte address in page. - * \{ */ current_page = fd->seek_pos / (dataflashAddr_t)DATAFLASH_PAGE_SIZE; byte_addr = fd->seek_pos % (dataflashAddr_t)DATAFLASH_PAGE_SIZE; - /* \} */ + size_t wr_len = MIN(size, DATAFLASH_PAGE_SIZE - byte_addr); @@ -482,11 +471,10 @@ static size_t dataflash_write(struct _KFile *fd, const void *_buf, size_t size) /* * Write byte in current page, and set true * page_modified flag. - *\{ */ dataflash_write_byte(byte_addr, DFO_WRITE_BUFF1, data); page_modified = true; - /* \} */ + data += wr_len; fd->seek_pos += wr_len; @@ -512,4 +500,99 @@ void dataflash_init(struct _KFile *fd) // Init data flash memory and micro pin. ASSERT(dataflash_pin_init()); -} \ No newline at end of file +} + +/** + * Test function for dataflash. + * + * This function test check low level driver for + * AT45xx (see dataflash.h for more info) data flash memory. + * We write a string in memory in some page ad read it. + */ +void dataflash_test(void) +{ + KFile fd; + + dataflash_init(&fd); + + uint8_t test_buf[] = "0123456789 Develer s.r.l."; + uint8_t cmp_buf[]; + + kprintf("\n======= Data Flash test function =========================================\n"); + kprintf("\nThe string test is: %s\n\n", test_buf); + + fd.open(&fd, NULL, 0); + + /* TEST 1 */ + + // Seek to addr 0 + if (fd.seek(&fd, 0, SEEK_SET) != 0) + goto dataflash_test_end; + + // Test flash write to address 0 (page 0) + if (!fd->write(&fd, test_buf, sizeof(test_buf))) + goto dataflash_test_end; + + // Seek to addr 0 + if (fd.seek(&fd, 0, SEEK_SET) != 0) + goto dataflash_test_end; + + // Test flash read to address 0 (page 0) + if (!fd->read(&fd, cmp_buf, sizeof(test_buf))) + goto dataflash_test_end; + + // Compare if are egual. + if ((memcmp(cmp_buf,test_buf, sizeof(test_buf)) == 0) + goto dataflash_test_end; + + /* TEST 2 */ + + // Go to middle address memory. + fd.seek(&fd, (((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) / 2), SEEK_CUR); + + // Test flash write at the middle of memory + if (!fd->write(&fd, test_buf, sizeof(test_buf))) + goto dataflash_test_end; + + // Go to middle address memory. + fd.seek(&fd, (((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) / 2), SEEK_CUR); + + // Test flash read at the middle of memory + if (!fd->read(&fd, cmp_buf, sizeof(test_buf))) + goto dataflash_test_end; + + // Compare if are egual. + if ((memcmp(cmp_buf,test_buf, sizeof(test_buf)) == 0) + goto dataflash_test_end; + + /* TEST 3 */ + + // Go to end of data flash. + fd.seek(&fd, ((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) - sizeof(test_buf), SEEK_END); + + // Test flash write at the end of memory + if (!fd->write(&fd, test_buf, sizeof(test_buf))) + goto dataflash_test_end; + + // Go to end of data flash. + fd.seek(&fd, ((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) - sizeof(test_buf), SEEK_END); + + // Test flash read at the end of memory + if (!fd->read(&fd, cmp_buf, sizeof(test_buf))) + goto dataflash_test_end; + + // Compare if are egual. + if ((memcmp(cmp_buf,test_buf, sizeof(test_buf)) == 0) + goto dataflash_test_end; + + kprintf("\n"); + + kprintf("\n====== Test end ===========================================================\n"); + fd.close(&fd); + return true; + +dataflash_test_end: + fd.close(&fd); + return false; + +}