diff options
Diffstat (limited to 'platforms')
| -rw-r--r-- | platforms/avr/drivers/spi_master.h | 68 | ||||
| -rw-r--r-- | platforms/chibios/drivers/spi_master.c | 43 | ||||
| -rw-r--r-- | platforms/chibios/drivers/spi_master.h | 102 |
3 files changed, 42 insertions, 171 deletions
diff --git a/platforms/avr/drivers/spi_master.h b/platforms/avr/drivers/spi_master.h deleted file mode 100644 index ebbf7ddeab..0000000000 --- a/platforms/avr/drivers/spi_master.h +++ /dev/null | |||
| @@ -1,68 +0,0 @@ | |||
| 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 | typedef struct spi_start_config_t { | ||
| 45 | pin_t slave_pin; | ||
| 46 | bool lsb_first; | ||
| 47 | uint8_t mode; | ||
| 48 | uint16_t divisor; | ||
| 49 | bool cs_active_low; | ||
| 50 | } spi_start_config_t; | ||
| 51 | |||
| 52 | void spi_init(void); | ||
| 53 | |||
| 54 | bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor); | ||
| 55 | bool spi_start_extended(spi_start_config_t *start_config); | ||
| 56 | |||
| 57 | spi_status_t spi_write(uint8_t data); | ||
| 58 | |||
| 59 | spi_status_t spi_read(void); | ||
| 60 | |||
| 61 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length); | ||
| 62 | |||
| 63 | spi_status_t spi_receive(uint8_t *data, uint16_t length); | ||
| 64 | |||
| 65 | void spi_stop(void); | ||
| 66 | #ifdef __cplusplus | ||
| 67 | } | ||
| 68 | #endif | ||
diff --git a/platforms/chibios/drivers/spi_master.c b/platforms/chibios/drivers/spi_master.c index 414e5b10a3..f5e48edfda 100644 --- a/platforms/chibios/drivers/spi_master.c +++ b/platforms/chibios/drivers/spi_master.c | |||
| @@ -15,8 +15,49 @@ | |||
| 15 | */ | 15 | */ |
| 16 | 16 | ||
| 17 | #include "spi_master.h" | 17 | #include "spi_master.h" |
| 18 | #include "chibios_config.h" | ||
| 19 | #include <ch.h> | ||
| 20 | #include <hal.h> | ||
| 18 | 21 | ||
| 19 | #include "timer.h" | 22 | #ifndef SPI_DRIVER |
| 23 | # define SPI_DRIVER SPID2 | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #ifndef SPI_SCK_PIN | ||
| 27 | # define SPI_SCK_PIN B13 | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #ifndef SPI_SCK_PAL_MODE | ||
| 31 | # ifdef USE_GPIOV1 | ||
| 32 | # define SPI_SCK_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL | ||
| 33 | # else | ||
| 34 | # define SPI_SCK_PAL_MODE 5 | ||
| 35 | # endif | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #ifndef SPI_MOSI_PIN | ||
| 39 | # define SPI_MOSI_PIN B15 | ||
| 40 | #endif | ||
| 41 | |||
| 42 | #ifndef SPI_MOSI_PAL_MODE | ||
| 43 | # ifdef USE_GPIOV1 | ||
| 44 | # define SPI_MOSI_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL | ||
| 45 | # else | ||
| 46 | # define SPI_MOSI_PAL_MODE 5 | ||
| 47 | # endif | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #ifndef SPI_MISO_PIN | ||
| 51 | # define SPI_MISO_PIN B14 | ||
| 52 | #endif | ||
| 53 | |||
| 54 | #ifndef SPI_MISO_PAL_MODE | ||
| 55 | # ifdef USE_GPIOV1 | ||
| 56 | # define SPI_MISO_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL | ||
| 57 | # else | ||
| 58 | # define SPI_MISO_PAL_MODE 5 | ||
| 59 | # endif | ||
| 60 | #endif | ||
| 20 | 61 | ||
| 21 | static bool spiStarted = false; | 62 | static bool spiStarted = false; |
| 22 | #if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE | 63 | #if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE |
diff --git a/platforms/chibios/drivers/spi_master.h b/platforms/chibios/drivers/spi_master.h deleted file mode 100644 index 4ad6144091..0000000000 --- a/platforms/chibios/drivers/spi_master.h +++ /dev/null | |||
| @@ -1,102 +0,0 @@ | |||
| 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_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_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_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 | typedef struct spi_start_config_t { | ||
| 79 | pin_t slave_pin; | ||
| 80 | bool lsb_first; | ||
| 81 | uint8_t mode; | ||
| 82 | uint16_t divisor; | ||
| 83 | bool cs_active_low; | ||
| 84 | } spi_start_config_t; | ||
| 85 | |||
| 86 | void spi_init(void); | ||
| 87 | |||
| 88 | bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor); | ||
| 89 | bool spi_start_extended(spi_start_config_t *start_config); | ||
| 90 | |||
| 91 | spi_status_t spi_write(uint8_t data); | ||
| 92 | |||
| 93 | spi_status_t spi_read(void); | ||
| 94 | |||
| 95 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length); | ||
| 96 | |||
| 97 | spi_status_t spi_receive(uint8_t *data, uint16_t length); | ||
| 98 | |||
| 99 | void spi_stop(void); | ||
| 100 | #ifdef __cplusplus | ||
| 101 | } | ||
| 102 | #endif | ||
