Skip to content

Commit

Permalink
more refactoring of the current sensing
Browse files Browse the repository at this point in the history
  • Loading branch information
askuric committed Jun 21, 2024
1 parent d89fa26 commit 710edb4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 99 deletions.
11 changes: 8 additions & 3 deletions src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "esp32_adc_driver.h"

#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
#include "esp32_mcu.h"

// maybe go to the fast ADC in future even outside the MCPWM (we'd have to remove the SOC_MCPWM_SUPPORTED flag)
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
#define SIMPLEFOC_ADC_ATTEN ADC_11db
#define SIMPLEFOC_ADC_RES 12


#ifdef CONFIG_IDF_TARGET_ESP32 // if esp32 variant
Expand Down Expand Up @@ -38,6 +40,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
{
int8_t channel = digitalPinToAnalogChannel(pin);
if(channel < 0){
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
return false;//not adc pin
}

Expand Down Expand Up @@ -76,7 +79,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)


// configure the ADCs in RTC mode
// no real gain
// no real gain - see if we do something with it later
// void __configFastADCs(){

// SET_PERI_REG_MASK(SENS_SAR_READER1_CTRL_REG, SENS_SAR1_DATA_INV);
Expand All @@ -101,6 +104,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
{
int8_t channel = digitalPinToAnalogChannel(pin);
if(channel < 0){
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
return false;//not adc pin
}

Expand Down Expand Up @@ -147,6 +151,7 @@ bool IRAM_ATTR adcInit(uint8_t pin){

int8_t channel = digitalPinToAnalogChannel(pin);
if(channel < 0){
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
return false;//not adc pin
}

Expand Down
4 changes: 0 additions & 4 deletions src/current_sense/hardware_specific/esp32/esp32_adc_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)


#define SIMPLEFOC_ADC_ATTEN ADC_11db
#define SIMPLEFOC_ADC_RES 12

/**
* Get ADC value for pin
* @param pin - pin number
Expand Down
27 changes: 0 additions & 27 deletions src/current_sense/hardware_specific/esp32/esp32_ledc_mcu.cpp

This file was deleted.

75 changes: 10 additions & 65 deletions src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "../../hardware_api.h"
#include "../../../drivers/hardware_api.h"
#include "../../../drivers/hardware_specific/esp32/esp32_driver_mcpwm.h"
#include "../../../drivers/hardware_specific/esp32/mcpwm_private.h"

#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)

Expand All @@ -12,22 +10,23 @@
#error SimpleFOC: ESP-IDF version 4 or lower detected. Please update to ESP-IDF 5.x and Arduino-esp32 3.0 (or higher)
#endif


#include "esp32_adc_driver.h"
#include "esp32_mcu.cpp"
#include "../../../drivers/hardware_specific/esp32/esp32_driver_mcpwm.h"
#include "../../../drivers/hardware_specific/esp32/mcpwm_private.h"

#include "driver/mcpwm_prelude.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"

#include <soc/sens_reg.h>
#include <soc/sens_struct.h>

#define SIMPLEFOC_ESP32_INTERRUPT_DEBUG

// adding a debug toggle pin to measure the time of the interrupt with oscilloscope

// #define SIMPLEFOC_ESP32_INTERRUPT_DEBUG

#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
#include "driver/gpio.h"


#ifdef CONFIG_IDF_TARGET_ESP32S3
#define DEBUGPIN 16
#define GPIO_NUM GPIO_NUM_16
Expand All @@ -38,60 +37,6 @@

#endif

#define _ADC_VOLTAGE 3.3f
#define _ADC_RESOLUTION 4095.0f



#define SIMPLEFOC_ESP32_CS_DEBUG(str)\
SIMPLEFOC_ESP32_DEBUG("CS", str);\

#define CHECK_CS_ERR(func_call, message) \
if ((func_call) != ESP_OK) { \
SIMPLEFOC_ESP32_CS_DEBUG("ERROR - " + String(message)); \
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED; \
}

typedef struct ESP32MCPWMCurrentSenseParams {
int pins[3];
float adc_voltage_conv;
int adc_buffer[3] = {};
int buffer_index = 0;
int no_adc_channels = 0;
} ESP32MCPWMCurrentSenseParams;


/**
* Inline adc reading implementation
*/
// function reading an ADC value and returning the read voltage
float _readADCVoltageInline(const int pinA, const void* cs_params){
uint32_t raw_adc = adcRead(pinA);
return raw_adc * ((ESP32MCPWMCurrentSenseParams*)cs_params)->adc_voltage_conv;
}

// 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){

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

// initialize the ADC pins
// fail if the pin is not an ADC pin
for (int i = 0; i < 3; i++){
if(_isset(params->pins[i])){
pinMode(params->pins[i], ANALOG);
if(!adcInit(params->pins[i])) {
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Failed to initialise ADC pin: "+String(params->pins[i]) + String(", maybe not an ADC pin?"));
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
}
}
}

return params;
}


/**
Expand All @@ -101,7 +46,7 @@ void* _configureADCInline(const void* driver_params, const int pinA, const int p

// function reading an ADC value and returning the read voltage
float _readADCVoltageLowSide(const int pin, const void* cs_params){
ESP32MCPWMCurrentSenseParams* p = (ESP32MCPWMCurrentSenseParams*)cs_params;
ESP32CurrentSenseParams* p = (ESP32CurrentSenseParams*)cs_params;
int no_channel = 0;
for(int i=0; i < 3; i++){
if(!_isset(p->pins[i])) continue;
Expand Down Expand Up @@ -132,7 +77,7 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
}


ESP32MCPWMCurrentSenseParams* params = new ESP32MCPWMCurrentSenseParams{};
ESP32CurrentSenseParams* params = new ESP32CurrentSenseParams{};
int no_adc_channels = 0;

// initialize the ADC pins
Expand Down Expand Up @@ -178,7 +123,7 @@ void* _driverSyncLowSide(void* driver_params, void* cs_params){
// - on_sync - sync event (not used with simplefoc)
auto cbs = mcpwm_timer_event_callbacks_t{
.on_full = [](mcpwm_timer_handle_t tim, const mcpwm_timer_event_data_t* edata, void* user_data){
ESP32MCPWMCurrentSenseParams *p = (ESP32MCPWMCurrentSenseParams*)user_data;
ESP32CurrentSenseParams *p = (ESP32CurrentSenseParams*)user_data;
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG // debugging toggle pin to measure the time of the interrupt with oscilloscope
gpio_set_level(GPIO_NUM,1); //cca 250ns for on+off
#endif
Expand Down
43 changes: 43 additions & 0 deletions src/current_sense/hardware_specific/esp32/esp32_mcu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "../../hardware_api.h"
#include "../../../drivers/hardware_api.h"

#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)

#include "esp32_adc_driver.h"
#include "esp32_mcu.h"


/**
* Inline adc reading implementation
*/
// function reading an ADC value and returning the read voltage
float _readADCVoltageInline(const int pinA, const void* cs_params){
uint32_t raw_adc = adcRead(pinA);
return raw_adc * ((ESP32CurrentSenseParams*)cs_params)->adc_voltage_conv;
}

// 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){

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

// initialize the ADC pins
// fail if the pin is not an ADC pin
for (int i = 0; i < 3; i++){
if(_isset(params->pins[i])){
pinMode(params->pins[i], ANALOG);
if(!adcInit(params->pins[i])) {
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Failed to initialise ADC pin: "+String(params->pins[i]) + String(", maybe not an ADC pin?"));
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
}
}
}

return params;
}


#endif
37 changes: 37 additions & 0 deletions src/current_sense/hardware_specific/esp32/esp32_mcu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef ESP32_MCU_CURRENT_SENSING_H
#define ESP32_MCU_CURRENT_SENSING_H


#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)

#include "../../hardware_api.h"
#include "../../../drivers/hardware_api.h"

#include "esp32_adc_driver.h"


// esp32 current sense parameters
typedef struct ESP32CurrentSenseParams {
int pins[3];
float adc_voltage_conv;
int adc_buffer[3] = {};
int buffer_index = 0;
int no_adc_channels = 0;
} ESP32CurrentSenseParams;

// macros for debugging wuing the simplefoc debug system
#define SIMPLEFOC_ESP32_CS_DEBUG(str)\
SimpleFOCDebug::println( "ESP32-CS"+String(tag)+ ": "+ String(str));\

#define CHECK_CS_ERR(func_call, message) \
if ((func_call) != ESP_OK) { \
SIMPLEFOC_ESP32_CS_DEBUG("ERROR - " + String(message)); \
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED; \
}


#define _ADC_VOLTAGE 3.3f
#define _ADC_RESOLUTION 4095.0f

#endif // ESP_H && ARDUINO_ARCH_ESP32
#endif

0 comments on commit 710edb4

Please sign in to comment.