1
1
#include " ../hardware_api.h"
2
+ #include " ../../drivers/hardware_api.h"
2
3
3
4
#ifdef ESP_H
4
5
14
15
#define _ADC_VOLTAGE 3 .3f
15
16
#define _ADC_RESOLUTION 4095 .0f
16
17
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
+ */
17
44
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
19
46
int _pinA, _pinB, _pinC;
20
47
static void IRAM_ATTR isr_handler (void *);
21
48
byte currentState = 1 ;
22
49
23
- // adc counts to voltage conversion ratio
24
- // some optimizing for faster execution
25
- #define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )
50
+
26
51
27
52
// function reading an ADC value and returning the read voltage
28
53
float _readADCVoltageLowSide (const int pin){
@@ -39,71 +64,61 @@ float _readADCVoltageLowSide(const int pin){
39
64
void _configureADCLowSide (const int pinA,const int pinB,const int pinC){
40
65
_pinA = pinA;
41
66
_pinB = pinB;
42
- if ( _isset (pinC) ) _pinC = pinC;
67
+ _pinC = pinC;
43
68
pinMode (pinA, INPUT);
44
69
pinMode (pinB, INPUT);
45
70
if ( _isset (pinC) ) pinMode (pinC, INPUT);
46
-
47
71
}
48
72
49
73
void _driverSyncLowSide (){
74
+ // high side registers enable interrupt
50
75
// MCPWM[MCPWM_UNIT_0]->int_ena.timer0_tez_int_ena = true;//A PWM timer 0 TEP event will trigger this interrupt
51
76
// 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
52
78
53
-
79
+ // low-side register enable interrupt
54
80
MCPWM[MCPWM_UNIT_0]->int_ena .timer0_tep_int_ena = true ;// A PWM timer 0 TEP event will trigger this interrupt
55
81
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
57
83
mcpwm_isr_register (MCPWM_UNIT_0, isr_handler, NULL , ESP_INTR_FLAG_IRAM, NULL ); // Set ISR Handler
58
84
}
59
85
60
86
// Read currents when interrupt is triggered
61
87
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
64
94
uint32_t mcpwm_intr_status_0 = MCPWM[MCPWM_UNIT_0]->int_st .timer0_tep_int_st ;
65
95
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 ;
67
97
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);
72
102
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);
77
110
currentState = 1 ;
111
+ break ;
78
112
}
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);
86
113
114
+ // high side
87
115
// MCPWM[MCPWM_UNIT_0]->int_clr.timer0_tez_int_clr = mcpwm_intr_status_0;
88
116
// 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
89
119
MCPWM[MCPWM_UNIT_0]->int_clr .timer0_tep_int_clr = mcpwm_intr_status_0;
90
120
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;
107
122
}
108
123
109
124
0 commit comments