-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi.c
48 lines (42 loc) · 994 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
/**
* @file spi.c
* @author Paul Nykiel
* @date 12.04.19
* @brief Implementation of the library functions for the serial peripheral interface.
* @ingroup HAL
*/
#include "spi.h"
#include <avr/interrupt.h>
#include <avr/io.h>
static volatile uint8_t *_buf = 0;
static volatile uint16_t _size = 0;
static spi_callback_t _callback = 0;
ISR(SPI_STC_vect) {
*_buf = SPDR;
++_buf;
if (_size > 0) {
SPDR = *_buf;
--_size;
} else {
if (_callback != 0) {
(*_callback)();
}
}
}
void spi_init(bool lsb_first, spi_prescaler_t prescaler) {
SPCR = 0b11010000; // Enabled, Interrupts enabled, Slave mode
if (lsb_first) {
SPCR |= (1 << DORD);
}
SPCR |= prescaler & 0b11u;
SPSR = (prescaler >> 2u) & 0b1u;
}
void spi_tx_rx(uint8_t *buf, uint16_t size, spi_callback_t callback) {
_buf = buf;
_size = size;
_callback = callback;
if (_size > 0) {
SPDR = *_buf;
--_size;
}
}