diff options
Diffstat (limited to 'platforms/chibios/drivers/spi_master.h')
-rw-r--r-- | platforms/chibios/drivers/spi_master.h | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/platforms/chibios/drivers/spi_master.h b/platforms/chibios/drivers/spi_master.h new file mode 100644 index 000000000..b5a6ef143 --- /dev/null +++ b/platforms/chibios/drivers/spi_master.h | |||
@@ -0,0 +1,93 @@ | |||
1 | /* Copyright 2020 Nick Brassel (tzarc) | ||
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 <ch.h> | ||
20 | #include <hal.h> | ||
21 | #include <stdbool.h> | ||
22 | |||
23 | #include "gpio.h" | ||
24 | #include "chibios_config.h" | ||
25 | |||
26 | #ifndef SPI_DRIVER | ||
27 | # define SPI_DRIVER SPID2 | ||
28 | #endif | ||
29 | |||
30 | #ifndef SPI_SCK_PIN | ||
31 | # define SPI_SCK_PIN B13 | ||
32 | #endif | ||
33 | |||
34 | #ifndef SPI_SCK_PAL_MODE | ||
35 | # if defined(USE_GPIOV1) | ||
36 | # define SPI_SCK_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL | ||
37 | # else | ||
38 | # define SPI_SCK_PAL_MODE 5 | ||
39 | # endif | ||
40 | #endif | ||
41 | |||
42 | #ifndef SPI_MOSI_PIN | ||
43 | # define SPI_MOSI_PIN B15 | ||
44 | #endif | ||
45 | |||
46 | #ifndef SPI_MOSI_PAL_MODE | ||
47 | # if defined(USE_GPIOV1) | ||
48 | # define SPI_MOSI_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL | ||
49 | # else | ||
50 | # define SPI_MOSI_PAL_MODE 5 | ||
51 | # endif | ||
52 | #endif | ||
53 | |||
54 | #ifndef SPI_MISO_PIN | ||
55 | # define SPI_MISO_PIN B14 | ||
56 | #endif | ||
57 | |||
58 | #ifndef SPI_MISO_PAL_MODE | ||
59 | # if defined(USE_GPIOV1) | ||
60 | # define SPI_MISO_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL | ||
61 | # else | ||
62 | # define SPI_MISO_PAL_MODE 5 | ||
63 | # endif | ||
64 | #endif | ||
65 | |||
66 | typedef int16_t spi_status_t; | ||
67 | |||
68 | #define SPI_STATUS_SUCCESS (0) | ||
69 | #define SPI_STATUS_ERROR (-1) | ||
70 | #define SPI_STATUS_TIMEOUT (-2) | ||
71 | |||
72 | #define SPI_TIMEOUT_IMMEDIATE (0) | ||
73 | #define SPI_TIMEOUT_INFINITE (0xFFFF) | ||
74 | |||
75 | #ifdef __cplusplus | ||
76 | extern "C" { | ||
77 | #endif | ||
78 | void spi_init(void); | ||
79 | |||
80 | bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor); | ||
81 | |||
82 | spi_status_t spi_write(uint8_t data); | ||
83 | |||
84 | spi_status_t spi_read(void); | ||
85 | |||
86 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length); | ||
87 | |||
88 | spi_status_t spi_receive(uint8_t *data, uint16_t length); | ||
89 | |||
90 | void spi_stop(void); | ||
91 | #ifdef __cplusplus | ||
92 | } | ||
93 | #endif | ||