summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/flash
diff options
context:
space:
mode:
authorNélio Laranjeiro2008-03-05 23:48:19 +0100
committerNélio Laranjeiro2008-03-05 23:48:19 +0100
commit93b2480091b13dbea6a1f1a7265cecbb6718aa56 (patch)
tree06364935bd6fcb554fe4200d123f03938df5ea8a /digital/avr/modules/flash
parent2697928e9c98c86f862ba86a306f3edc36b385b7 (diff)
Added the polling of the busy device status for the write process.
The function write_array shall be fix in a few time.
Diffstat (limited to 'digital/avr/modules/flash')
-rw-r--r--digital/avr/modules/flash/flash.c46
-rw-r--r--digital/avr/modules/flash/flash.h6
-rw-r--r--digital/avr/modules/flash/test/test-flash.c26
3 files changed, 56 insertions, 22 deletions
diff --git a/digital/avr/modules/flash/flash.c b/digital/avr/modules/flash/flash.c
index 010f0816..326afb59 100644
--- a/digital/avr/modules/flash/flash.c
+++ b/digital/avr/modules/flash/flash.c
@@ -61,6 +61,22 @@ flash_erase (uint8_t cmd, uint32_t start_addr)
}
}
+/** Poll the busy bit in the Software Status Register of the flash memory.
+ * \return the busy bit state.
+ */
+uint8_t
+flash_is_busy (void)
+{
+ uint8_t busy;
+
+ AC_FLASH_PORT &= ~_BV(AC_FLASH_BIT_SS);
+ spi_send (FLASH_RDSR);
+ busy = spi_recv() & 0x1;
+ AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS);
+
+ return busy;
+}
+
/** Initialise the flsah memory.
*/
void
@@ -153,6 +169,9 @@ flash_write (uint32_t addr, uint8_t data)
flash_address (addr);
spi_send (data);
AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS);
+
+ /* Wait for the flash until it is busy */
+ while (flash_is_busy());
}
/** Read the data at the address provided.
@@ -169,7 +188,6 @@ flash_read (uint32_t addr)
flash_address (addr);
data = spi_recv ();
AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS);
-
return data;
}
@@ -182,27 +200,16 @@ flash_read (uint32_t addr)
void
flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length)
{
- uint8_t data[16];
- uint8_t i,j;
+ uint8_t i;
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]);
- }
- }
+ for (i = 0; i < length; i++)
+ {
+ buffer[i] = spi_recv ();
+ }
AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS);
}
@@ -210,6 +217,7 @@ flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length)
* \param addr the address to store the data.
* \param data the array to store.
* \param length the array length
+ * TODO : Fix this function.
*/
void
flash_write_array (uint32_t addr, uint8_t *data, uint32_t length)
@@ -231,8 +239,8 @@ flash_write_array (uint32_t addr, uint8_t *data, uint32_t length)
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);
+ /* Wait for the flash until it is busy */
+ while (flash_is_busy());
}
}
diff --git a/digital/avr/modules/flash/flash.h b/digital/avr/modules/flash/flash.h
index a3ef00de..8d72ba43 100644
--- a/digital/avr/modules/flash/flash.h
+++ b/digital/avr/modules/flash/flash.h
@@ -92,6 +92,12 @@ flash_erase (uint8_t cmd, uint32_t start_addr);
void
flash_send_command (uint8_t cmd);
+/** Poll the busy bit in the Software Status Register of the flash memory.
+ * \return the busy bit state.
+ */
+uint8_t
+flash_is_busy (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 35d4049d..ace1de8a 100644
--- a/digital/avr/modules/flash/test/test-flash.c
+++ b/digital/avr/modules/flash/test/test-flash.c
@@ -29,6 +29,8 @@
#include "modules/utils/utils.h"
#include "modules/uart/uart.h"
+#define TEST_BASE 0x50
+
void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
@@ -51,14 +53,32 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
int
main (void)
{
+ uint8_t data[26];
+ uint8_t data_rsp[26];
+ uint8_t i;
+
uart0_init ();
proto_send0 ('z');
proto_send0 ('c');
flash_init ();
proto_send0 ('f');
- flash_write (0x10, 'a');
- utils_delay_us (FLASH_TBP_US);
- proto_send1b ('o',flash_read(0x10));
+ /*flash_write (TEST_BASE, 'a');
+ proto_send1b ('o',flash_read(TEST_BASE));
+*/
+ for (i = 0; i < 26; i++)
+ {
+ data[i] = i + 'a';
+ }
+
+ /* Write a full array. */
+ flash_write_array (TEST_BASE + 1, data, 26);
+
+ /* Read a full array. */
+ flash_read_array (TEST_BASE + 1, data_rsp, 26);
+
+ /* Print the data_rsp to the i2c */
+ proto_send ('g', 26, data_rsp);
+
while (1)
proto_accept (uart0_getc ());
}