Skip to content

Commit ff1108c

Browse files
committed
Make pio number a settable parameter, so we can have multiple motor using the same PIO with different state machine, ans also be compatible with other library that uses other PIOs (encoder for example)
1 parent 2d8b529 commit ff1108c

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/current/rp2350/RP2350PIOCurrentSense.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "RP2350PIOCurrentSense.h"
22

3-
RP2350PIOCurrentSense::RP2350PIOCurrentSense(float gain, uint32_t max_adc_value, int pinSCK, int pinCSB, int pinD0, int pinTRIG) : CurrentSense() {
3+
RP2350PIOCurrentSense::RP2350PIOCurrentSense(PIO pio, float gain, uint32_t max_adc_value, int pinSCK, int pinCSB, int pinD0, int pinTRIG) : CurrentSense() {
4+
this->pio = pio;
45
this->pinSCK = pinSCK;
56
this->pinCSB = pinCSB;
67
this->pinD0 = pinD0;
@@ -28,12 +29,11 @@
2829
// TODO init timer to trigger PIO conversions at required frequency (check driver settings)
2930
// TDB: do we need config input to know which timer slice and channel to use? or can we pick automatically?
3031
// TODO start everything up
31-
32+
// TODO implement offset and calibration like other current sensor classes.
3233
float sck_hz = 20e6;
33-
PIO pio = pio0;
34-
int sm = pio_claim_unused_sm(pio0, true);
34+
int sm = pio_claim_unused_sm(this->pio, true);
3535
//if (sm < 0) { pio = pio1; sm = pio_claim_unused_sm(pio1, true); } //For now, let say we have to use PIO0, this is simpler for quick DMA setup
36-
36+
3737
// --- patch program instructions with chosen trigger pin ---
3838
size_t prog_len = bu79100g_parallel3_program.length;
3939
uint16_t insns[prog_len];
@@ -43,7 +43,7 @@
4343
struct pio_program prog = bu79100g_parallel3_program; // copy metadata
4444
prog.instructions = insns;
4545

46-
uint off = pio_add_program(pio, &prog);
46+
uint off = pio_add_program(this->pio, &prog);
4747
pio_sm_config c = bu79100g_parallel3_program_get_default_config(off);
4848

4949
// Map pins to the SM
@@ -52,11 +52,11 @@
5252
sm_config_set_sideset_pins(&c, this->pinSCK); // SCK (sideset)
5353

5454
// Put pins into PIO control
55-
pio_gpio_init(pio, this->pinSCK);
56-
pio_gpio_init(pio, this->pinCSB);
57-
pio_gpio_init(pio, this->pinD0);
58-
pio_gpio_init(pio, this->pinD1);
59-
pio_gpio_init(pio, this->pinD2);
55+
pio_gpio_init(this->pio, this->pinSCK);
56+
pio_gpio_init(this->pio, this->pinCSB);
57+
pio_gpio_init(this->pio, this->pinD0);
58+
pio_gpio_init(this->pio, this->pinD1);
59+
pio_gpio_init(this->pio, this->pinD2);
6060

6161
// Directions (from the SM’s point of view)
6262
pio_sm_set_consecutive_pindirs(pio, sm, this->pinSCK, 1, true); // SCK out

src/current/rp2350/RP2350PIOCurrentSense.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class RP2350PIOCurrentSense: public CurrentSense {
1010
public:
11-
RP2350PIOCurrentSense(float gain, uint32_t max_adc_value, int pinSCK, int pinCSB, int pinD0, int pinTRIG);
11+
RP2350PIOCurrentSense(PIO pio, float gain, uint32_t max_adc_value, int pinSCK, int pinCSB, int pinD0, int pinTRIG);
1212
~RP2350PIOCurrentSense();
1313

1414
int init() override;
@@ -27,15 +27,15 @@ class RP2350PIOCurrentSense: public CurrentSense {
2727
int dma_a = -1; // PIO RX -> ring (streamer)
2828
int dma_b = -1; // reloader
2929

30+
PIO pio;
31+
3032
uint32_t max_adc_value; //!< maximum ADC value (e.g. 4096 for 12 bit ADC)
3133
int pinCSB;
3234
int pinSCK;
3335
int pinD0;
3436
int pinD1;
3537
int pinD2;
3638
int pinTRIG;
37-
float gain_a;
38-
float gain_b;
39-
float gain_c;
39+
4040
protected: //For debug, all public
4141
};

0 commit comments

Comments
 (0)