aboutsummaryrefslogtreecommitdiff
path: root/drivers/chibios
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2020-04-29 14:01:20 +1000
committerGitHub <noreply@github.com>2020-04-29 14:01:20 +1000
commitb17482c3fdca11788c0863465ed4a7e56a13d0f5 (patch)
tree83a36cc8f2a755dcbc2821fceea4d11e84501f28 /drivers/chibios
parent5b4870c046fa81b5574904cd21e2d13da41cf2e4 (diff)
downloadqmk_firmware-b17482c3fdca11788c0863465ed4a7e56a13d0f5.tar.gz
qmk_firmware-b17482c3fdca11788c0863465ed4a7e56a13d0f5.zip
Add SPI master for ChibiOS/ARM. (#8779)
Diffstat (limited to 'drivers/chibios')
-rw-r--r--drivers/chibios/spi_master.c137
-rw-r--r--drivers/chibios/spi_master.h78
2 files changed, 215 insertions, 0 deletions
diff --git a/drivers/chibios/spi_master.c b/drivers/chibios/spi_master.c
new file mode 100644
index 000000000..552ac663c
--- /dev/null
+++ b/drivers/chibios/spi_master.c
@@ -0,0 +1,137 @@
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#include "spi_master.h"
18#include "quantum.h"
19#include "timer.h"
20
21static pin_t currentSlavePin = NO_PIN;
22static SPIConfig spiConfig = {false, NULL, 0, 0, 0, 0};
23
24__attribute__((weak)) void spi_init(void) {
25 // Try releasing special pins for a short time
26 palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_INPUT);
27 palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_INPUT);
28 palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_INPUT);
29
30 chThdSleepMilliseconds(10);
31#if defined(USE_GPIOV1)
32 palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
33 palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
34 palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
35#else
36 palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_ALTERNATE(SPI_SCK_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
37 palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_ALTERNATE(SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
38 palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_ALTERNATE(SPI_MISO_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
39#endif
40}
41
42bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
43 if (currentSlavePin != NO_PIN || slavePin == NO_PIN) {
44 return false;
45 }
46
47 uint16_t roundedDivisor = 2;
48 while (roundedDivisor < divisor) {
49 roundedDivisor <<= 1;
50 }
51
52 if (roundedDivisor < 2 || roundedDivisor > 256) {
53 return false;
54 }
55
56 spiConfig.cr1 = 0;
57
58 if (lsbFirst) {
59 spiConfig.cr1 |= SPI_CR1_LSBFIRST;
60 }
61
62 switch (mode) {
63 case 0:
64 break;
65 case 1:
66 spiConfig.cr1 |= SPI_CR1_CPHA;
67 break;
68 case 2:
69 spiConfig.cr1 |= SPI_CR1_CPOL;
70 break;
71 case 3:
72 spiConfig.cr1 |= SPI_CR1_CPHA | SPI_CR1_CPOL;
73 break;
74 }
75
76 switch (roundedDivisor) {
77 case 2:
78 break;
79 case 4:
80 spiConfig.cr1 |= SPI_CR1_BR_0;
81 break;
82 case 8:
83 spiConfig.cr1 |= SPI_CR1_BR_1;
84 break;
85 case 16:
86 spiConfig.cr1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0;
87 break;
88 case 32:
89 spiConfig.cr1 |= SPI_CR1_BR_2;
90 break;
91 case 64:
92 spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0;
93 break;
94 case 128:
95 spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1;
96 break;
97 case 256:
98 spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;
99 break;
100 }
101
102 currentSlavePin = slavePin;
103 spiConfig.ssport = PAL_PORT(slavePin);
104 spiConfig.sspad = PAL_PAD(slavePin);
105
106 setPinOutput(slavePin);
107 spiStart(&SPI_DRIVER, &spiConfig);
108 spiSelect(&SPI_DRIVER);
109
110 return true;
111}
112
113spi_status_t spi_write(uint8_t data) { return spi_transmit(&data, 1); }
114
115spi_status_t spi_read(void) {
116 uint8_t data = 0;
117 spi_receive(&data, 1);
118 return data;
119}
120
121spi_status_t spi_transmit(const uint8_t *data, uint16_t length) {
122 spiSend(&SPI_DRIVER, length, data);
123 return SPI_STATUS_SUCCESS;
124}
125
126spi_status_t spi_receive(uint8_t *data, uint16_t length) {
127 spiReceive(&SPI_DRIVER, length, data);
128 return SPI_STATUS_SUCCESS;
129}
130
131void spi_stop(void) {
132 if (currentSlavePin != NO_PIN) {
133 spiUnselect(&SPI_DRIVER);
134 spiStop(&SPI_DRIVER);
135 currentSlavePin = NO_PIN;
136 }
137}
diff --git a/drivers/chibios/spi_master.h b/drivers/chibios/spi_master.h
new file mode 100644
index 000000000..0c18587c9
--- /dev/null
+++ b/drivers/chibios/spi_master.h
@@ -0,0 +1,78 @@
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 <quantum.h>
22
23#ifndef SPI_DRIVER
24# define SPI_DRIVER SPID2
25#endif
26
27#ifndef SPI_SCK_PIN
28# define SPI_SCK_PIN B13
29#endif
30
31#ifndef SPI_SCK_PAL_MODE
32# define SPI_SCK_PAL_MODE 5
33#endif
34
35#ifndef SPI_MOSI_PIN
36# define SPI_MOSI_PIN B15
37#endif
38
39#ifndef SPI_MOSI_PAL_MODE
40# define SPI_MOSI_PAL_MODE 5
41#endif
42
43#ifndef SPI_MISO_PIN
44# define SPI_MISO_PIN B14
45#endif
46
47#ifndef SPI_MISO_PAL_MODE
48# define SPI_MISO_PAL_MODE 5
49#endif
50
51typedef int16_t spi_status_t;
52
53#define SPI_STATUS_SUCCESS (0)
54#define SPI_STATUS_ERROR (-1)
55#define SPI_STATUS_TIMEOUT (-2)
56
57#define SPI_TIMEOUT_IMMEDIATE (0)
58#define SPI_TIMEOUT_INFINITE (0xFFFF)
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63void spi_init(void);
64
65bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor);
66
67spi_status_t spi_write(uint8_t data);
68
69spi_status_t spi_read(void);
70
71spi_status_t spi_transmit(const uint8_t *data, uint16_t length);
72
73spi_status_t spi_receive(uint8_t *data, uint16_t length);
74
75void spi_stop(void);
76#ifdef __cplusplus
77}
78#endif