summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNélio Laranjeiro2008-03-01 18:51:45 +0100
committerNélio Laranjeiro2008-03-01 18:51:45 +0100
commit566ebcdcc810172118dd0519225ad65846ea3cde (patch)
treea6e01ff80472721d1b3b4321ec81f62a36e3e957
parent99e387e488e94ab78f50529fdc58b3fad7ec9cae (diff)
Update the spi and flash module.
testing in progress...
-rw-r--r--digital/avr/modules/flash/avrconfig.h37
-rw-r--r--digital/avr/modules/flash/flash.c64
-rw-r--r--digital/avr/modules/flash/flash.h24
-rw-r--r--digital/avr/modules/flash/test/Makefile2
-rw-r--r--digital/avr/modules/flash/test/avrconfig.h17
-rw-r--r--digital/avr/modules/flash/test/test-flash.c10
-rw-r--r--digital/avr/modules/spi/spi.c53
-rw-r--r--digital/avr/modules/spi/spi.h36
-rwxr-xr-xdigital/avr/modules/spi/test/Makefile2
-rw-r--r--digital/avr/modules/spi/test/test_spi.c23
10 files changed, 206 insertions, 62 deletions
diff --git a/digital/avr/modules/flash/avrconfig.h b/digital/avr/modules/flash/avrconfig.h
new file mode 100644
index 00000000..56d64ced
--- /dev/null
+++ b/digital/avr/modules/flash/avrconfig.h
@@ -0,0 +1,37 @@
+#ifndef avrconfig_h
+#define avrconfig_h
+/* avrconfig.h */
+/* avr.flash - Flash SPI AVR module. {{{
+ *
+ * Copyright (C) 2008 Nélio Laranjeiro
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+
+/* flash - Flash SPI AVR module. */
+
+/** Flash PORT used. */
+#define AC_FLASH_PORT PORTX
+/** Flash DDR used. */
+#define AC_FLASH_DDR DDRX
+/** Flash SS pin. */
+#define AC_FLASH_BIT_SS N
+
+#endif /* avrconfig_h */
diff --git a/digital/avr/modules/flash/flash.c b/digital/avr/modules/flash/flash.c
index f781097d..e964728b 100644
--- a/digital/avr/modules/flash/flash.c
+++ b/digital/avr/modules/flash/flash.c
@@ -65,47 +65,72 @@ flash_erase (uint8_t cmd, uint32_t start_addr)
void
flash_init (void)
{
- uint8_t rsp;
+ uint8_t rsp[3];
flash_global.addr = 0x0;
/* send the read-ID instruction. */
- spi_send (0x90);
- rsp = flash_read (0x0);
+ AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS);
+ AC_FLASH_DDR |= _BV(AC_FLASH_BIT_SS);
- if (rsp == 0xBF)
- proto_send1b (0x0 ,0x1);
+ spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MASTER | SPI_MSB_FIRST |
+ SPI_CPOL_FALLING | SPI_CPHA_SETUP | SPI_FOSC_DIV16);
+
+ AC_FLASH_PORT &= ~_BV(AC_FLASH_BIT_SS);
+ spi_send (FLASH_READ_ID);
+ rsp[0] = spi_recv ();
+ rsp[1] = spi_recv ();
+ rsp[2] = spi_recv ();
+ AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS);
+
+ proto_send3b ('f',rsp[0], rsp[1], rsp[2]);
+
+ /* Search for the next address to start writting. */
+ /*for (flash_global.addr = 0, rsp = 0xFF; rsp != 0xFF; flash_global.addr +=
+ FLASH_PAGE_SIZE - 1)
+ {
+ rsp = flash_read ();
+ }
+ */
}
/** 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 (uint32_t addr, uint8_t data)
+flash_write (uint8_t data)
{
spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MSB_FIRST | SPI_MASTER |
SPI_CPOL_RISING | SPI_CPHA_SAMPLE | SPI_FOSC_DIV2);
/* Write instruction. */
- spi_send (0x2);
- flash_address (addr);
+ spi_send (FLASH_WRITE);
+ flash_address (flash_global.addr);
spi_send (data);
+ /* increment the address and verify if the address is in the range of the
+ * flash memory */
+ FLASH_ADDRESS_INC_MASK(flash_global.addr);
}
/** Read the data at the address provided.
- * \param addr the address of the data to read.
* \return the data read.
*/
uint8_t
-flash_read (uint32_t addr)
+flash_read (void)
{
+ uint8_t data;
+
spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MSB_FIRST | SPI_MASTER |
SPI_CPOL_RISING | SPI_CPHA_SAMPLE | SPI_FOSC_DIV2);
/* Send the read instruction. */
- spi_send (0x3);
- flash_address (addr);
- return spi_recv ();
+ spi_send (FLASH_READ);
+ flash_address (flash_global.addr);
+ data = spi_recv ();
+
+ /* increment the address and verify if the address is in the range of the
+ * flash memory */
+ FLASH_ADDRESS_INC_MASK(flash_global.addr);
+ return data;
}
/** Read a data from the flash memory from the address provided and for a
@@ -131,16 +156,7 @@ flash_write_array (uint32_t addr, uint8_t *data, uint32_t length);
* \return data read from the memory.
*/
uint8_t
-flash_read_managed (uint32_t offset)
-{
- if ((int32_t) flash_global.addr - (int32_t) offset < 0)
- {
- // Shall not happen.
- // TODO Assert this.
- }
-
- return flash_read (flash_global.addr - 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
diff --git a/digital/avr/modules/flash/flash.h b/digital/avr/modules/flash/flash.h
index 73c582d4..cc12786b 100644
--- a/digital/avr/modules/flash/flash.h
+++ b/digital/avr/modules/flash/flash.h
@@ -27,11 +27,22 @@
#include "common.h"
#include "io.h"
+#define FLASH_HIGH_ADDRESS 0x1FFFFF
+#define FLASH_ADDRESS_INC_MASK(val) (val = (val+1) & FLASH_HIGH_ADDRESS)
+
+#define FLASH_PAGE_SIZE 0x1000
+#define FLASH_PAGE_MASK (0xFFF)
+#define FLASH_PAGE(val) (val & FLASH_PAGE_MASK)
+
#define FLASH_ERASE_FULL 0x60
#define FLASH_ERASE_4K 0x20
#define FLASH_ERASE_32K 0x52
#define FLASH_ERASE_64K 0xD8
+#define FLASH_READ_ID 0x9F
+#define FLASH_READ 0x03
+#define FLASH_WRITE 0x2
+
struct flash_t
{
/** Current Address in the flash memory. */
@@ -52,6 +63,14 @@ flash_init (void);
void
flash_address (uint32_t addr);
+/** Flash init page.
+ * Initialise the page by write an value different of 0xFF to indicate that
+ * the page is use.
+ * \param addr
+ */
+void
+flash_init_page (uint32_t addr);
+
/** Erase the memory.
* \param erase_type the erase type..
* \param start_addr the start address.
@@ -60,18 +79,17 @@ 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 (uint32_t addr, uint8_t data);
+flash_write (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 (uint32_t addr);
+flash_read (void);
/** Read a data from the flash memory from the address provided and for a
* length of the number of bytes provided.
diff --git a/digital/avr/modules/flash/test/Makefile b/digital/avr/modules/flash/test/Makefile
index e8552eb6..9fcc8790 100644
--- a/digital/avr/modules/flash/test/Makefile
+++ b/digital/avr/modules/flash/test/Makefile
@@ -4,7 +4,7 @@ test_flash_SOURCES = test-flash.c
MODULES = utils spi flash proto uart
CONFIGFILE = avrconfig.h
# atmega8, atmega8535, atmega128...
-AVR_MCU = atmega8
+AVR_MCU = atmega128
# -O2 : speed
# -Os : size
OPTIMIZE = -O2
diff --git a/digital/avr/modules/flash/test/avrconfig.h b/digital/avr/modules/flash/test/avrconfig.h
index ac0b3b3a..c4d9f159 100644
--- a/digital/avr/modules/flash/test/avrconfig.h
+++ b/digital/avr/modules/flash/test/avrconfig.h
@@ -32,16 +32,16 @@
/* uart - UART module. */
/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */
-#define AC_UART0_PORT 0
+#define AC_UART0_PORT 1
/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800,
* 115200, 230400, 250000, 500000, 1000000. */
-#define AC_UART0_BAUDRATE 115200
+#define AC_UART0_BAUDRATE 38400
/** Send mode:
* - POLLING: no interrupts.
* - RING: interrupts, ring buffer. */
-#define AC_UART0_SEND_MODE RING
+#define AC_UART0_SEND_MODE POLLING
/** Recv mode, same as send mode. */
-#define AC_UART0_RECV_MODE RING
+#define AC_UART0_RECV_MODE POLLING
/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */
#define AC_UART0_CHAR_SIZE 8
/** Parity : ODD, EVEN, NONE. */
@@ -83,4 +83,13 @@
/** Support for quote parameter. */
#define AC_PROTO_QUOTE 1
+/* flash - Flash SPI AVR module. */
+/** Flash PORT used. */
+#define AC_FLASH_PORT PORTD
+/** Flash DDR used. */
+#define AC_FLASH_DDR DDRD
+/** Flash SS pin. */
+#define AC_FLASH_BIT_SS 5
+
+
#endif /* avrconfig_h */
diff --git a/digital/avr/modules/flash/test/test-flash.c b/digital/avr/modules/flash/test/test-flash.c
index 5ca564a7..e90db20f 100644
--- a/digital/avr/modules/flash/test/test-flash.c
+++ b/digital/avr/modules/flash/test/test-flash.c
@@ -25,6 +25,7 @@
#include "common.h"
#include "io.h"
#include "../flash.h"
+#include "modules/proto/proto.h"
void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
@@ -35,12 +36,9 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
int
main (void)
{
- uint8_t data;
-
+ proto_send0 ('z');
flash_init ();
- flash_write (0x0, 0x14);
- data = flash_read (0x0);
- data = flash_read_managed (1);
- return 1;
+
+ while (1);
}
diff --git a/digital/avr/modules/spi/spi.c b/digital/avr/modules/spi/spi.c
index ea8743a8..9039e267 100644
--- a/digital/avr/modules/spi/spi.c
+++ b/digital/avr/modules/spi/spi.c
@@ -47,8 +47,32 @@ static spi_t spi_global;
void
spi_init(uint8_t sprc)
{
+ /* Master configuration. */
+ if (sprc & _BV(MSTR))
+ {
+ SPI_DDR &= ~_BV(SPI_BIT_MISO);
+ SPI_PORT &= ~_BV(SPI_BIT_MISO);
+
+ SPI_PORT |= _BV(SPI_BIT_MOSI);
+ SPI_DDR |= _BV(SPI_BIT_MOSI);
+
+ SPI_PORT |= _BV(SPI_BIT_SCK);
+ SPI_DDR |= _BV(SPI_BIT_SCK);
+ }
+ else
+ {
+ SPI_PORT |= _BV(SPI_BIT_MISO);
+ SPI_DDR |= _BV(SPI_BIT_MISO);
+
+ SPI_DDR &= ~_BV(SPI_BIT_MOSI);
+ SPI_PORT &= ~_BV(SPI_BIT_MOSI);
+
+ SPI_DDR &= ~_BV(SPI_BIT_SCK);
+ SPI_PORT &= ~_BV(SPI_BIT_SCK);
+ }
+
SPCR = sprc;
- spi_global.interruption = sprc >> 7;
+ spi_global.interruption = SPCR & _BV(SPIE);
}
/** Send a data to the Slave.
@@ -57,16 +81,10 @@ spi_init(uint8_t sprc)
void
spi_send(uint8_t data)
{
- // enables the SPI if not enabled.
- SPCR |= SPI_ENABLE;
+ // Wait the end of the transmission.
+ while(!(SPSR & _BV(SPIF)));
SPDR = data;
-
- if (!spi_global.interruption)
- {
- // Wait the end of the transmission.
- while(!(SPSR & (1<<SPIF)));
- }
}
/** Receive a data from the SPI bus.
@@ -76,12 +94,26 @@ uint8_t
spi_recv(void)
{
/* Wait for reception complete */
- while(!(SPSR & (1<<SPIF)));
+ while(!(SPSR & _BV(SPIF)));
/* Return data register */
return SPDR;
}
+/** Send and receive data.
+ * \param data the data to send.
+ * \return the data received.
+ */
+uint8_t
+spi_send_and_recv (uint8_t data)
+{
+ // Wait the end of the transmission.
+ while(!(SPSR & _BV(SPIF)));
+
+ SPDR = data;
+ return SPDR;
+}
+
/** Return the status register from the SPI driver.
* \return the status register value
*/
@@ -91,4 +123,3 @@ spi_status(void)
return SPSR;
}
-
diff --git a/digital/avr/modules/spi/spi.h b/digital/avr/modules/spi/spi.h
index be1ec0e7..e7e2caa8 100644
--- a/digital/avr/modules/spi/spi.h
+++ b/digital/avr/modules/spi/spi.h
@@ -26,22 +26,37 @@
* }}} */
#include "io.h"
-#define SPI_IT_ENABLE 0x80
+#define SPI_DDR DDRB
+#define SPI_PORT PORTB
+
+#if defined (__AVR_ATmega128__)
+#define SPI_BIT_SCK 1
+#define SPI_BIT_MOSI 2
+#define SPI_BIT_MISO 3
+#elif defined (__AVR_ATmega16__)
+#define SPI_BIT_SCK 7
+#define SPI_BIT_MOSI 5
+#define SPI_BIT_MISO 6
+#else
+#error "Not implemented"
+#endif
+
+#define SPI_IT_ENABLE _BV(SPIE)
#define SPI_IT_DISABLE 0x0
-#define SPI_ENABLE 0x40
+#define SPI_ENABLE _BV(SPE)
#define SPI_DISABLE 0x0
#define SPI_MSB_FIRST 0x00
-#define SPI_LSB_FIRST 0x20
-#define SPI_MASTER 0x10
+#define SPI_LSB_FIRST _BV(DORD)
+#define SPI_MASTER _BV(MSTR)
#define SPI_SLAVE 0x00
#define SPI_CPOL_RISING 0x0
-#define SPI_CPOL_FALLING 0x8
+#define SPI_CPOL_FALLING _BV(CPOL)
#define SPI_CPHA_SAMPLE 0x0
-#define SPI_CPHA_SETUP 0x4
+#define SPI_CPHA_SETUP _BV(CPHA)
enum spi_fosc_t
{
- SPI_FOSC_DIV_4,
+ SPI_FOSC_DIV4,
SPI_FOSC_DIV16,
SPI_FOSC_DIV64,
SPI_FOSC_DIV128,
@@ -83,6 +98,13 @@ spi_send(uint8_t data);
uint8_t
spi_recv(void);
+/** Send and receive data.
+ * \param data the data to send.
+ * \return the data received.
+ */
+uint8_t
+spi_send_and_recv (uint8_t data);
+
/** Return the status register from the SPI driver.
* \return the status register value
*/
diff --git a/digital/avr/modules/spi/test/Makefile b/digital/avr/modules/spi/test/Makefile
index 6992c660..9da68382 100755
--- a/digital/avr/modules/spi/test/Makefile
+++ b/digital/avr/modules/spi/test/Makefile
@@ -1,7 +1,7 @@
BASE = ../../..
PROGS = test_spi
test_spi_SOURCES = test_spi.c
-MODULES = utils spi
+MODULES = utils spi uart proto
CONFIGFILE = avrconfig.h
# atmega8, atmega8535, atmega128...
AVR_MCU = atmega8
diff --git a/digital/avr/modules/spi/test/test_spi.c b/digital/avr/modules/spi/test/test_spi.c
index a7f512f1..a17e6271 100644
--- a/digital/avr/modules/spi/test/test_spi.c
+++ b/digital/avr/modules/spi/test/test_spi.c
@@ -24,6 +24,8 @@
* }}} */
#include <stdint.h>
#include <common.h>
+
+#include "modules/proto/proto.h"
#include "io.h"
#include "../spi.h"
@@ -33,13 +35,19 @@ spi_interruption_function (void *user_data, uint8_t data)
{
}
+void
+proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
+{
+ //TODO Still don't know what to to
+}
+
int
main (void)
{
- uint8_t test [10];
- uint8_t res;
+ //uint8_t test [10];
+ //uint8_t res;
- res = SPI_IT_ENABLE;
+ /*res = SPI_IT_ENABLE;
spi_init (res);
res = SPI_IT_DISABLE;
spi_init (res);
@@ -63,18 +71,23 @@ main (void)
spi_init (res);
res = SPI_CPHA_SETUP;
spi_init (res);
+ */
//initialise the spi.
spi_init (0x14);
- test[0] = 0x2;
+ proto_send0 (PORTB);
+ proto_send0 (DDRB);
+
+
+ /*test[0] = 0x2;
test[1] = 0x3;
test[2] = 0x4;
spi_send (3);
res = spi_recv ();
-
+ */
return 0;
}