diff options
Diffstat (limited to 'src/inc/spi/spi.c')
-rwxr-xr-x | src/inc/spi/spi.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/inc/spi/spi.c b/src/inc/spi/spi.c new file mode 100755 index 0000000..b04fe7e --- /dev/null +++ b/src/inc/spi/spi.c | |||
@@ -0,0 +1,56 @@ | |||
1 | #include "spi.h" | ||
2 | |||
3 | void spi_init() | ||
4 | { | ||
5 | // setup SPI-Port | ||
6 | DDR_SPI = (1<<DD_MOSI) | (1<<DD_SS) | (1<<DD_SCK); | ||
7 | PORT_SPI |= (1<<DD_SS); | ||
8 | |||
9 | // SPI Control Register (SPCR) | ||
10 | // SPI Enable=1 | Data Order=LSB | SPI_Master=1 (!make SS stay HIGH!) | ||
11 | // SPCR |= (1<<SPE) | (1<<DORD) | (1<<MSTR) | (1<<CPOL) | (1<<CPHA) | (1<<SPR1) | (1<<SPR0); | ||
12 | // (1<<DORD) - would be LSB-mode (, but we need MSB-mode!) | ||
13 | SPCR = (1<<SPE) | (1<<MSTR); // (1<<SPR1) | (1<<SPR0) | ||
14 | spi_setspeed(SPICLK_DEFAULT); | ||
15 | // setup SPI options | ||
16 | /// SPSR &= ~(1<<SPI2X); // do not use SPI speed-doubler | ||
17 | } | ||
18 | |||
19 | void spi_setspeed(SPICLK speed) | ||
20 | { | ||
21 | if(speed < 4) // nominal clock speeds | ||
22 | { | ||
23 | SPSR &= ~(1<<SPI2X); // do not use SPI speed-doubler | ||
24 | SPCR |= speed; | ||
25 | } | ||
26 | else // use SPI2X | ||
27 | { | ||
28 | SPSR |= (1<<SPI2X); // use SPI speed-doubler | ||
29 | SPCR |= (speed - 0x4); | ||
30 | } | ||
31 | _delay_ms(2); | ||
32 | } | ||
33 | |||
34 | void spi_putc(unsigned char c) | ||
35 | { | ||
36 | /// uart_putc('>'); | ||
37 | /// uart_putc(c); | ||
38 | /// uart_putc('\n'); | ||
39 | SPDR = c; | ||
40 | loop_until_bit_is_set(SPSR, SPIF); | ||
41 | /// stdout_put_string(uart_putc, "- [done]\n"); | ||
42 | } | ||
43 | |||
44 | unsigned char spi_getc() | ||
45 | { | ||
46 | SPDR = 0xFF; // init receive register with value | ||
47 | /* Wait for reception complete */ | ||
48 | while(!(SPSR & (1<<SPIF))); | ||
49 | /* Return data register */ | ||
50 | return SPDR; | ||
51 | /// unsigned char c = SPDR; | ||
52 | /// uart_putc('<'); | ||
53 | /// uart_putc(c); | ||
54 | /// uart_putc('\n'); | ||
55 | /// return c; | ||
56 | } | ||