diff options
| author | Ryan <fauxpark@gmail.com> | 2020-04-08 11:04:31 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-08 11:04:31 +1000 |
| commit | 400ca2d035d7bdb85c077a53b2cd85cdd04882ab (patch) | |
| tree | dd6c88d9deee75d52af52ee3d208abfa99072386 /drivers | |
| parent | e409fb47f27f9cf56479928ed86eb2eb346eec54 (diff) | |
| download | qmk_firmware-400ca2d035d7bdb85c077a53b2cd85cdd04882ab.tar.gz qmk_firmware-400ca2d035d7bdb85c077a53b2cd85cdd04882ab.zip | |
spi_master for AVR (#8299)
* Change _delay_ms/us() to wait_ms/us()
* Switch to platform-agnostic GPIO macros
* Add AVR spi_master and migrate Adafruit BLE code
* Set verbose back to false
* Add clock divisor, bit order and SPI mode configuration for init
* Add start and stop functions
* Move configuration of mode, endianness and speed to `spi_start()`
* Some breaks here would be good
* Default Adafruit BLE clock divisor to 4 (2MHz on the Feather 32U4)
* Remove mode and divisor enums
* Add some docs
* No hr at EOF
* Add links in sidebar
Diffstat (limited to 'drivers')
| -rw-r--r-- | drivers/avr/spi_master.c | 163 | ||||
| -rw-r--r-- | drivers/avr/spi_master.h | 57 |
2 files changed, 220 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 | |||
| 37 | static pin_t currentSlavePin = NO_PIN; | ||
| 38 | static uint8_t currentSlaveConfig = 0; | ||
| 39 | static bool currentSlave2X = false; | ||
| 40 | |||
| 41 | void 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 | |||
| 50 | void 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 | |||
| 103 | spi_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 | |||
| 116 | spi_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 | |||
| 129 | spi_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 | |||
| 139 | spi_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 | |||
| 153 | void 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 | } | ||
diff --git a/drivers/avr/spi_master.h b/drivers/avr/spi_master.h new file mode 100644 index 000000000..0bab2dc24 --- /dev/null +++ b/drivers/avr/spi_master.h | |||
| @@ -0,0 +1,57 @@ | |||
| 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 "quantum.h" | ||
| 20 | |||
| 21 | typedef int16_t spi_status_t; | ||
| 22 | |||
| 23 | // Hardware SS pin is defined in the header so that user code can refer to it | ||
| 24 | #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__) | ||
| 25 | # define SPI_SS_PIN B0 | ||
| 26 | #elif defined(__AVR_ATmega32A__) | ||
| 27 | # define SPI_SS_PIN B4 | ||
| 28 | #elif defined(__AVR_ATmega328P__) | ||
| 29 | # define SPI_SS_PIN B2 | ||
| 30 | #endif | ||
| 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 | void spi_init(void); | ||
| 43 | |||
| 44 | void spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint8_t divisor); | ||
| 45 | |||
| 46 | spi_status_t spi_write(uint8_t data, uint16_t timeout); | ||
| 47 | |||
| 48 | spi_status_t spi_read(uint16_t timeout); | ||
| 49 | |||
| 50 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length, uint16_t timeout); | ||
| 51 | |||
| 52 | spi_status_t spi_receive(uint8_t *data, uint16_t length, uint16_t timeout); | ||
| 53 | |||
| 54 | void spi_stop(void); | ||
| 55 | #ifdef __cplusplus | ||
| 56 | } | ||
| 57 | #endif | ||
