]> cloudbase.mooo.com Git - z180-stamp.git/blob - include/spi.h
Integrate fatfs. Add some sd card test commands.
[z180-stamp.git] / include / spi.h
1 /*
2 * spi.h
3 *
4 * Created on: 17.04.2009
5 * Author: leo
6 */
7
8 #ifndef SPI_H_
9 #define SPI_H_
10
11 #define SPI_PORT PORTB /* SPI Connection port */
12 #define SPI_DDR DDRB /* SPI Direction port */
13 #define SPI_SS 0
14 #define SPI_SCK 1
15 #define SPI_MOSI 2
16 #define SPI_MISO 3
17
18
19 /* SPI macros */
20
21 #define SPI_SET_SPEED_F_2 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (0<<SPR0); SPSR = (1<<SPI2X); } while(0)
22 #define SPI_SET_SPEED_F_4 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (0<<SPR0); SPSR = (0<<SPI2X); } while(0)
23 #define SPI_SET_SPEED_F_8 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (1<<SPR0); SPSR = (1<<SPI2X); } while(0)
24 #define SPI_SET_SPEED_F_16 do {SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (1<<SPR0); SPSR = (0<<SPI2X); } while(0)
25 #define SPI_SET_SPEED_F_32 do {SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (0<<SPR0); SPSR = (1<<SPI2X); } while(0)
26 #define SPI_SET_SPEED_F_64 do {SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (0<<SPR0); SPSR = (0<<SPI2X); } while(0)
27 #define SPI_SET_SPEED_F_128 do {SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0); SPSR = (0<<SPI2X); } while(0)
28
29 /** switch to fast SPI Clock */
30 #define SPISetFastClock() SPI_SET_SPEED_F_2
31 #define SPISetSlowClock() SPI_SET_SPEED_F_8
32 #define SPISetMMCInitClock() SPI_SET_SPEED_F_64
33
34
35 static inline void spi_wait() {
36 loop_until_bit_is_set(SPSR,SPIF);
37 }
38
39 static inline void spi_write(uint8_t a) {
40 SPDR = a;
41 }
42
43 static inline void spi_xmit(uint8_t a){
44 spi_write(a);
45 spi_wait();
46 }
47
48 static inline uint8_t spi_rcvr(void) {
49 SPDR = 0xFF;
50 spi_wait();
51 return SPDR;
52 }
53
54 #endif /* SPI_H_ */