Skip to content

Commit a875e40

Browse files
committed
esp32 2 or 3 adc lines automatic
1 parent 98c10e6 commit a875e40

File tree

2 files changed

+59
-42
lines changed

2 files changed

+59
-42
lines changed

src/current_sense/LowsideCurrentSense.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ LowsideCurrentSense::LowsideCurrentSense(float _shunt_resistor, float _gain, int
2323
void LowsideCurrentSense::init(){
2424
// configure ADC variables
2525
_configureADCLowSide(pinA,pinB,pinC);
26+
// sync the driver
27+
_driverSyncLowSide();
2628
// calibrate zero offsets
2729
calibrateOffsets();
2830
}

src/current_sense/hardware_specific/esp32_mcu.cpp

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../hardware_api.h"
2+
#include "../../drivers/hardware_api.h"
23

34
#ifdef ESP_H
45

@@ -14,15 +15,39 @@
1415
#define _ADC_VOLTAGE 3.3f
1516
#define _ADC_RESOLUTION 4095.0f
1617

18+
// adc counts to voltage conversion ratio
19+
// some optimizing for faster execution
20+
#define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )
21+
22+
23+
/**
24+
* Inline adc reading implementation
25+
*/
26+
// function reading an ADC value and returning the read voltage
27+
float _readADCVoltageInline(const int pinA){
28+
uint32_t raw_adc = adcRead(pinA);
29+
// uint32_t raw_adc = analogRead(pinA);
30+
return raw_adc * _ADC_CONV;
31+
}
32+
33+
// function reading an ADC value and returning the read voltage
34+
void _configureADCInline(const int pinA,const int pinB, const int pinC){
35+
pinMode(pinA, INPUT);
36+
pinMode(pinB, INPUT);
37+
if( _isset(pinC) ) pinMode(pinC, INPUT);
38+
}
39+
40+
41+
/**
42+
* Low side adc reading implementation
43+
*/
1744
static mcpwm_dev_t *MCPWM[2] = {&MCPWM0, &MCPWM1};
18-
int a1, a2, a3; //Current readings from internal current sensor amplifiers
45+
int a1, a2, a3; // Current readings from internal current sensor amplifiers
1946
int _pinA, _pinB, _pinC;
2047
static void IRAM_ATTR isr_handler(void*);
2148
byte currentState = 1;
2249

23-
// adc counts to voltage conversion ratio
24-
// some optimizing for faster execution
25-
#define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )
50+
2651

2752
// function reading an ADC value and returning the read voltage
2853
float _readADCVoltageLowSide(const int pin){
@@ -39,71 +64,61 @@ float _readADCVoltageLowSide(const int pin){
3964
void _configureADCLowSide(const int pinA,const int pinB,const int pinC){
4065
_pinA = pinA;
4166
_pinB = pinB;
42-
if( _isset(pinC) ) _pinC = pinC;
67+
_pinC = pinC;
4368
pinMode(pinA, INPUT);
4469
pinMode(pinB, INPUT);
4570
if( _isset(pinC) ) pinMode(pinC, INPUT);
46-
4771
}
4872

4973
void _driverSyncLowSide(){
74+
// high side registers enable interrupt
5075
// MCPWM[MCPWM_UNIT_0]->int_ena.timer0_tez_int_ena = true;//A PWM timer 0 TEP event will trigger this interrupt
5176
// MCPWM[MCPWM_UNIT_0]->int_ena.timer1_tez_int_ena = true;//A PWM timer 1 TEP event will trigger this interrupt
77+
// if( _isset(_pinC) ) MCPWM[MCPWM_UNIT_0]->int_ena.timer2_tez_int_ena = true;//A PWM timer 2 TEP event will trigger this interrupt
5278

53-
79+
// low-side register enable interrupt
5480
MCPWM[MCPWM_UNIT_0]->int_ena.timer0_tep_int_ena = true;//A PWM timer 0 TEP event will trigger this interrupt
5581
MCPWM[MCPWM_UNIT_0]->int_ena.timer1_tep_int_ena = true;//A PWM timer 1 TEP event will trigger this interrupt
56-
//MCPWM[MCPWM_UNIT_0]->int_ena.timer2_tep_int_ena = true;//A PWM timer 2 TEP event will trigger this interrupt
82+
if( _isset(_pinC) ) MCPWM[MCPWM_UNIT_0]->int_ena.timer2_tep_int_ena = true;//A PWM timer 2 TEP event will trigger this interrupt
5783
mcpwm_isr_register(MCPWM_UNIT_0, isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL); //Set ISR Handler
5884
}
5985

6086
// Read currents when interrupt is triggered
6187
static void IRAM_ATTR isr_handler(void*){
62-
// uint32_t mcpwm_intr_status_0 = MCPWM[MCPWM_UNIT_0]->int_st.timer0_tez_int_st;
63-
// uint32_t mcpwm_intr_status_1 = MCPWM[MCPWM_UNIT_0]->int_st.timer1_tez_int_st;
88+
// // high side
89+
// uint32_t mcpwm_intr_status_0 = MCPWM[MCPWM_UNIT_0]->int_st.timer0_tez_int_st;
90+
// uint32_t mcpwm_intr_status_1 = MCPWM[MCPWM_UNIT_0]->int_st.timer1_tez_int_st;
91+
// uint32_t mcpwm_intr_status_2 = _isset(_pinC) ? MCPWM[MCPWM_UNIT_0]->int_st.timer2_tez_int_st : 0;
92+
93+
// low side
6494
uint32_t mcpwm_intr_status_0 = MCPWM[MCPWM_UNIT_0]->int_st.timer0_tep_int_st;
6595
uint32_t mcpwm_intr_status_1 = MCPWM[MCPWM_UNIT_0]->int_st.timer1_tep_int_st;
66-
//uint32_t mcpwm_intr_status_2 = MCPWM[MCPWM_UNIT_0]->int_st.timer2_tep_int_st;
96+
uint32_t mcpwm_intr_status_2 = _isset(_pinC) ? MCPWM[MCPWM_UNIT_0]->int_st.timer2_tep_int_st : 0;
6797

68-
// digitalWrite(14,HIGH);
69-
if(mcpwm_intr_status_0 > 0 && currentState == 1){
70-
a1 = adcRead(_pinA);
71-
// a2 = analogRead(_pinA);
98+
switch (currentState)
99+
{
100+
case 1 :
101+
if (mcpwm_intr_status_0 > 0) a1 = adcRead(_pinA);
72102
currentState = 2;
73-
}
74-
else if(mcpwm_intr_status_1 > 0 && currentState == 2){
75-
a2 = adcRead(_pinB);
76-
// a3 = analogRead(_pinB);
103+
break;
104+
case 2 :
105+
if (mcpwm_intr_status_1 > 0) a2 = adcRead(_pinB);
106+
currentState = _isset(_pinC) ? 3 : 1;
107+
break;
108+
case 3 :
109+
if (mcpwm_intr_status_2 > 0) a3 = adcRead(_pinC);
77110
currentState = 1;
111+
break;
78112
}
79-
/*
80-
else if(mcpwm_intr_status_2 > 0 && currentState == 3){
81-
a3 = adcRead(_pinC);
82-
//a1 = analogRead(_pinA);
83-
currentState = 1;
84-
}*/
85-
// digitalWrite(14,LOW);
86113

114+
// high side
87115
// MCPWM[MCPWM_UNIT_0]->int_clr.timer0_tez_int_clr = mcpwm_intr_status_0;
88116
// MCPWM[MCPWM_UNIT_0]->int_clr.timer1_tez_int_clr = mcpwm_intr_status_1;
117+
// if( _isset(_pinC) ) MCPWM[MCPWM_UNIT_0]->int_clr.timer2_tez_int_clr = mcpwm_intr_status_2;
118+
// low side
89119
MCPWM[MCPWM_UNIT_0]->int_clr.timer0_tep_int_clr = mcpwm_intr_status_0;
90120
MCPWM[MCPWM_UNIT_0]->int_clr.timer1_tep_int_clr = mcpwm_intr_status_1;
91-
//MCPWM[MCPWM_UNIT_0]->int_clr.timer2_tep_int_clr = mcpwm_intr_status_2;
92-
}
93-
94-
95-
// function reading an ADC value and returning the read voltage
96-
float _readADCVoltageInline(const int pinA){
97-
uint32_t raw_adc = adcRead(pinA);
98-
// uint32_t raw_adc = analogRead(pinA);
99-
return raw_adc * _ADC_CONV;
100-
}
101-
102-
// function reading an ADC value and returning the read voltage
103-
void _configureADCInline(const int pinA,const int pinB, const int pinC){
104-
pinMode(pinA, INPUT);
105-
pinMode(pinB, INPUT);
106-
if( _isset(pinC) ) pinMode(pinC, INPUT);
121+
if( _isset(_pinC) ) MCPWM[MCPWM_UNIT_0]->int_clr.timer2_tep_int_clr = mcpwm_intr_status_2;
107122
}
108123

109124

0 commit comments

Comments
 (0)