Skip to content

Commit

Permalink
initial integration
Browse files Browse the repository at this point in the history
  • Loading branch information
mcells committed Feb 12, 2024
1 parent 689f88a commit 9fcd4e5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
40 changes: 23 additions & 17 deletions src/current_sense/hardware_specific/esp32/esp32_i2s_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
* I2S reading implementation by @mcells.
*/

uint32_t IRAM_ATTR adc_buffer[ADC1_CHANNEL_MAX] = {0};
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
uint32_t IRAM_ATTR readscnt = 0;
uint32_t IRAM_ATTR intcnt = 0;
Expand All @@ -51,7 +51,7 @@ unsigned long IRAM_ATTR fifotime = 0;
#endif

// This function reads data from the I2S FIFO and processes it to obtain average readings for each channel.
// The ADC counts get saved in uint32_t adc_buffer[].
// The ADC counts get saved in uint32_t i2s_adc_buffer[].
static void IRAM_ATTR readFiFo()
{
// uint32_t readings[ADC1_CHANNEL_MAX][ADC1_CHANNEL_MAX*BUF_LEN];
Expand Down Expand Up @@ -134,22 +134,22 @@ static void IRAM_ATTR readFiFo()
{
if (counts[j] != 0)
{
adc_buffer[j] = avgreadings[j] / counts[j];
i2s_adc_buffer[j] = avgreadings[j] / counts[j];

// int32_t leastdiff = 4095;
// uint32_t idx = 0;
// for (size_t k = 0; k < counts[j]; k++)
// {
// int32_t diff = abs(int(adc_buffer[j]) - int(readings[j][k]));
// int32_t diff = abs(int(i2s_adc_buffer[j]) - int(readings[j][k]));
// if (leastdiff > diff && diff != 0)
// {
// leastdiff = diff;
// idx = k;
// }
// }
// adc_buffer[j] = readings[j][idx];
// i2s_adc_buffer[j] = readings[j][idx];

// Serial.printf(">Channel%d:%d\n", j, adc_buffer[j]);
// Serial.printf(">Channel%d:%d\n", j, i2s_adc_buffer[j]);
}
}
}
Expand Down Expand Up @@ -180,7 +180,10 @@ static void IRAM_ATTR i2s_isr(void *arg)
void IRAM_ATTR _startADC3PinConversionLowSide()
{
#if I2S_USE_INTERRUPT != true
readFiFo();
if (sampleOnCommand)
{
readFiFo();
}
#endif

#if DEBUG_ADC
Expand Down Expand Up @@ -233,7 +236,7 @@ void IRAM_ATTR _startADC3PinConversionLowSide()

for (size_t i = 0; i < ADC1_CHANNEL_MAX; i++)
{
Serial.printf(">Channel%d:%d\n", i, adc_buffer[i]);
Serial.printf(">Channel%d:%d\n", i, i2s_adc_buffer[i]);
}
}
#endif
Expand All @@ -244,16 +247,18 @@ float IRAM_ATTR _readADCVoltageI2S(const int pin, const void *cs_params)
{
float adc_voltage_conv = ((ESP32MCPWMCurrentSenseParams *)cs_params)->adc_voltage_conv;

return adc_buffer[digitalPinToAnalogChannel(pin)] * adc_voltage_conv;
return i2s_adc_buffer[digitalPinToAnalogChannel(pin)] * adc_voltage_conv;
}

// Sets up the I2S peripheral and ADC. Can be run multiple times to configure multiple motors.
void* IRAM_ATTR _configureI2S(const void* driver_params, const int pinA, const int pinB, const int pinC){
void* IRAM_ATTR _configureI2S(const bool lowside, const void* driver_params, const int pinA, const int pinB, const int pinC){
sampleOnCommand = !lowside;

mcpwm_unit_t unit = ((ESP32MCPWMDriverParams*)driver_params)->mcpwm_unit;

if( _isset(pinA) ) channels[digitalPinToAnalogChannel(pinA)] = 1; pinMode(pinA, GPIO_MODE_DISABLE );
if( _isset(pinB) ) channels[digitalPinToAnalogChannel(pinB)] = 1; pinMode(pinB, GPIO_MODE_DISABLE );
if( _isset(pinC) ) channels[digitalPinToAnalogChannel(pinC)] = 1; pinMode(pinC, GPIO_MODE_DISABLE );
if( _isset(pinA) ) channels[digitalPinToAnalogChannel(pinA)] = 1; pinMode(pinA, INPUT);
if( _isset(pinB) ) channels[digitalPinToAnalogChannel(pinB)] = 1; pinMode(pinB, INPUT);
if( _isset(pinC) ) channels[digitalPinToAnalogChannel(pinC)] = 1; pinMode(pinC, INPUT);

int activeChannels = 0;
u_int some_channel = 0;
Expand All @@ -267,9 +272,10 @@ void* IRAM_ATTR _configureI2S(const void* driver_params, const int pinA, const i
}
globalActiveChannels = activeChannels;

ESP32MCPWMCurrentSenseParams *params = new ESP32MCPWMCurrentSenseParams{
.pins = {pinA, pinB, pinC},
.adc_voltage_conv = (_ADC_VOLTAGE) / (_ADC_RESOLUTION),
ESP32MCPWMCurrentSenseParams* params = new ESP32MCPWMCurrentSenseParams {
.pins = { pinA, pinB, pinC },
.adc_voltage_conv = (_ADC_VOLTAGE)/(_ADC_RESOLUTION),
.mcpwm_unit = unit
};

periph_module_reset(PERIPH_I2S0_MODULE);
Expand Down
34 changes: 29 additions & 5 deletions src/current_sense/hardware_specific/esp32/esp32_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)

#include "esp32_adc_driver.h"
#include "esp32_i2s_driver.h"

#include "driver/mcpwm.h"
#include "soc/mcpwm_reg.h"
Expand All @@ -15,7 +16,7 @@

#define _ADC_VOLTAGE 3.3f
#define _ADC_RESOLUTION 4095.0f

#define _I2S_ADC true

typedef struct ESP32MCPWMCurrentSenseParams {
int pins[3];
Expand All @@ -29,13 +30,21 @@ typedef struct ESP32MCPWMCurrentSenseParams {
* Inline adc reading implementation
*/
// function reading an ADC value and returning the read voltage
float _readADCVoltageInline(const int pinA, const void* cs_params){
float _readADCVoltageInline(const int pinA, const void *cs_params)
{
#if _I2S_ADC == true
return _readADCVoltageI2S(pinA, cs_params);
#else
uint32_t raw_adc = adcRead(pinA);
return raw_adc * ((ESP32MCPWMCurrentSenseParams*)cs_params)->adc_voltage_conv;
return raw_adc * ((ESP32MCPWMCurrentSenseParams *)cs_params)->adc_voltage_conv;
#endif
}

// function reading an ADC value and returning the read voltage
void* _configureADCInline(const void* driver_params, const int pinA, const int pinB, const int pinC){
#if _I2S_ADC == true
return _configureI2S(false, driver_params, pinA, pinB, pinC);
#else
_UNUSED(driver_params);

if( _isset(pinA) ) pinMode(pinA, INPUT);
Expand All @@ -46,8 +55,8 @@ void* _configureADCInline(const void* driver_params, const int pinA, const int p
.pins = { pinA, pinB, pinC },
.adc_voltage_conv = (_ADC_VOLTAGE)/(_ADC_RESOLUTION)
};

return params;
#endif
}


Expand All @@ -68,6 +77,9 @@ int adc_read_index[2]={0};

// function reading an ADC value and returning the read voltage
float _readADCVoltageLowSide(const int pin, const void* cs_params){
#if _I2S_ADC == true
return _readADCVoltageI2S(pin, cs_params);
#else
mcpwm_unit_t unit = ((ESP32MCPWMCurrentSenseParams*)cs_params)->mcpwm_unit;
int buffer_index = ((ESP32MCPWMCurrentSenseParams*)cs_params)->buffer_index;
float adc_voltage_conv = ((ESP32MCPWMCurrentSenseParams*)cs_params)->adc_voltage_conv;
Expand All @@ -78,11 +90,14 @@ float _readADCVoltageLowSide(const int pin, const void* cs_params){
}
// not found
return 0;
#endif
}

// function configuring low-side current sensing
void* _configureADCLowSide(const void* driver_params, const int pinA,const int pinB,const int pinC){

#if _I2S_ADC == true
return _configureI2S(true, driver_params, pinA, pinB, pinC);
#else
mcpwm_unit_t unit = ((ESP32MCPWMDriverParams*)driver_params)->mcpwm_unit;
int index_start = adc_pin_count[unit];
if( _isset(pinA) ) adc_pins[unit][adc_pin_count[unit]++] = pinA;
Expand All @@ -101,6 +116,7 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
};

return params;
#endif
}


Expand Down Expand Up @@ -131,9 +147,13 @@ static void IRAM_ATTR mcpwm0_isr_handler(void*){
// low side
uint32_t mcpwm_intr_status = MCPWM0.int_st.timer0_tep_int_st;
if(mcpwm_intr_status){
#if _I2S_ADC == true
readFiFo();
#else
adc_buffer[0][adc_read_index[0]] = adcRead(adc_pins[0][adc_read_index[0]]);
adc_read_index[0]++;
if(adc_read_index[0] == adc_pin_count[0]) adc_read_index[0] = 0;
#endif
}
// low side
MCPWM0.int_clr.timer0_tep_int_clr = mcpwm_intr_status;
Expand All @@ -151,9 +171,13 @@ static void IRAM_ATTR mcpwm1_isr_handler(void*){
// low side
uint32_t mcpwm_intr_status = MCPWM1.int_st.timer0_tep_int_st;
if(mcpwm_intr_status){
#if _I2S_ADC == true
readFiFo();
#else
adc_buffer[1][adc_read_index[1]] = adcRead(adc_pins[1][adc_read_index[1]]);
adc_read_index[1]++;
if(adc_read_index[1] == adc_pin_count[1]) adc_read_index[1] = 0;
#endif
}
// low side
MCPWM1.int_clr.timer0_tep_int_clr = mcpwm_intr_status;
Expand Down

0 comments on commit 9fcd4e5

Please sign in to comment.