Skip to content

Commit

Permalink
Take only one Sample (good for HFI), read whole fifo and move debug t…
Browse files Browse the repository at this point in the history
…o function
  • Loading branch information
mcells authored Feb 23, 2024
1 parent e390a32 commit 3f3aba8
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 143 deletions.
235 changes: 124 additions & 111 deletions src/current_sense/hardware_specific/esp32/esp32_i2s_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ typedef struct ESP32MCPWMCurrentSenseParams {
* I2S reading implementation by @mcells.
*/

uint32_t IRAM_ATTR i2s_adc_buffer[ADC1_CHANNEL_MAX] = {0};
static uint32_t IRAM_ATTR i2s_adc_buffer[ADC1_CHANNEL_MAX] = {0};
int globalActiveChannels = 0;
int channels[ADC1_CHANNEL_MAX] = {0};
bool running = false;
bool sampleOnCommand = true;
#if DEBUG_ADC == true
uint32_t IRAM_ATTR readscnt = 0;
volatile uint32_t IRAM_ATTR buffersread = 0;
uint32_t IRAM_ATTR intcnt = 0;
uint32_t IRAM_ATTR skipped = 0;
uint32_t IRAM_ATTR equal = 0;
Expand All @@ -70,19 +71,19 @@ void IRAM_ATTR readFiFo()
uint32_t avgreadings[ADC1_CHANNEL_MAX] = {0};
uint32_t counts[ADC1_CHANNEL_MAX] = {0};
uint32_t fifolen = GET_PERI_REG_BITS2(I2S_FIFO_CONF_REG(0), I2S_RX_DATA_NUM_M, I2S_RX_DATA_NUM_S); // I2S0.fifo_conf.rx_data_num;

#if DEBUG_ADC
uint32_t lastrd = 0;
uint32_t lasth = 0;
uint32_t lastl = 0;
uint32_t internalequal = 0;
uint32_t internal_buffersread = 0;
#endif

for (size_t i = 0; i < fifolen; i++)
{
// while(!GET_PERI_REG_MASK(I2S_INT_RAW_REG(0), I2S_RX_REMPTY_INT_RAW_M)){
// I2S0.in_fifo_pop.pop = 1;
// I2S0.in_fifo_pop.pop = 0;
I2S0.int_ena.rx_rempty = 1;
I2S0.int_clr.rx_rempty = 1;

// FiFo length seems a fixed size -> read until empty for latest samples
while(!GET_PERI_REG_MASK(I2S_INT_RAW_REG(0), I2S_RX_REMPTY_INT_RAW_M)){
SET_PERI_REG_MASK(I2S_INFIFO_POP_REG(0), I2S_INFIFO_POP_M);
CLEAR_PERI_REG_MASK(I2S_INFIFO_POP_REG(0), I2S_INFIFO_POP_M);

Expand All @@ -92,57 +93,65 @@ void IRAM_ATTR readFiFo()
uint32_t lowVal = rd & 0xFFFF;

#if DEBUG_ADC == true
if (rd == lastrd || highVal == lastl || lowVal == lasth)
{ //
internalequal++;
// currfifo = rd;
// lastfifo = lastrd;
// Serial.printf("\n|\nlast: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(lastfifo >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(lastfifo >> i ) & 1);
// }

// Serial.printf("\ncurr: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(currfifo >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(currfifo >> i ) & 1);
// }
// Serial.printf("\n");
}
if (rd == lastrd || highVal == lastl || lowVal == lasth)
{ //
internalequal++;
// currfifo = rd;
// lastfifo = lastrd;
// Serial.printf("\n|\nlast: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(lastfifo >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(lastfifo >> i ) & 1);
// }

// Serial.printf("\ncurr: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(currfifo >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(currfifo >> i ) & 1);
// }
// Serial.printf("\n");
}

lasth = highVal;
lastl = lowVal;
lastrd = rd;
readscnt += 2;
lasth = highVal;
lastl = lowVal;
lastrd = rd;
readscnt += 2;
internal_buffersread++;
#endif
uint32_t chan = (lowVal >> 12) & 0x07;
uint32_t adc_value = lowVal & 0xfff;
// readings[chan][counts[chan]] = adc_value;
avgreadings[chan] += adc_value;
counts[chan]++;

chan = (highVal >> 12) & 0x07;
adc_value = highVal & 0xfff;
// readings[chan][counts[chan]] = adc_value;
avgreadings[chan] += adc_value;
counts[chan]++;
}
uint32_t chan = (lowVal >> 12) & 0x07;
uint32_t adc_value = lowVal & 0xfff;
// readings[chan][counts[chan]] = adc_value;
avgreadings[chan] = adc_value;
i2s_adc_buffer[chan] = adc_value;
//avgreadings[chan] += adc_value;
counts[chan]++;

chan = (highVal >> 12) & 0x07;
adc_value = highVal & 0xfff;
// readings[chan][counts[chan]] = adc_value;
avgreadings[chan] = adc_value;
i2s_adc_buffer[chan] = adc_value;
//avgreadings[chan] += adc_value;
counts[chan]++;
}
I2S0.int_ena.rx_rempty = 0;
I2S0.int_clr.rx_rempty = 1;
#if DEBUG_ADC == true
equal = internalequal;
intcnt += 1; // I2S0.fifo_conf.rx_data_num;
buffersread = internal_buffersread;
#endif
for (int j = 0; j < ADC1_CHANNEL_MAX; j++)
/*for (int j = 0; j < ADC1_CHANNEL_MAX; j++)
{
if (counts[j] != 0)
{
Expand All @@ -163,7 +172,7 @@ void IRAM_ATTR readFiFo()
// Serial.printf(">Channel%d:%d\n", j, i2s_adc_buffer[j]);
}
}
}*/
}

#if I2S_USE_INTERRUPT == true
Expand All @@ -173,7 +182,7 @@ static void IRAM_ATTR i2s_isr(void *arg)
unsigned long fifostart = micros();
#endif

if (I2S0.int_st.rx_take_data) // fifo is full
if (I2S0.int_st.rx_wfull) // fifo is full
{
readFiFo();
}
Expand All @@ -187,6 +196,64 @@ static void IRAM_ATTR i2s_isr(void *arg)
}
#endif

void printdbg(){
#if DEBUG_ADC == true
// skipped++;
// if (skipped >= 1000)
{
// skipped = 0;
unsigned long now = micros();
volatile uint32_t interr = intcnt;
volatile uint32_t samplesss = readscnt;
intcnt = 0;
readscnt = 0;

float readspersec = (1000000.0f * interr) / (now - ts);
float samplespersec = (1000000.0f * samplesss) / (now - ts);

ts = now;
Serial.printf(">ips:%f\n", readspersec);
Serial.printf(">sps:%f\n", samplespersec); // readspersec * GET_PERI_REG_BITS2(I2S_FIFO_CONF_REG(0), I2S_RX_DATA_NUM_M, I2S_RX_DATA_NUM_S)); //I2S0.fifo_conf.rx_data_num
Serial.printf(">fifo:%ld\n", fifotime);
Serial.printf(">doubles:%ld\n", equal);
Serial.printf(">bufreads:%d\n", buffersread);
// if(equal > 0){
// volatile uint32_t aktuell = currfifo;
// volatile uint32_t zuletzt = lastfifo;

// Serial.printf("\n|\nlast: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(zuletzt >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(zuletzt >> i ) & 1);
// }

// Serial.printf("\ncurr: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(aktuell >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(aktuell >> i ) & 1);
// }
// Serial.printf("\n");

// }

for (size_t i = 0; i < ADC1_CHANNEL_MAX; i++)
{
Serial.printf(">Channel%d:%d\n", i, i2s_adc_buffer[i]);
}
}
#endif
}

// Contrary to its name (so it can be called by the library), this function reads the already converted values from fifo
// and prints optional debug information.
// When using interrupt driven sampling, it only prints debug information.
Expand All @@ -198,61 +265,6 @@ void IRAM_ATTR _startADC3PinConversionLowSide()
readFiFo();
}
#endif

#if DEBUG_ADC == true
skipped++;
if (skipped >= 1000)
{
skipped = 0;
unsigned long now = micros();
volatile uint32_t interr = intcnt;
volatile uint32_t samplesss = readscnt;
intcnt = 0;
readscnt = 0;

float readspersec = (1000000.0f * interr) / (now - ts);
float samplespersec = (1000000.0f * samplesss) / (now - ts);

ts = now;
Serial.printf(">ips:%f\n", readspersec);
Serial.printf(">sps:%f\n", samplespersec); // readspersec * GET_PERI_REG_BITS2(I2S_FIFO_CONF_REG(0), I2S_RX_DATA_NUM_M, I2S_RX_DATA_NUM_S)); //I2S0.fifo_conf.rx_data_num
Serial.printf(">fifo:%ld\n", fifotime);
Serial.printf(">doubles:%ld\n", equal);
// if(equal > 0){
// volatile uint32_t aktuell = currfifo;
// volatile uint32_t zuletzt = lastfifo;

// Serial.printf("\n|\nlast: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(zuletzt >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(zuletzt >> i ) & 1);
// }

// Serial.printf("\ncurr: ");
// for (int i = 31; i >= 15; i--)
// {
// Serial.printf("%d",(aktuell >> i ) & 1);
// }
// Serial.printf(" ");
// for (int i = 15; i >= 0; i--)
// {
// Serial.printf("%d",(aktuell >> i ) & 1);
// }
// Serial.printf("\n");

// }

for (size_t i = 0; i < ADC1_CHANNEL_MAX; i++)
{
Serial.printf(">Channel%d:%d\n", i, i2s_adc_buffer[i]);
}
}
#endif
}

void IRAM_ATTR _startADC3PinConversionInline(){
Expand Down Expand Up @@ -335,9 +347,10 @@ void* IRAM_ATTR _configureI2S(const bool lowside, const void* driver_params, con
I2S0.fifo_conf.dscr_en = 0; // disable dma transfer.

I2S0.int_ena.val = 0;

#if I2S_USE_INTERRUPT == true
I2S0.int_ena.rx_take_data = 1;
I2S0.int_ena.rx_wfull = 1;

// I2S0.int_ena.rx_take_data = 1;
// I2S0.int_ena.rx_rempty = 1;
#endif

Expand Down
2 changes: 2 additions & 0 deletions src/current_sense/hardware_specific/esp32/esp32_i2s_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// The ADC counts get saved in uint32_t i2s_adc_buffer[].
void readFiFo();

void printdbg();

// Contrary to its name (so it can be called by the library), this function reads the already converted values from fifo
// and prints optional debug information.
// When using interrupt driven sampling, it only prints debug information.
Expand Down
Loading

0 comments on commit 3f3aba8

Please sign in to comment.