diff options
Diffstat (limited to 'include/spi.h')
-rw-r--r-- | include/spi.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/include/spi.h b/include/spi.h new file mode 100644 index 0000000..4638476 --- /dev/null +++ b/include/spi.h @@ -0,0 +1,54 @@ +/* + * (C) Copyright 2009,2014 Leo C. <erbl259-lmu@yahoo.de> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef SPI_H_ +#define SPI_H_ + +#define SPI_PORT PORTB /* SPI Connection port */ +#define SPI_DDR DDRB /* SPI Direction port */ +#define SPI_SS 0 +#define SPI_SCK 1 +#define SPI_MOSI 2 +#define SPI_MISO 3 + + +/* SPI macros */ + +#define SPI_SET_SPEED_F_2 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (0<<SPR0); SPSR = (1<<SPI2X); } while(0) +#define SPI_SET_SPEED_F_4 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (0<<SPR0); SPSR = (0<<SPI2X); } while(0) +#define SPI_SET_SPEED_F_8 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (1<<SPR0); SPSR = (1<<SPI2X); } while(0) +#define SPI_SET_SPEED_F_16 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (1<<SPR0); SPSR = (0<<SPI2X); } while(0) +#define SPI_SET_SPEED_F_32 do {SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (0<<SPR0); SPSR = (1<<SPI2X); } while(0) +#define SPI_SET_SPEED_F_64 do {SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (0<<SPR0); SPSR = (0<<SPI2X); } while(0) +#define SPI_SET_SPEED_F_128 do {SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0); SPSR = (0<<SPI2X); } while(0) + +/** switch to fast SPI Clock */ +#define SPISetFastClock() SPI_SET_SPEED_F_2 +#define SPISetSlowClock() SPI_SET_SPEED_F_8 +#define SPISetMMCInitClock() SPI_SET_SPEED_F_64 + +#define SPI_OFF() do { SPCR = 0; } while(0) + +static inline __attribute__((always_inline)) void spi_wait() { + loop_until_bit_is_set(SPSR,SPIF); +} + +static inline __attribute__((always_inline)) void spi_write(uint8_t a) { + SPDR = a; +} + +static inline void spi_xmit(uint8_t a){ + spi_write(a); + spi_wait(); +} + +static inline uint8_t spi_rcvr(void) { + SPDR = 0xFF; + spi_wait(); + return SPDR; +} + +#endif /* SPI_H_ */ |