diff options
Diffstat (limited to 'platforms/avr/drivers/spi_master.h')
-rw-r--r-- | platforms/avr/drivers/spi_master.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/platforms/avr/drivers/spi_master.h b/platforms/avr/drivers/spi_master.h new file mode 100644 index 000000000..8a30f47ae --- /dev/null +++ b/platforms/avr/drivers/spi_master.h | |||
@@ -0,0 +1,59 @@ | |||
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 | #pragma once | ||
18 | |||
19 | #include <stdbool.h> | ||
20 | |||
21 | #include "gpio.h" | ||
22 | |||
23 | typedef int16_t spi_status_t; | ||
24 | |||
25 | // Hardware SS pin is defined in the header so that user code can refer to it | ||
26 | #if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) | ||
27 | # define SPI_SS_PIN B0 | ||
28 | #elif defined(__AVR_ATmega32A__) | ||
29 | # define SPI_SS_PIN B4 | ||
30 | #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) | ||
31 | # define SPI_SS_PIN B2 | ||
32 | #endif | ||
33 | |||
34 | #define SPI_STATUS_SUCCESS (0) | ||
35 | #define SPI_STATUS_ERROR (-1) | ||
36 | #define SPI_STATUS_TIMEOUT (-2) | ||
37 | |||
38 | #define SPI_TIMEOUT_IMMEDIATE (0) | ||
39 | #define SPI_TIMEOUT_INFINITE (0xFFFF) | ||
40 | |||
41 | #ifdef __cplusplus | ||
42 | extern "C" { | ||
43 | #endif | ||
44 | void spi_init(void); | ||
45 | |||
46 | bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor); | ||
47 | |||
48 | spi_status_t spi_write(uint8_t data); | ||
49 | |||
50 | spi_status_t spi_read(void); | ||
51 | |||
52 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length); | ||
53 | |||
54 | spi_status_t spi_receive(uint8_t *data, uint16_t length); | ||
55 | |||
56 | void spi_stop(void); | ||
57 | #ifdef __cplusplus | ||
58 | } | ||
59 | #endif | ||