From a55965008ec50fe902e3c3f9c27912b7eb827ba3 Mon Sep 17 00:00:00 2001 From: NĂ©lio Laranjeiro Date: Tue, 4 Mar 2008 23:20:03 +0100 Subject: Flash : Read and write operationnal. --- digital/avr/modules/flash/flash.c | 51 +++++++++++++++++++++++++++-- digital/avr/modules/flash/flash.h | 3 ++ digital/avr/modules/flash/test/test-flash.c | 5 +-- 3 files changed, 55 insertions(+), 4 deletions(-) (limited to 'digital/avr/modules') diff --git a/digital/avr/modules/flash/flash.c b/digital/avr/modules/flash/flash.c index d7fd0f28..010f0816 100644 --- a/digital/avr/modules/flash/flash.c +++ b/digital/avr/modules/flash/flash.c @@ -25,6 +25,7 @@ #include "flash.h" #include "modules/proto/proto.h" #include "modules/spi/spi.h" +#include "modules/utils/utils.h" static flash_t flash_global; @@ -179,7 +180,31 @@ flash_read (uint32_t addr) * \param length the length of the data to read. */ void -flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length); +flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length) +{ + uint8_t data[16]; + uint8_t i,j; + + AC_FLASH_PORT &= ~_BV(AC_FLASH_BIT_SS); + /* Send the read instruction. */ + spi_send (FLASH_READ); + flash_address (addr); + + while (length) + { + for (i = 0; i < 16; i++) + { + data[i] = spi_recv (); + length --; + } + + for (j = 0; j < i; j++) + { + proto_send1b('r', data[i]); + } + } + AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); +} /** Write in the flash byte provided in parameter. * \param addr the address to store the data. @@ -187,7 +212,29 @@ flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length); * \param length the array length */ void -flash_write_array (uint32_t addr, uint8_t *data, uint32_t length); +flash_write_array (uint32_t addr, uint8_t *data, uint32_t length) +{ + uint32_t i; + + AC_FLASH_PORT &= ~_BV(AC_FLASH_BIT_SS); + spi_send (FLASH_AAI); + /* send the start address */ + flash_address (addr); + AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); + + /* Send two bytes */ + for (i = 0; i < length; i += 2) + { + AC_FLASH_PORT &= ~_BV(AC_FLASH_BIT_SS); + spi_send (FLASH_AAI); + spi_send (data[i]); + spi_send (data[i+1]); + AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); + + /* Wait for the end of the register. */ + utils_delay_us (FLASH_TBP_US); + } +} /** Read the memory from the address automaticaly managed, the offset of the * data shall be provided to get the data. diff --git a/digital/avr/modules/flash/flash.h b/digital/avr/modules/flash/flash.h index 833e3596..a3ef00de 100644 --- a/digital/avr/modules/flash/flash.h +++ b/digital/avr/modules/flash/flash.h @@ -47,6 +47,9 @@ #define FLASH_WREN 0x6 #define FLASH_WEDI 0x4 #define FLASH_EWSR 0x50 +#define FLASH_AAI 0xAD + +#define FLASH_TBP_US 10 struct flash_t { diff --git a/digital/avr/modules/flash/test/test-flash.c b/digital/avr/modules/flash/test/test-flash.c index eb5ebb29..35d4049d 100644 --- a/digital/avr/modules/flash/test/test-flash.c +++ b/digital/avr/modules/flash/test/test-flash.c @@ -56,8 +56,9 @@ main (void) proto_send0 ('c'); flash_init (); proto_send0 ('f'); - flash_write (0x1, 0xab); - proto_send0 (flash_read(0x1)); + flash_write (0x10, 'a'); + utils_delay_us (FLASH_TBP_US); + proto_send1b ('o',flash_read(0x10)); while (1) proto_accept (uart0_getc ()); } -- cgit v1.2.3