summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/spi
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 /digital/avr/modules/spi
parent99e387e488e94ab78f50529fdc58b3fad7ec9cae (diff)
Update the spi and flash module.
testing in progress...
Diffstat (limited to 'digital/avr/modules/spi')
-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
4 files changed, 90 insertions, 24 deletions
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;
}