Skip to content

Commit 2385915

Browse files
committed
Optimize de-interleaving (getPhaseCurrents() < 1uS on Rp2350), Unroll the loop, only do abc, only 12bits
1 parent b373096 commit 2385915

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/current/rp2350/RP2350PIOCurrentSense.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,6 @@
115115
return 0;
116116
};
117117

118-
static inline 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) ? w0 : w1;
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-
136118
PhaseCurrent_s RP2350PIOCurrentSense::getPhaseCurrents() {
137119
PhaseCurrent_s current;
138120

@@ -141,16 +123,35 @@
141123
const uint32_t i_dma = (dma_hw->ch[dma_a].write_addr - base)>>2;
142124
//For a safe read, get the one that is an even number and at least <2.wi, manage looping
143125
const uint32_t i_last = (i_dma <= 1) ? RING_WORDS -2 : ((i_dma / 2)*2 - 2);
144-
//copy them quickly (before print!)
126+
//copy them quickly (before any print!)
145127
const uint32_t w0 = buff[i_last];
146128
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);
150129

151-
current.a = (0x00000fff & a) * gain_a;
152-
current.b = (0x00000fff & b) * gain_b;
153-
current.c = (0x00000fff & c) * gain_c;
130+
//Reconstruct the 3 current from interleaved data
131+
uint32_t a,b,c = 0;
132+
uint32_t g;
133+
134+
//g = (w0 >> 28) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u); d = (d<<1)|((g>>3)&1u);
135+
//g = (w0 >> 24) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u); d = (d<<1)|((g>>3)&1u);
136+
//g = (w0 >> 20) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u); d = (d<<1)|((g>>3)&1u);
137+
//g = (w0 >> 16) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u); d = (d<<1)|((g>>3)&1u);
138+
g = (w0 >> 12) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
139+
g = (w0 >> 8) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
140+
g = (w0 >> 4) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
141+
g = (w0 >> 0) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
142+
143+
g = (w1 >> 28) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
144+
g = (w1 >> 24) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
145+
g = (w1 >> 20) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
146+
g = (w1 >> 16) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
147+
g = (w1 >> 12) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
148+
g = (w1 >> 8) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
149+
g = (w1 >> 4) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
150+
g = (w1 >> 0) & 0xFu; a = (a<<1)|((g>>0)&1u); b = (b<<1)|((g>>1)&1u); c = (c<<1)|((g>>2)&1u);// d = (d<<1)|((g>>3)&1u);
151+
152+
current.a = a * gain_a;
153+
current.b = b * gain_b;
154+
current.c = c * gain_c;
154155

155156
return current;
156157
};

0 commit comments

Comments
 (0)