|
112 | 112 | dma_channel_start(dma_a); |
113 | 113 | pio_sm_set_enabled(pio, sm, true); |
114 | 114 |
|
115 | | - |
116 | 115 | return 0; |
117 | 116 | }; |
118 | 117 |
|
| 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 | + |
119 | 136 | PhaseCurrent_s RP2350PIOCurrentSense::getPhaseCurrents() { |
120 | 137 | 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 | + |
123 | 155 | return current; |
124 | 156 | }; |
125 | 157 |
|
|
0 commit comments