Prusa3d Marlin fork
spi.h
1 //spi.h - hardware SPI
2 #ifndef SPI_H
3 #define SPI_H
4 
5 #include <inttypes.h>
6 #include <avr/io.h>
7 #include "config.h"
8 
9 
10 #define SPI_SPCR(rat, pha, pol, mst, dor) ((rat & 3) | (pha?(1<<CPHA):0) | (pol?(1<<CPOL):0) | (mst?(1<<MSTR):0) | (dor?(1<<DORD):0) | (1<<SPE))
11 #define SPI_SPSR(rat) ((rat & 4)?(1<<SPI2X):0)
12 
13 #define DD_SS 0
14 #define DD_SCK 1
15 #define DD_MOSI 2
16 #define DD_MISO 3
17 
18 #if defined(__cplusplus)
19 extern "C" {
20 #endif //defined(__cplusplus)
21 
22 static inline void spi_init()
23 {
24  DDRB &= ~((1 << DD_SCK) | (1 << DD_MOSI) | (1 << DD_MISO));
25  DDRB |= (1 << DD_SS) | (1 << DD_SCK) | (1 << DD_MOSI);
26  PORTB &= ~((1 << DD_SCK) | (1 << DD_MOSI) | (1 << DD_MISO));
27  PORTB |= (1 << DD_SS);
28  SPCR = SPI_SPCR(0, 0, 0, 1, 0); //SPE=1, MSTR=1 (0x50)
29  SPSR = 0x00;
30 }
31 
32 static inline void spi_setup(uint8_t spcr, uint8_t spsr)
33 {
34  SPCR = spcr;
35  SPSR = spsr;
36 }
37 
38 static inline uint8_t spi_txrx(uint8_t tx)
39 {
40  SPDR = tx;
41  while (!(SPSR & (1 << SPIF)));
42  return SPDR;
43 }
44 
45 #if defined(__cplusplus)
46 }
47 #endif //defined(__cplusplus)
48 
49 #endif //SPI_H