diff options
Diffstat (limited to 'drivers/chibios')
| -rw-r--r-- | drivers/chibios/analog.c | 321 | ||||
| -rw-r--r-- | drivers/chibios/analog.h | 41 | ||||
| -rw-r--r-- | drivers/chibios/i2c_master.c | 121 | ||||
| -rw-r--r-- | drivers/chibios/i2c_master.h | 113 | ||||
| -rw-r--r-- | drivers/chibios/serial.c | 290 | ||||
| -rw-r--r-- | drivers/chibios/serial.h | 62 | ||||
| -rw-r--r-- | drivers/chibios/serial_usart.c | 208 | ||||
| -rw-r--r-- | drivers/chibios/serial_usart.h | 90 | ||||
| -rw-r--r-- | drivers/chibios/serial_usart_duplex.c | 261 | ||||
| -rw-r--r-- | drivers/chibios/spi_master.c | 148 | ||||
| -rw-r--r-- | drivers/chibios/spi_master.h | 80 | ||||
| -rw-r--r-- | drivers/chibios/uart.c | 50 | ||||
| -rw-r--r-- | drivers/chibios/uart.h | 77 | ||||
| -rw-r--r-- | drivers/chibios/usbpd_stm32g4.c | 76 | ||||
| -rw-r--r-- | drivers/chibios/ws2812.c | 114 | ||||
| -rw-r--r-- | drivers/chibios/ws2812_pwm.c | 311 | ||||
| -rw-r--r-- | drivers/chibios/ws2812_spi.c | 159 |
17 files changed, 0 insertions, 2522 deletions
diff --git a/drivers/chibios/analog.c b/drivers/chibios/analog.c deleted file mode 100644 index 8c476fcac..000000000 --- a/drivers/chibios/analog.c +++ /dev/null | |||
| @@ -1,321 +0,0 @@ | |||
| 1 | /* Copyright 2019 Drew Mills | ||
| 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 2 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 <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "quantum.h" | ||
| 18 | #include "analog.h" | ||
| 19 | #include <ch.h> | ||
| 20 | #include <hal.h> | ||
| 21 | |||
| 22 | #if !HAL_USE_ADC | ||
| 23 | # error "You need to set HAL_USE_ADC to TRUE in your halconf.h to use the ADC." | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #if !STM32_ADC_USE_ADC1 && !STM32_ADC_USE_ADC2 && !STM32_ADC_USE_ADC3 && !STM32_ADC_USE_ADC4 | ||
| 27 | # error "You need to set one of the 'STM32_ADC_USE_ADCx' settings to TRUE in your mcuconf.h to use the ADC." | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #if STM32_ADC_DUAL_MODE | ||
| 31 | # error "STM32 ADC Dual Mode is not supported at this time." | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #if STM32_ADCV3_OVERSAMPLING | ||
| 35 | # error "STM32 ADCV3 Oversampling is not supported at this time." | ||
| 36 | #endif | ||
| 37 | |||
| 38 | // Otherwise assume V3 | ||
| 39 | #if defined(STM32F0XX) || defined(STM32L0XX) | ||
| 40 | # define USE_ADCV1 | ||
| 41 | #elif defined(STM32F1XX) || defined(STM32F2XX) || defined(STM32F4XX) | ||
| 42 | # define USE_ADCV2 | ||
| 43 | #endif | ||
| 44 | |||
| 45 | // BODGE to make v2 look like v1,3 and 4 | ||
| 46 | #ifdef USE_ADCV2 | ||
| 47 | # if !defined(ADC_SMPR_SMP_1P5) && defined(ADC_SAMPLE_3) | ||
| 48 | # define ADC_SMPR_SMP_1P5 ADC_SAMPLE_3 | ||
| 49 | # define ADC_SMPR_SMP_7P5 ADC_SAMPLE_15 | ||
| 50 | # define ADC_SMPR_SMP_13P5 ADC_SAMPLE_28 | ||
| 51 | # define ADC_SMPR_SMP_28P5 ADC_SAMPLE_56 | ||
| 52 | # define ADC_SMPR_SMP_41P5 ADC_SAMPLE_84 | ||
| 53 | # define ADC_SMPR_SMP_55P5 ADC_SAMPLE_112 | ||
| 54 | # define ADC_SMPR_SMP_71P5 ADC_SAMPLE_144 | ||
| 55 | # define ADC_SMPR_SMP_239P5 ADC_SAMPLE_480 | ||
| 56 | # endif | ||
| 57 | |||
| 58 | # if !defined(ADC_SMPR_SMP_1P5) && defined(ADC_SAMPLE_1P5) | ||
| 59 | # define ADC_SMPR_SMP_1P5 ADC_SAMPLE_1P5 | ||
| 60 | # define ADC_SMPR_SMP_7P5 ADC_SAMPLE_7P5 | ||
| 61 | # define ADC_SMPR_SMP_13P5 ADC_SAMPLE_13P5 | ||
| 62 | # define ADC_SMPR_SMP_28P5 ADC_SAMPLE_28P5 | ||
| 63 | # define ADC_SMPR_SMP_41P5 ADC_SAMPLE_41P5 | ||
| 64 | # define ADC_SMPR_SMP_55P5 ADC_SAMPLE_55P5 | ||
| 65 | # define ADC_SMPR_SMP_71P5 ADC_SAMPLE_71P5 | ||
| 66 | # define ADC_SMPR_SMP_239P5 ADC_SAMPLE_239P5 | ||
| 67 | # endif | ||
| 68 | |||
| 69 | // we still sample at 12bit, but scale down to the requested bit range | ||
| 70 | # define ADC_CFGR1_RES_12BIT 12 | ||
| 71 | # define ADC_CFGR1_RES_10BIT 10 | ||
| 72 | # define ADC_CFGR1_RES_8BIT 8 | ||
| 73 | # define ADC_CFGR1_RES_6BIT 6 | ||
| 74 | #endif | ||
| 75 | |||
| 76 | /* User configurable ADC options */ | ||
| 77 | #ifndef ADC_COUNT | ||
| 78 | # if defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F4XX) | ||
| 79 | # define ADC_COUNT 1 | ||
| 80 | # elif defined(STM32F3XX) | ||
| 81 | # define ADC_COUNT 4 | ||
| 82 | # else | ||
| 83 | # error "ADC_COUNT has not been set for this ARM microcontroller." | ||
| 84 | # endif | ||
| 85 | #endif | ||
| 86 | |||
| 87 | #ifndef ADC_NUM_CHANNELS | ||
| 88 | # define ADC_NUM_CHANNELS 1 | ||
| 89 | #elif ADC_NUM_CHANNELS != 1 | ||
| 90 | # error "The ARM ADC implementation currently only supports reading one channel at a time." | ||
| 91 | #endif | ||
| 92 | |||
| 93 | #ifndef ADC_BUFFER_DEPTH | ||
| 94 | # define ADC_BUFFER_DEPTH 1 | ||
| 95 | #endif | ||
| 96 | |||
| 97 | // For more sampling rate options, look at hal_adc_lld.h in ChibiOS | ||
| 98 | #ifndef ADC_SAMPLING_RATE | ||
| 99 | # define ADC_SAMPLING_RATE ADC_SMPR_SMP_1P5 | ||
| 100 | #endif | ||
| 101 | |||
| 102 | // Options are 12, 10, 8, and 6 bit. | ||
| 103 | #ifndef ADC_RESOLUTION | ||
| 104 | # ifdef ADC_CFGR_RES_10BITS // ADCv3, ADCv4 | ||
| 105 | # define ADC_RESOLUTION ADC_CFGR_RES_10BITS | ||
| 106 | # else // ADCv1, ADCv5, or the bodge for ADCv2 above | ||
| 107 | # define ADC_RESOLUTION ADC_CFGR1_RES_10BIT | ||
| 108 | # endif | ||
| 109 | #endif | ||
| 110 | |||
| 111 | static ADCConfig adcCfg = {}; | ||
| 112 | static adcsample_t sampleBuffer[ADC_NUM_CHANNELS * ADC_BUFFER_DEPTH]; | ||
| 113 | |||
| 114 | // Initialize to max number of ADCs, set to empty object to initialize all to false. | ||
| 115 | static bool adcInitialized[ADC_COUNT] = {}; | ||
| 116 | |||
| 117 | // TODO: add back TR handling??? | ||
| 118 | static ADCConversionGroup adcConversionGroup = { | ||
| 119 | .circular = FALSE, | ||
| 120 | .num_channels = (uint16_t)(ADC_NUM_CHANNELS), | ||
| 121 | #if defined(USE_ADCV1) | ||
| 122 | .cfgr1 = ADC_CFGR1_CONT | ADC_RESOLUTION, | ||
| 123 | .smpr = ADC_SAMPLING_RATE, | ||
| 124 | #elif defined(USE_ADCV2) | ||
| 125 | # if !defined(STM32F1XX) | ||
| 126 | .cr2 = ADC_CR2_SWSTART, // F103 seem very unhappy with, F401 seems very unhappy without... | ||
| 127 | # endif | ||
| 128 | .smpr2 = ADC_SMPR2_SMP_AN0(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN1(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN2(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN3(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN4(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN5(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN6(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN7(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN8(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN9(ADC_SAMPLING_RATE), | ||
| 129 | .smpr1 = ADC_SMPR1_SMP_AN10(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN11(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN12(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN13(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN14(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN15(ADC_SAMPLING_RATE), | ||
| 130 | #else | ||
| 131 | .cfgr = ADC_CFGR_CONT | ADC_RESOLUTION, | ||
| 132 | .smpr = {ADC_SMPR1_SMP_AN0(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN1(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN2(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN3(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN4(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN5(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN6(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN7(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN8(ADC_SAMPLING_RATE) | ADC_SMPR1_SMP_AN9(ADC_SAMPLING_RATE), ADC_SMPR2_SMP_AN10(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN11(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN12(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN13(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN14(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN15(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN16(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN17(ADC_SAMPLING_RATE) | ADC_SMPR2_SMP_AN18(ADC_SAMPLING_RATE)}, | ||
| 133 | #endif | ||
| 134 | }; | ||
| 135 | |||
| 136 | // clang-format off | ||
| 137 | __attribute__((weak)) adc_mux pinToMux(pin_t pin) { | ||
| 138 | switch (pin) { | ||
| 139 | #if defined(STM32F0XX) | ||
| 140 | case A0: return TO_MUX( ADC_CHSELR_CHSEL0, 0 ); | ||
| 141 | case A1: return TO_MUX( ADC_CHSELR_CHSEL1, 0 ); | ||
| 142 | case A2: return TO_MUX( ADC_CHSELR_CHSEL2, 0 ); | ||
| 143 | case A3: return TO_MUX( ADC_CHSELR_CHSEL3, 0 ); | ||
| 144 | case A4: return TO_MUX( ADC_CHSELR_CHSEL4, 0 ); | ||
| 145 | case A5: return TO_MUX( ADC_CHSELR_CHSEL5, 0 ); | ||
| 146 | case A6: return TO_MUX( ADC_CHSELR_CHSEL6, 0 ); | ||
| 147 | case A7: return TO_MUX( ADC_CHSELR_CHSEL7, 0 ); | ||
| 148 | case B0: return TO_MUX( ADC_CHSELR_CHSEL8, 0 ); | ||
| 149 | case B1: return TO_MUX( ADC_CHSELR_CHSEL9, 0 ); | ||
| 150 | case C0: return TO_MUX( ADC_CHSELR_CHSEL10, 0 ); | ||
| 151 | case C1: return TO_MUX( ADC_CHSELR_CHSEL11, 0 ); | ||
| 152 | case C2: return TO_MUX( ADC_CHSELR_CHSEL12, 0 ); | ||
| 153 | case C3: return TO_MUX( ADC_CHSELR_CHSEL13, 0 ); | ||
| 154 | case C4: return TO_MUX( ADC_CHSELR_CHSEL14, 0 ); | ||
| 155 | case C5: return TO_MUX( ADC_CHSELR_CHSEL15, 0 ); | ||
| 156 | #elif defined(STM32F3XX) | ||
| 157 | case A0: return TO_MUX( ADC_CHANNEL_IN1, 0 ); | ||
| 158 | case A1: return TO_MUX( ADC_CHANNEL_IN2, 0 ); | ||
| 159 | case A2: return TO_MUX( ADC_CHANNEL_IN3, 0 ); | ||
| 160 | case A3: return TO_MUX( ADC_CHANNEL_IN4, 0 ); | ||
| 161 | case A4: return TO_MUX( ADC_CHANNEL_IN1, 1 ); | ||
| 162 | case A5: return TO_MUX( ADC_CHANNEL_IN2, 1 ); | ||
| 163 | case A6: return TO_MUX( ADC_CHANNEL_IN3, 1 ); | ||
| 164 | case A7: return TO_MUX( ADC_CHANNEL_IN4, 1 ); | ||
| 165 | case B0: return TO_MUX( ADC_CHANNEL_IN12, 2 ); | ||
| 166 | case B1: return TO_MUX( ADC_CHANNEL_IN1, 2 ); | ||
| 167 | case B2: return TO_MUX( ADC_CHANNEL_IN12, 1 ); | ||
| 168 | case B12: return TO_MUX( ADC_CHANNEL_IN3, 3 ); | ||
| 169 | case B13: return TO_MUX( ADC_CHANNEL_IN5, 2 ); | ||
| 170 | case B14: return TO_MUX( ADC_CHANNEL_IN4, 3 ); | ||
| 171 | case B15: return TO_MUX( ADC_CHANNEL_IN5, 3 ); | ||
| 172 | case C0: return TO_MUX( ADC_CHANNEL_IN6, 0 ); // Can also be ADC2 | ||
| 173 | case C1: return TO_MUX( ADC_CHANNEL_IN7, 0 ); // Can also be ADC2 | ||
| 174 | case C2: return TO_MUX( ADC_CHANNEL_IN8, 0 ); // Can also be ADC2 | ||
| 175 | case C3: return TO_MUX( ADC_CHANNEL_IN9, 0 ); // Can also be ADC2 | ||
| 176 | case C4: return TO_MUX( ADC_CHANNEL_IN5, 1 ); | ||
| 177 | case C5: return TO_MUX( ADC_CHANNEL_IN11, 1 ); | ||
| 178 | case D8: return TO_MUX( ADC_CHANNEL_IN12, 3 ); | ||
| 179 | case D9: return TO_MUX( ADC_CHANNEL_IN13, 3 ); | ||
| 180 | case D10: return TO_MUX( ADC_CHANNEL_IN7, 2 ); // Can also be ADC4 | ||
| 181 | case D11: return TO_MUX( ADC_CHANNEL_IN8, 2 ); // Can also be ADC4 | ||
| 182 | case D12: return TO_MUX( ADC_CHANNEL_IN9, 2 ); // Can also be ADC4 | ||
| 183 | case D13: return TO_MUX( ADC_CHANNEL_IN10, 2 ); // Can also be ADC4 | ||
| 184 | case D14: return TO_MUX( ADC_CHANNEL_IN11, 2 ); // Can also be ADC4 | ||
| 185 | case E7: return TO_MUX( ADC_CHANNEL_IN13, 2 ); | ||
| 186 | case E8: return TO_MUX( ADC_CHANNEL_IN6, 2 ); // Can also be ADC4 | ||
| 187 | case E9: return TO_MUX( ADC_CHANNEL_IN2, 2 ); | ||
| 188 | case E10: return TO_MUX( ADC_CHANNEL_IN14, 2 ); | ||
| 189 | case E11: return TO_MUX( ADC_CHANNEL_IN15, 2 ); | ||
| 190 | case E12: return TO_MUX( ADC_CHANNEL_IN16, 2 ); | ||
| 191 | case E13: return TO_MUX( ADC_CHANNEL_IN3, 2 ); | ||
| 192 | case E14: return TO_MUX( ADC_CHANNEL_IN1, 3 ); | ||
| 193 | case E15: return TO_MUX( ADC_CHANNEL_IN2, 3 ); | ||
| 194 | case F2: return TO_MUX( ADC_CHANNEL_IN10, 0 ); // Can also be ADC2 | ||
| 195 | case F4: return TO_MUX( ADC_CHANNEL_IN5, 0 ); | ||
| 196 | #elif defined(STM32F4XX) | ||
| 197 | case A0: return TO_MUX( ADC_CHANNEL_IN0, 0 ); | ||
| 198 | case A1: return TO_MUX( ADC_CHANNEL_IN1, 0 ); | ||
| 199 | case A2: return TO_MUX( ADC_CHANNEL_IN2, 0 ); | ||
| 200 | case A3: return TO_MUX( ADC_CHANNEL_IN3, 0 ); | ||
| 201 | case A4: return TO_MUX( ADC_CHANNEL_IN4, 0 ); | ||
| 202 | case A5: return TO_MUX( ADC_CHANNEL_IN5, 0 ); | ||
| 203 | case A6: return TO_MUX( ADC_CHANNEL_IN6, 0 ); | ||
| 204 | case A7: return TO_MUX( ADC_CHANNEL_IN7, 0 ); | ||
| 205 | case B0: return TO_MUX( ADC_CHANNEL_IN8, 0 ); | ||
| 206 | case B1: return TO_MUX( ADC_CHANNEL_IN9, 0 ); | ||
| 207 | case C0: return TO_MUX( ADC_CHANNEL_IN10, 0 ); | ||
| 208 | case C1: return TO_MUX( ADC_CHANNEL_IN11, 0 ); | ||
| 209 | case C2: return TO_MUX( ADC_CHANNEL_IN12, 0 ); | ||
| 210 | case C3: return TO_MUX( ADC_CHANNEL_IN13, 0 ); | ||
| 211 | case C4: return TO_MUX( ADC_CHANNEL_IN14, 0 ); | ||
| 212 | case C5: return TO_MUX( ADC_CHANNEL_IN15, 0 ); | ||
| 213 | # if STM32_ADC_USE_ADC3 | ||
| 214 | case F3: return TO_MUX( ADC_CHANNEL_IN9, 2 ); | ||
| 215 | case F4: return TO_MUX( ADC_CHANNEL_IN14, 2 ); | ||
| 216 | case F5: return TO_MUX( ADC_CHANNEL_IN15, 2 ); | ||
| 217 | case F6: return TO_MUX( ADC_CHANNEL_IN4, 2 ); | ||
| 218 | case F7: return TO_MUX( ADC_CHANNEL_IN5, 2 ); | ||
| 219 | case F8: return TO_MUX( ADC_CHANNEL_IN6, 2 ); | ||
| 220 | case F9: return TO_MUX( ADC_CHANNEL_IN7, 2 ); | ||
| 221 | case F10: return TO_MUX( ADC_CHANNEL_IN8, 2 ); | ||
| 222 | # endif | ||
| 223 | #elif defined(STM32F1XX) | ||
| 224 | case A0: return TO_MUX( ADC_CHANNEL_IN0, 0 ); | ||
| 225 | case A1: return TO_MUX( ADC_CHANNEL_IN1, 0 ); | ||
| 226 | case A2: return TO_MUX( ADC_CHANNEL_IN2, 0 ); | ||
| 227 | case A3: return TO_MUX( ADC_CHANNEL_IN3, 0 ); | ||
| 228 | case A4: return TO_MUX( ADC_CHANNEL_IN4, 0 ); | ||
| 229 | case A5: return TO_MUX( ADC_CHANNEL_IN5, 0 ); | ||
| 230 | case A6: return TO_MUX( ADC_CHANNEL_IN6, 0 ); | ||
| 231 | case A7: return TO_MUX( ADC_CHANNEL_IN7, 0 ); | ||
| 232 | case B0: return TO_MUX( ADC_CHANNEL_IN8, 0 ); | ||
| 233 | case B1: return TO_MUX( ADC_CHANNEL_IN9, 0 ); | ||
| 234 | case C0: return TO_MUX( ADC_CHANNEL_IN10, 0 ); | ||
| 235 | case C1: return TO_MUX( ADC_CHANNEL_IN11, 0 ); | ||
| 236 | case C2: return TO_MUX( ADC_CHANNEL_IN12, 0 ); | ||
| 237 | case C3: return TO_MUX( ADC_CHANNEL_IN13, 0 ); | ||
| 238 | case C4: return TO_MUX( ADC_CHANNEL_IN14, 0 ); | ||
| 239 | case C5: return TO_MUX( ADC_CHANNEL_IN15, 0 ); | ||
| 240 | // STM32F103x[C-G] in 144-pin packages also have analog inputs on F6...F10, but they are on ADC3, and the | ||
| 241 | // ChibiOS ADC driver for STM32F1xx currently supports only ADC1, therefore these pins are not usable. | ||
| 242 | #endif | ||
| 243 | } | ||
| 244 | |||
| 245 | // return an adc that would never be used so intToADCDriver will bail out | ||
| 246 | return TO_MUX(0, 0xFF); | ||
| 247 | } | ||
| 248 | // clang-format on | ||
| 249 | |||
| 250 | static inline ADCDriver* intToADCDriver(uint8_t adcInt) { | ||
| 251 | switch (adcInt) { | ||
| 252 | #if STM32_ADC_USE_ADC1 | ||
| 253 | case 0: | ||
| 254 | return &ADCD1; | ||
| 255 | #endif | ||
| 256 | #if STM32_ADC_USE_ADC2 | ||
| 257 | case 1: | ||
| 258 | return &ADCD2; | ||
| 259 | #endif | ||
| 260 | #if STM32_ADC_USE_ADC3 | ||
| 261 | case 2: | ||
| 262 | return &ADCD3; | ||
| 263 | #endif | ||
| 264 | #if STM32_ADC_USE_ADC4 | ||
| 265 | case 3: | ||
| 266 | return &ADCD4; | ||
| 267 | #endif | ||
| 268 | } | ||
| 269 | |||
| 270 | return NULL; | ||
| 271 | } | ||
| 272 | |||
| 273 | static inline void manageAdcInitializationDriver(uint8_t adc, ADCDriver* adcDriver) { | ||
| 274 | if (!adcInitialized[adc]) { | ||
| 275 | adcStart(adcDriver, &adcCfg); | ||
| 276 | adcInitialized[adc] = true; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | int16_t analogReadPin(pin_t pin) { | ||
| 281 | palSetLineMode(pin, PAL_MODE_INPUT_ANALOG); | ||
| 282 | |||
| 283 | return adc_read(pinToMux(pin)); | ||
| 284 | } | ||
| 285 | |||
| 286 | int16_t analogReadPinAdc(pin_t pin, uint8_t adc) { | ||
| 287 | palSetLineMode(pin, PAL_MODE_INPUT_ANALOG); | ||
| 288 | |||
| 289 | adc_mux target = pinToMux(pin); | ||
| 290 | target.adc = adc; | ||
| 291 | return adc_read(target); | ||
| 292 | } | ||
| 293 | |||
| 294 | int16_t adc_read(adc_mux mux) { | ||
| 295 | #if defined(USE_ADCV1) | ||
| 296 | // TODO: fix previous assumption of only 1 input... | ||
| 297 | adcConversionGroup.chselr = 1 << mux.input; /*no macro to convert N to ADC_CHSELR_CHSEL1*/ | ||
| 298 | #elif defined(USE_ADCV2) | ||
| 299 | adcConversionGroup.sqr3 = ADC_SQR3_SQ1_N(mux.input); | ||
| 300 | #else | ||
| 301 | adcConversionGroup.sqr[0] = ADC_SQR1_SQ1_N(mux.input); | ||
| 302 | #endif | ||
| 303 | |||
| 304 | ADCDriver* targetDriver = intToADCDriver(mux.adc); | ||
| 305 | if (!targetDriver) { | ||
| 306 | return 0; | ||
| 307 | } | ||
| 308 | |||
| 309 | manageAdcInitializationDriver(mux.adc, targetDriver); | ||
| 310 | if (adcConvert(targetDriver, &adcConversionGroup, &sampleBuffer[0], ADC_BUFFER_DEPTH) != MSG_OK) { | ||
| 311 | return 0; | ||
| 312 | } | ||
| 313 | |||
| 314 | #ifdef USE_ADCV2 | ||
| 315 | // fake 12-bit -> N-bit scale | ||
| 316 | return (*sampleBuffer) >> (12 - ADC_RESOLUTION); | ||
| 317 | #else | ||
| 318 | // already handled as part of adcConvert | ||
| 319 | return *sampleBuffer; | ||
| 320 | #endif | ||
| 321 | } | ||
diff --git a/drivers/chibios/analog.h b/drivers/chibios/analog.h deleted file mode 100644 index e61c39426..000000000 --- a/drivers/chibios/analog.h +++ /dev/null | |||
| @@ -1,41 +0,0 @@ | |||
| 1 | /* Copyright 2019 Drew Mills | ||
| 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 2 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 <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #pragma once | ||
| 18 | |||
| 19 | #include <stdint.h> | ||
| 20 | #include "quantum.h" | ||
| 21 | |||
| 22 | #ifdef __cplusplus | ||
| 23 | extern "C" { | ||
| 24 | #endif | ||
| 25 | |||
| 26 | typedef struct { | ||
| 27 | uint16_t input; | ||
| 28 | uint8_t adc; | ||
| 29 | } adc_mux; | ||
| 30 | #define TO_MUX(i, a) \ | ||
| 31 | (adc_mux) { i, a } | ||
| 32 | |||
| 33 | int16_t analogReadPin(pin_t pin); | ||
| 34 | int16_t analogReadPinAdc(pin_t pin, uint8_t adc); | ||
| 35 | adc_mux pinToMux(pin_t pin); | ||
| 36 | |||
| 37 | int16_t adc_read(adc_mux mux); | ||
| 38 | |||
| 39 | #ifdef __cplusplus | ||
| 40 | } | ||
| 41 | #endif | ||
diff --git a/drivers/chibios/i2c_master.c b/drivers/chibios/i2c_master.c deleted file mode 100644 index fc4bb2ab3..000000000 --- a/drivers/chibios/i2c_master.c +++ /dev/null | |||
| @@ -1,121 +0,0 @@ | |||
| 1 | /* Copyright 2018 Jack Humbert | ||
| 2 | * Copyright 2018 Yiancar | ||
| 3 | * | ||
| 4 | * This program is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* This library is only valid for STM32 processors. | ||
| 19 | * This library follows the convention of the AVR i2c_master library. | ||
| 20 | * As a result addresses are expected to be already shifted (addr << 1). | ||
| 21 | * I2CD1 is the default driver which corresponds to pins B6 and B7. This | ||
| 22 | * can be changed. | ||
| 23 | * Please ensure that HAL_USE_I2C is TRUE in the halconf.h file and that | ||
| 24 | * STM32_I2C_USE_I2C1 is TRUE in the mcuconf.h file. Pins B6 and B7 are used | ||
| 25 | * but using any other I2C pins should be trivial. | ||
| 26 | */ | ||
| 27 | #include "quantum.h" | ||
| 28 | #include "i2c_master.h" | ||
| 29 | #include <string.h> | ||
| 30 | #include <hal.h> | ||
| 31 | |||
| 32 | static uint8_t i2c_address; | ||
| 33 | |||
| 34 | static const I2CConfig i2cconfig = { | ||
| 35 | #if defined(USE_I2CV1_CONTRIB) | ||
| 36 | I2C1_CLOCK_SPEED, | ||
| 37 | #elif defined(USE_I2CV1) | ||
| 38 | I2C1_OPMODE, | ||
| 39 | I2C1_CLOCK_SPEED, | ||
| 40 | I2C1_DUTY_CYCLE, | ||
| 41 | #else | ||
| 42 | // This configures the I2C clock to 400khz assuming a 72Mhz clock | ||
| 43 | // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html | ||
| 44 | STM32_TIMINGR_PRESC(I2C1_TIMINGR_PRESC) | STM32_TIMINGR_SCLDEL(I2C1_TIMINGR_SCLDEL) | STM32_TIMINGR_SDADEL(I2C1_TIMINGR_SDADEL) | STM32_TIMINGR_SCLH(I2C1_TIMINGR_SCLH) | STM32_TIMINGR_SCLL(I2C1_TIMINGR_SCLL), 0, 0 | ||
| 45 | #endif | ||
| 46 | }; | ||
| 47 | |||
| 48 | static i2c_status_t chibios_to_qmk(const msg_t* status) { | ||
| 49 | switch (*status) { | ||
| 50 | case I2C_NO_ERROR: | ||
| 51 | return I2C_STATUS_SUCCESS; | ||
| 52 | case I2C_TIMEOUT: | ||
| 53 | return I2C_STATUS_TIMEOUT; | ||
| 54 | // I2C_BUS_ERROR, I2C_ARBITRATION_LOST, I2C_ACK_FAILURE, I2C_OVERRUN, I2C_PEC_ERROR, I2C_SMB_ALERT | ||
| 55 | default: | ||
| 56 | return I2C_STATUS_ERROR; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | __attribute__((weak)) void i2c_init(void) { | ||
| 61 | static bool is_initialised = false; | ||
| 62 | if (!is_initialised) { | ||
| 63 | is_initialised = true; | ||
| 64 | |||
| 65 | // Try releasing special pins for a short time | ||
| 66 | palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_INPUT); | ||
| 67 | palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_INPUT); | ||
| 68 | |||
| 69 | chThdSleepMilliseconds(10); | ||
| 70 | #if defined(USE_GPIOV1) | ||
| 71 | palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, I2C1_SCL_PAL_MODE); | ||
| 72 | palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, I2C1_SDA_PAL_MODE); | ||
| 73 | #else | ||
| 74 | palSetPadMode(I2C1_SCL_BANK, I2C1_SCL, PAL_MODE_ALTERNATE(I2C1_SCL_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
| 75 | palSetPadMode(I2C1_SDA_BANK, I2C1_SDA, PAL_MODE_ALTERNATE(I2C1_SDA_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
| 76 | #endif | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | i2c_status_t i2c_start(uint8_t address) { | ||
| 81 | i2c_address = address; | ||
| 82 | i2cStart(&I2C_DRIVER, &i2cconfig); | ||
| 83 | return I2C_STATUS_SUCCESS; | ||
| 84 | } | ||
| 85 | |||
| 86 | i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) { | ||
| 87 | i2c_address = address; | ||
| 88 | i2cStart(&I2C_DRIVER, &i2cconfig); | ||
| 89 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, 0, 0, TIME_MS2I(timeout)); | ||
| 90 | return chibios_to_qmk(&status); | ||
| 91 | } | ||
| 92 | |||
| 93 | i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) { | ||
| 94 | i2c_address = address; | ||
| 95 | i2cStart(&I2C_DRIVER, &i2cconfig); | ||
| 96 | msg_t status = i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, TIME_MS2I(timeout)); | ||
| 97 | return chibios_to_qmk(&status); | ||
| 98 | } | ||
| 99 | |||
| 100 | i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) { | ||
| 101 | i2c_address = devaddr; | ||
| 102 | i2cStart(&I2C_DRIVER, &i2cconfig); | ||
| 103 | |||
| 104 | uint8_t complete_packet[length + 1]; | ||
| 105 | for (uint8_t i = 0; i < length; i++) { | ||
| 106 | complete_packet[i + 1] = data[i]; | ||
| 107 | } | ||
| 108 | complete_packet[0] = regaddr; | ||
| 109 | |||
| 110 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), complete_packet, length + 1, 0, 0, TIME_MS2I(timeout)); | ||
| 111 | return chibios_to_qmk(&status); | ||
| 112 | } | ||
| 113 | |||
| 114 | i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { | ||
| 115 | i2c_address = devaddr; | ||
| 116 | i2cStart(&I2C_DRIVER, &i2cconfig); | ||
| 117 | msg_t status = i2cMasterTransmitTimeout(&I2C_DRIVER, (i2c_address >> 1), ®addr, 1, data, length, TIME_MS2I(timeout)); | ||
| 118 | return chibios_to_qmk(&status); | ||
| 119 | } | ||
| 120 | |||
| 121 | void i2c_stop(void) { i2cStop(&I2C_DRIVER); } | ||
diff --git a/drivers/chibios/i2c_master.h b/drivers/chibios/i2c_master.h deleted file mode 100644 index c68109acb..000000000 --- a/drivers/chibios/i2c_master.h +++ /dev/null | |||
| @@ -1,113 +0,0 @@ | |||
| 1 | /* Copyright 2018 Jack Humbert | ||
| 2 | * Copyright 2018 Yiancar | ||
| 3 | * | ||
| 4 | * This program is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* This library follows the convention of the AVR i2c_master library. | ||
| 19 | * As a result addresses are expected to be already shifted (addr << 1). | ||
| 20 | * I2CD1 is the default driver which corresponds to pins B6 and B7. This | ||
| 21 | * can be changed. | ||
| 22 | * Please ensure that HAL_USE_I2C is TRUE in the halconf.h file and that | ||
| 23 | * STM32_I2C_USE_I2C1 is TRUE in the mcuconf.h file. | ||
| 24 | */ | ||
| 25 | #pragma once | ||
| 26 | |||
| 27 | #include <ch.h> | ||
| 28 | #include <hal.h> | ||
| 29 | |||
| 30 | #ifdef I2C1_BANK | ||
| 31 | # define I2C1_SCL_BANK I2C1_BANK | ||
| 32 | # define I2C1_SDA_BANK I2C1_BANK | ||
| 33 | #endif | ||
| 34 | |||
| 35 | #ifndef I2C1_SCL_BANK | ||
| 36 | # define I2C1_SCL_BANK GPIOB | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #ifndef I2C1_SDA_BANK | ||
| 40 | # define I2C1_SDA_BANK GPIOB | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #ifndef I2C1_SCL | ||
| 44 | # define I2C1_SCL 6 | ||
| 45 | #endif | ||
| 46 | #ifndef I2C1_SDA | ||
| 47 | # define I2C1_SDA 7 | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #ifdef USE_I2CV1 | ||
| 51 | # ifndef I2C1_OPMODE | ||
| 52 | # define I2C1_OPMODE OPMODE_I2C | ||
| 53 | # endif | ||
| 54 | # ifndef I2C1_CLOCK_SPEED | ||
| 55 | # define I2C1_CLOCK_SPEED 100000 /* 400000 */ | ||
| 56 | # endif | ||
| 57 | # ifndef I2C1_DUTY_CYCLE | ||
| 58 | # define I2C1_DUTY_CYCLE STD_DUTY_CYCLE /* FAST_DUTY_CYCLE_2 */ | ||
| 59 | # endif | ||
| 60 | #else | ||
| 61 | // The default timing values below configures the I2C clock to 400khz assuming a 72Mhz clock | ||
| 62 | // For more info : https://www.st.com/en/embedded-software/stsw-stm32126.html | ||
| 63 | # ifndef I2C1_TIMINGR_PRESC | ||
| 64 | # define I2C1_TIMINGR_PRESC 0U | ||
| 65 | # endif | ||
| 66 | # ifndef I2C1_TIMINGR_SCLDEL | ||
| 67 | # define I2C1_TIMINGR_SCLDEL 7U | ||
| 68 | # endif | ||
| 69 | # ifndef I2C1_TIMINGR_SDADEL | ||
| 70 | # define I2C1_TIMINGR_SDADEL 0U | ||
| 71 | # endif | ||
| 72 | # ifndef I2C1_TIMINGR_SCLH | ||
| 73 | # define I2C1_TIMINGR_SCLH 38U | ||
| 74 | # endif | ||
| 75 | # ifndef I2C1_TIMINGR_SCLL | ||
| 76 | # define I2C1_TIMINGR_SCLL 129U | ||
| 77 | # endif | ||
| 78 | #endif | ||
| 79 | |||
| 80 | #ifndef I2C_DRIVER | ||
| 81 | # define I2C_DRIVER I2CD1 | ||
| 82 | #endif | ||
| 83 | |||
| 84 | #ifdef USE_GPIOV1 | ||
| 85 | # ifndef I2C1_SCL_PAL_MODE | ||
| 86 | # define I2C1_SCL_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN | ||
| 87 | # endif | ||
| 88 | # ifndef I2C1_SDA_PAL_MODE | ||
| 89 | # define I2C1_SDA_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN | ||
| 90 | # endif | ||
| 91 | #else | ||
| 92 | // The default PAL alternate modes are used to signal that the pins are used for I2C | ||
| 93 | # ifndef I2C1_SCL_PAL_MODE | ||
| 94 | # define I2C1_SCL_PAL_MODE 4 | ||
| 95 | # endif | ||
| 96 | # ifndef I2C1_SDA_PAL_MODE | ||
| 97 | # define I2C1_SDA_PAL_MODE 4 | ||
| 98 | # endif | ||
| 99 | #endif | ||
| 100 | |||
| 101 | typedef int16_t i2c_status_t; | ||
| 102 | |||
| 103 | #define I2C_STATUS_SUCCESS (0) | ||
| 104 | #define I2C_STATUS_ERROR (-1) | ||
| 105 | #define I2C_STATUS_TIMEOUT (-2) | ||
| 106 | |||
| 107 | void i2c_init(void); | ||
| 108 | i2c_status_t i2c_start(uint8_t address); | ||
| 109 | i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 110 | i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 111 | i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 112 | i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout); | ||
| 113 | void i2c_stop(void); | ||
diff --git a/drivers/chibios/serial.c b/drivers/chibios/serial.c deleted file mode 100644 index 54f7e1321..000000000 --- a/drivers/chibios/serial.c +++ /dev/null | |||
| @@ -1,290 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * WARNING: be careful changing this code, it is very timing dependent | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "quantum.h" | ||
| 6 | #include "serial.h" | ||
| 7 | #include "wait.h" | ||
| 8 | |||
| 9 | #include <hal.h> | ||
| 10 | |||
| 11 | // TODO: resolve/remove build warnings | ||
| 12 | #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) && defined(PROTOCOL_CHIBIOS) && defined(WS2812_DRIVER_BITBANG) | ||
| 13 | # warning "RGBLED_SPLIT not supported with bitbang WS2812 driver" | ||
| 14 | #endif | ||
| 15 | |||
| 16 | // default wait implementation cannot be called within interrupt | ||
| 17 | // this method seems to be more accurate than GPT timers | ||
| 18 | #if PORT_SUPPORTS_RT == FALSE | ||
| 19 | # error "chSysPolledDelayX method not supported on this platform" | ||
| 20 | #else | ||
| 21 | # undef wait_us | ||
| 22 | # define wait_us(x) chSysPolledDelayX(US2RTC(STM32_SYSCLK, x)) | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #ifndef SELECT_SOFT_SERIAL_SPEED | ||
| 26 | # define SELECT_SOFT_SERIAL_SPEED 1 | ||
| 27 | // TODO: correct speeds... | ||
| 28 | // 0: about 189kbps (Experimental only) | ||
| 29 | // 1: about 137kbps (default) | ||
| 30 | // 2: about 75kbps | ||
| 31 | // 3: about 39kbps | ||
| 32 | // 4: about 26kbps | ||
| 33 | // 5: about 20kbps | ||
| 34 | #endif | ||
| 35 | |||
| 36 | // Serial pulse period in microseconds. At the moment, going lower than 12 causes communication failure | ||
| 37 | #if SELECT_SOFT_SERIAL_SPEED == 0 | ||
| 38 | # define SERIAL_DELAY 12 | ||
| 39 | #elif SELECT_SOFT_SERIAL_SPEED == 1 | ||
| 40 | # define SERIAL_DELAY 16 | ||
| 41 | #elif SELECT_SOFT_SERIAL_SPEED == 2 | ||
| 42 | # define SERIAL_DELAY 24 | ||
| 43 | #elif SELECT_SOFT_SERIAL_SPEED == 3 | ||
| 44 | # define SERIAL_DELAY 32 | ||
| 45 | #elif SELECT_SOFT_SERIAL_SPEED == 4 | ||
| 46 | # define SERIAL_DELAY 48 | ||
| 47 | #elif SELECT_SOFT_SERIAL_SPEED == 5 | ||
| 48 | # define SERIAL_DELAY 64 | ||
| 49 | #else | ||
| 50 | # error invalid SELECT_SOFT_SERIAL_SPEED value | ||
| 51 | #endif | ||
| 52 | |||
| 53 | inline static void serial_delay(void) { wait_us(SERIAL_DELAY); } | ||
| 54 | inline static void serial_delay_half(void) { wait_us(SERIAL_DELAY / 2); } | ||
| 55 | inline static void serial_delay_blip(void) { wait_us(1); } | ||
| 56 | inline static void serial_output(void) { setPinOutput(SOFT_SERIAL_PIN); } | ||
| 57 | inline static void serial_input(void) { setPinInputHigh(SOFT_SERIAL_PIN); } | ||
| 58 | inline static bool serial_read_pin(void) { return !!readPin(SOFT_SERIAL_PIN); } | ||
| 59 | inline static void serial_low(void) { writePinLow(SOFT_SERIAL_PIN); } | ||
| 60 | inline static void serial_high(void) { writePinHigh(SOFT_SERIAL_PIN); } | ||
| 61 | |||
| 62 | void interrupt_handler(void *arg); | ||
| 63 | |||
| 64 | // Use thread + palWaitLineTimeout instead of palSetLineCallback | ||
| 65 | // - Methods like setPinOutput and palEnableLineEvent/palDisableLineEvent | ||
| 66 | // cause the interrupt to lock up, which would limit to only receiving data... | ||
| 67 | static THD_WORKING_AREA(waThread1, 128); | ||
| 68 | static THD_FUNCTION(Thread1, arg) { | ||
| 69 | (void)arg; | ||
| 70 | chRegSetThreadName("blinker"); | ||
| 71 | while (true) { | ||
| 72 | palWaitLineTimeout(SOFT_SERIAL_PIN, TIME_INFINITE); | ||
| 73 | interrupt_handler(NULL); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | static SSTD_t *Transaction_table = NULL; | ||
| 78 | static uint8_t Transaction_table_size = 0; | ||
| 79 | |||
| 80 | void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size) { | ||
| 81 | Transaction_table = sstd_table; | ||
| 82 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
| 83 | |||
| 84 | serial_output(); | ||
| 85 | serial_high(); | ||
| 86 | } | ||
| 87 | |||
| 88 | void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size) { | ||
| 89 | Transaction_table = sstd_table; | ||
| 90 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
| 91 | |||
| 92 | serial_input(); | ||
| 93 | |||
| 94 | palEnablePadEvent(PAL_PORT(SOFT_SERIAL_PIN), PAL_PAD(SOFT_SERIAL_PIN), PAL_EVENT_MODE_FALLING_EDGE); | ||
| 95 | chThdCreateStatic(waThread1, sizeof(waThread1), HIGHPRIO, Thread1, NULL); | ||
| 96 | } | ||
| 97 | |||
| 98 | // Used by the master to synchronize timing with the slave. | ||
| 99 | static void __attribute__((noinline)) sync_recv(void) { | ||
| 100 | serial_input(); | ||
| 101 | // This shouldn't hang if the slave disconnects because the | ||
| 102 | // serial line will float to high if the slave does disconnect. | ||
| 103 | while (!serial_read_pin()) { | ||
| 104 | } | ||
| 105 | |||
| 106 | serial_delay(); | ||
| 107 | } | ||
| 108 | |||
| 109 | // Used by the slave to send a synchronization signal to the master. | ||
| 110 | static void __attribute__((noinline)) sync_send(void) { | ||
| 111 | serial_output(); | ||
| 112 | |||
| 113 | serial_low(); | ||
| 114 | serial_delay(); | ||
| 115 | |||
| 116 | serial_high(); | ||
| 117 | } | ||
| 118 | |||
| 119 | // Reads a byte from the serial line | ||
| 120 | static uint8_t __attribute__((noinline)) serial_read_byte(void) { | ||
| 121 | uint8_t byte = 0; | ||
| 122 | serial_input(); | ||
| 123 | for (uint8_t i = 0; i < 8; ++i) { | ||
| 124 | byte = (byte << 1) | serial_read_pin(); | ||
| 125 | serial_delay(); | ||
| 126 | } | ||
| 127 | |||
| 128 | return byte; | ||
| 129 | } | ||
| 130 | |||
| 131 | // Sends a byte with MSB ordering | ||
| 132 | static void __attribute__((noinline)) serial_write_byte(uint8_t data) { | ||
| 133 | uint8_t b = 8; | ||
| 134 | serial_output(); | ||
| 135 | while (b--) { | ||
| 136 | if (data & (1 << b)) { | ||
| 137 | serial_high(); | ||
| 138 | } else { | ||
| 139 | serial_low(); | ||
| 140 | } | ||
| 141 | serial_delay(); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | // interrupt handle to be used by the slave device | ||
| 146 | void interrupt_handler(void *arg) { | ||
| 147 | chSysLockFromISR(); | ||
| 148 | |||
| 149 | sync_send(); | ||
| 150 | |||
| 151 | // read mid pulses | ||
| 152 | serial_delay_blip(); | ||
| 153 | |||
| 154 | uint8_t checksum_computed = 0; | ||
| 155 | int sstd_index = 0; | ||
| 156 | |||
| 157 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 158 | sstd_index = serial_read_byte(); | ||
| 159 | sync_send(); | ||
| 160 | #endif | ||
| 161 | |||
| 162 | SSTD_t *trans = &Transaction_table[sstd_index]; | ||
| 163 | for (int i = 0; i < trans->initiator2target_buffer_size; ++i) { | ||
| 164 | trans->initiator2target_buffer[i] = serial_read_byte(); | ||
| 165 | sync_send(); | ||
| 166 | checksum_computed += trans->initiator2target_buffer[i]; | ||
| 167 | } | ||
| 168 | checksum_computed ^= 7; | ||
| 169 | uint8_t checksum_received = serial_read_byte(); | ||
| 170 | sync_send(); | ||
| 171 | |||
| 172 | // wait for the sync to finish sending | ||
| 173 | serial_delay(); | ||
| 174 | |||
| 175 | uint8_t checksum = 0; | ||
| 176 | for (int i = 0; i < trans->target2initiator_buffer_size; ++i) { | ||
| 177 | serial_write_byte(trans->target2initiator_buffer[i]); | ||
| 178 | sync_send(); | ||
| 179 | serial_delay_half(); | ||
| 180 | checksum += trans->target2initiator_buffer[i]; | ||
| 181 | } | ||
| 182 | serial_write_byte(checksum ^ 7); | ||
| 183 | sync_send(); | ||
| 184 | |||
| 185 | // wait for the sync to finish sending | ||
| 186 | serial_delay(); | ||
| 187 | |||
| 188 | *trans->status = (checksum_computed == checksum_received) ? TRANSACTION_ACCEPTED : TRANSACTION_DATA_ERROR; | ||
| 189 | |||
| 190 | // end transaction | ||
| 191 | serial_input(); | ||
| 192 | |||
| 193 | // TODO: remove extra delay between transactions | ||
| 194 | serial_delay(); | ||
| 195 | |||
| 196 | chSysUnlockFromISR(); | ||
| 197 | } | ||
| 198 | |||
| 199 | ///////// | ||
| 200 | // start transaction by initiator | ||
| 201 | // | ||
| 202 | // int soft_serial_transaction(int sstd_index) | ||
| 203 | // | ||
| 204 | // Returns: | ||
| 205 | // TRANSACTION_END | ||
| 206 | // TRANSACTION_NO_RESPONSE | ||
| 207 | // TRANSACTION_DATA_ERROR | ||
| 208 | // this code is very time dependent, so we need to disable interrupts | ||
| 209 | #ifndef SERIAL_USE_MULTI_TRANSACTION | ||
| 210 | int soft_serial_transaction(void) { | ||
| 211 | int sstd_index = 0; | ||
| 212 | #else | ||
| 213 | int soft_serial_transaction(int sstd_index) { | ||
| 214 | #endif | ||
| 215 | |||
| 216 | if (sstd_index > Transaction_table_size) return TRANSACTION_TYPE_ERROR; | ||
| 217 | SSTD_t *trans = &Transaction_table[sstd_index]; | ||
| 218 | |||
| 219 | // TODO: remove extra delay between transactions | ||
| 220 | serial_delay(); | ||
| 221 | |||
| 222 | // this code is very time dependent, so we need to disable interrupts | ||
| 223 | chSysLock(); | ||
| 224 | |||
| 225 | // signal to the slave that we want to start a transaction | ||
| 226 | serial_output(); | ||
| 227 | serial_low(); | ||
| 228 | serial_delay_blip(); | ||
| 229 | |||
| 230 | // wait for the slaves response | ||
| 231 | serial_input(); | ||
| 232 | serial_high(); | ||
| 233 | serial_delay(); | ||
| 234 | |||
| 235 | // check if the slave is present | ||
| 236 | if (serial_read_pin()) { | ||
| 237 | // slave failed to pull the line low, assume not present | ||
| 238 | dprintf("serial::NO_RESPONSE\n"); | ||
| 239 | chSysUnlock(); | ||
| 240 | return TRANSACTION_NO_RESPONSE; | ||
| 241 | } | ||
| 242 | |||
| 243 | // if the slave is present syncronize with it | ||
| 244 | |||
| 245 | uint8_t checksum = 0; | ||
| 246 | // send data to the slave | ||
| 247 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 248 | serial_write_byte(sstd_index); // first chunk is transaction id | ||
| 249 | sync_recv(); | ||
| 250 | #endif | ||
| 251 | for (int i = 0; i < trans->initiator2target_buffer_size; ++i) { | ||
| 252 | serial_write_byte(trans->initiator2target_buffer[i]); | ||
| 253 | sync_recv(); | ||
| 254 | checksum += trans->initiator2target_buffer[i]; | ||
| 255 | } | ||
| 256 | serial_write_byte(checksum ^ 7); | ||
| 257 | sync_recv(); | ||
| 258 | |||
| 259 | serial_delay(); | ||
| 260 | serial_delay(); // read mid pulses | ||
| 261 | |||
| 262 | // receive data from the slave | ||
| 263 | uint8_t checksum_computed = 0; | ||
| 264 | for (int i = 0; i < trans->target2initiator_buffer_size; ++i) { | ||
| 265 | trans->target2initiator_buffer[i] = serial_read_byte(); | ||
| 266 | sync_recv(); | ||
| 267 | checksum_computed += trans->target2initiator_buffer[i]; | ||
| 268 | } | ||
| 269 | checksum_computed ^= 7; | ||
| 270 | uint8_t checksum_received = serial_read_byte(); | ||
| 271 | |||
| 272 | sync_recv(); | ||
| 273 | serial_delay(); | ||
| 274 | |||
| 275 | if ((checksum_computed) != (checksum_received)) { | ||
| 276 | dprintf("serial::FAIL[%u,%u,%u]\n", checksum_computed, checksum_received, sstd_index); | ||
| 277 | serial_output(); | ||
| 278 | serial_high(); | ||
| 279 | |||
| 280 | chSysUnlock(); | ||
| 281 | return TRANSACTION_DATA_ERROR; | ||
| 282 | } | ||
| 283 | |||
| 284 | // always, release the line when not in use | ||
| 285 | serial_high(); | ||
| 286 | serial_output(); | ||
| 287 | |||
| 288 | chSysUnlock(); | ||
| 289 | return TRANSACTION_END; | ||
| 290 | } | ||
diff --git a/drivers/chibios/serial.h b/drivers/chibios/serial.h deleted file mode 100644 index 0c1857d52..000000000 --- a/drivers/chibios/serial.h +++ /dev/null | |||
| @@ -1,62 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <stdbool.h> | ||
| 4 | |||
| 5 | // ///////////////////////////////////////////////////////////////// | ||
| 6 | // Need Soft Serial defines in config.h | ||
| 7 | // ///////////////////////////////////////////////////////////////// | ||
| 8 | // ex. | ||
| 9 | // #define SOFT_SERIAL_PIN ?? // ?? = D0,D1,D2,D3,E6 | ||
| 10 | // OPTIONAL: #define SELECT_SOFT_SERIAL_SPEED ? // ? = 1,2,3,4,5 | ||
| 11 | // // 1: about 137kbps (default) | ||
| 12 | // // 2: about 75kbps | ||
| 13 | // // 3: about 39kbps | ||
| 14 | // // 4: about 26kbps | ||
| 15 | // // 5: about 20kbps | ||
| 16 | // | ||
| 17 | // //// USE simple API (using signle-type transaction function) | ||
| 18 | // /* nothing */ | ||
| 19 | // //// USE flexible API (using multi-type transaction function) | ||
| 20 | // #define SERIAL_USE_MULTI_TRANSACTION | ||
| 21 | // | ||
| 22 | // ///////////////////////////////////////////////////////////////// | ||
| 23 | |||
| 24 | // Soft Serial Transaction Descriptor | ||
| 25 | typedef struct _SSTD_t { | ||
| 26 | uint8_t *status; | ||
| 27 | uint8_t initiator2target_buffer_size; | ||
| 28 | uint8_t *initiator2target_buffer; | ||
| 29 | uint8_t target2initiator_buffer_size; | ||
| 30 | uint8_t *target2initiator_buffer; | ||
| 31 | } SSTD_t; | ||
| 32 | #define TID_LIMIT(table) (sizeof(table) / sizeof(SSTD_t)) | ||
| 33 | |||
| 34 | // initiator is transaction start side | ||
| 35 | void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size); | ||
| 36 | // target is interrupt accept side | ||
| 37 | void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size); | ||
| 38 | |||
| 39 | // initiator result | ||
| 40 | #define TRANSACTION_END 0 | ||
| 41 | #define TRANSACTION_NO_RESPONSE 0x1 | ||
| 42 | #define TRANSACTION_DATA_ERROR 0x2 | ||
| 43 | #define TRANSACTION_TYPE_ERROR 0x4 | ||
| 44 | #ifndef SERIAL_USE_MULTI_TRANSACTION | ||
| 45 | int soft_serial_transaction(void); | ||
| 46 | #else | ||
| 47 | int soft_serial_transaction(int sstd_index); | ||
| 48 | #endif | ||
| 49 | |||
| 50 | // target status | ||
| 51 | // *SSTD_t.status has | ||
| 52 | // initiator: | ||
| 53 | // TRANSACTION_END | ||
| 54 | // or TRANSACTION_NO_RESPONSE | ||
| 55 | // or TRANSACTION_DATA_ERROR | ||
| 56 | // target: | ||
| 57 | // TRANSACTION_DATA_ERROR | ||
| 58 | // or TRANSACTION_ACCEPTED | ||
| 59 | #define TRANSACTION_ACCEPTED 0x8 | ||
| 60 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 61 | int soft_serial_get_and_clean_status(int sstd_index); | ||
| 62 | #endif | ||
diff --git a/drivers/chibios/serial_usart.c b/drivers/chibios/serial_usart.c deleted file mode 100644 index cae29388c..000000000 --- a/drivers/chibios/serial_usart.c +++ /dev/null | |||
| @@ -1,208 +0,0 @@ | |||
| 1 | /* Copyright 2021 QMK | ||
| 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 <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "serial_usart.h" | ||
| 18 | |||
| 19 | #ifndef USE_GPIOV1 | ||
| 20 | // The default PAL alternate modes are used to signal that the pins are used for USART | ||
| 21 | # ifndef SERIAL_USART_TX_PAL_MODE | ||
| 22 | # define SERIAL_USART_TX_PAL_MODE 7 | ||
| 23 | # endif | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #ifndef SERIAL_USART_DRIVER | ||
| 27 | # define SERIAL_USART_DRIVER SD1 | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #ifdef SOFT_SERIAL_PIN | ||
| 31 | # define SERIAL_USART_TX_PIN SOFT_SERIAL_PIN | ||
| 32 | #endif | ||
| 33 | |||
| 34 | static inline msg_t sdWriteHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size) { | ||
| 35 | msg_t ret = sdWrite(driver, data, size); | ||
| 36 | |||
| 37 | // Half duplex requires us to read back the data we just wrote - just throw it away | ||
| 38 | uint8_t dump[size]; | ||
| 39 | sdRead(driver, dump, size); | ||
| 40 | |||
| 41 | return ret; | ||
| 42 | } | ||
| 43 | #undef sdWrite | ||
| 44 | #define sdWrite sdWriteHalfDuplex | ||
| 45 | |||
| 46 | static inline msg_t sdWriteTimeoutHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size, uint32_t timeout) { | ||
| 47 | msg_t ret = sdWriteTimeout(driver, data, size, timeout); | ||
| 48 | |||
| 49 | // Half duplex requires us to read back the data we just wrote - just throw it away | ||
| 50 | uint8_t dump[size]; | ||
| 51 | sdReadTimeout(driver, dump, size, timeout); | ||
| 52 | |||
| 53 | return ret; | ||
| 54 | } | ||
| 55 | #undef sdWriteTimeout | ||
| 56 | #define sdWriteTimeout sdWriteTimeoutHalfDuplex | ||
| 57 | |||
| 58 | static inline void sdClear(SerialDriver* driver) { | ||
| 59 | while (sdGetTimeout(driver, TIME_IMMEDIATE) != MSG_TIMEOUT) { | ||
| 60 | // Do nothing with the data | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | static SerialConfig sdcfg = { | ||
| 65 | (SERIAL_USART_SPEED), // speed - mandatory | ||
| 66 | (SERIAL_USART_CR1), // CR1 | ||
| 67 | (SERIAL_USART_CR2), // CR2 | ||
| 68 | (SERIAL_USART_CR3) // CR3 | ||
| 69 | }; | ||
| 70 | |||
| 71 | void handle_soft_serial_slave(void); | ||
| 72 | |||
| 73 | /* | ||
| 74 | * This thread runs on the slave and responds to transactions initiated | ||
| 75 | * by the master | ||
| 76 | */ | ||
| 77 | static THD_WORKING_AREA(waSlaveThread, 2048); | ||
| 78 | static THD_FUNCTION(SlaveThread, arg) { | ||
| 79 | (void)arg; | ||
| 80 | chRegSetThreadName("slave_transport"); | ||
| 81 | |||
| 82 | while (true) { | ||
| 83 | handle_soft_serial_slave(); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | __attribute__((weak)) void usart_init(void) { | ||
| 88 | #if defined(USE_GPIOV1) | ||
| 89 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
| 90 | #else | ||
| 91 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
| 92 | #endif | ||
| 93 | |||
| 94 | #if defined(USART_REMAP) | ||
| 95 | USART_REMAP; | ||
| 96 | #endif | ||
| 97 | } | ||
| 98 | |||
| 99 | void usart_master_init(void) { | ||
| 100 | usart_init(); | ||
| 101 | |||
| 102 | sdcfg.cr3 |= USART_CR3_HDSEL; | ||
| 103 | sdStart(&SERIAL_USART_DRIVER, &sdcfg); | ||
| 104 | } | ||
| 105 | |||
| 106 | void usart_slave_init(void) { | ||
| 107 | usart_init(); | ||
| 108 | |||
| 109 | sdcfg.cr3 |= USART_CR3_HDSEL; | ||
| 110 | sdStart(&SERIAL_USART_DRIVER, &sdcfg); | ||
| 111 | |||
| 112 | // Start transport thread | ||
| 113 | chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); | ||
| 114 | } | ||
| 115 | |||
| 116 | static SSTD_t* Transaction_table = NULL; | ||
| 117 | static uint8_t Transaction_table_size = 0; | ||
| 118 | |||
| 119 | void soft_serial_initiator_init(SSTD_t* sstd_table, int sstd_table_size) { | ||
| 120 | Transaction_table = sstd_table; | ||
| 121 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
| 122 | |||
| 123 | usart_master_init(); | ||
| 124 | } | ||
| 125 | |||
| 126 | void soft_serial_target_init(SSTD_t* sstd_table, int sstd_table_size) { | ||
| 127 | Transaction_table = sstd_table; | ||
| 128 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
| 129 | |||
| 130 | usart_slave_init(); | ||
| 131 | } | ||
| 132 | |||
| 133 | void handle_soft_serial_slave(void) { | ||
| 134 | uint8_t sstd_index = sdGet(&SERIAL_USART_DRIVER); // first chunk is always transaction id | ||
| 135 | SSTD_t* trans = &Transaction_table[sstd_index]; | ||
| 136 | |||
| 137 | // Always write back the sstd_index as part of a basic handshake | ||
| 138 | sstd_index ^= HANDSHAKE_MAGIC; | ||
| 139 | sdWrite(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index)); | ||
| 140 | |||
| 141 | if (trans->initiator2target_buffer_size) { | ||
| 142 | sdRead(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, trans->initiator2target_buffer_size); | ||
| 143 | } | ||
| 144 | |||
| 145 | if (trans->target2initiator_buffer_size) { | ||
| 146 | sdWrite(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size); | ||
| 147 | } | ||
| 148 | |||
| 149 | if (trans->status) { | ||
| 150 | *trans->status = TRANSACTION_ACCEPTED; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | ///////// | ||
| 155 | // start transaction by initiator | ||
| 156 | // | ||
| 157 | // int soft_serial_transaction(int sstd_index) | ||
| 158 | // | ||
| 159 | // Returns: | ||
| 160 | // TRANSACTION_END | ||
| 161 | // TRANSACTION_NO_RESPONSE | ||
| 162 | // TRANSACTION_DATA_ERROR | ||
| 163 | #ifndef SERIAL_USE_MULTI_TRANSACTION | ||
| 164 | int soft_serial_transaction(void) { | ||
| 165 | uint8_t sstd_index = 0; | ||
| 166 | #else | ||
| 167 | int soft_serial_transaction(int index) { | ||
| 168 | uint8_t sstd_index = index; | ||
| 169 | #endif | ||
| 170 | |||
| 171 | if (sstd_index > Transaction_table_size) return TRANSACTION_TYPE_ERROR; | ||
| 172 | SSTD_t* trans = &Transaction_table[sstd_index]; | ||
| 173 | msg_t res = 0; | ||
| 174 | |||
| 175 | sdClear(&SERIAL_USART_DRIVER); | ||
| 176 | |||
| 177 | // First chunk is always transaction id | ||
| 178 | sdWriteTimeout(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index), TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 179 | |||
| 180 | uint8_t sstd_index_shake = 0xFF; | ||
| 181 | |||
| 182 | // Which we always read back first so that we can error out correctly | ||
| 183 | // - due to the half duplex limitations on return codes, we always have to read *something* | ||
| 184 | // - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready | ||
| 185 | res = sdReadTimeout(&SERIAL_USART_DRIVER, &sstd_index_shake, sizeof(sstd_index_shake), TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 186 | if (res < 0 || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { | ||
| 187 | dprintf("serial::usart_shake NO_RESPONSE\n"); | ||
| 188 | return TRANSACTION_NO_RESPONSE; | ||
| 189 | } | ||
| 190 | |||
| 191 | if (trans->initiator2target_buffer_size) { | ||
| 192 | res = sdWriteTimeout(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, trans->initiator2target_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 193 | if (res < 0) { | ||
| 194 | dprintf("serial::usart_transmit NO_RESPONSE\n"); | ||
| 195 | return TRANSACTION_NO_RESPONSE; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | if (trans->target2initiator_buffer_size) { | ||
| 200 | res = sdReadTimeout(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 201 | if (res < 0) { | ||
| 202 | dprintf("serial::usart_receive NO_RESPONSE\n"); | ||
| 203 | return TRANSACTION_NO_RESPONSE; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | return TRANSACTION_END; | ||
| 208 | } | ||
diff --git a/drivers/chibios/serial_usart.h b/drivers/chibios/serial_usart.h deleted file mode 100644 index fee7b4d15..000000000 --- a/drivers/chibios/serial_usart.h +++ /dev/null | |||
| @@ -1,90 +0,0 @@ | |||
| 1 | /* Copyright 2021 QMK | ||
| 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 <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #pragma once | ||
| 18 | |||
| 19 | #include "quantum.h" | ||
| 20 | #include "serial.h" | ||
| 21 | #include "printf.h" | ||
| 22 | |||
| 23 | #include <ch.h> | ||
| 24 | #include <hal.h> | ||
| 25 | |||
| 26 | #ifndef USART_CR1_M0 | ||
| 27 | # define USART_CR1_M0 USART_CR1_M // some platforms (f1xx) dont have this so | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #ifndef SERIAL_USART_CR1 | ||
| 31 | # define SERIAL_USART_CR1 (USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0) // parity enable, odd parity, 9 bit length | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #ifndef SERIAL_USART_CR2 | ||
| 35 | # define SERIAL_USART_CR2 (USART_CR2_STOP_1) // 2 stop bits | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #ifndef SERIAL_USART_CR3 | ||
| 39 | # define SERIAL_USART_CR3 0 | ||
| 40 | #endif | ||
| 41 | |||
| 42 | #if defined(USART1_REMAP) | ||
| 43 | # define USART_REMAP \ | ||
| 44 | do { \ | ||
| 45 | (AFIO->MAPR |= AFIO_MAPR_USART1_REMAP); \ | ||
| 46 | } while (0) | ||
| 47 | #elif defined(USART2_REMAP) | ||
| 48 | # define USART_REMAP \ | ||
| 49 | do { \ | ||
| 50 | (AFIO->MAPR |= AFIO_MAPR_USART2_REMAP); \ | ||
| 51 | } while (0) | ||
| 52 | #elif defined(USART3_PARTIALREMAP) | ||
| 53 | # define USART_REMAP \ | ||
| 54 | do { \ | ||
| 55 | (AFIO->MAPR |= AFIO_MAPR_USART3_REMAP_PARTIALREMAP); \ | ||
| 56 | } while (0) | ||
| 57 | #elif defined(USART3_FULLREMAP) | ||
| 58 | # define USART_REMAP \ | ||
| 59 | do { \ | ||
| 60 | (AFIO->MAPR |= AFIO_MAPR_USART3_REMAP_FULLREMAP); \ | ||
| 61 | } while (0) | ||
| 62 | #endif | ||
| 63 | |||
| 64 | #ifndef SELECT_SOFT_SERIAL_SPEED | ||
| 65 | # define SELECT_SOFT_SERIAL_SPEED 1 | ||
| 66 | #endif | ||
| 67 | |||
| 68 | #ifdef SERIAL_USART_SPEED | ||
| 69 | // Allow advanced users to directly set SERIAL_USART_SPEED | ||
| 70 | #elif SELECT_SOFT_SERIAL_SPEED == 0 | ||
| 71 | # define SERIAL_USART_SPEED 460800 | ||
| 72 | #elif SELECT_SOFT_SERIAL_SPEED == 1 | ||
| 73 | # define SERIAL_USART_SPEED 230400 | ||
| 74 | #elif SELECT_SOFT_SERIAL_SPEED == 2 | ||
| 75 | # define SERIAL_USART_SPEED 115200 | ||
| 76 | #elif SELECT_SOFT_SERIAL_SPEED == 3 | ||
| 77 | # define SERIAL_USART_SPEED 57600 | ||
| 78 | #elif SELECT_SOFT_SERIAL_SPEED == 4 | ||
| 79 | # define SERIAL_USART_SPEED 38400 | ||
| 80 | #elif SELECT_SOFT_SERIAL_SPEED == 5 | ||
| 81 | # define SERIAL_USART_SPEED 19200 | ||
| 82 | #else | ||
| 83 | # error invalid SELECT_SOFT_SERIAL_SPEED value | ||
| 84 | #endif | ||
| 85 | |||
| 86 | #ifndef SERIAL_USART_TIMEOUT | ||
| 87 | # define SERIAL_USART_TIMEOUT 100 | ||
| 88 | #endif | ||
| 89 | |||
| 90 | #define HANDSHAKE_MAGIC 7 | ||
diff --git a/drivers/chibios/serial_usart_duplex.c b/drivers/chibios/serial_usart_duplex.c deleted file mode 100644 index cc9b889ac..000000000 --- a/drivers/chibios/serial_usart_duplex.c +++ /dev/null | |||
| @@ -1,261 +0,0 @@ | |||
| 1 | /* Copyright 2021 QMK | ||
| 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 <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "serial_usart.h" | ||
| 18 | |||
| 19 | #include <stdatomic.h> | ||
| 20 | |||
| 21 | #if !defined(USE_GPIOV1) | ||
| 22 | // The default PAL alternate modes are used to signal that the pins are used for USART | ||
| 23 | # if !defined(SERIAL_USART_TX_PAL_MODE) | ||
| 24 | # define SERIAL_USART_TX_PAL_MODE 7 | ||
| 25 | # endif | ||
| 26 | # if !defined(SERIAL_USART_RX_PAL_MODE) | ||
| 27 | # define SERIAL_USART_RX_PAL_MODE 7 | ||
| 28 | # endif | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #if !defined(SERIAL_USART_DRIVER) | ||
| 32 | # define SERIAL_USART_DRIVER UARTD1 | ||
| 33 | #endif | ||
| 34 | |||
| 35 | #if !defined(SERIAL_USART_TX_PIN) | ||
| 36 | # define SERIAL_USART_TX_PIN A9 | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #if !defined(SERIAL_USART_RX_PIN) | ||
| 40 | # define SERIAL_USART_RX_PIN A10 | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #define SIGNAL_HANDSHAKE_RECEIVED 0x1 | ||
| 44 | |||
| 45 | void handle_transactions_slave(uint8_t sstd_index); | ||
| 46 | static void receive_transaction_handshake(UARTDriver* uartp, uint16_t received_handshake); | ||
| 47 | |||
| 48 | /* | ||
| 49 | * UART driver configuration structure. We use the blocking DMA enabled API and | ||
| 50 | * the rxchar callback to receive handshake tokens but only on the slave halve. | ||
| 51 | */ | ||
| 52 | // clang-format off | ||
| 53 | static UARTConfig uart_config = { | ||
| 54 | .txend1_cb = NULL, | ||
| 55 | .txend2_cb = NULL, | ||
| 56 | .rxend_cb = NULL, | ||
| 57 | .rxchar_cb = NULL, | ||
| 58 | .rxerr_cb = NULL, | ||
| 59 | .timeout_cb = NULL, | ||
| 60 | .speed = (SERIAL_USART_SPEED), | ||
| 61 | .cr1 = (SERIAL_USART_CR1), | ||
| 62 | .cr2 = (SERIAL_USART_CR2), | ||
| 63 | .cr3 = (SERIAL_USART_CR3) | ||
| 64 | }; | ||
| 65 | // clang-format on | ||
| 66 | |||
| 67 | static SSTD_t* Transaction_table = NULL; | ||
| 68 | static uint8_t Transaction_table_size = 0; | ||
| 69 | static atomic_uint_least8_t handshake = 0xFF; | ||
| 70 | static thread_reference_t tp_target = NULL; | ||
| 71 | |||
| 72 | /* | ||
| 73 | * This callback is invoked when a character is received but the application | ||
| 74 | * was not ready to receive it, the character is passed as parameter. | ||
| 75 | * Receive transaction table index from initiator, which doubles as basic handshake token. */ | ||
| 76 | static void receive_transaction_handshake(UARTDriver* uartp, uint16_t received_handshake) { | ||
| 77 | /* Check if received handshake is not a valid transaction id. | ||
| 78 | * Please note that we can still catch a seemingly valid handshake | ||
| 79 | * i.e. a byte from a ongoing transfer which is in the allowed range. | ||
| 80 | * So this check mainly prevents any obviously wrong handshakes and | ||
| 81 | * subsequent wakeups of the receiving thread, which is a costly operation. */ | ||
| 82 | if (received_handshake > Transaction_table_size) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | handshake = (uint8_t)received_handshake; | ||
| 87 | chSysLockFromISR(); | ||
| 88 | /* Wakeup receiving thread to start a transaction. */ | ||
| 89 | chEvtSignalI(tp_target, (eventmask_t)SIGNAL_HANDSHAKE_RECEIVED); | ||
| 90 | chSysUnlockFromISR(); | ||
| 91 | } | ||
| 92 | |||
| 93 | __attribute__((weak)) void usart_init(void) { | ||
| 94 | #if defined(USE_GPIOV1) | ||
| 95 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL); | ||
| 96 | palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_INPUT); | ||
| 97 | #else | ||
| 98 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_TX_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); | ||
| 99 | palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_RX_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); | ||
| 100 | #endif | ||
| 101 | } | ||
| 102 | |||
| 103 | /* | ||
| 104 | * This thread runs on the slave half and reacts to transactions initiated from the master. | ||
| 105 | */ | ||
| 106 | static THD_WORKING_AREA(waSlaveThread, 1024); | ||
| 107 | static THD_FUNCTION(SlaveThread, arg) { | ||
| 108 | (void)arg; | ||
| 109 | chRegSetThreadName("slave_usart_tx_rx"); | ||
| 110 | |||
| 111 | while (true) { | ||
| 112 | /* We sleep as long as there is no handshake waiting for us. */ | ||
| 113 | chEvtWaitAny((eventmask_t)SIGNAL_HANDSHAKE_RECEIVED); | ||
| 114 | handle_transactions_slave(handshake); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | void soft_serial_target_init(SSTD_t* const sstd_table, int sstd_table_size) { | ||
| 119 | Transaction_table = sstd_table; | ||
| 120 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
| 121 | usart_init(); | ||
| 122 | |||
| 123 | #if defined(USART_REMAP) | ||
| 124 | USART_REMAP; | ||
| 125 | #endif | ||
| 126 | |||
| 127 | tp_target = chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); | ||
| 128 | |||
| 129 | // Start receiving handshake tokens on slave halve | ||
| 130 | uart_config.rxchar_cb = receive_transaction_handshake; | ||
| 131 | uartStart(&SERIAL_USART_DRIVER, &uart_config); | ||
| 132 | } | ||
| 133 | |||
| 134 | /** | ||
| 135 | * @brief React to transactions started by the master. | ||
| 136 | * This version uses duplex send and receive usart pheriphals and DMA backed transfers. | ||
| 137 | */ | ||
| 138 | void inline handle_transactions_slave(uint8_t sstd_index) { | ||
| 139 | size_t buffer_size = 0; | ||
| 140 | msg_t msg = 0; | ||
| 141 | SSTD_t* trans = &Transaction_table[sstd_index]; | ||
| 142 | |||
| 143 | /* Send back the handshake which is XORed as a simple checksum, | ||
| 144 | to signal that the slave is ready to receive possible transaction buffers */ | ||
| 145 | sstd_index ^= HANDSHAKE_MAGIC; | ||
| 146 | buffer_size = (size_t)sizeof(sstd_index); | ||
| 147 | msg = uartSendTimeout(&SERIAL_USART_DRIVER, &buffer_size, &sstd_index, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 148 | |||
| 149 | if (msg != MSG_OK) { | ||
| 150 | if (trans->status) { | ||
| 151 | *trans->status = TRANSACTION_NO_RESPONSE; | ||
| 152 | } | ||
| 153 | return; | ||
| 154 | } | ||
| 155 | |||
| 156 | /* Receive transaction buffer from the master. If this transaction requires it.*/ | ||
| 157 | buffer_size = (size_t)trans->initiator2target_buffer_size; | ||
| 158 | if (buffer_size) { | ||
| 159 | msg = uartReceiveTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->initiator2target_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 160 | if (msg != MSG_OK) { | ||
| 161 | if (trans->status) { | ||
| 162 | *trans->status = TRANSACTION_NO_RESPONSE; | ||
| 163 | } | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | /* Send transaction buffer to the master. If this transaction requires it. */ | ||
| 169 | buffer_size = (size_t)trans->target2initiator_buffer_size; | ||
| 170 | if (buffer_size) { | ||
| 171 | msg = uartSendFullTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->target2initiator_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 172 | if (msg != MSG_OK) { | ||
| 173 | if (trans->status) { | ||
| 174 | *trans->status = TRANSACTION_NO_RESPONSE; | ||
| 175 | } | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | if (trans->status) { | ||
| 181 | *trans->status = TRANSACTION_ACCEPTED; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | void soft_serial_initiator_init(SSTD_t* const sstd_table, int sstd_table_size) { | ||
| 186 | Transaction_table = sstd_table; | ||
| 187 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
| 188 | usart_init(); | ||
| 189 | |||
| 190 | #if defined(SERIAL_USART_PIN_SWAP) | ||
| 191 | uart_config.cr2 |= USART_CR2_SWAP; // master has swapped TX/RX pins | ||
| 192 | #endif | ||
| 193 | |||
| 194 | #if defined(USART_REMAP) | ||
| 195 | USART_REMAP; | ||
| 196 | #endif | ||
| 197 | |||
| 198 | uartStart(&SERIAL_USART_DRIVER, &uart_config); | ||
| 199 | } | ||
| 200 | |||
| 201 | /** | ||
| 202 | * @brief Start transaction from the master to the slave. | ||
| 203 | * This version uses duplex send and receive usart pheriphals and DMA backed transfers. | ||
| 204 | * | ||
| 205 | * @param index Transaction Table index of the transaction to start. | ||
| 206 | * @return int TRANSACTION_NO_RESPONSE in case of Timeout. | ||
| 207 | * TRANSACTION_TYPE_ERROR in case of invalid transaction index. | ||
| 208 | * TRANSACTION_END in case of success. | ||
| 209 | */ | ||
| 210 | #if !defined(SERIAL_USE_MULTI_TRANSACTION) | ||
| 211 | int soft_serial_transaction(void) { | ||
| 212 | uint8_t sstd_index = 0; | ||
| 213 | #else | ||
| 214 | int soft_serial_transaction(int index) { | ||
| 215 | uint8_t sstd_index = index; | ||
| 216 | #endif | ||
| 217 | |||
| 218 | if (sstd_index > Transaction_table_size) { | ||
| 219 | return TRANSACTION_TYPE_ERROR; | ||
| 220 | } | ||
| 221 | |||
| 222 | SSTD_t* const trans = &Transaction_table[sstd_index]; | ||
| 223 | msg_t msg = 0; | ||
| 224 | size_t buffer_size = (size_t)sizeof(sstd_index); | ||
| 225 | |||
| 226 | /* Send transaction table index to the slave, which doubles as basic handshake token. */ | ||
| 227 | uartSendFullTimeout(&SERIAL_USART_DRIVER, &buffer_size, &sstd_index, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 228 | |||
| 229 | uint8_t sstd_index_shake = 0xFF; | ||
| 230 | buffer_size = (size_t)sizeof(sstd_index_shake); | ||
| 231 | |||
| 232 | /* Receive the handshake token from the slave. The token was XORed by the slave as a simple checksum. | ||
| 233 | If the tokens match, the master will start to send and receive possible transaction buffers. */ | ||
| 234 | msg = uartReceiveTimeout(&SERIAL_USART_DRIVER, &buffer_size, &sstd_index_shake, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 235 | if (msg != MSG_OK || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { | ||
| 236 | dprintln("USART: Handshake Failed"); | ||
| 237 | return TRANSACTION_NO_RESPONSE; | ||
| 238 | } | ||
| 239 | |||
| 240 | /* Send transaction buffer to the slave. If this transaction requires it. */ | ||
| 241 | buffer_size = (size_t)trans->initiator2target_buffer_size; | ||
| 242 | if (buffer_size) { | ||
| 243 | msg = uartSendFullTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->initiator2target_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 244 | if (msg != MSG_OK) { | ||
| 245 | dprintln("USART: Send Failed"); | ||
| 246 | return TRANSACTION_NO_RESPONSE; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | /* Receive transaction buffer from the slave. If this transaction requires it. */ | ||
| 251 | buffer_size = (size_t)trans->target2initiator_buffer_size; | ||
| 252 | if (buffer_size) { | ||
| 253 | msg = uartReceiveTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->target2initiator_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
| 254 | if (msg != MSG_OK) { | ||
| 255 | dprintln("USART: Receive Failed"); | ||
| 256 | return TRANSACTION_NO_RESPONSE; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | return TRANSACTION_END; | ||
| 261 | } | ||
diff --git a/drivers/chibios/spi_master.c b/drivers/chibios/spi_master.c deleted file mode 100644 index 4852a6eba..000000000 --- a/drivers/chibios/spi_master.c +++ /dev/null | |||
| @@ -1,148 +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 | #include "spi_master.h" | ||
| 18 | |||
| 19 | #include "timer.h" | ||
| 20 | |||
| 21 | static pin_t currentSlavePin = NO_PIN; | ||
| 22 | static SPIConfig spiConfig = {false, NULL, 0, 0, 0, 0}; | ||
| 23 | |||
| 24 | __attribute__((weak)) void spi_init(void) { | ||
| 25 | static bool is_initialised = false; | ||
| 26 | if (!is_initialised) { | ||
| 27 | is_initialised = true; | ||
| 28 | |||
| 29 | // Try releasing special pins for a short time | ||
| 30 | palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_INPUT); | ||
| 31 | palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_INPUT); | ||
| 32 | palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_INPUT); | ||
| 33 | |||
| 34 | chThdSleepMilliseconds(10); | ||
| 35 | #if defined(USE_GPIOV1) | ||
| 36 | palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL); | ||
| 37 | palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL); | ||
| 38 | palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL); | ||
| 39 | #else | ||
| 40 | 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); | ||
| 41 | 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); | ||
| 42 | 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); | ||
| 43 | #endif | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) { | ||
| 48 | if (currentSlavePin != NO_PIN || slavePin == NO_PIN) { | ||
| 49 | return false; | ||
| 50 | } | ||
| 51 | |||
| 52 | uint16_t roundedDivisor = 2; | ||
| 53 | while (roundedDivisor < divisor) { | ||
| 54 | roundedDivisor <<= 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (roundedDivisor < 2 || roundedDivisor > 256) { | ||
| 58 | return false; | ||
| 59 | } | ||
| 60 | |||
| 61 | spiConfig.cr1 = 0; | ||
| 62 | |||
| 63 | if (lsbFirst) { | ||
| 64 | spiConfig.cr1 |= SPI_CR1_LSBFIRST; | ||
| 65 | } | ||
| 66 | |||
| 67 | switch (mode) { | ||
| 68 | case 0: | ||
| 69 | break; | ||
| 70 | case 1: | ||
| 71 | spiConfig.cr1 |= SPI_CR1_CPHA; | ||
| 72 | break; | ||
| 73 | case 2: | ||
| 74 | spiConfig.cr1 |= SPI_CR1_CPOL; | ||
| 75 | break; | ||
| 76 | case 3: | ||
| 77 | spiConfig.cr1 |= SPI_CR1_CPHA | SPI_CR1_CPOL; | ||
| 78 | break; | ||
| 79 | } | ||
| 80 | |||
| 81 | switch (roundedDivisor) { | ||
| 82 | case 2: | ||
| 83 | break; | ||
| 84 | case 4: | ||
| 85 | spiConfig.cr1 |= SPI_CR1_BR_0; | ||
| 86 | break; | ||
| 87 | case 8: | ||
| 88 | spiConfig.cr1 |= SPI_CR1_BR_1; | ||
| 89 | break; | ||
| 90 | case 16: | ||
| 91 | spiConfig.cr1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0; | ||
| 92 | break; | ||
| 93 | case 32: | ||
| 94 | spiConfig.cr1 |= SPI_CR1_BR_2; | ||
| 95 | break; | ||
| 96 | case 64: | ||
| 97 | spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0; | ||
| 98 | break; | ||
| 99 | case 128: | ||
| 100 | spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1; | ||
| 101 | break; | ||
| 102 | case 256: | ||
| 103 | spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0; | ||
| 104 | break; | ||
| 105 | } | ||
| 106 | |||
| 107 | currentSlavePin = slavePin; | ||
| 108 | spiConfig.ssport = PAL_PORT(slavePin); | ||
| 109 | spiConfig.sspad = PAL_PAD(slavePin); | ||
| 110 | |||
| 111 | setPinOutput(slavePin); | ||
| 112 | spiStart(&SPI_DRIVER, &spiConfig); | ||
| 113 | spiSelect(&SPI_DRIVER); | ||
| 114 | |||
| 115 | return true; | ||
| 116 | } | ||
| 117 | |||
| 118 | spi_status_t spi_write(uint8_t data) { | ||
| 119 | uint8_t rxData; | ||
| 120 | spiExchange(&SPI_DRIVER, 1, &data, &rxData); | ||
| 121 | |||
| 122 | return rxData; | ||
| 123 | } | ||
| 124 | |||
| 125 | spi_status_t spi_read(void) { | ||
| 126 | uint8_t data = 0; | ||
| 127 | spiReceive(&SPI_DRIVER, 1, &data); | ||
| 128 | |||
| 129 | return data; | ||
| 130 | } | ||
| 131 | |||
| 132 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length) { | ||
| 133 | spiSend(&SPI_DRIVER, length, data); | ||
| 134 | return SPI_STATUS_SUCCESS; | ||
| 135 | } | ||
| 136 | |||
| 137 | spi_status_t spi_receive(uint8_t *data, uint16_t length) { | ||
| 138 | spiReceive(&SPI_DRIVER, length, data); | ||
| 139 | return SPI_STATUS_SUCCESS; | ||
| 140 | } | ||
| 141 | |||
| 142 | void spi_stop(void) { | ||
| 143 | if (currentSlavePin != NO_PIN) { | ||
| 144 | spiUnselect(&SPI_DRIVER); | ||
| 145 | spiStop(&SPI_DRIVER); | ||
| 146 | currentSlavePin = NO_PIN; | ||
| 147 | } | ||
| 148 | } | ||
diff --git a/drivers/chibios/spi_master.h b/drivers/chibios/spi_master.h deleted file mode 100644 index e93580e31..000000000 --- a/drivers/chibios/spi_master.h +++ /dev/null | |||
| @@ -1,80 +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 | |||
| 25 | #ifndef SPI_DRIVER | ||
| 26 | # define SPI_DRIVER SPID2 | ||
| 27 | #endif | ||
| 28 | |||
| 29 | #ifndef SPI_SCK_PIN | ||
| 30 | # define SPI_SCK_PIN B13 | ||
| 31 | #endif | ||
| 32 | |||
| 33 | #ifndef SPI_SCK_PAL_MODE | ||
| 34 | # define SPI_SCK_PAL_MODE 5 | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #ifndef SPI_MOSI_PIN | ||
| 38 | # define SPI_MOSI_PIN B15 | ||
| 39 | #endif | ||
| 40 | |||
| 41 | #ifndef SPI_MOSI_PAL_MODE | ||
| 42 | # define SPI_MOSI_PAL_MODE 5 | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #ifndef SPI_MISO_PIN | ||
| 46 | # define SPI_MISO_PIN B14 | ||
| 47 | #endif | ||
| 48 | |||
| 49 | #ifndef SPI_MISO_PAL_MODE | ||
| 50 | # define SPI_MISO_PAL_MODE 5 | ||
| 51 | #endif | ||
| 52 | |||
| 53 | typedef int16_t spi_status_t; | ||
| 54 | |||
| 55 | #define SPI_STATUS_SUCCESS (0) | ||
| 56 | #define SPI_STATUS_ERROR (-1) | ||
| 57 | #define SPI_STATUS_TIMEOUT (-2) | ||
| 58 | |||
| 59 | #define SPI_TIMEOUT_IMMEDIATE (0) | ||
| 60 | #define SPI_TIMEOUT_INFINITE (0xFFFF) | ||
| 61 | |||
| 62 | #ifdef __cplusplus | ||
| 63 | extern "C" { | ||
| 64 | #endif | ||
| 65 | void spi_init(void); | ||
| 66 | |||
| 67 | bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor); | ||
| 68 | |||
| 69 | spi_status_t spi_write(uint8_t data); | ||
| 70 | |||
| 71 | spi_status_t spi_read(void); | ||
| 72 | |||
| 73 | spi_status_t spi_transmit(const uint8_t *data, uint16_t length); | ||
| 74 | |||
| 75 | spi_status_t spi_receive(uint8_t *data, uint16_t length); | ||
| 76 | |||
| 77 | void spi_stop(void); | ||
| 78 | #ifdef __cplusplus | ||
| 79 | } | ||
| 80 | #endif | ||
diff --git a/drivers/chibios/uart.c b/drivers/chibios/uart.c deleted file mode 100644 index 030335b34..000000000 --- a/drivers/chibios/uart.c +++ /dev/null | |||
| @@ -1,50 +0,0 @@ | |||
| 1 | /* Copyright 2021 | ||
| 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 "uart.h" | ||
| 18 | |||
| 19 | #include "quantum.h" | ||
| 20 | |||
| 21 | static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE, SD1_CR1, SD1_CR2, SD1_CR3}; | ||
| 22 | |||
| 23 | void uart_init(uint32_t baud) { | ||
| 24 | static bool is_initialised = false; | ||
| 25 | |||
| 26 | if (!is_initialised) { | ||
| 27 | is_initialised = true; | ||
| 28 | |||
| 29 | serialConfig.speed = baud; | ||
| 30 | |||
| 31 | #if defined(USE_GPIOV1) | ||
| 32 | palSetLineMode(SD1_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
| 33 | palSetLineMode(SD1_RX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
| 34 | #else | ||
| 35 | palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
| 36 | palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
| 37 | #endif | ||
| 38 | sdStart(&SERIAL_DRIVER, &serialConfig); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | void uart_putchar(uint8_t c) { sdPut(&SERIAL_DRIVER, c); } | ||
| 43 | |||
| 44 | uint8_t uart_getchar(void) { | ||
| 45 | msg_t res = sdGet(&SERIAL_DRIVER); | ||
| 46 | |||
| 47 | return (uint8_t)res; | ||
| 48 | } | ||
| 49 | |||
| 50 | bool uart_available(void) { return !sdGetWouldBlock(&SERIAL_DRIVER); } | ||
diff --git a/drivers/chibios/uart.h b/drivers/chibios/uart.h deleted file mode 100644 index b4e20e9fd..000000000 --- a/drivers/chibios/uart.h +++ /dev/null | |||
| @@ -1,77 +0,0 @@ | |||
| 1 | /* Copyright 2021 | ||
| 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 <stdint.h> | ||
| 20 | |||
| 21 | #include <hal.h> | ||
| 22 | |||
| 23 | #ifndef SERIAL_DRIVER | ||
| 24 | # define SERIAL_DRIVER SD1 | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #ifndef SD1_TX_PIN | ||
| 28 | # define SD1_TX_PIN A9 | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #ifndef SD1_TX_PAL_MODE | ||
| 32 | # define SD1_TX_PAL_MODE 7 | ||
| 33 | #endif | ||
| 34 | |||
| 35 | #ifndef SD1_RX_PIN | ||
| 36 | # define SD1_RX_PIN A10 | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #ifndef SD1_RX_PAL_MODE | ||
| 40 | # define SD1_RX_PAL_MODE 7 | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #ifndef SD1_CTS_PIN | ||
| 44 | # define SD1_CTS_PIN A11 | ||
| 45 | #endif | ||
| 46 | |||
| 47 | #ifndef SD1_CTS_PAL_MODE | ||
| 48 | # define SD1_CTS_PAL_MODE 7 | ||
| 49 | #endif | ||
| 50 | |||
| 51 | #ifndef SD1_RTS_PIN | ||
| 52 | # define SD1_RTS_PIN A12 | ||
| 53 | #endif | ||
| 54 | |||
| 55 | #ifndef SD1_RTS_PAL_MODE | ||
| 56 | # define SD1_RTS_PAL_MODE 7 | ||
| 57 | #endif | ||
| 58 | |||
| 59 | #ifndef SD1_CR1 | ||
| 60 | # define SD1_CR1 0 | ||
| 61 | #endif | ||
| 62 | |||
| 63 | #ifndef SD1_CR2 | ||
| 64 | # define SD1_CR2 0 | ||
| 65 | #endif | ||
| 66 | |||
| 67 | #ifndef SD1_CR3 | ||
| 68 | # define SD1_CR3 0 | ||
| 69 | #endif | ||
| 70 | |||
| 71 | void uart_init(uint32_t baud); | ||
| 72 | |||
| 73 | void uart_putchar(uint8_t c); | ||
| 74 | |||
| 75 | uint8_t uart_getchar(void); | ||
| 76 | |||
| 77 | bool uart_available(void); | ||
diff --git a/drivers/chibios/usbpd_stm32g4.c b/drivers/chibios/usbpd_stm32g4.c deleted file mode 100644 index f16ca8aea..000000000 --- a/drivers/chibios/usbpd_stm32g4.c +++ /dev/null | |||
| @@ -1,76 +0,0 @@ | |||
| 1 | /* Copyright 2021 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 2 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 <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <quantum.h> | ||
| 18 | |||
| 19 | #ifndef USBPD_UCPD1_CFG1 | ||
| 20 | # define USBPD_UCPD1_CFG1 (UCPD_CFG1_PSC_UCPDCLK_0 | UCPD_CFG1_TRANSWIN_3 | UCPD_CFG1_IFRGAP_4 | UCPD_CFG1_HBITCLKDIV_4) | ||
| 21 | #endif // USBPD_UCPD1_CFG1 | ||
| 22 | |||
| 23 | // Initialises the USBPD subsystem | ||
| 24 | __attribute__((weak)) void usbpd_init(void) { | ||
| 25 | // Disable dead-battery signals | ||
| 26 | PWR->CR3 |= PWR_CR3_UCPD_DBDIS; | ||
| 27 | // Enable the clock for the UCPD1 peripheral | ||
| 28 | RCC->APB1ENR2 |= RCC_APB1ENR2_UCPD1EN; | ||
| 29 | |||
| 30 | // Copy the existing value | ||
| 31 | uint32_t CFG1 = UCPD1->CFG1; | ||
| 32 | // Force-disable UCPD1 before configuring | ||
| 33 | CFG1 &= ~UCPD_CFG1_UCPDEN; | ||
| 34 | // Configure UCPD1 | ||
| 35 | CFG1 = USBPD_UCPD1_CFG1; | ||
| 36 | // Apply the changes | ||
| 37 | UCPD1->CFG1 = CFG1; | ||
| 38 | // Enable UCPD1 | ||
| 39 | UCPD1->CFG1 |= UCPD_CFG1_UCPDEN; | ||
| 40 | |||
| 41 | // Copy the existing value | ||
| 42 | uint32_t CR = UCPD1->CR; | ||
| 43 | // Clear out ANASUBMODE (irrelevant as a sink device) | ||
| 44 | CR &= ~UCPD_CR_ANASUBMODE_Msk; | ||
| 45 | // Advertise our capabilities as a sink, with both CC lines enabled | ||
| 46 | CR |= UCPD_CR_ANAMODE | UCPD_CR_CCENABLE_Msk; | ||
| 47 | // Apply the changes | ||
| 48 | UCPD1->CR = CR; | ||
| 49 | } | ||
| 50 | |||
| 51 | // Gets the current state of the USBPD allowance | ||
| 52 | __attribute__((weak)) usbpd_allowance_t usbpd_get_allowance(void) { | ||
| 53 | uint32_t CR = UCPD1->CR; | ||
| 54 | |||
| 55 | int ucpd_enabled = (UCPD1->CFG1 & UCPD_CFG1_UCPDEN_Msk) >> UCPD_CFG1_UCPDEN_Pos; | ||
| 56 | int anamode = (CR & UCPD_CR_ANAMODE_Msk) >> UCPD_CR_ANAMODE_Pos; | ||
| 57 | int cc_enabled = (CR & UCPD_CR_CCENABLE_Msk) >> UCPD_CR_CCENABLE_Pos; | ||
| 58 | |||
| 59 | if (ucpd_enabled && anamode && cc_enabled) { | ||
| 60 | uint32_t SR = UCPD1->SR; | ||
| 61 | int vstate_cc1 = (SR & UCPD_SR_TYPEC_VSTATE_CC1_Msk) >> UCPD_SR_TYPEC_VSTATE_CC1_Pos; | ||
| 62 | int vstate_cc2 = (SR & UCPD_SR_TYPEC_VSTATE_CC2_Msk) >> UCPD_SR_TYPEC_VSTATE_CC2_Pos; | ||
| 63 | int vstate_max = vstate_cc1 > vstate_cc2 ? vstate_cc1 : vstate_cc2; | ||
| 64 | switch (vstate_max) { | ||
| 65 | case 0: | ||
| 66 | case 1: | ||
| 67 | return USBPD_500MA; // Note that this is 500mA (i.e. max USB 2.0), not 900mA, as we're not using USB 3.1 as a sink device. | ||
| 68 | case 2: | ||
| 69 | return USBPD_1500MA; | ||
| 70 | case 3: | ||
| 71 | return USBPD_3000MA; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | return USBPD_500MA; | ||
| 76 | } \ No newline at end of file | ||
diff --git a/drivers/chibios/ws2812.c b/drivers/chibios/ws2812.c deleted file mode 100644 index 0d12e2fb7..000000000 --- a/drivers/chibios/ws2812.c +++ /dev/null | |||
| @@ -1,114 +0,0 @@ | |||
| 1 | #include "quantum.h" | ||
| 2 | #include "ws2812.h" | ||
| 3 | #include <ch.h> | ||
| 4 | #include <hal.h> | ||
| 5 | |||
| 6 | /* Adapted from https://github.com/bigjosh/SimpleNeoPixelDemo/ */ | ||
| 7 | |||
| 8 | #ifndef NOP_FUDGE | ||
| 9 | # if defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F3XX) || defined(STM32F4XX) || defined(STM32L0XX) | ||
| 10 | # define NOP_FUDGE 0.4 | ||
| 11 | # else | ||
| 12 | # error("NOP_FUDGE configuration required") | ||
| 13 | # define NOP_FUDGE 1 // this just pleases the compile so the above error is easier to spot | ||
| 14 | # endif | ||
| 15 | #endif | ||
| 16 | |||
| 17 | // Push Pull or Open Drain Configuration | ||
| 18 | // Default Push Pull | ||
| 19 | #ifndef WS2812_EXTERNAL_PULLUP | ||
| 20 | # define WS2812_OUTPUT_MODE PAL_MODE_OUTPUT_PUSHPULL | ||
| 21 | #else | ||
| 22 | # define WS2812_OUTPUT_MODE PAL_MODE_OUTPUT_OPENDRAIN | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #define NUMBER_NOPS 6 | ||
| 26 | #define CYCLES_PER_SEC (STM32_SYSCLK / NUMBER_NOPS * NOP_FUDGE) | ||
| 27 | #define NS_PER_SEC (1000000000L) // Note that this has to be SIGNED since we want to be able to check for negative values of derivatives | ||
| 28 | #define NS_PER_CYCLE (NS_PER_SEC / CYCLES_PER_SEC) | ||
| 29 | #define NS_TO_CYCLES(n) ((n) / NS_PER_CYCLE) | ||
| 30 | |||
| 31 | #define wait_ns(x) \ | ||
| 32 | do { \ | ||
| 33 | for (int i = 0; i < NS_TO_CYCLES(x); i++) { \ | ||
| 34 | __asm__ volatile("nop\n\t" \ | ||
| 35 | "nop\n\t" \ | ||
| 36 | "nop\n\t" \ | ||
| 37 | "nop\n\t" \ | ||
| 38 | "nop\n\t" \ | ||
| 39 | "nop\n\t"); \ | ||
| 40 | } \ | ||
| 41 | } while (0) | ||
| 42 | |||
| 43 | // These are the timing constraints taken mostly from the WS2812 datasheets | ||
| 44 | // These are chosen to be conservative and avoid problems rather than for maximum throughput | ||
| 45 | |||
| 46 | #define T1H 900 // Width of a 1 bit in ns | ||
| 47 | #define T1L (1250 - T1H) // Width of a 1 bit in ns | ||
| 48 | |||
| 49 | #define T0H 350 // Width of a 0 bit in ns | ||
| 50 | #define T0L (1250 - T0H) // Width of a 0 bit in ns | ||
| 51 | |||
| 52 | // The reset gap can be 6000 ns, but depending on the LED strip it may have to be increased | ||
| 53 | // to values like 600000 ns. If it is too small, the pixels will show nothing most of the time. | ||
| 54 | #define RES (1000 * WS2812_TRST_US) // Width of the low gap between bits to cause a frame to latch | ||
| 55 | |||
| 56 | void sendByte(uint8_t byte) { | ||
| 57 | // WS2812 protocol wants most significant bits first | ||
| 58 | for (unsigned char bit = 0; bit < 8; bit++) { | ||
| 59 | bool is_one = byte & (1 << (7 - bit)); | ||
| 60 | // using something like wait_ns(is_one ? T1L : T0L) here throws off timings | ||
| 61 | if (is_one) { | ||
| 62 | // 1 | ||
| 63 | writePinHigh(RGB_DI_PIN); | ||
| 64 | wait_ns(T1H); | ||
| 65 | writePinLow(RGB_DI_PIN); | ||
| 66 | wait_ns(T1L); | ||
| 67 | } else { | ||
| 68 | // 0 | ||
| 69 | writePinHigh(RGB_DI_PIN); | ||
| 70 | wait_ns(T0H); | ||
| 71 | writePinLow(RGB_DI_PIN); | ||
| 72 | wait_ns(T0L); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | void ws2812_init(void) { palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE); } | ||
| 78 | |||
| 79 | // Setleds for standard RGB | ||
| 80 | void ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { | ||
| 81 | static bool s_init = false; | ||
| 82 | if (!s_init) { | ||
| 83 | ws2812_init(); | ||
| 84 | s_init = true; | ||
| 85 | } | ||
| 86 | |||
| 87 | // this code is very time dependent, so we need to disable interrupts | ||
| 88 | chSysLock(); | ||
| 89 | |||
| 90 | for (uint8_t i = 0; i < leds; i++) { | ||
| 91 | // WS2812 protocol dictates grb order | ||
| 92 | #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB) | ||
| 93 | sendByte(ledarray[i].g); | ||
| 94 | sendByte(ledarray[i].r); | ||
| 95 | sendByte(ledarray[i].b); | ||
| 96 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB) | ||
| 97 | sendByte(ledarray[i].r); | ||
| 98 | sendByte(ledarray[i].g); | ||
| 99 | sendByte(ledarray[i].b); | ||
| 100 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR) | ||
| 101 | sendByte(ledarray[i].b); | ||
| 102 | sendByte(ledarray[i].g); | ||
| 103 | sendByte(ledarray[i].r); | ||
| 104 | #endif | ||
| 105 | |||
| 106 | #ifdef RGBW | ||
| 107 | sendByte(ledarray[i].w); | ||
| 108 | #endif | ||
| 109 | } | ||
| 110 | |||
| 111 | wait_ns(RES); | ||
| 112 | |||
| 113 | chSysUnlock(); | ||
| 114 | } | ||
diff --git a/drivers/chibios/ws2812_pwm.c b/drivers/chibios/ws2812_pwm.c deleted file mode 100644 index e6af55b6b..000000000 --- a/drivers/chibios/ws2812_pwm.c +++ /dev/null | |||
| @@ -1,311 +0,0 @@ | |||
| 1 | #include "ws2812.h" | ||
| 2 | #include "quantum.h" | ||
| 3 | #include <hal.h> | ||
| 4 | |||
| 5 | /* Adapted from https://github.com/joewa/WS2812-LED-Driver_ChibiOS/ */ | ||
| 6 | |||
| 7 | #ifdef RGBW | ||
| 8 | # error "RGBW not supported" | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #ifndef WS2812_PWM_DRIVER | ||
| 12 | # define WS2812_PWM_DRIVER PWMD2 // TIMx | ||
| 13 | #endif | ||
| 14 | #ifndef WS2812_PWM_CHANNEL | ||
| 15 | # define WS2812_PWM_CHANNEL 2 // Channel | ||
| 16 | #endif | ||
| 17 | #ifndef WS2812_PWM_PAL_MODE | ||
| 18 | # define WS2812_PWM_PAL_MODE 2 // DI Pin's alternate function value | ||
| 19 | #endif | ||
| 20 | #ifndef WS2812_DMA_STREAM | ||
| 21 | # define WS2812_DMA_STREAM STM32_DMA1_STREAM2 // DMA Stream for TIMx_UP | ||
| 22 | #endif | ||
| 23 | #ifndef WS2812_DMA_CHANNEL | ||
| 24 | # define WS2812_DMA_CHANNEL 2 // DMA Channel for TIMx_UP | ||
| 25 | #endif | ||
| 26 | #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) && !defined(WS2812_DMAMUX_ID) | ||
| 27 | # error "please consult your MCU's datasheet and specify in your config.h: #define WS2812_DMAMUX_ID STM32_DMAMUX1_TIM?_UP" | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #ifndef WS2812_PWM_COMPLEMENTARY_OUTPUT | ||
| 31 | # define WS2812_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH | ||
| 32 | #else | ||
| 33 | # if !STM32_PWM_USE_ADVANCED | ||
| 34 | # error "WS2812_PWM_COMPLEMENTARY_OUTPUT requires STM32_PWM_USE_ADVANCED == TRUE" | ||
| 35 | # endif | ||
| 36 | # define WS2812_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH | ||
| 37 | #endif | ||
| 38 | |||
| 39 | // Push Pull or Open Drain Configuration | ||
| 40 | // Default Push Pull | ||
| 41 | #ifndef WS2812_EXTERNAL_PULLUP | ||
| 42 | # if defined(USE_GPIOV1) | ||
| 43 | # define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL | ||
| 44 | # else | ||
| 45 | # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | ||
| 46 | # endif | ||
| 47 | #else | ||
| 48 | # if defined(USE_GPIOV1) | ||
| 49 | # define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN | ||
| 50 | # else | ||
| 51 | # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | ||
| 52 | # endif | ||
| 53 | #endif | ||
| 54 | |||
| 55 | #ifndef WS2812_PWM_TARGET_PERIOD | ||
| 56 | //# define WS2812_PWM_TARGET_PERIOD 800000 // Original code is 800k...? | ||
| 57 | # define WS2812_PWM_TARGET_PERIOD 80000 // TODO: work out why 10x less on f303/f4x1 | ||
| 58 | #endif | ||
| 59 | |||
| 60 | /* --- PRIVATE CONSTANTS ---------------------------------------------------- */ | ||
| 61 | |||
| 62 | #define WS2812_PWM_FREQUENCY (STM32_SYSCLK / 2) /**< Clock frequency of PWM, must be valid with respect to system clock! */ | ||
| 63 | #define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / WS2812_PWM_TARGET_PERIOD) /**< Clock period in ticks. 1 / 800kHz = 1.25 uS (as per datasheet) */ | ||
| 64 | |||
| 65 | /** | ||
| 66 | * @brief Number of bit-periods to hold the data line low at the end of a frame | ||
| 67 | * | ||
| 68 | * The reset period for each frame is defined in WS2812_TRST_US. | ||
| 69 | * Calculate the number of zeroes to add at the end assuming 1.25 uS/bit: | ||
| 70 | */ | ||
| 71 | #define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / 1250) | ||
| 72 | #define WS2812_COLOR_BIT_N (RGBLED_NUM * 24) /**< Number of data bits */ | ||
| 73 | #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */ | ||
| 74 | |||
| 75 | /** | ||
| 76 | * @brief High period for a zero, in ticks | ||
| 77 | * | ||
| 78 | * Per the datasheet: | ||
| 79 | * WS2812: | ||
| 80 | * - T0H: 200 nS to 500 nS, inclusive | ||
| 81 | * - T0L: 650 nS to 950 nS, inclusive | ||
| 82 | * WS2812B: | ||
| 83 | * - T0H: 200 nS to 500 nS, inclusive | ||
| 84 | * - T0L: 750 nS to 1050 nS, inclusive | ||
| 85 | * | ||
| 86 | * The duty cycle is calculated for a high period of 350 nS. | ||
| 87 | */ | ||
| 88 | #define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 350)) | ||
| 89 | |||
| 90 | /** | ||
| 91 | * @brief High period for a one, in ticks | ||
| 92 | * | ||
| 93 | * Per the datasheet: | ||
| 94 | * WS2812: | ||
| 95 | * - T1H: 550 nS to 850 nS, inclusive | ||
| 96 | * - T1L: 450 nS to 750 nS, inclusive | ||
| 97 | * WS2812B: | ||
| 98 | * - T1H: 750 nS to 1050 nS, inclusive | ||
| 99 | * - T1L: 200 nS to 500 nS, inclusive | ||
| 100 | * | ||
| 101 | * The duty cycle is calculated for a high period of 800 nS. | ||
| 102 | * This is in the middle of the specifications of the WS2812 and WS2812B. | ||
| 103 | */ | ||
| 104 | #define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 800)) | ||
| 105 | |||
| 106 | /* --- PRIVATE MACROS ------------------------------------------------------- */ | ||
| 107 | |||
| 108 | /** | ||
| 109 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit | ||
| 110 | * | ||
| 111 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 112 | * @param[in] byte: The byte number [0, 2] | ||
| 113 | * @param[in] bit: The bit number [0, 7] | ||
| 114 | * | ||
| 115 | * @return The bit index | ||
| 116 | */ | ||
| 117 | #define WS2812_BIT(led, byte, bit) (24 * (led) + 8 * (byte) + (7 - (bit))) | ||
| 118 | |||
| 119 | #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB) | ||
| 120 | /** | ||
| 121 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit | ||
| 122 | * | ||
| 123 | * @note The red byte is the middle byte in the color packet | ||
| 124 | * | ||
| 125 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 126 | * @param[in] bit: The bit number [0, 7] | ||
| 127 | * | ||
| 128 | * @return The bit index | ||
| 129 | */ | ||
| 130 | # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit)) | ||
| 131 | |||
| 132 | /** | ||
| 133 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit | ||
| 134 | * | ||
| 135 | * @note The red byte is the first byte in the color packet | ||
| 136 | * | ||
| 137 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 138 | * @param[in] bit: The bit number [0, 7] | ||
| 139 | * | ||
| 140 | * @return The bit index | ||
| 141 | */ | ||
| 142 | # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit)) | ||
| 143 | |||
| 144 | /** | ||
| 145 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit | ||
| 146 | * | ||
| 147 | * @note The red byte is the last byte in the color packet | ||
| 148 | * | ||
| 149 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 150 | * @param[in] bit: The bit index [0, 7] | ||
| 151 | * | ||
| 152 | * @return The bit index | ||
| 153 | */ | ||
| 154 | # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit)) | ||
| 155 | |||
| 156 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB) | ||
| 157 | /** | ||
| 158 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit | ||
| 159 | * | ||
| 160 | * @note The red byte is the middle byte in the color packet | ||
| 161 | * | ||
| 162 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 163 | * @param[in] bit: The bit number [0, 7] | ||
| 164 | * | ||
| 165 | * @return The bit index | ||
| 166 | */ | ||
| 167 | # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 0, (bit)) | ||
| 168 | |||
| 169 | /** | ||
| 170 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit | ||
| 171 | * | ||
| 172 | * @note The red byte is the first byte in the color packet | ||
| 173 | * | ||
| 174 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 175 | * @param[in] bit: The bit number [0, 7] | ||
| 176 | * | ||
| 177 | * @return The bit index | ||
| 178 | */ | ||
| 179 | # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit)) | ||
| 180 | |||
| 181 | /** | ||
| 182 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit | ||
| 183 | * | ||
| 184 | * @note The red byte is the last byte in the color packet | ||
| 185 | * | ||
| 186 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 187 | * @param[in] bit: The bit index [0, 7] | ||
| 188 | * | ||
| 189 | * @return The bit index | ||
| 190 | */ | ||
| 191 | # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit)) | ||
| 192 | |||
| 193 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR) | ||
| 194 | /** | ||
| 195 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit | ||
| 196 | * | ||
| 197 | * @note The red byte is the middle byte in the color packet | ||
| 198 | * | ||
| 199 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 200 | * @param[in] bit: The bit number [0, 7] | ||
| 201 | * | ||
| 202 | * @return The bit index | ||
| 203 | */ | ||
| 204 | # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 2, (bit)) | ||
| 205 | |||
| 206 | /** | ||
| 207 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit | ||
| 208 | * | ||
| 209 | * @note The red byte is the first byte in the color packet | ||
| 210 | * | ||
| 211 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 212 | * @param[in] bit: The bit number [0, 7] | ||
| 213 | * | ||
| 214 | * @return The bit index | ||
| 215 | */ | ||
| 216 | # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit)) | ||
| 217 | |||
| 218 | /** | ||
| 219 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit | ||
| 220 | * | ||
| 221 | * @note The red byte is the last byte in the color packet | ||
| 222 | * | ||
| 223 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 224 | * @param[in] bit: The bit index [0, 7] | ||
| 225 | * | ||
| 226 | * @return The bit index | ||
| 227 | */ | ||
| 228 | # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 0, (bit)) | ||
| 229 | #endif | ||
| 230 | |||
| 231 | /* --- PRIVATE VARIABLES ---------------------------------------------------- */ | ||
| 232 | |||
| 233 | static uint32_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */ | ||
| 234 | |||
| 235 | /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */ | ||
| 236 | /* | ||
| 237 | * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for | ||
| 238 | the memory (while the DMA is reading/writing from/to a buffer, the application can | ||
| 239 | write/read to/from the other buffer). | ||
| 240 | */ | ||
| 241 | |||
| 242 | void ws2812_init(void) { | ||
| 243 | // Initialize led frame buffer | ||
| 244 | uint32_t i; | ||
| 245 | for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle | ||
| 246 | for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero | ||
| 247 | |||
| 248 | palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE); | ||
| 249 | |||
| 250 | // PWM Configuration | ||
| 251 | //#pragma GCC diagnostic ignored "-Woverride-init" // Turn off override-init warning for this struct. We use the overriding ability to set a "default" channel config | ||
| 252 | static const PWMConfig ws2812_pwm_config = { | ||
| 253 | .frequency = WS2812_PWM_FREQUENCY, | ||
| 254 | .period = WS2812_PWM_PERIOD, // Mit dieser Periode wird UDE-Event erzeugt und ein neuer Wert (Länge WS2812_BIT_N) vom DMA ins CCR geschrieben | ||
| 255 | .callback = NULL, | ||
| 256 | .channels = | ||
| 257 | { | ||
| 258 | [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled | ||
| 259 | [WS2812_PWM_CHANNEL - 1] = {.mode = WS2812_PWM_OUTPUT_MODE, .callback = NULL}, // Turn on the channel we care about | ||
| 260 | }, | ||
| 261 | .cr2 = 0, | ||
| 262 | .dier = TIM_DIER_UDE, // DMA on update event for next period | ||
| 263 | }; | ||
| 264 | //#pragma GCC diagnostic pop // Restore command-line warning options | ||
| 265 | |||
| 266 | // Configure DMA | ||
| 267 | // dmaInit(); // Joe added this | ||
| 268 | dmaStreamAlloc(WS2812_DMA_STREAM - STM32_DMA_STREAM(0), 10, NULL, NULL); | ||
| 269 | dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register | ||
| 270 | dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer); | ||
| 271 | dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N); | ||
| 272 | dmaStreamSetMode(WS2812_DMA_STREAM, STM32_DMA_CR_CHSEL(WS2812_DMA_CHANNEL) | STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_WORD | STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3)); | ||
| 273 | // M2P: Memory 2 Periph; PL: Priority Level | ||
| 274 | |||
| 275 | #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) | ||
| 276 | // If the MCU has a DMAMUX we need to assign the correct resource | ||
| 277 | dmaSetRequestSource(WS2812_DMA_STREAM, WS2812_DMAMUX_ID); | ||
| 278 | #endif | ||
| 279 | |||
| 280 | // Start DMA | ||
| 281 | dmaStreamEnable(WS2812_DMA_STREAM); | ||
| 282 | |||
| 283 | // Configure PWM | ||
| 284 | // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the | ||
| 285 | // ChibiOS driver code, so we don't have to do anything special to the timer. If we did, we'd have to start the timer, | ||
| 286 | // disable counting, enable the channel, and then make whatever configuration changes we need. | ||
| 287 | pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config); | ||
| 288 | pwmEnableChannel(&WS2812_PWM_DRIVER, WS2812_PWM_CHANNEL - 1, 0); // Initial period is 0; output will be low until first duty cycle is DMA'd in | ||
| 289 | } | ||
| 290 | |||
| 291 | void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) { | ||
| 292 | // Write color to frame buffer | ||
| 293 | for (uint8_t bit = 0; bit < 8; bit++) { | ||
| 294 | ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0; | ||
| 295 | ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0; | ||
| 296 | ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | // Setleds for standard RGB | ||
| 301 | void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) { | ||
| 302 | static bool s_init = false; | ||
| 303 | if (!s_init) { | ||
| 304 | ws2812_init(); | ||
| 305 | s_init = true; | ||
| 306 | } | ||
| 307 | |||
| 308 | for (uint16_t i = 0; i < leds; i++) { | ||
| 309 | ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b); | ||
| 310 | } | ||
| 311 | } | ||
diff --git a/drivers/chibios/ws2812_spi.c b/drivers/chibios/ws2812_spi.c deleted file mode 100644 index 377a929b9..000000000 --- a/drivers/chibios/ws2812_spi.c +++ /dev/null | |||
| @@ -1,159 +0,0 @@ | |||
| 1 | #include "quantum.h" | ||
| 2 | #include "ws2812.h" | ||
| 3 | |||
| 4 | /* Adapted from https://github.com/gamazeps/ws2812b-chibios-SPIDMA/ */ | ||
| 5 | |||
| 6 | #ifdef RGBW | ||
| 7 | # error "RGBW not supported" | ||
| 8 | #endif | ||
| 9 | |||
| 10 | // Define the spi your LEDs are plugged to here | ||
| 11 | #ifndef WS2812_SPI | ||
| 12 | # define WS2812_SPI SPID1 | ||
| 13 | #endif | ||
| 14 | |||
| 15 | #ifndef WS2812_SPI_MOSI_PAL_MODE | ||
| 16 | # define WS2812_SPI_MOSI_PAL_MODE 5 | ||
| 17 | #endif | ||
| 18 | |||
| 19 | #ifndef WS2812_SPI_SCK_PAL_MODE | ||
| 20 | # define WS2812_SPI_SCK_PAL_MODE 5 | ||
| 21 | #endif | ||
| 22 | |||
| 23 | // Push Pull or Open Drain Configuration | ||
| 24 | // Default Push Pull | ||
| 25 | #ifndef WS2812_EXTERNAL_PULLUP | ||
| 26 | # if defined(USE_GPIOV1) | ||
| 27 | # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL | ||
| 28 | # else | ||
| 29 | # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | ||
| 30 | # endif | ||
| 31 | #else | ||
| 32 | # if defined(USE_GPIOV1) | ||
| 33 | # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN | ||
| 34 | # else | ||
| 35 | # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN | ||
| 36 | # endif | ||
| 37 | #endif | ||
| 38 | |||
| 39 | // Define SPI config speed | ||
| 40 | // baudrate should target 3.2MHz | ||
| 41 | // F072 fpclk = 48MHz | ||
| 42 | // 48/16 = 3Mhz | ||
| 43 | #if WS2812_SPI_DIVISOR == 2 | ||
| 44 | # define WS2812_SPI_DIVISOR (0) | ||
| 45 | #elif WS2812_SPI_DIVISOR == 4 | ||
| 46 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_0) | ||
| 47 | #elif WS2812_SPI_DIVISOR == 8 | ||
| 48 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_1) | ||
| 49 | #elif WS2812_SPI_DIVISOR == 16 // same as default | ||
| 50 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_1 | SPI_CR1_BR_0) | ||
| 51 | #elif WS2812_SPI_DIVISOR == 32 | ||
| 52 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2) | ||
| 53 | #elif WS2812_SPI_DIVISOR == 64 | ||
| 54 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_0) | ||
| 55 | #elif WS2812_SPI_DIVISOR == 128 | ||
| 56 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_1) | ||
| 57 | #elif WS2812_SPI_DIVISOR == 256 | ||
| 58 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0) | ||
| 59 | #else | ||
| 60 | # define WS2812_SPI_DIVISOR (SPI_CR1_BR_1 | SPI_CR1_BR_0) // default | ||
| 61 | #endif | ||
| 62 | |||
| 63 | // Use SPI circular buffer | ||
| 64 | #ifdef WS2812_SPI_USE_CIRCULAR_BUFFER | ||
| 65 | # define WS2812_SPI_BUFFER_MODE 1 // circular buffer | ||
| 66 | #else | ||
| 67 | # define WS2812_SPI_BUFFER_MODE 0 // normal buffer | ||
| 68 | #endif | ||
| 69 | |||
| 70 | #if defined(USE_GPIOV1) | ||
| 71 | # define WS2812_SCK_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL | ||
| 72 | #else | ||
| 73 | # define WS2812_SCK_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_SCK_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | ||
| 74 | #endif | ||
| 75 | |||
| 76 | #define BYTES_FOR_LED_BYTE 4 | ||
| 77 | #define NB_COLORS 3 | ||
| 78 | #define BYTES_FOR_LED (BYTES_FOR_LED_BYTE * NB_COLORS) | ||
| 79 | #define DATA_SIZE (BYTES_FOR_LED * RGBLED_NUM) | ||
| 80 | #define RESET_SIZE (1000 * WS2812_TRST_US / (2 * 1250)) | ||
| 81 | #define PREAMBLE_SIZE 4 | ||
| 82 | |||
| 83 | static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE] = {0}; | ||
| 84 | |||
| 85 | /* | ||
| 86 | * As the trick here is to use the SPI to send a huge pattern of 0 and 1 to | ||
| 87 | * the ws2812b protocol, we use this helper function to translate bytes into | ||
| 88 | * 0s and 1s for the LED (with the appropriate timing). | ||
| 89 | */ | ||
| 90 | static uint8_t get_protocol_eq(uint8_t data, int pos) { | ||
| 91 | uint8_t eq = 0; | ||
| 92 | if (data & (1 << (2 * (3 - pos)))) | ||
| 93 | eq = 0b1110; | ||
| 94 | else | ||
| 95 | eq = 0b1000; | ||
| 96 | if (data & (2 << (2 * (3 - pos)))) | ||
| 97 | eq += 0b11100000; | ||
| 98 | else | ||
| 99 | eq += 0b10000000; | ||
| 100 | return eq; | ||
| 101 | } | ||
| 102 | |||
| 103 | static void set_led_color_rgb(LED_TYPE color, int pos) { | ||
| 104 | uint8_t* tx_start = &txbuf[PREAMBLE_SIZE]; | ||
| 105 | |||
| 106 | #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB) | ||
| 107 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + j] = get_protocol_eq(color.g, j); | ||
| 108 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE + j] = get_protocol_eq(color.r, j); | ||
| 109 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE * 2 + j] = get_protocol_eq(color.b, j); | ||
| 110 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB) | ||
| 111 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + j] = get_protocol_eq(color.r, j); | ||
| 112 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE + j] = get_protocol_eq(color.g, j); | ||
| 113 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE * 2 + j] = get_protocol_eq(color.b, j); | ||
| 114 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR) | ||
| 115 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + j] = get_protocol_eq(color.b, j); | ||
| 116 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE + j] = get_protocol_eq(color.g, j); | ||
| 117 | for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE * 2 + j] = get_protocol_eq(color.r, j); | ||
| 118 | #endif | ||
| 119 | } | ||
| 120 | |||
| 121 | void ws2812_init(void) { | ||
| 122 | palSetLineMode(RGB_DI_PIN, WS2812_MOSI_OUTPUT_MODE); | ||
| 123 | |||
| 124 | #ifdef WS2812_SPI_SCK_PIN | ||
| 125 | palSetLineMode(WS2812_SPI_SCK_PIN, WS2812_SCK_OUTPUT_MODE); | ||
| 126 | #endif // WS2812_SPI_SCK_PIN | ||
| 127 | |||
| 128 | // TODO: more dynamic baudrate | ||
| 129 | static const SPIConfig spicfg = {WS2812_SPI_BUFFER_MODE, NULL, PAL_PORT(RGB_DI_PIN), PAL_PAD(RGB_DI_PIN), WS2812_SPI_DIVISOR}; | ||
| 130 | |||
| 131 | spiAcquireBus(&WS2812_SPI); /* Acquire ownership of the bus. */ | ||
| 132 | spiStart(&WS2812_SPI, &spicfg); /* Setup transfer parameters. */ | ||
| 133 | spiSelect(&WS2812_SPI); /* Slave Select assertion. */ | ||
| 134 | #ifdef WS2812_SPI_USE_CIRCULAR_BUFFER | ||
| 135 | spiStartSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf); | ||
| 136 | #endif | ||
| 137 | } | ||
| 138 | |||
| 139 | void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) { | ||
| 140 | static bool s_init = false; | ||
| 141 | if (!s_init) { | ||
| 142 | ws2812_init(); | ||
| 143 | s_init = true; | ||
| 144 | } | ||
| 145 | |||
| 146 | for (uint8_t i = 0; i < leds; i++) { | ||
| 147 | set_led_color_rgb(ledarray[i], i); | ||
| 148 | } | ||
| 149 | |||
| 150 | // Send async - each led takes ~0.03ms, 50 leds ~1.5ms, animations flushing faster than send will cause issues. | ||
| 151 | // Instead spiSend can be used to send synchronously (or the thread logic can be added back). | ||
| 152 | #ifndef WS2812_SPI_USE_CIRCULAR_BUFFER | ||
| 153 | # ifdef WS2812_SPI_SYNC | ||
| 154 | spiSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf); | ||
| 155 | # else | ||
| 156 | spiStartSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf); | ||
| 157 | # endif | ||
| 158 | #endif | ||
| 159 | } | ||
