From a87ddfd43ed21ce3227b41624066d2b4ea468e7e Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sun, 2 Mar 2008 11:26:02 +0100 Subject: * digital/avr/modules/spi, digital/avr/modules/flash: - master should drive SS. - to receive, master should send something. - testing SPIF before the first sent does not work. - fixed flash test. --- digital/avr/modules/spi/spi.c | 14 +++++++++----- digital/avr/modules/spi/spi.h | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'digital/avr/modules/spi') diff --git a/digital/avr/modules/spi/spi.c b/digital/avr/modules/spi/spi.c index 9039e267..73cb1706 100644 --- a/digital/avr/modules/spi/spi.c +++ b/digital/avr/modules/spi/spi.c @@ -26,8 +26,6 @@ #include "common.h" #include "spi.h" -#define SPI_DIRVER_SIGNAL SIGNAL_SPI - /** For host */ #ifdef HOST static uint8_t SPCR; @@ -50,6 +48,9 @@ spi_init(uint8_t sprc) /* Master configuration. */ if (sprc & _BV(MSTR)) { + SPI_PORT |= _BV(SPI_BIT_SS); + SPI_DDR |= _BV(SPI_BIT_SS); + SPI_DDR &= ~_BV(SPI_BIT_MISO); SPI_PORT &= ~_BV(SPI_BIT_MISO); @@ -61,6 +62,9 @@ spi_init(uint8_t sprc) } else { + SPI_DDR &= ~_BV(SPI_BIT_SS); + SPI_PORT |= _BV(SPI_BIT_SS); + SPI_PORT |= _BV(SPI_BIT_MISO); SPI_DDR |= _BV(SPI_BIT_MISO); @@ -81,10 +85,9 @@ spi_init(uint8_t sprc) void spi_send(uint8_t data) { + SPDR = data; // Wait the end of the transmission. while(!(SPSR & _BV(SPIF))); - - SPDR = data; } /** Receive a data from the SPI bus. @@ -93,6 +96,7 @@ spi_send(uint8_t data) uint8_t spi_recv(void) { + SPDR = 0; /* Wait for reception complete */ while(!(SPSR & _BV(SPIF))); @@ -107,10 +111,10 @@ spi_recv(void) uint8_t spi_send_and_recv (uint8_t data) { + SPDR = data; // Wait the end of the transmission. while(!(SPSR & _BV(SPIF))); - SPDR = data; return SPDR; } diff --git a/digital/avr/modules/spi/spi.h b/digital/avr/modules/spi/spi.h index e7e2caa8..9d43eec0 100644 --- a/digital/avr/modules/spi/spi.h +++ b/digital/avr/modules/spi/spi.h @@ -30,10 +30,12 @@ #define SPI_PORT PORTB #if defined (__AVR_ATmega128__) +#define SPI_BIT_SS 0 #define SPI_BIT_SCK 1 #define SPI_BIT_MOSI 2 #define SPI_BIT_MISO 3 #elif defined (__AVR_ATmega16__) +#define SPI_BIT_SS 4 #define SPI_BIT_SCK 7 #define SPI_BIT_MOSI 5 #define SPI_BIT_MISO 6 -- cgit v1.2.3