diff options
Diffstat (limited to 'spi.c')
-rw-r--r-- | spi.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -0,0 +1,39 @@ +#include <stdint.h> +#include <avr/io.h> +#include <avr/interrupt.h> +#include "spi.h" + +void spi_init() +{ + DDR_SPI &= ~((1<<DD_MOSI)|(1<<DD_MISO)|(1<<DD_SS)|(1<<DD_SCK)); + DDR_SPI |= ((1<<DD_MOSI)|(1<<DD_SS)|(1<<DD_SCK)); + + SPCR = ((1<<SPE)| // SPI Enable + (0<<SPIE)| // SPI Interupt Enable + (0<<DORD)| // Data Order (0:MSB first / 1:LSB first) + (1<<MSTR)| // Master/Slave select + (1<<SPR1)|(1<<SPR0)| // SPI Clock Rate + (0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle) + (0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling) + + SPSR = (0<<SPIF)|(0<<WCOL)|(0<<SPI2X); // Double Clock Rate +} + +uint8_t spi_fast_shift(uint8_t data) +{ + SPDR = data; + while((SPSR & (1<<SPIF))==0); + return SPDR; +} + +uint8_t spi_two_byte(uint8_t cmd, uint8_t arg) +{ + uint8_t data_buf; + + spi_cs_low(); + spi_fast_shift(cmd); + data_buf = spi_fast_shift(arg); + spi_cs_high(); + + return data_buf; +} |