From 7500ab5bbecf67689c5069d2a8ae358b4f75899c Mon Sep 17 00:00:00 2001 From: NĂ©lio Laranjeiro Date: Tue, 21 Oct 2008 00:19:59 +0200 Subject: avr/module/flash: Closes #34 * Removed the flah context. * split the page search to know the next page usable. (to debug) * Added a new case in the flash test. --- digital/avr/modules/flash/flash.c | 44 +++++++++++++---------------- digital/avr/modules/flash/flash.h | 29 +++++++------------ digital/avr/modules/flash/test/test-flash.c | 5 ++++ 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/digital/avr/modules/flash/flash.c b/digital/avr/modules/flash/flash.c index bc5d50ef..edca7273 100644 --- a/digital/avr/modules/flash/flash.c +++ b/digital/avr/modules/flash/flash.c @@ -27,8 +27,6 @@ #include "modules/spi/spi.h" #include "modules/utils/utils.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. @@ -62,10 +60,6 @@ flash_erase (uint8_t cmd, uint32_t start_addr) /* Send the start address */ flash_address (start_addr); } - else - { - flash_global.write_addr = 0x0; - } AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); while (flash_is_busy()); @@ -100,15 +94,13 @@ flash_read_status (void) } /** Initialise the flash memory. - * \return the flash context useful to access to the addr for debug. + * \return true if the flash is present, false otherwise. */ -flash_t * +uint8_t flash_init (void) { uint8_t rsp[3]; - uint32_t addr; - flash_global.write_addr = 0x0; AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); AC_FLASH_DDR |= _BV(AC_FLASH_BIT_SS); @@ -124,8 +116,10 @@ flash_init (void) rsp[2] = spi_recv (); AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); + if (rsp[0] != 0xBF) + return 0; + proto_send3b ('f',rsp[0], rsp[1], rsp[2]); - proto_send1b ('s', flash_read_status()); if (flash_status_aai()) { @@ -143,29 +137,29 @@ flash_init (void) /* Read the flash status. */ proto_send1b ('s', flash_read_status()); - /* If flash read status id not correct disable the flash */ - if (flash_read_status()) - flash_global.status = FLASH_DISABLE; + return 1; +} - /* Search for the next address to start writing. */ +/** Find the next sector to write. + * \return the address of the next sector. + */ +uint32_t +flash_sector_next (void) +{ + uint32_t addr; + uint8_t rsp = 0; + /* Search for the next address to start writing. */ for (addr = 0; - (rsp[0] != 0xFF) && (addr < 0x200000); + (rsp != 0xFF) && (addr < FLASH_ADDRESS_HIGH); addr += FLASH_PAGE_SIZE - 1) { - AC_FLASH_PORT &= ~_BV(AC_FLASH_BIT_SS); - spi_send (FLASH_READ); - flash_address (addr); - rsp[0] = spi_recv(); - AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); + rsp = flash_read (addr); } - flash_global.write_addr = addr - FLASH_PAGE_SIZE + 1; - - return &flash_global; + return addr < (FLASH_ADDRESS_HIGH + 1) ? addr : FLASH_ADDRESS_ERROR; } - /** Write in the flash byte provided in parameter. * \param data the buffer to store the data. */ diff --git a/digital/avr/modules/flash/flash.h b/digital/avr/modules/flash/flash.h index 4d06fd47..80bff9ec 100644 --- a/digital/avr/modules/flash/flash.h +++ b/digital/avr/modules/flash/flash.h @@ -27,7 +27,8 @@ #include "common.h" #include "io.h" -#define FLASH_HIGH_ADDRESS 0x1FFFFF +#define FLASH_ADDRESS_HIGH 0x1FFFFF +#define FLASH_ADDRESS_ERROR 0xFFFFFF #define FLASH_ADDRESS_INC_MASK(val) (val = (val+1) & FLASH_HIGH_ADDRESS) #define FLASH_PAGE_SIZE 0x1000 @@ -51,22 +52,6 @@ #define FLASH_TBP_US 10 -enum flash_status_t -{ - FLASH_DISABLE, - FLASH_ENABLE, - FLASH_ASSERT -}; - -struct flash_t -{ - /* The next address to write the data. */ - uint32_t write_addr; - /* Status. */ - enum flash_status_t status; -}; -typedef struct flash_t flash_t; - /** 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. @@ -113,11 +98,17 @@ flash_status_aai (void) } /** Initialise the flash memory. - * \return the flash context useful to access to the addr for debug. + * \return true if the flash is present, false otherwise. */ -flash_t * +uint8_t flash_init (void); +/** Find the next sector to write. + * \return the address of the next sector. + */ +uint32_t +flash_sector_next (void); + /** Write in the flash byte provided in parameter. * \param data the buffer to store the data. */ diff --git a/digital/avr/modules/flash/test/test-flash.c b/digital/avr/modules/flash/test/test-flash.c index 546ce71b..aea03e44 100644 --- a/digital/avr/modules/flash/test/test-flash.c +++ b/digital/avr/modules/flash/test/test-flash.c @@ -86,6 +86,11 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) * - 1b: byte. */ flash_write (addr, args[3]); break; + case c ('p', 0): + /* Find the next page to write. */ + addr = flash_sector_next (); + proto_send3b ('p', addr >> 16, addr >> 8, addr); + break; default: if (cmd == 'w' && size > 4) { -- cgit v1.2.3