diff --git a/Makefile b/Makefile index 3faee76..dd39ff1 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ RESETPORT = /dev/ttyU0 TARGET = pyrorf -SRC = main.c lcd.c +SRC = main.c lcd.c spi.c CXXSRC = ASRC = i2cmaster.S MCU = atmega2560 diff --git a/schema.pdf b/schema.pdf new file mode 100644 index 0000000..0710a30 Binary files /dev/null and b/schema.pdf differ diff --git a/spi.c b/spi.c index cb4a1b8..e293a04 100644 --- a/spi.c +++ b/spi.c @@ -2,78 +2,21 @@ #include -void spi_begin() { -#if defined(SPCR) -#error to write ... - // Set SS to high so a connected chip will be "deselected" by default - digitalWrite(SS, HIGH); - // When the SS pin is set as OUTPUT, it can be used as - // a general purpose output port (it doesn't influence - // SPI operations). - pinMode(SS, OUTPUT); - // Warning: if the SS pin ever becomes a LOW INPUT then SPI - // automatically switches to Slave, so the data direction of - // the SS pin MUST be kept as OUTPUT. - SPCR |= _BV(MSTR); - SPCR |= _BV(SPE); - // Set direction register for SCK and MOSI pin. - // MISO pin automatically overrides to INPUT. - // By doing this AFTER enabling SPI, we avoid accidentally - // clocking in a single bit since the lines go directly - // from "input" to SPI control. - // http://code.google.com/p/arduino/issues/detail?id=888 - pinMode(SCK, OUTPUT); - pinMode(MOSI, OUTPUT); -#else - PRR &= ~(_BV(PRUSI)); - USICR = _BV(USIWM0); - USCK_DDR |= _BV(USCK_BIT); //set the USCK pin as output - DO_DDR |= _BV(DO_BIT); //set the DO pin as output - DI_DDR &= ~_BV(DI_BIT); //set the DI pin as input -#endif + +void spi_init_master() { + + SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0); + + SPI_PORT |= _BV(SCK_PIN); + SPI_DDR |= _BV(MOSI_PIN) | _BV(SCK_PIN) | _BV(SS_PIN); + } uint8_t spi_transfer(uint8_t b) { -#if defined(SPCR) + SPDR = b; while (!(SPSR & _BV(SPIF))); return SPDR; -#else - USIDR = b; - USISR = _BV(USIOIF); - do { - USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC); -// _delay_ms(1); - }while ((USISR & _BV(USIOIF)) == 0); - return USIDR; -#endif -} - -void spi_end() { -#if defined(SPCR) - SPCR &= ~_BV(SPE); -#else - USICR &= ~(_BV(USIWM1) | _BV(USIWM0)); -#endif -} - -void spi_setBitOrder(uint8_t bitOrder) -{ -#if defined(SPCR) - if(bitOrder == LSBFIRST) { - SPCR |= _BV(DORD); - } else { - SPCR &= ~(_BV(DORD)); - } -#endif -} - -void spi_setDataMode(uint8_t mode) -{ -#if defined(SPCR) - SPCR = (SPCR & ~SPI_MODE_MASK) | mode; -#else - -#endif + } diff --git a/spi.h b/spi.h index eabdb91..10325b4 100644 --- a/spi.h +++ b/spi.h @@ -4,20 +4,32 @@ #include #include #include +#include "defines.h" + #ifdef __cplusplus extern "C"{ #endif +#if defined (__AVR_ATmega32__) -//SPI data modes -#define SPI_MODE0 0x00 -#define SPI_MODE1 0x04 +#elif defined (__AVR_ATmega2560__) +#define SPI_PORT PORTB +#define SPI_DDR DDRB + +#define SS_PIN PB0 +#define SCK_PIN PB1 +#define MOSI_PIN PB2 +#define MISO_PIN PB3 + +#endif + + +void spi_init_master(void); +void spi_init_slave(void); -void spi_begin(void); -void spi_setDataMode(uint8_t spiDataMode); uint8_t spi_transfer(uint8_t spiData); -void spi_end(void); + #ifdef __cplusplus