diff options
Diffstat (limited to 'drivers/arm/analog.c')
| -rw-r--r-- | drivers/arm/analog.c | 53 |
1 files changed, 20 insertions, 33 deletions
diff --git a/drivers/arm/analog.c b/drivers/arm/analog.c index 57f649a81..427381f28 100644 --- a/drivers/arm/analog.c +++ b/drivers/arm/analog.c | |||
| @@ -17,35 +17,32 @@ | |||
| 17 | #include "analog.h" | 17 | #include "analog.h" |
| 18 | #include "quantum.h" | 18 | #include "quantum.h" |
| 19 | 19 | ||
| 20 | |||
| 21 | /* User configurable ADC options */ | 20 | /* User configurable ADC options */ |
| 22 | #ifndef ADC_CIRCULAR_BUFFER | 21 | #ifndef ADC_CIRCULAR_BUFFER |
| 23 | #define ADC_CIRCULAR_BUFFER FALSE | 22 | # define ADC_CIRCULAR_BUFFER FALSE |
| 24 | #endif | 23 | #endif |
| 25 | 24 | ||
| 26 | #ifndef ADC_NUM_CHANNELS | 25 | #ifndef ADC_NUM_CHANNELS |
| 27 | #define ADC_NUM_CHANNELS 1 | 26 | # define ADC_NUM_CHANNELS 1 |
| 28 | #elif ADC_NUM_CHANNELS != 1 | 27 | #elif ADC_NUM_CHANNELS != 1 |
| 29 | #error "The ARM ADC implementation currently only supports reading one channel at a time." | 28 | # error "The ARM ADC implementation currently only supports reading one channel at a time." |
| 30 | #endif | 29 | #endif |
| 31 | 30 | ||
| 32 | #ifndef ADC_BUFFER_DEPTH | 31 | #ifndef ADC_BUFFER_DEPTH |
| 33 | #define ADC_BUFFER_DEPTH 2 | 32 | # define ADC_BUFFER_DEPTH 2 |
| 34 | #endif | 33 | #endif |
| 35 | 34 | ||
| 36 | // For more sampling rate options, look at hal_adc_lld.h in ChibiOS | 35 | // For more sampling rate options, look at hal_adc_lld.h in ChibiOS |
| 37 | #ifndef ADC_SAMPLING_RATE | 36 | #ifndef ADC_SAMPLING_RATE |
| 38 | #define ADC_SAMPLING_RATE ADC_SMPR_SMP_1P5 | 37 | # define ADC_SAMPLING_RATE ADC_SMPR_SMP_1P5 |
| 39 | #endif | 38 | #endif |
| 40 | 39 | ||
| 41 | // Options are 12, 10, 8, and 6 bit. | 40 | // Options are 12, 10, 8, and 6 bit. |
| 42 | #ifndef ADC_RESOLUTION | 41 | #ifndef ADC_RESOLUTION |
| 43 | #define ADC_RESOLUTION ADC_CFGR1_RES_12BIT | 42 | # define ADC_RESOLUTION ADC_CFGR1_RES_12BIT |
| 44 | #endif | 43 | #endif |
| 45 | 44 | ||
| 46 | 45 | static ADCConfig adcCfg = {}; | |
| 47 | |||
| 48 | static ADCConfig adcCfg = {}; | ||
| 49 | static adcsample_t sampleBuffer[ADC_NUM_CHANNELS * ADC_BUFFER_DEPTH]; | 46 | static adcsample_t sampleBuffer[ADC_NUM_CHANNELS * ADC_BUFFER_DEPTH]; |
| 50 | 47 | ||
| 51 | // Initialize to max number of ADCs, set to empty object to initialize all to false. | 48 | // Initialize to max number of ADCs, set to empty object to initialize all to false. |
| @@ -54,21 +51,18 @@ static bool adcInitialized[1] = {}; | |||
| 54 | #elif defined(STM32F3XX) | 51 | #elif defined(STM32F3XX) |
| 55 | static bool adcInitialized[4] = {}; | 52 | static bool adcInitialized[4] = {}; |
| 56 | #else | 53 | #else |
| 57 | #error "adcInitialized has not been implemented for this ARM microcontroller." | 54 | # error "adcInitialized has not been implemented for this ARM microcontroller." |
| 58 | #endif | 55 | #endif |
| 59 | 56 | ||
| 60 | |||
| 61 | |||
| 62 | static ADCConversionGroup adcConversionGroup = { | 57 | static ADCConversionGroup adcConversionGroup = { |
| 63 | ADC_CIRCULAR_BUFFER, | 58 | ADC_CIRCULAR_BUFFER, |
| 64 | (uint16_t)(ADC_NUM_CHANNELS), | 59 | (uint16_t)(ADC_NUM_CHANNELS), |
| 65 | NULL, // No end callback | 60 | NULL, // No end callback |
| 66 | NULL, // No error callback | 61 | NULL, // No error callback |
| 67 | #if defined(STM32F0XX) | 62 | #if defined(STM32F0XX) |
| 68 | ADC_CFGR1_CONT | ADC_RESOLUTION, | 63 | ADC_CFGR1_CONT | ADC_RESOLUTION, |
| 69 | ADC_TR(0, 0). | 64 | ADC_TR(0, 0).ADC_SAMPLING_RATE, |
| 70 | ADC_SAMPLING_RATE, | 65 | NULL, // Doesn't specify a default channel |
| 71 | NULL, // Doesn't specify a default channel | ||
| 72 | #elif defined(STM32F3XX) | 66 | #elif defined(STM32F3XX) |
| 73 | ADC_CFGR_CONT | ADC_RESOLUTION, | 67 | ADC_CFGR_CONT | ADC_RESOLUTION, |
| 74 | ADC_TR(0, 4095), | 68 | ADC_TR(0, 4095), |
| @@ -77,7 +71,7 @@ static ADCConversionGroup adcConversionGroup = { | |||
| 77 | ADC_SAMPLING_RATE, | 71 | ADC_SAMPLING_RATE, |
| 78 | }, | 72 | }, |
| 79 | { | 73 | { |
| 80 | 0, // Doesn't specify a default channel | 74 | 0, // Doesn't specify a default channel |
| 81 | 0, | 75 | 0, |
| 82 | 0, | 76 | 0, |
| 83 | 0, | 77 | 0, |
| @@ -85,10 +79,7 @@ static ADCConversionGroup adcConversionGroup = { | |||
| 85 | #endif | 79 | #endif |
| 86 | }; | 80 | }; |
| 87 | 81 | ||
| 88 | |||
| 89 | |||
| 90 | static inline ADCDriver* intToADCDriver(uint8_t adcInt) { | 82 | static inline ADCDriver* intToADCDriver(uint8_t adcInt) { |
| 91 | |||
| 92 | ADCDriver* target; | 83 | ADCDriver* target; |
| 93 | 84 | ||
| 94 | switch (adcInt) { | 85 | switch (adcInt) { |
| @@ -106,7 +97,7 @@ static inline ADCDriver* intToADCDriver(uint8_t adcInt) { | |||
| 106 | case 3: target = &ADCD4; break; | 97 | case 3: target = &ADCD4; break; |
| 107 | #endif | 98 | #endif |
| 108 | default: target = NULL; break; | 99 | default: target = NULL; break; |
| 109 | // clang-format on | 100 | // clang-format on |
| 110 | } | 101 | } |
| 111 | 102 | ||
| 112 | return target; | 103 | return target; |
| @@ -119,12 +110,10 @@ static inline void manageAdcInitializationDriver(uint8_t adc, ADCDriver* adcDriv | |||
| 119 | } | 110 | } |
| 120 | } | 111 | } |
| 121 | 112 | ||
| 122 | static inline void manageAdcInitialization(uint8_t adc) { | 113 | static inline void manageAdcInitialization(uint8_t adc) { manageAdcInitializationDriver(adc, intToADCDriver(adc)); } |
| 123 | manageAdcInitializationDriver(adc, intToADCDriver(adc)); | ||
| 124 | } | ||
| 125 | 114 | ||
| 126 | pin_and_adc pinToMux(pin_t pin) { | 115 | pin_and_adc pinToMux(pin_t pin) { |
| 127 | switch(pin) { | 116 | switch (pin) { |
| 128 | // clang-format off | 117 | // clang-format off |
| 129 | #if defined(STM32F0XX) | 118 | #if defined(STM32F0XX) |
| 130 | case A0: return (pin_and_adc){ ADC_CHANNEL_IN0, 0 }; | 119 | case A0: return (pin_and_adc){ ADC_CHANNEL_IN0, 0 }; |
| @@ -187,17 +176,15 @@ pin_and_adc pinToMux(pin_t pin) { | |||
| 187 | #error "An ADC pin-to-mux configuration has not been specified for this microcontroller." | 176 | #error "An ADC pin-to-mux configuration has not been specified for this microcontroller." |
| 188 | #endif | 177 | #endif |
| 189 | default: return (pin_and_adc){ 0, 0 }; | 178 | default: return (pin_and_adc){ 0, 0 }; |
| 190 | // clang-format on | 179 | // clang-format on |
| 191 | } | 180 | } |
| 192 | } | 181 | } |
| 193 | 182 | ||
| 194 | adcsample_t analogReadPin(pin_t pin) { | 183 | adcsample_t analogReadPin(pin_t pin) { return adc_read(pinToMux(pin)); } |
| 195 | return adc_read(pinToMux(pin)); | ||
| 196 | } | ||
| 197 | 184 | ||
| 198 | adcsample_t analogReadPinAdc(pin_t pin, uint8_t adc) { | 185 | adcsample_t analogReadPinAdc(pin_t pin, uint8_t adc) { |
| 199 | pin_and_adc target = pinToMux(pin); | 186 | pin_and_adc target = pinToMux(pin); |
| 200 | target.adc = adc; | 187 | target.adc = adc; |
| 201 | return adc_read(target); | 188 | return adc_read(target); |
| 202 | } | 189 | } |
| 203 | 190 | ||
| @@ -207,7 +194,7 @@ adcsample_t adc_read(pin_and_adc mux) { | |||
| 207 | #elif defined(STM32F3XX) | 194 | #elif defined(STM32F3XX) |
| 208 | adcConversionGroup.sqr[0] = ADC_SQR1_SQ1_N(mux.pin); | 195 | adcConversionGroup.sqr[0] = ADC_SQR1_SQ1_N(mux.pin); |
| 209 | #else | 196 | #else |
| 210 | #error "adc_read has not been updated to support this ARM microcontroller." | 197 | # error "adc_read has not been updated to support this ARM microcontroller." |
| 211 | #endif | 198 | #endif |
| 212 | 199 | ||
| 213 | ADCDriver* targetDriver = intToADCDriver(mux.adc); | 200 | ADCDriver* targetDriver = intToADCDriver(mux.adc); |
