-
Notifications
You must be signed in to change notification settings - Fork 5
/
spi.c
52 lines (43 loc) · 875 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
/*
* spi.c
*
* Created on: 22.09.2010
* Author: krichevskiy
*/
#include "spi.h"
#include "macros.h"
void spi_init(spi_mode_enum spi_mode,
spi_data_order_enum data_order,
spi_sampling_mode_enum sampling_mode,
spi_clock_rate_enum clock_rate)
{
SPSR = (clock_rate >> 2);
SPCR = _BV(SPE)
| (spi_mode << MSTR)
| (data_order << DORD)
| (sampling_mode << CPHA)
| ((clock_rate & 0x03) << SPR0);
}
void spi_enable_interrupt(void)
{
bit_set(SPCR, SPIE);
}
void spi_disable_interupt(void)
{
bit_clr(SPCR, SPIE);
}
unsigned char spi_write_byte (unsigned char byte)
{
SPDR = byte;
loop_until_bit_is_set(SPSR, SPIF);
return SPDR;
}
void spi_write_sequence (const unsigned char *bytes, unsigned char length)
{
while (length)
{
SPDR = *bytes++;
loop_until_bit_is_set(SPSR, SPIF);
--length;
}
}