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/flash/flash.c | 9 +++++---- digital/avr/modules/flash/test/Makefile | 7 +------ digital/avr/modules/flash/test/test-flash.c | 24 +++++++++++++++++++++--- digital/avr/modules/spi/spi.c | 14 +++++++++----- digital/avr/modules/spi/spi.h | 2 ++ 5 files changed, 38 insertions(+), 18 deletions(-) (limited to 'digital/avr') diff --git a/digital/avr/modules/flash/flash.c b/digital/avr/modules/flash/flash.c index e964728b..990fac26 100644 --- a/digital/avr/modules/flash/flash.c +++ b/digital/avr/modules/flash/flash.c @@ -68,13 +68,13 @@ flash_init (void) uint8_t rsp[3]; flash_global.addr = 0x0; - /* send the read-ID instruction. */ AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); AC_FLASH_DDR |= _BV(AC_FLASH_BIT_SS); - spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MASTER | SPI_MSB_FIRST | - SPI_CPOL_FALLING | SPI_CPHA_SETUP | SPI_FOSC_DIV16); - + /* send the read-ID instruction. */ + spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MASTER | SPI_MSB_FIRST + | SPI_MASTER | 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 (); @@ -83,6 +83,7 @@ flash_init (void) AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); proto_send3b ('f',rsp[0], rsp[1], rsp[2]); + /* TODO: disable flash usage if no flash is found? */ /* Search for the next address to start writting. */ /*for (flash_global.addr = 0, rsp = 0xFF; rsp != 0xFF; flash_global.addr += diff --git a/digital/avr/modules/flash/test/Makefile b/digital/avr/modules/flash/test/Makefile index 9fcc8790..ff34bc7f 100644 --- a/digital/avr/modules/flash/test/Makefile +++ b/digital/avr/modules/flash/test/Makefile @@ -1,5 +1,5 @@ BASE = ../../.. -PROGS = test_flash +AVR_PROGS = test_flash test_flash_SOURCES = test-flash.c MODULES = utils spi flash proto uart CONFIGFILE = avrconfig.h @@ -9,9 +9,4 @@ AVR_MCU = atmega128 # -Os : size OPTIMIZE = -O2 -# Test compilations. -#TEST_MCU = atmega8 atmega8535 atmega128 -#TEST_CONFIGFILES = avrconfig_ring.h avrconfig_polling.h avrconfig_twoports.h -#avrconfig_twoports_TEST_MCU = atmega128 - include $(BASE)/make/Makefile.gen diff --git a/digital/avr/modules/flash/test/test-flash.c b/digital/avr/modules/flash/test/test-flash.c index e90db20f..08417f64 100644 --- a/digital/avr/modules/flash/test/test-flash.c +++ b/digital/avr/modules/flash/test/test-flash.c @@ -26,19 +26,37 @@ #include "io.h" #include "../flash.h" #include "modules/proto/proto.h" +#include "modules/utils/utils.h" +#include "modules/uart/uart.h" void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) { - //TODO Still don't know what to to +#define c(cmd, size) (cmd << 8 | size) + switch (c (cmd, size)) + { + case c ('z', 0): + /* Reset */ + utils_reset (); + break; + default: + /* Error */ + proto_send0 ('?'); + return; + } + /* Acknowledge what has been done */ + proto_send (cmd, size, args); } int main (void) { + uart0_init (); proto_send0 ('z'); + proto_send0 ('c'); flash_init (); - - while (1); + proto_send0 ('f'); + while (1) + proto_accept (uart0_getc ()); } 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