diff options
| -rw-r--r-- | docs/drivers/spi.md | 8 | ||||
| -rw-r--r-- | drivers/spi_master.h | 116 | ||||
| -rw-r--r-- | keyboards/darkproject/kd83a_bfg_edition/config.h | 3 | ||||
| -rw-r--r-- | keyboards/darkproject/kd87a_bfg_edition/config.h | 3 | ||||
| -rw-r--r-- | keyboards/gmmk/gmmk2/p96/config.h | 3 | ||||
| -rw-r--r-- | keyboards/jukaie/jk01/config.h | 3 | ||||
| -rw-r--r-- | keyboards/projectd/65/projectd_65_ansi/config.h | 3 | ||||
| -rw-r--r-- | keyboards/projectd/75/ansi/config.h | 3 | ||||
| -rw-r--r-- | keyboards/projectd/75/iso/config.h | 3 | ||||
| -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 |
12 files changed, 183 insertions, 175 deletions
diff --git a/docs/drivers/spi.md b/docs/drivers/spi.md index 43d2a056d5..140b204945 100644 --- a/docs/drivers/spi.md +++ b/docs/drivers/spi.md | |||
| @@ -88,7 +88,7 @@ Start an SPI transaction. | |||
| 88 | #### Arguments {#api-spi-start-arguments} | 88 | #### Arguments {#api-spi-start-arguments} |
| 89 | 89 | ||
| 90 | - `pin_t slavePin` | 90 | - `pin_t slavePin` |
| 91 | The QMK pin to assert as the slave select pin, eg. `B4`. | 91 | The GPIO pin connected to the desired device's `SS` line. |
| 92 | - `bool lsbFirst` | 92 | - `bool lsbFirst` |
| 93 | Determines the endianness of the transmission. If `true`, the least significant bit of each byte is sent first. | 93 | Determines the endianness of the transmission. If `true`, the least significant bit of each byte is sent first. |
| 94 | - `uint8_t mode` | 94 | - `uint8_t mode` |
| @@ -106,7 +106,7 @@ Start an SPI transaction. | |||
| 106 | 106 | ||
| 107 | #### Return Value {#api-spi-start-return} | 107 | #### Return Value {#api-spi-start-return} |
| 108 | 108 | ||
| 109 | `false` if the supplied parameters are invalid or the SPI peripheral is already in use, or `true`. | 109 | `true` if the operation was successful, otherwise `false` if the supplied parameters are invalid or the SPI peripheral is already in use. |
| 110 | 110 | ||
| 111 | --- | 111 | --- |
| 112 | 112 | ||
| @@ -131,7 +131,7 @@ Read a byte from the selected SPI device. | |||
| 131 | 131 | ||
| 132 | #### Return Value {#api-spi-read-return} | 132 | #### Return Value {#api-spi-read-return} |
| 133 | 133 | ||
| 134 | `SPI_STATUS_TIMEOUT` if the timeout period elapses, or the byte read from the device. | 134 | `SPI_STATUS_TIMEOUT` if the timeout period elapses, otherwise the byte read from the device. |
| 135 | 135 | ||
| 136 | --- | 136 | --- |
| 137 | 137 | ||
| @@ -159,7 +159,7 @@ Receive multiple bytes from the selected SPI device. | |||
| 159 | #### Arguments {#api-spi-receive-arguments} | 159 | #### Arguments {#api-spi-receive-arguments} |
| 160 | 160 | ||
| 161 | - `uint8_t *data` | 161 | - `uint8_t *data` |
| 162 | A pointer to the buffer to read into. | 162 | A pointer to a buffer to read into. |
| 163 | - `uint16_t length` | 163 | - `uint16_t length` |
| 164 | The number of bytes to read. Take care not to overrun the length of `data`. | 164 | The number of bytes to read. Take care not to overrun the length of `data`. |
| 165 | 165 | ||
diff --git a/drivers/spi_master.h b/drivers/spi_master.h new file mode 100644 index 0000000000..d206b812bf --- /dev/null +++ b/drivers/spi_master.h | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | // Copyright 2025 QMK | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <stdint.h> | ||
| 7 | #include <stdbool.h> | ||
| 8 | #include "gpio.h" | ||
| 9 | |||
| 10 | /** | ||
| 11 | * \file | ||
| 12 | * | ||
| 13 | * \defgroup spi_master SPI Master API | ||
| 14 | * | ||
| 15 | * \brief API to communicate with SPI devices. | ||
| 16 | * \{ | ||
| 17 | */ | ||
| 18 | |||
| 19 | // Hardware SS pin is defined in the header so that user code can refer to it | ||
| 20 | #ifdef __AVR__ | ||
| 21 | # 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__) | ||
| 22 | # define SPI_SS_PIN B0 | ||
| 23 | # elif defined(__AVR_ATmega32A__) | ||
| 24 | # define SPI_SS_PIN B4 | ||
| 25 | # elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) | ||
| 26 | # define SPI_SS_PIN B2 | ||
| 27 | # endif | ||
| 28 | #endif | ||
| 29 | |||
| 30 | typedef int16_t spi_status_t; | ||
| 31 | |||
| 32 | #define SPI_STATUS_SUCCESS (0) | ||
| 33 | #define SPI_STATUS_ERROR (-1) | ||
| 34 | #define SPI_STATUS_TIMEOUT (-2) | ||
| 35 | |||
| 36 | #define SPI_TIMEOUT_IMMEDIATE (0) | ||
| 37 | #define SPI_TIMEOUT_INFINITE (0xFFFF) | ||
| 38 | |||
| 39 | #ifdef __cplusplus | ||
| 40 | extern "C" { | ||
| 41 | #endif | ||
| 42 | |||
| 43 | typedef struct spi_start_config_t { | ||
| 44 | pin_t slave_pin; | ||
| 45 | bool lsb_first; | ||
| 46 | uint8_t mode; | ||
| 47 | uint16_t divisor; | ||
| 48 | bool cs_active_low; | ||
| 49 | } spi_start_config_t; | ||
| 50 | |||
| 51 | /** | ||
| 52 | * \brief Initialize the SPI driver. This function must be called only once, before any of the below functions can be called. | ||
| 53 | */ | ||
| 54 | void spi_init(void); | ||
| 55 | |||
| 56 | /** | ||
| 57 | * \brief Start an SPI transaction. | ||
| 58 | * | ||
| 59 | * \param slavePin The GPIO pin connected to the desired device's `SS` line. | ||
| 60 | * \param lsbFirst Determines the endianness of the transmission. If `true`, the least significant bit of each byte is sent first. | ||
| 61 | * \param mode The SPI mode to use. | ||
| 62 | * \param divisor The SPI clock divisor. | ||
| 63 | * | ||
| 64 | * \return `true` if the operation was successful, otherwise `false` if the supplied parameters are invalid or the SPI peripheral is already in use. | ||
| 65 | */ | ||
| 66 | bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor); | ||
| 67 | |||
| 68 | bool spi_start_extended(spi_start_config_t *start_config); | ||
| 69 | |||
| 70 | /** | ||
| 71 | * \brief Write a byte to the selected SPI device. | ||
| 72 | * | ||
| 73 | * \param data The byte to write. | ||
| 74 | * | ||
| 75 | * \return `SPI_STATUS_TIMEOUT` if the timeout period elapses, or `SPI_STATUS_SUCCESS`. | ||
| 76 | */ | ||
| 77 | spi_status_t spi_write(uint8_t data); | ||
| 78 | |||
| 79 | /** | ||
| 80 | * \brief Read a byte from the selected SPI device. | ||
| 81 | * | ||
| 82 | * \return `SPI_STATUS_TIMEOUT` if the timeout period elapses, otherwise the byte read from the device. | ||
| 83 | */ | ||
| 84 | spi_status_t spi_read(void); | ||
| 85 | |||
| 86 | /** | ||
| 87 | * \brief Send multiple bytes to the selected SPI device. | ||
| 88 | * | ||
| 89 | * \param data A pointer to the data to write from. | ||
| 90 | * \param length The number of bytes to write. Take care not to overrun the length of `data`. | ||
| 91 | * | ||
| 92 | * \return `SPI_STATUS_TIMEOUT` if the timeout period elapses, `SPI_STATUS_ERROR` if some other error occurs, otherwise `SPI_STATUS_SUCCESS`. | ||
| 93 | */ | ||
| 94 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length); | ||
| 95 | |||
| 96 | /** | ||
| 97 | * \brief Receive multiple bytes from the selected SPI device. | ||
| 98 | * | ||
| 99 | * \param data A pointer to a buffer to read into. | ||
| 100 | * \param length The number of bytes to read. Take care not to overrun the length of `data`. | ||
| 101 | * | ||
| 102 | * \return `SPI_STATUS_TIMEOUT` if the timeout period elapses, `SPI_STATUS_ERROR` if some other error occurs, otherwise `SPI_STATUS_SUCCESS`. | ||
| 103 | */ | ||
| 104 | spi_status_t spi_receive(uint8_t *data, uint16_t length); | ||
| 105 | |||
| 106 | /** | ||
| 107 | * \brief End the current SPI transaction. This will deassert the slave select pin and reset the endianness, mode and divisor configured by `spi_start()`. | ||
| 108 | * | ||
| 109 | */ | ||
| 110 | void spi_stop(void); | ||
| 111 | |||
| 112 | #ifdef __cplusplus | ||
| 113 | } | ||
| 114 | #endif | ||
| 115 | |||
| 116 | /** \} */ | ||
diff --git a/keyboards/darkproject/kd83a_bfg_edition/config.h b/keyboards/darkproject/kd83a_bfg_edition/config.h index 880aabd5d7..fce00d29ab 100644 --- a/keyboards/darkproject/kd83a_bfg_edition/config.h +++ b/keyboards/darkproject/kd83a_bfg_edition/config.h | |||
| @@ -22,8 +22,11 @@ | |||
| 22 | /* SPI Config for LED Driver */ | 22 | /* SPI Config for LED Driver */ |
| 23 | #define SPI_DRIVER SPIDQ | 23 | #define SPI_DRIVER SPIDQ |
| 24 | #define SPI_SCK_PIN A5 | 24 | #define SPI_SCK_PIN A5 |
| 25 | #define SPI_SCK_PAL_MODE 5 | ||
| 25 | #define SPI_MOSI_PIN A7 | 26 | #define SPI_MOSI_PIN A7 |
| 27 | #define SPI_MOSI_PAL_MODE 5 | ||
| 26 | #define SPI_MISO_PIN A6 | 28 | #define SPI_MISO_PIN A6 |
| 29 | #define SPI_MISO_PAL_MODE 5 | ||
| 27 | 30 | ||
| 28 | #define AW20216S_CS_PIN_1 A15 | 31 | #define AW20216S_CS_PIN_1 A15 |
| 29 | #define AW20216S_CS_PIN_2 B15 | 32 | #define AW20216S_CS_PIN_2 B15 |
diff --git a/keyboards/darkproject/kd87a_bfg_edition/config.h b/keyboards/darkproject/kd87a_bfg_edition/config.h index a32f712231..9e68844daf 100644 --- a/keyboards/darkproject/kd87a_bfg_edition/config.h +++ b/keyboards/darkproject/kd87a_bfg_edition/config.h | |||
| @@ -22,8 +22,11 @@ | |||
| 22 | /* SPI Config for LED Driver */ | 22 | /* SPI Config for LED Driver */ |
| 23 | #define SPI_DRIVER SPIDQ | 23 | #define SPI_DRIVER SPIDQ |
| 24 | #define SPI_SCK_PIN A5 | 24 | #define SPI_SCK_PIN A5 |
| 25 | #define SPI_SCK_PAL_MODE 5 | ||
| 25 | #define SPI_MOSI_PIN A7 | 26 | #define SPI_MOSI_PIN A7 |
| 27 | #define SPI_MOSI_PAL_MODE 5 | ||
| 26 | #define SPI_MISO_PIN A6 | 28 | #define SPI_MISO_PIN A6 |
| 29 | #define SPI_MISO_PAL_MODE 5 | ||
| 27 | 30 | ||
| 28 | #define AW20216S_CS_PIN_1 A15 | 31 | #define AW20216S_CS_PIN_1 A15 |
| 29 | #define AW20216S_CS_PIN_2 B15 | 32 | #define AW20216S_CS_PIN_2 B15 |
diff --git a/keyboards/gmmk/gmmk2/p96/config.h b/keyboards/gmmk/gmmk2/p96/config.h index 1b246e4f3f..852d29df65 100644 --- a/keyboards/gmmk/gmmk2/p96/config.h +++ b/keyboards/gmmk/gmmk2/p96/config.h | |||
| @@ -22,8 +22,11 @@ | |||
| 22 | /* SPI Config for LED Driver */ | 22 | /* SPI Config for LED Driver */ |
| 23 | #define SPI_DRIVER SPIDQ | 23 | #define SPI_DRIVER SPIDQ |
| 24 | #define SPI_SCK_PIN A5 | 24 | #define SPI_SCK_PIN A5 |
| 25 | #define SPI_SCK_PAL_MODE 5 | ||
| 25 | #define SPI_MOSI_PIN A7 | 26 | #define SPI_MOSI_PIN A7 |
| 27 | #define SPI_MOSI_PAL_MODE 5 | ||
| 26 | #define SPI_MISO_PIN A6 | 28 | #define SPI_MISO_PIN A6 |
| 29 | #define SPI_MISO_PAL_MODE 5 | ||
| 27 | 30 | ||
| 28 | #define AW20216S_CS_PIN_1 A15 | 31 | #define AW20216S_CS_PIN_1 A15 |
| 29 | #define AW20216S_CS_PIN_2 B15 | 32 | #define AW20216S_CS_PIN_2 B15 |
diff --git a/keyboards/jukaie/jk01/config.h b/keyboards/jukaie/jk01/config.h index d8dfb9f535..41534ce6c4 100644 --- a/keyboards/jukaie/jk01/config.h +++ b/keyboards/jukaie/jk01/config.h | |||
| @@ -22,8 +22,11 @@ | |||
| 22 | /* SPI Config for LED Driver */ | 22 | /* SPI Config for LED Driver */ |
| 23 | #define SPI_DRIVER SPIDQ | 23 | #define SPI_DRIVER SPIDQ |
| 24 | #define SPI_SCK_PIN A5 | 24 | #define SPI_SCK_PIN A5 |
| 25 | #define SPI_SCK_PAL_MODE 5 | ||
| 25 | #define SPI_MOSI_PIN A7 | 26 | #define SPI_MOSI_PIN A7 |
| 27 | #define SPI_MOSI_PAL_MODE 5 | ||
| 26 | #define SPI_MISO_PIN A6 | 28 | #define SPI_MISO_PIN A6 |
| 29 | #define SPI_MISO_PAL_MODE 5 | ||
| 27 | 30 | ||
| 28 | #define AW20216S_CS_PIN_1 A15 | 31 | #define AW20216S_CS_PIN_1 A15 |
| 29 | #define AW20216S_CS_PIN_2 B15 | 32 | #define AW20216S_CS_PIN_2 B15 |
diff --git a/keyboards/projectd/65/projectd_65_ansi/config.h b/keyboards/projectd/65/projectd_65_ansi/config.h index c8da5c42a7..6e23afc902 100644 --- a/keyboards/projectd/65/projectd_65_ansi/config.h +++ b/keyboards/projectd/65/projectd_65_ansi/config.h | |||
| @@ -22,8 +22,11 @@ | |||
| 22 | /* SPI Config for LED Driver */ | 22 | /* SPI Config for LED Driver */ |
| 23 | #define SPI_DRIVER SPIDQ | 23 | #define SPI_DRIVER SPIDQ |
| 24 | #define SPI_SCK_PIN A5 | 24 | #define SPI_SCK_PIN A5 |
| 25 | #define SPI_SCK_PAL_MODE 5 | ||
| 25 | #define SPI_MOSI_PIN A7 | 26 | #define SPI_MOSI_PIN A7 |
| 27 | #define SPI_MOSI_PAL_MODE 5 | ||
| 26 | #define SPI_MISO_PIN A6 | 28 | #define SPI_MISO_PIN A6 |
| 29 | #define SPI_MISO_PAL_MODE 5 | ||
| 27 | 30 | ||
| 28 | #define AW20216S_CS_PIN_1 A15 | 31 | #define AW20216S_CS_PIN_1 A15 |
| 29 | #define AW20216S_EN_PIN C13 | 32 | #define AW20216S_EN_PIN C13 |
diff --git a/keyboards/projectd/75/ansi/config.h b/keyboards/projectd/75/ansi/config.h index 282e20a8e2..acbe853949 100644 --- a/keyboards/projectd/75/ansi/config.h +++ b/keyboards/projectd/75/ansi/config.h | |||
| @@ -22,8 +22,11 @@ | |||
| 22 | /* SPI Config for LED Driver */ | 22 | /* SPI Config for LED Driver */ |
| 23 | #define SPI_DRIVER SPIDQ | 23 | #define SPI_DRIVER SPIDQ |
| 24 | #define SPI_SCK_PIN A5 | 24 | #define SPI_SCK_PIN A5 |
| 25 | #define SPI_SCK_PAL_MODE 5 | ||
| 25 | #define SPI_MOSI_PIN A7 | 26 | #define SPI_MOSI_PIN A7 |
| 27 | #define SPI_MOSI_PAL_MODE 5 | ||
| 26 | #define SPI_MISO_PIN A6 | 28 | #define SPI_MISO_PIN A6 |
| 29 | #define SPI_MISO_PAL_MODE 5 | ||
| 27 | 30 | ||
| 28 | #define AW20216S_CS_PIN_1 A15 | 31 | #define AW20216S_CS_PIN_1 A15 |
| 29 | #define AW20216S_CS_PIN_2 B15 | 32 | #define AW20216S_CS_PIN_2 B15 |
diff --git a/keyboards/projectd/75/iso/config.h b/keyboards/projectd/75/iso/config.h index 282e20a8e2..acbe853949 100644 --- a/keyboards/projectd/75/iso/config.h +++ b/keyboards/projectd/75/iso/config.h | |||
| @@ -22,8 +22,11 @@ | |||
| 22 | /* SPI Config for LED Driver */ | 22 | /* SPI Config for LED Driver */ |
| 23 | #define SPI_DRIVER SPIDQ | 23 | #define SPI_DRIVER SPIDQ |
| 24 | #define SPI_SCK_PIN A5 | 24 | #define SPI_SCK_PIN A5 |
| 25 | #define SPI_SCK_PAL_MODE 5 | ||
| 25 | #define SPI_MOSI_PIN A7 | 26 | #define SPI_MOSI_PIN A7 |
| 27 | #define SPI_MOSI_PAL_MODE 5 | ||
| 26 | #define SPI_MISO_PIN A6 | 28 | #define SPI_MISO_PIN A6 |
| 29 | #define SPI_MISO_PAL_MODE 5 | ||
| 27 | 30 | ||
| 28 | #define AW20216S_CS_PIN_1 A15 | 31 | #define AW20216S_CS_PIN_1 A15 |
| 29 | #define AW20216S_CS_PIN_2 B15 | 32 | #define AW20216S_CS_PIN_2 B15 |
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 | ||
