-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspi.c
59 lines (47 loc) · 902 Bytes
/
spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
#include "spi.h"
void spi_init(void)
{
// pin spi i/o
// *b0 /cs
// b1 oc1a
// *b2 /ss oc1b
// *b3 mosi
// *b4 miso
// *b5 sck
// d3 oc2b
// d5 oc0b
// d6 oc0a
#if 1
SPCR = 0x00; // ensure SPI is properly reset
_delay_ms(1); // this doesn't happen when entering DebugWire
PRR |= _BV(PRSPI); // turn SPI off
_delay_ms(1);
#endif
DDRB |= _BV(0) | _BV(2) | _BV(3) | _BV(5);
DDRB &= ~(_BV(4));
PORTB |= _BV(0); // /CS = 1
PRR &= ~_BV(PRSPI); // turn SPI on
_delay_ms(1);
SPCR = _BV(SPE) | _BV(MSTR); // use clock / 4 = 2 Mhz
SPSR = 0;
}
void spi_start(void)
{
PORTB |= _BV(6);
PORTB &= ~_BV(0); // /CS = 0
}
void spi_stop(void)
{
PORTB |= _BV(0); // /CS = 1
PORTB &= ~_BV(6);
}
uint8_t spi_io(uint8_t out)
{
SPDR = out;
while(!(SPSR & _BV(SPIF)))
(void)0;
return(SPDR);
}