From bc57a1e5aa236e9830d7a193e98c6d4de60f78d6 Mon Sep 17 00:00:00 2001 From: NĂ©lio Laranjeiro Date: Thu, 28 Feb 2008 00:34:10 +0100 Subject: Update the flash memory accesses. --- digital/avr/modules/flash/flash.c | 65 +++++++++++++++++++++++++---- digital/avr/modules/flash/flash.h | 35 ++++++++++++---- digital/avr/modules/flash/test/Makefile | 2 +- digital/avr/modules/flash/test/test-flash.c | 7 ++++ 4 files changed, 92 insertions(+), 17 deletions(-) diff --git a/digital/avr/modules/flash/flash.c b/digital/avr/modules/flash/flash.c index ae35501e..f781097d 100644 --- a/digital/avr/modules/flash/flash.c +++ b/digital/avr/modules/flash/flash.c @@ -23,16 +23,57 @@ * * }}} */ #include "flash.h" +#include "modules/proto/proto.h" #include "modules/spi/spi.h" static flash_t flash_global; +/** Flash access. + * The flash contains an address of 21 bits in a range from 0x0-0x1fffff. + * This function shall access the memory directly by the SPI. + * \param addr the address to provide to the flash memory. + */ +void +flash_address (uint32_t addr) +{ + /* The address must be sent */ + spi_send ((addr >> 16) & 0x1f); + spi_send (addr >> 8); + spi_send (addr); +} + +/** Erase the memory. + * \param erase_type the erase type.. + * \param start_addr the start address. + */ +void +flash_erase (uint8_t cmd, uint32_t start_addr) +{ + /* send the command. */ + spi_send (cmd); + + /* verify if the cmd is the full erase. */ + if (cmd != FLASH_ERASE_FULL) + { + /* Send the start address */ + flash_address (start_addr); + } +} + /** Initialise the flsah memory. */ void flash_init (void) { + uint8_t rsp; + flash_global.addr = 0x0; + /* send the read-ID instruction. */ + spi_send (0x90); + rsp = flash_read (0x0); + + if (rsp == 0xBF) + proto_send1b (0x0 ,0x1); } /** Write in the flash byte provided in parameter. @@ -40,11 +81,14 @@ flash_init (void) * \param data the buffer to store the data. */ void -flash_write (uint8_t addr, uint8_t data) +flash_write (uint32_t addr, uint8_t data) { spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MSB_FIRST | SPI_MASTER | SPI_CPOL_RISING | SPI_CPHA_SAMPLE | SPI_FOSC_DIV2); - spi_send (addr); + + /* Write instruction. */ + spi_send (0x2); + flash_address (addr); spi_send (data); } @@ -53,11 +97,14 @@ flash_write (uint8_t addr, uint8_t data) * \return the data read. */ uint8_t -flash_read (uint8_t addr) +flash_read (uint32_t addr) { spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MSB_FIRST | SPI_MASTER | SPI_CPOL_RISING | SPI_CPHA_SAMPLE | SPI_FOSC_DIV2); - spi_send (addr); + + /* Send the read instruction. */ + spi_send (0x3); + flash_address (addr); return spi_recv (); } @@ -68,7 +115,7 @@ flash_read (uint8_t addr) * \param length the length of the data to read. */ void -flash_read_array (uint8_t addr, uint8_t *buffer, uint8_t length); +flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length); /** Write in the flash byte provided in parameter. * \param addr the address to store the data. @@ -76,7 +123,7 @@ flash_read_array (uint8_t addr, uint8_t *buffer, uint8_t length); * \param length the array length */ void -flash_write_array (uint8_t addr, uint8_t *data, uint8_t length); +flash_write_array (uint32_t addr, uint8_t *data, uint32_t length); /** Read the memory from the address automaticaly managed, the offset of the * data shall be provided to get the data. @@ -84,9 +131,9 @@ flash_write_array (uint8_t addr, uint8_t *data, uint8_t length); * \return data read from the memory. */ uint8_t -flash_read_managed (uint8_t offset) +flash_read_managed (uint32_t offset) { - if ((int8_t) flash_global.addr - (int8_t) offset < 0) + if ((int32_t) flash_global.addr - (int32_t) offset < 0) { // Shall not happen. // TODO Assert this. @@ -103,7 +150,7 @@ flash_read_managed (uint8_t offset) * \param buffer the buffer to store the data. */ void -flash_read_managed_array (uint8_t offset, uint8_t *buffer); +flash_read_managed_array (uint32_t offset, uint8_t *buffer); /** Write a data with a managed array. * \param data to store in the memory. diff --git a/digital/avr/modules/flash/flash.h b/digital/avr/modules/flash/flash.h index f5360c00..73c582d4 100644 --- a/digital/avr/modules/flash/flash.h +++ b/digital/avr/modules/flash/flash.h @@ -24,12 +24,18 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ +#include "common.h" #include "io.h" +#define FLASH_ERASE_FULL 0x60 +#define FLASH_ERASE_4K 0x20 +#define FLASH_ERASE_32K 0x52 +#define FLASH_ERASE_64K 0xD8 + struct flash_t { /** Current Address in the flash memory. */ - uint8_t addr; + uint32_t addr; }; typedef struct flash_t flash_t; @@ -38,19 +44,34 @@ typedef struct flash_t flash_t; void flash_init (void); +/** Flash access. + * The flash contains an address of 21 bits in a range from 0x0-0x1fffff. + * This function shall access the memory directly by the SPI. + * \param addr the address to provide to the flash memory. + */ +void +flash_address (uint32_t addr); + +/** Erase the memory. + * \param erase_type the erase type.. + * \param start_addr the start address. + */ +void +flash_erase (uint8_t cmd, uint32_t start_addr); + /** Write in the flash byte provided in parameter. * \param addr the address to store the data. * \param data the buffer to store the data. */ void -flash_write (uint8_t addr, uint8_t data); +flash_write (uint32_t addr, uint8_t data); /** Read the data at the address provided. * \param addr the address of the data to read. * \return the data read. */ uint8_t -flash_read (uint8_t addr); +flash_read (uint32_t addr); /** Read a data from the flash memory from the address provided and for a * length of the number of bytes provided. @@ -59,7 +80,7 @@ flash_read (uint8_t addr); * \param length the length of the data to read. */ void -flash_read_array (uint8_t addr, uint8_t *buffer, uint8_t length); +flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length); /** Write in the flash byte provided in parameter. * \param addr the address to store the data. @@ -67,7 +88,7 @@ flash_read_array (uint8_t addr, uint8_t *buffer, uint8_t length); * \param length the array length */ void -flash_write_array (uint8_t addr, uint8_t *data, uint8_t length); +flash_write_array (uint32_t addr, uint8_t *data, uint32_t length); /** Read the memory from the address automaticaly managed, the offset of the * data shall be provided to get the data. @@ -75,7 +96,7 @@ flash_write_array (uint8_t addr, uint8_t *data, uint8_t length); * \return data read from the memory. */ uint8_t -flash_read_managed (uint8_t offset); +flash_read_managed (uint32_t offset); /** Read an array of data from the memory starting at the address less the * offset provided in parameters. The data read will be stored in the buffer @@ -85,7 +106,7 @@ flash_read_managed (uint8_t offset); * \param buffer the buffer to store the data. */ void -flash_read_managed_array (uint8_t offset, uint8_t *buffer); +flash_read_managed_array (uint32_t offset, uint8_t *buffer); /** Write a data with a managed array. * \param data to store in the memory. diff --git a/digital/avr/modules/flash/test/Makefile b/digital/avr/modules/flash/test/Makefile index 38fa6706..e8552eb6 100644 --- a/digital/avr/modules/flash/test/Makefile +++ b/digital/avr/modules/flash/test/Makefile @@ -1,7 +1,7 @@ BASE = ../../.. PROGS = test_flash test_flash_SOURCES = test-flash.c -MODULES = utils spi flash +MODULES = utils spi flash proto uart CONFIGFILE = avrconfig.h # atmega8, atmega8535, atmega128... AVR_MCU = atmega8 diff --git a/digital/avr/modules/flash/test/test-flash.c b/digital/avr/modules/flash/test/test-flash.c index d5e13089..5ca564a7 100644 --- a/digital/avr/modules/flash/test/test-flash.c +++ b/digital/avr/modules/flash/test/test-flash.c @@ -22,9 +22,16 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ +#include "common.h" #include "io.h" #include "../flash.h" +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ + //TODO Still don't know what to to +} + int main (void) { -- cgit v1.2.3