aboutsummaryrefslogtreecommitdiff
path: root/drivers/avr/spi_master.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/avr/spi_master.c')
-rw-r--r--drivers/avr/spi_master.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/drivers/avr/spi_master.c b/drivers/avr/spi_master.c
new file mode 100644
index 000000000..497d50536
--- /dev/null
+++ b/drivers/avr/spi_master.c
@@ -0,0 +1,163 @@
1/* Copyright 2020
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17#include <avr/io.h>
18
19#include "spi_master.h"
20#include "quantum.h"
21#include "timer.h"
22
23#if defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
24# define SPI_SCK_PIN B1
25# define SPI_MOSI_PIN B2
26# define SPI_MISO_PIN B3
27#elif defined(__AVR_ATmega32A__)
28# define SPI_SCK_PIN B7
29# define SPI_MOSI_PIN B5
30# define SPI_MISO_PIN B6
31#elif defined(__AVR_ATmega328P__)
32# define SPI_SCK_PIN B5
33# define SPI_MOSI_PIN B3
34# define SPI_MISO_PIN B4
35#endif
36
37static pin_t currentSlavePin = NO_PIN;
38static uint8_t currentSlaveConfig = 0;
39static bool currentSlave2X = false;
40
41void spi_init(void) {
42 writePinHigh(SPI_SS_PIN);
43 setPinOutput(SPI_SCK_PIN);
44 setPinOutput(SPI_MOSI_PIN);
45 setPinInput(SPI_MISO_PIN);
46
47 SPCR = (_BV(SPE) | _BV(MSTR));
48}
49
50void spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint8_t divisor) {
51 if (currentSlavePin == NO_PIN && slavePin != NO_PIN) {
52 if (lsbFirst) {
53 currentSlaveConfig |= _BV(DORD);
54 }
55
56 switch (mode) {
57 case 1:
58 currentSlaveConfig |= _BV(CPHA);
59 break;
60 case 2:
61 currentSlaveConfig |= _BV(CPOL);
62 break;
63 case 3:
64 currentSlaveConfig |= (_BV(CPOL) | _BV(CPHA));
65 break;
66 }
67
68 uint8_t roundedDivisor = 1;
69 while (roundedDivisor < divisor) {
70 roundedDivisor <<= 1;
71 }
72
73 switch (roundedDivisor) {
74 case 16:
75 currentSlaveConfig |= _BV(SPR0);
76 break;
77 case 64:
78 currentSlaveConfig |= _BV(SPR1);
79 break;
80 case 128:
81 currentSlaveConfig |= (_BV(SPR1) | _BV(SPR0));
82 break;
83 case 2:
84 currentSlave2X = true;
85 break;
86 case 8:
87 currentSlave2X = true;
88 currentSlaveConfig |= _BV(SPR0);
89 break;
90 case 32:
91 currentSlave2X = true;
92 currentSlaveConfig |= _BV(SPR1);
93 break;
94 }
95
96 SPSR |= currentSlaveConfig;
97 currentSlavePin = slavePin;
98 setPinOutput(currentSlavePin);
99 writePinLow(currentSlavePin);
100 }
101}
102
103spi_status_t spi_write(uint8_t data, uint16_t timeout) {
104 SPDR = data;
105
106 uint16_t timeout_timer = timer_read();
107 while (!(SPSR & _BV(SPIF))) {
108 if ((timeout != SPI_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
109 return SPI_STATUS_TIMEOUT;
110 }
111 }
112
113 return SPDR;
114}
115
116spi_status_t spi_read(uint16_t timeout) {
117 SPDR = 0x00; // Dummy
118
119 uint16_t timeout_timer = timer_read();
120 while (!(SPSR & _BV(SPIF))) {
121 if ((timeout != SPI_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
122 return SPI_STATUS_TIMEOUT;
123 }
124 }
125
126 return SPDR;
127}
128
129spi_status_t spi_transmit(const uint8_t *data, uint16_t length, uint16_t timeout) {
130 spi_status_t status = SPI_STATUS_ERROR;
131
132 for (uint16_t i = 0; i < length; i++) {
133 status = spi_write(data[i], timeout);
134 }
135
136 return status;
137}
138
139spi_status_t spi_receive(uint8_t *data, uint16_t length, uint16_t timeout) {
140 spi_status_t status = SPI_STATUS_ERROR;
141
142 for (uint16_t i = 0; i < length; i++) {
143 status = spi_read(timeout);
144
145 if (status > 0) {
146 data[i] = status;
147 }
148 }
149
150 return (status < 0) ? status : SPI_STATUS_SUCCESS;
151}
152
153void spi_stop(void) {
154 if (currentSlavePin != NO_PIN) {
155 setPinOutput(currentSlavePin);
156 writePinHigh(currentSlavePin);
157 currentSlavePin = NO_PIN;
158 SPCR &= ~(currentSlaveConfig);
159 currentSlaveConfig = 0;
160 SPSR = 0;
161 currentSlave2X = false;
162 }
163}