Skip to content

Commit 5e54639

Browse files
committed
add code to read the current from last measurement in buffer.
1 parent cc97db0 commit 5e54639

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/current/rp2350/RP2350PIOCurrentSense.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,46 @@
112112
dma_channel_start(dma_a);
113113
pio_sm_set_enabled(pio, sm, true);
114114

115-
116115
return 0;
117116
};
118117

118+
void extract_bit_interleaved(const uint32_t w0, const uint32_t w1, uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d) {
119+
*a = 0;
120+
*b = 0;
121+
*c = 0;
122+
*d = 0;
123+
for (int i = 0; i < 64; i += 4) {
124+
uint32_t w = (i < 32) ? w1 : w0;
125+
int shift = 28 - (i % 32); // 28, 24, ..., 0 for each group of 4 bits
126+
127+
uint32_t group = (w >> shift) & 0xF; // extract aN bN cN dN
128+
129+
*a = (*a << 1) | ((group >> 0) & 0x1);
130+
*b = (*b << 1) | ((group >> 1) & 0x1);
131+
*c = (*c << 1) | ((group >> 2) & 0x1);
132+
*d = (*d << 1) | ((group >> 3) & 0x1);
133+
}
134+
}
135+
119136
PhaseCurrent_s RP2350PIOCurrentSense::getPhaseCurrents() {
120137
PhaseCurrent_s current;
121-
// TODO copy values from latest ADC reading
122-
// TODO process raw values to get currents in mAmps
138+
139+
const uintptr_t base = (uintptr_t)buff;
140+
//Get the index the DMA is about to write
141+
const uint32_t i_dma = (dma_hw->ch[dma_a].write_addr - base)>>2;
142+
//For a safe read, get the one that is an even number and at least <2.wi, manage looping
143+
const uint32_t i_last = (i_dma <= 1) ? RING_WORDS -2 : ((i_dma / 2)*2 - 2);
144+
//copy them quickly (before print!)
145+
const uint32_t w0 = buff[i_last];
146+
const uint32_t w1 = buff[i_last+1];
147+
//Reconstruct the 3 current from interleaved data
148+
uint32_t a,b,c,d = 0;
149+
extract_bit_interleaved(w0,w1, &a, &b, &c, &d);
150+
151+
current.a = (0x00000fff & a) * gain_a;
152+
current.b = (0x00000fff & b) * gain_b;
153+
current.c = (0x00000fff & c) * gain_c;
154+
123155
return current;
124156
};
125157

src/current/rp2350/RP2350PIOCurrentSense.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RP2350PIOCurrentSense: public CurrentSense {
1515

1616
PhaseCurrent_s getPhaseCurrents() override;
1717

18-
static constexpr uint32_t RING_WORDS = 16; // ring span (needs to be a power of two)
18+
static constexpr uint32_t RING_WORDS = 64; // ring span (needs to be a power of two)
1919
static constexpr uint32_t RING_BYTES = RING_WORDS * 4;
2020

2121
// Buffer base must be aligned to ring span for write-ring:
@@ -24,20 +24,18 @@ class RP2350PIOCurrentSense: public CurrentSense {
2424
// Single word used by DMA B to rearm A:
2525
alignas(4) volatile uint32_t reload_count = RING_WORDS;
2626

27-
2827
int dma_a = -1; // PIO RX -> ring (streamer)
2928
int dma_b = -1; // reloader
3029

31-
3230
uint32_t max_adc_value; //!< maximum ADC value (e.g. 4096 for 12 bit ADC)
3331
int pinCSB;
3432
int pinSCK;
3533
int pinD0;
3634
int pinD1;
3735
int pinD2;
3836
int pinTRIG;
39-
int gain_a;
40-
int gain_b;
41-
int gain_c;
37+
float gain_a;
38+
float gain_b;
39+
float gain_c;
4240
protected: //For debug, all public
4341
};

0 commit comments

Comments
 (0)