aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Vlasov <sigprof@gmail.com>2021-04-25 04:15:37 +0300
committerGitHub <noreply@github.com>2021-04-25 11:15:37 +1000
commit65c97527624d4865d3867371722742c477f71268 (patch)
treed7642cd24f760ce349e13a6489854cb1a9864aff
parentf12aea5dfb8102a3ffba33e710c1161ad8a2580f (diff)
downloadqmk_firmware-65c97527624d4865d3867371722742c477f71268.tar.gz
qmk_firmware-65c97527624d4865d3867371722742c477f71268.zip
Update ADC driver for STM32F1xx, STM32F3xx, STM32F4xx (#12403)
* Fix default ADC_RESOLUTION for ADCv3 (and ADCv4) Recent ChibiOS update removed ADC_CFGR1_RES_10BIT from the ADCv3 headers (that macro should not have been there, because ADCv3 has CFGR instead of CFGR1). Fix the default value for ADC_RESOLUTION to use ADC_CFGR_RES_10BITS if it is defined (that name is used for ADCv3 and ADCv4). * Update ADC docs to match the actually used resolution ADC driver for ChibiOS actually uses the 10-bit resolution by default (probably to match AVR); fix the documentation accordingly. Also add both ADC_CFGR_RES_10BITS and ADC_CFGR1_RES_10BIT constants (these names differ according to the ADC implementation in the particular MCU). * Fix pinToMux() for B12 and B13 on STM32F3xx Testing on STM32F303CCT6 revealed that the ADC mux values for B12 and B13 pins were wrong. * Add support for all possible analog pins on STM32F1xx Added ADC mux values for pins A0...A7, B0, B1, C0...C5 on STM32F1xx (they are the same at least for STM32F103x8 and larger F103 devices, and also F102, F105, F107 families). Actually tested on STM32F103C8T6 (therefore pins C0...C5 were not tested). Pins F6...F10, which are present on STM32F103x[C-G] in 144-pin packages, cannot be supported at the moment, because those pins are connected only to ADC3, but the ChibiOS ADC driver for STM32F1xx supports only ADC1. * Add support for all possible analog pins on STM32F4xx Added ADC mux values for pins A0...A7, B0, B1, C0...C5 and optionally F3...F10 (if STM32_ADC_USE_ADC3 is enabled). These mux values are apparently the same for all F4xx devices, except some smaller devices may not have ADC3. Actually tested on STM32F401CCU6, STM32F401CEU6, STM32F411CEU6 (using various WeAct “Blackpill” boards); only pins A0...A7, B0, B1 were tested. Pins F3...F10 are inside `#if STM32_ADC_USE_ADC3` because some devices which don't have ADC3 also don't have the GPIOF port, therefore the code which refers to Fx pins does not compile. * Fix STM32F3xx ADC mux table in documentation The ADC driver documentation had some errors in the mux table for STM32F3xx. Fix this table to match the datasheet and the actual code (mux settings for B12 and B13 were also tested on a real STM32F303CCT6 chip). * Add STM32F1xx ADC pins to the documentation * Add STM32F4xx ADC pins to the documentation
-rw-r--r--docs/adc_driver.md154
-rw-r--r--drivers/chibios/analog.c57
2 files changed, 131 insertions, 80 deletions
diff --git a/docs/adc_driver.md b/docs/adc_driver.md
index 6e3d51386..69fff4b3c 100644
--- a/docs/adc_driver.md
+++ b/docs/adc_driver.md
@@ -47,73 +47,79 @@ Note that some of these pins are doubled-up on ADCs with the same channel. This
47 47
48Also note that the F0 and F3 use different numbering schemes. The F0 has a single ADC and the channels are 0-indexed, whereas the F3 has 4 ADCs and the channels are 1-indexed. This is because the F0 uses the `ADCv1` implementation of the ADC, whereas the F3 uses the `ADCv3` implementation. 48Also note that the F0 and F3 use different numbering schemes. The F0 has a single ADC and the channels are 0-indexed, whereas the F3 has 4 ADCs and the channels are 1-indexed. This is because the F0 uses the `ADCv1` implementation of the ADC, whereas the F3 uses the `ADCv3` implementation.
49 49
50|ADC|Channel|STM32F0xx|STM32F3xx| 50|ADC|Channel|STM32F0xx|STM32F1xx|STM32F3xx|STM32F4xx|
51|---|-------|---------|---------| 51|---|-------|---------|---------|---------|---------|
52|1 |0 |`A0` | | 52|1 |0 |`A0` |`A0` | |`A0` |
53|1 |1 |`A1` |`A0` | 53|1 |1 |`A1` |`A1` |`A0` |`A1` |
54|1 |2 |`A2` |`A1` | 54|1 |2 |`A2` |`A2` |`A1` |`A2` |
55|1 |3 |`A3` |`A2` | 55|1 |3 |`A3` |`A3` |`A2` |`A3` |
56|1 |4 |`A4` |`A3` | 56|1 |4 |`A4` |`A4` |`A3` |`A4` |
57|1 |5 |`A5` |`F4` | 57|1 |5 |`A5` |`A5` |`F4` |`A5` |
58|1 |6 |`A6` |`C0` | 58|1 |6 |`A6` |`A6` |`C0` |`A6` |
59|1 |7 |`A7` |`C1` | 59|1 |7 |`A7` |`A7` |`C1` |`A7` |
60|1 |8 |`B0` |`C2` | 60|1 |8 |`B0` |`B0` |`C2` |`B0` |
61|1 |9 |`B1` |`C3` | 61|1 |9 |`B1` |`B1` |`C3` |`B1` |
62|1 |10 |`C0` |`F2` | 62|1 |10 |`C0` |`C0` |`F2` |`C0` |
63|1 |11 |`C1` | | 63|1 |11 |`C1` |`C1` | |`C1` |
64|1 |12 |`C2` | | 64|1 |12 |`C2` |`C2` | |`C2` |
65|1 |13 |`C3` | | 65|1 |13 |`C3` |`C3` | |`C3` |
66|1 |14 |`C4` | | 66|1 |14 |`C4` |`C4` | |`C4` |
67|1 |15 |`C5` | | 67|1 |15 |`C5` |`C5` | |`C5` |
68|1 |16 | | | 68|1 |16 | | | | |
69|2 |1 | |`A4` | 69|2 |0 | |`A0`¹ | |`A0`² |
70|2 |2 | |`A5` | 70|2 |1 | |`A1`¹ |`A4` |`A1`² |
71|2 |3 | |`A6` | 71|2 |2 | |`A2`¹ |`A5` |`A2`² |
72|2 |4 | |`A7` | 72|2 |3 | |`A3`¹ |`A6` |`A3`² |
73|2 |5 | |`C4` | 73|2 |4 | |`A4`¹ |`A7` |`A4`² |
74|2 |6 | |`C0` | 74|2 |5 | |`A5`¹ |`C4` |`A5`² |
75|2 |7 | |`C1` | 75|2 |6 | |`A6`¹ |`C0` |`A6`² |
76|2 |8 | |`C2` | 76|2 |7 | |`A7`¹ |`C1` |`A7`² |
77|2 |9 | |`C3` | 77|2 |8 | |`B0`¹ |`C2` |`B0`² |
78|2 |10 | |`F2` | 78|2 |9 | |`B1`¹ |`C3` |`B1`² |
79|2 |11 | |`C5` | 79|2 |10 | |`C0`¹ |`F2` |`C0`² |
80|2 |12 | |`B2` | 80|2 |11 | |`C1`¹ |`C5` |`C1`² |
81|2 |13 | | | 81|2 |12 | |`C2`¹ |`B2` |`C2`² |
82|2 |14 | | | 82|2 |13 | |`C3`¹ | |`C3`² |
83|2 |15 | | | 83|2 |14 | |`C4`¹ | |`C4`² |
84|2 |16 | | | 84|2 |15 | |`C5`¹ | |`C5`² |
85|3 |1 | |`B1` | 85|2 |16 | | | | |
86|3 |2 | |`E9` | 86|3 |0 | |`A0`¹ | |`A0`² |
87|3 |3 | |`E13` | 87|3 |1 | |`A1`¹ |`B1` |`A1`² |
88|3 |4 | | | 88|3 |2 | |`A2`¹ |`E9` |`A2`² |
89|3 |5 | | | 89|3 |3 | |`A3`¹ |`E13` |`A3`² |
90|3 |6 | |`E8` | 90|3 |4 | |`F6`¹ | |`F6`² |
91|3 |7 | |`D10` | 91|3 |5 | |`F7`¹ |`B13` |`F7`² |
92|3 |8 | |`D11` | 92|3 |6 | |`F8`¹ |`E8` |`F8`² |
93|3 |9 | |`D12` | 93|3 |7 | |`F9`¹ |`D10` |`F9`² |
94|3 |10 | |`D13` | 94|3 |8 | |`F10`¹ |`D11` |`F10`² |
95|3 |11 | |`D14` | 95|3 |9 | | |`D12` |`F3`² |
96|3 |12 | |`B0` | 96|3 |10 | |`C0`¹ |`D13` |`C0`² |
97|3 |13 | |`E7` | 97|3 |11 | |`C1`¹ |`D14` |`C1`² |
98|3 |14 | |`E10` | 98|3 |12 | |`C2`¹ |`B0` |`C2`² |
99|3 |15 | |`E11` | 99|3 |13 | |`C3`¹ |`E7` |`C3`² |
100|3 |16 | |`E12` | 100|3 |14 | | |`E10` |`F4`² |
101|4 |1 | |`E14` | 101|3 |15 | | |`E11` |`F5`² |
102|4 |2 | |`B12` | 102|3 |16 | | |`E12` | |
103|4 |3 | |`B13` | 103|4 |1 | | |`E14` | |
104|4 |4 | |`B14` | 104|4 |2 | | |`E15` | |
105|4 |5 | |`B15` | 105|4 |3 | | |`B12` | |
106|4 |6 | |`E8` | 106|4 |4 | | |`B14` | |
107|4 |7 | |`D10` | 107|4 |5 | | |`B15` | |
108|4 |8 | |`D11` | 108|4 |6 | | |`E8` | |
109|4 |9 | |`D12` | 109|4 |7 | | |`D10` | |
110|4 |10 | |`D13` | 110|4 |8 | | |`D11` | |
111|4 |11 | |`D14` | 111|4 |9 | | |`D12` | |
112|4 |12 | |`D8` | 112|4 |10 | | |`D13` | |
113|4 |13 | |`D9` | 113|4 |11 | | |`D14` | |
114|4 |14 | | | 114|4 |12 | | |`D8` | |
115|4 |15 | | | 115|4 |13 | | |`D9` | |
116|4 |16 | | | 116|4 |14 | | | | |
117|4 |15 | | | | |
118|4 |16 | | | | |
119
120<sup>¹ As of ChibiOS 20.3.4, the ADC driver for STM32F1xx devices supports only ADC1, therefore any configurations involving ADC2 or ADC3 cannot actually be used. In particular, pins `F6`…`F10`, which are present at least on some STM32F103x[C-G] devices, cannot be used as ADC inputs because of this driver limitation.</sup>
121
122<sup>² Not all STM32F4xx devices have ADC2 and/or ADC3, therefore some configurations shown in this table may be unavailable; in particular, pins `F4`…`F10` cannot be used as ADC inputs on devices which do not have ADC3. Check the device datasheet to confirm which pin functions are supported.</sup>
117 123
118## Functions 124## Functions
119 125
@@ -141,10 +147,10 @@ Also note that the F0 and F3 use different numbering schemes. The F0 has a singl
141 147
142The ARM implementation of the ADC has a few additional options that you can override in your own keyboards and keymaps to change how it operates. Please consult the corresponding `hal_adc_lld.h` in ChibiOS for your specific microcontroller for further documentation on your available options. 148The ARM implementation of the ADC has a few additional options that you can override in your own keyboards and keymaps to change how it operates. Please consult the corresponding `hal_adc_lld.h` in ChibiOS for your specific microcontroller for further documentation on your available options.
143 149
144|`#define` |Type |Default |Description | 150|`#define` |Type |Default |Description |
145|---------------------|------|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 151|---------------------|------|----------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
146|`ADC_CIRCULAR_BUFFER`|`bool`|`false` |If `true`, then the implementation will use a circular buffer. | 152|`ADC_CIRCULAR_BUFFER`|`bool`|`false` |If `true`, then the implementation will use a circular buffer. |
147|`ADC_NUM_CHANNELS` |`int` |`1` |Sets the number of channels that will be scanned as part of an ADC operation. The current implementation only supports `1`. | 153|`ADC_NUM_CHANNELS` |`int` |`1` |Sets the number of channels that will be scanned as part of an ADC operation. The current implementation only supports `1`. |
148|`ADC_BUFFER_DEPTH` |`int` |`2` |Sets the depth of each result. Since we are only getting a 12-bit result by default, we set this to 2 bytes so we can contain our one value. This could be set to 1 if you opt for an 8-bit or lower result.| 154|`ADC_BUFFER_DEPTH` |`int` |`2` |Sets the depth of each result. Since we are only getting a 10-bit result by default, we set this to 2 bytes so we can contain our one value. This could be set to 1 if you opt for an 8-bit or lower result.|
149|`ADC_SAMPLING_RATE` |`int` |`ADC_SMPR_SMP_1P5` |Sets the sampling rate of the ADC. By default, it is set to the fastest setting. | 155|`ADC_SAMPLING_RATE` |`int` |`ADC_SMPR_SMP_1P5` |Sets the sampling rate of the ADC. By default, it is set to the fastest setting. |
150|`ADC_RESOLUTION` |`int` |`ADC_CFGR1_RES_12BIT`|The resolution of your result. We choose 12 bit by default, but you can opt for 12, 10, 8, or 6 bit. | 156|`ADC_RESOLUTION` |`int` |`ADC_CFGR1_RES_10BIT` or `ADC_CFGR_RES_10BITS`|The resolution of your result. We choose 10 bit by default, but you can opt for 12, 10, 8, or 6 bit. Different MCUs use slightly different names for the resolution constants. |
diff --git a/drivers/chibios/analog.c b/drivers/chibios/analog.c
index 2b3872afb..b1081623d 100644
--- a/drivers/chibios/analog.c
+++ b/drivers/chibios/analog.c
@@ -101,7 +101,11 @@
101 101
102// Options are 12, 10, 8, and 6 bit. 102// Options are 12, 10, 8, and 6 bit.
103#ifndef ADC_RESOLUTION 103#ifndef ADC_RESOLUTION
104# define ADC_RESOLUTION ADC_CFGR1_RES_10BIT 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
105#endif 109#endif
106 110
107static ADCConfig adcCfg = {}; 111static ADCConfig adcCfg = {};
@@ -161,8 +165,8 @@ __attribute__((weak)) adc_mux pinToMux(pin_t pin) {
161 case B0: return TO_MUX( ADC_CHANNEL_IN12, 2 ); 165 case B0: return TO_MUX( ADC_CHANNEL_IN12, 2 );
162 case B1: return TO_MUX( ADC_CHANNEL_IN1, 2 ); 166 case B1: return TO_MUX( ADC_CHANNEL_IN1, 2 );
163 case B2: return TO_MUX( ADC_CHANNEL_IN12, 1 ); 167 case B2: return TO_MUX( ADC_CHANNEL_IN12, 1 );
164 case B12: return TO_MUX( ADC_CHANNEL_IN2, 3 ); 168 case B12: return TO_MUX( ADC_CHANNEL_IN3, 3 );
165 case B13: return TO_MUX( ADC_CHANNEL_IN3, 3 ); 169 case B13: return TO_MUX( ADC_CHANNEL_IN5, 2 );
166 case B14: return TO_MUX( ADC_CHANNEL_IN4, 3 ); 170 case B14: return TO_MUX( ADC_CHANNEL_IN4, 3 );
167 case B15: return TO_MUX( ADC_CHANNEL_IN5, 3 ); 171 case B15: return TO_MUX( ADC_CHANNEL_IN5, 3 );
168 case C0: return TO_MUX( ADC_CHANNEL_IN6, 0 ); // Can also be ADC2 172 case C0: return TO_MUX( ADC_CHANNEL_IN6, 0 ); // Can also be ADC2
@@ -189,11 +193,52 @@ __attribute__((weak)) adc_mux pinToMux(pin_t pin) {
189 case E15: return TO_MUX( ADC_CHANNEL_IN2, 3 ); 193 case E15: return TO_MUX( ADC_CHANNEL_IN2, 3 );
190 case F2: return TO_MUX( ADC_CHANNEL_IN10, 0 ); // Can also be ADC2 194 case F2: return TO_MUX( ADC_CHANNEL_IN10, 0 ); // Can also be ADC2
191 case F4: return TO_MUX( ADC_CHANNEL_IN5, 0 ); 195 case F4: return TO_MUX( ADC_CHANNEL_IN5, 0 );
192#elif defined(STM32F4XX) // TODO: add all pins 196#elif defined(STM32F4XX)
193 case A0: return TO_MUX( ADC_CHANNEL_IN0, 0 ); 197 case A0: return TO_MUX( ADC_CHANNEL_IN0, 0 );
194 //case A1: return TO_MUX( ADC_CHANNEL_IN1, 0 ); 198 case A1: return TO_MUX( ADC_CHANNEL_IN1, 0 );
195#elif defined(STM32F1XX) // TODO: add all pins 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)
196 case A0: return TO_MUX( ADC_CHANNEL_IN0, 0 ); 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.
197#endif 242#endif
198 } 243 }
199 244