You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/current_sense/hardware_specific/rp2040/rp2040_mcu.h
+26-21Lines changed: 26 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -17,35 +17,29 @@
17
17
* To use the other ADC channels, use them via this engine. Use addPin() to add them to the conversion, and getLastResult()
18
18
* to retrieve their value at any time.
19
19
*
20
-
* For motor current sensing, the engine supports both inline sensing and low-side sensing.
20
+
* For motor current sensing, the engine supports inline sensing only.
21
21
*
22
22
* Inline sensing is supported by offering a user-selectable fixed ADC sampling rate, which can be set between 500kHz and 1Hz.
23
23
* After starting the engine it will continuously sample and provide new values at the configured rate.
24
24
*
25
-
* Low-side sensing is supported by configuring a trigger from the PWM signal. The trigger happens at the middle-point of the
26
-
* up/down counting PWM, which is the mid-point of the on-period.
27
-
* So in the case of low-side sensing, all ADC channels are converted at the rate of the PWM frequency.
25
+
* Low-side sensing is currently not supported.
28
26
*
29
27
* The SimpleFOC PWM driver for RP2040 syncs all the slices, so the PWM trigger is applied to the first used slice. For current
30
28
* sensing to work correctly, all PWM slices have to be set to the same PWM frequency.
31
-
* In theory, two motors could be sensed using 2 shunts on each motor. In practice, due to the slow conversion rate of the RP2040's
32
-
* ADC, this would mean 8us conversion time, which would have to fit in the low-side on-time even at low duty-cycles... It remains
33
-
* to be seen how well this can work, or if it works at all, but presumably the PWM frequency would have to be quite low.
34
-
*
35
-
* TODO we need the mid-point of the low-side, which is actually the beginning/end of the PWM cycle - hmmmm...
29
+
* In theory, two motors could be sensed using 2 shunts on each motor.
36
30
*
37
31
* Note that if using other ADC channels along with the motor current sensing, those channels will be subject to the same conversion schedule as the motor's ADC channels, i.e. convert at the same fixed rate in case
38
-
* of inline sensing, or based on the motor PWM in case of PWM-triggered low-side sensing.
32
+
* of inline sensing.
39
33
*
40
34
* Solution to trigger ADC conversion from PWM via DMA:
41
35
* use the PWM wrap as a DREQ to a DMA channel, and have the DMA channel write to the ADC's CS register to trigger an ADC sample.
36
+
* Unfortunately, I could not get this to work, so no low side sensing for the moment.
37
+
*
42
38
* Solution for ADC conversion:
43
39
* ADC converts all channels in round-robin mode, and writes to FIFO. FIFO is emptied by a DMA which triggers after N conversions,
44
-
* where N is the number of ADC channels used. So this DMA copies all the values from one round-robin conversion. This first DMA
45
-
* triggers a second DMA which does a 32bit copy of all converted values (up to 4 channels x 8bit) at once, and triggers an interrupt.
46
-
* The interrupt routine copies the values to the output buffer.
40
+
* where N is the number of ADC channels used. So this DMA copies all the values from one round-robin conversion.
41
+
*
47
42
*
48
-
* TODO think about whether the second DMA is needed
49
43
*/
50
44
51
45
@@ -54,32 +48,43 @@
54
48
#defineSIMPLEFOC_RP2040_ADC_VDDA3.3f
55
49
#endif
56
50
51
+
52
+
union ADCResults {
53
+
uint32_t value;
54
+
uint8_t raw[4];
55
+
struct {
56
+
uint8_t ch0;
57
+
uint8_t ch1;
58
+
uint8_t ch2;
59
+
uint8_t ch3;
60
+
};
61
+
};
62
+
63
+
57
64
classRP2040ADCEngine {
58
65
59
66
public:
60
67
RP2040ADCEngine();
61
68
voidaddPin(int pin);
62
-
voidsetPWMTrigger(uint slice);
69
+
//void setPWMTrigger(uint slice);
63
70
64
71
boolinit();
65
72
voidstart();
66
73
voidstop();
67
74
68
-
voidgetLastResult();
69
-
70
-
voidhandleADCUpdate();
75
+
ADCResults getLastResults(); // TODO find a better API and representation for this
71
76
72
77
int samples_per_second = 0; // leave at 0 to convert in tight loop
73
78
float adc_conv = (SIMPLEFOC_RP2040_ADC_VDDA / SIMPLEFOC_RP2040_ADC_RESOLUTION); // conversion from raw ADC to float
0 commit comments