Skip to content

Commit 1d1c5dd

Browse files
committed
HAL / LL only PWM driver
1 parent 4395c22 commit 1d1c5dd

18 files changed

+1650
-929
lines changed

src/current_sense/hardware_specific/stm32/stm32_mcu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef struct Stm32CurrentSenseParams {
1414
int pins[3] = {(int)NOT_SET};
1515
float adc_voltage_conv;
1616
ADC_HandleTypeDef* adc_handle = NP;
17-
HardwareTimer* timer_handle = NP;
17+
TIM_HandleTypeDef* timer_handle = NP;
1818
} Stm32CurrentSenseParams;
1919

2020
#endif

src/current_sense/hardware_specific/stm32/stm32f4/stm32f4_hal.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
7676

7777
// automating TRGO flag finding - hardware specific
7878
uint8_t tim_num = 0;
79-
while(driver_params->timers[tim_num] != NP && tim_num < 6){
80-
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers[tim_num++]);
79+
while(driver_params->timers_handle[tim_num] != NP && tim_num < 6){
80+
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers_handle[tim_num++]);
8181
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
8282

8383
// if the code comes here, it has found the timer available
8484
// timer does have trgo flag for injected channels
8585
sConfigInjected.ExternalTrigInjecConv = trigger_flag;
8686

8787
// this will be the timer with which the ADC will sync
88-
cs_params->timer_handle = driver_params->timers[tim_num-1];
88+
cs_params->timer_handle = driver_params->timers_handle[tim_num-1];
8989
// done
9090
break;
9191
}
@@ -99,7 +99,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
9999
// display which timer is being used
100100
#ifdef SIMPLEFOC_STM32_DEBUG
101101
// it would be better to use the getTimerNumber from driver
102-
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->getHandle()->Instance) + 1);
102+
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->Instance) + 1);
103103
#endif
104104

105105

src/current_sense/hardware_specific/stm32/stm32f4/stm32f4_mcu.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
4848
if (cs_params->timer_handle == NULL) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
4949

5050
// stop all the timers for the driver
51-
_stopTimers(driver_params->timers, 6);
51+
stm32_pause(driver_params);
5252

5353
// if timer has repetition counter - it will downsample using it
5454
// and it does not need the software downsample
55-
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance) ){
55+
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->Instance) ){
5656
// adjust the initial timer state such that the trigger
5757
// - for DMA transfer aligns with the pwm peaks instead of throughs.
5858
// - for interrupt based ADC transfer
5959
// - only necessary for the timers that have repetition counters
60-
cs_params->timer_handle->getHandle()->Instance->CR1 |= TIM_CR1_DIR;
61-
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
60+
cs_params->timer_handle->Instance->CR1 |= TIM_CR1_DIR;
61+
cs_params->timer_handle->Instance->CNT = cs_params->timer_handle->Instance->ARR;
6262
// remember that this timer has repetition counter - no need to downasmple
6363
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
6464
}else{
@@ -71,7 +71,7 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
7171
}
7272
}
7373
// set the trigger output event
74-
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
74+
LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
7575

7676
// start the adc
7777
if (use_adc_interrupt){
@@ -85,7 +85,7 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
8585
}
8686

8787
// restart all the timers of the driver
88-
_startTimers(driver_params->timers, 6);
88+
stm32_resume(driver_params);
8989

9090
// return the cs parameters
9191
// successfully initialized

src/current_sense/hardware_specific/stm32/stm32f4/stm32f4_utils.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,19 @@ uint32_t _getADCChannel(PinName pin)
133133

134134
// timer to injected TRGO
135135
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h#L179
136-
uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
137-
if(timer->getHandle()->Instance == TIM1)
136+
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer){
137+
if(timer->Instance == TIM1)
138138
return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
139139
#ifdef TIM2 // if defined timer 2
140-
else if(timer->getHandle()->Instance == TIM2)
140+
else if(timer->Instance == TIM2)
141141
return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
142142
#endif
143143
#ifdef TIM4 // if defined timer 4
144-
else if(timer->getHandle()->Instance == TIM4)
144+
else if(timer->Instance == TIM4)
145145
return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
146146
#endif
147147
#ifdef TIM5 // if defined timer 5
148-
else if(timer->getHandle()->Instance == TIM5)
148+
else if(timer->Instance == TIM5)
149149
return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
150150
#endif
151151
else
@@ -154,15 +154,15 @@ uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
154154

155155
// timer to regular TRGO
156156
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h#L331
157-
uint32_t _timerToRegularTRGO(HardwareTimer* timer){
158-
if(timer->getHandle()->Instance == TIM2)
157+
uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer){
158+
if(timer->Instance == TIM2)
159159
return ADC_EXTERNALTRIGCONV_T2_TRGO;
160160
#ifdef TIM3 // if defined timer 3
161-
else if(timer->getHandle()->Instance == TIM3)
161+
else if(timer->Instance == TIM3)
162162
return ADC_EXTERNALTRIGCONV_T3_TRGO;
163163
#endif
164164
#ifdef TIM8 // if defined timer 8
165-
else if(timer->getHandle()->Instance == TIM8)
165+
else if(timer->Instance == TIM8)
166166
return ADC_EXTERNALTRIGCONV_T8_TRGO;
167167
#endif
168168
else

src/current_sense/hardware_specific/stm32/stm32f4/stm32f4_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ uint32_t _getADCChannel(PinName pin);
1919

2020
// timer to injected TRGO
2121
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h#L179
22-
uint32_t _timerToInjectedTRGO(HardwareTimer* timer);
22+
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer);
2323

2424
// timer to regular TRGO
2525
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h#L331
26-
uint32_t _timerToRegularTRGO(HardwareTimer* timer);
26+
uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer);
2727

2828
// function returning index of the ADC instance
2929
int _adcToIndex(ADC_HandleTypeDef *AdcHandle);

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_hal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
126126

127127
// automating TRGO flag finding - hardware specific
128128
uint8_t tim_num = 0;
129-
while(driver_params->timers[tim_num] != NP && tim_num < 6){
130-
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers[tim_num++]);
129+
while(driver_params->timers_handle[tim_num] != NP && tim_num < 6){
130+
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers_handle[tim_num++]);
131131
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
132132

133133
// if the code comes here, it has found the timer available
134134
// timer does have trgo flag for injected channels
135135
sConfigInjected.ExternalTrigInjecConv = trigger_flag;
136136

137137
// this will be the timer with which the ADC will sync
138-
cs_params->timer_handle = driver_params->timers[tim_num-1];
138+
cs_params->timer_handle = driver_params->timers_handle[tim_num-1];
139139
// done
140140
break;
141141
}

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_mcu.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
5050
if (cs_params->timer_handle == NULL) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
5151

5252
// stop all the timers for the driver
53-
_stopTimers(driver_params->timers, 6);
53+
stm32_pause(driver_params);
5454

5555
// if timer has repetition counter - it will downsample using it
5656
// and it does not need the software downsample
57-
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance) ){
57+
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->Instance) ){
5858
// adjust the initial timer state such that the trigger
5959
// - for DMA transfer aligns with the pwm peaks instead of throughs.
6060
// - for interrupt based ADC transfer
6161
// - only necessary for the timers that have repetition counters
62-
cs_params->timer_handle->getHandle()->Instance->CR1 |= TIM_CR1_DIR;
63-
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
62+
cs_params->timer_handle->Instance->CR1 |= TIM_CR1_DIR;
63+
cs_params->timer_handle->Instance->CNT = cs_params->timer_handle->Instance->ARR;
6464
// remember that this timer has repetition counter - no need to downasmple
6565
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
6666
}else{
@@ -74,7 +74,7 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
7474
}
7575

7676
// set the trigger output event
77-
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
77+
LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
7878

7979
// Start the adc calibration
8080
HAL_ADCEx_Calibration_Start(cs_params->adc_handle,ADC_SINGLE_ENDED);
@@ -122,7 +122,7 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
122122
}
123123

124124
// restart all the timers of the driver
125-
_startTimers(driver_params->timers, 6);
125+
stm32_resume(driver_params);
126126

127127
// return the cs parameters
128128
// successfully initialized

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_utils.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,39 +133,39 @@ uint32_t _getADCChannel(PinName pin)
133133

134134
// timer to injected TRGO
135135
// https://github.com/stm32duino/Arduino_Core_STM32/blob/6588dee03382e73ed42c4a5e473900ab3b79d6e4/system/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_adc_ex.h#L217
136-
uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
137-
if(timer->getHandle()->Instance == TIM1)
136+
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer){
137+
if(timer->Instance == TIM1)
138138
return ADC_EXTERNALTRIGINJEC_T1_TRGO;
139139
#ifdef TIM2 // if defined timer 2
140-
else if(timer->getHandle()->Instance == TIM2)
140+
else if(timer->Instance == TIM2)
141141
return ADC_EXTERNALTRIGINJEC_T2_TRGO;
142142
#endif
143143
#ifdef TIM3 // if defined timer 3
144-
else if(timer->getHandle()->Instance == TIM3)
144+
else if(timer->Instance == TIM3)
145145
return ADC_EXTERNALTRIGINJEC_T3_TRGO;
146146
#endif
147147
#ifdef TIM4 // if defined timer 4
148-
else if(timer->getHandle()->Instance == TIM4)
148+
else if(timer->Instance == TIM4)
149149
return ADC_EXTERNALTRIGINJEC_T4_TRGO;
150150
#endif
151151
#ifdef TIM6 // if defined timer 6
152-
else if(timer->getHandle()->Instance == TIM6)
152+
else if(timer->Instance == TIM6)
153153
return ADC_EXTERNALTRIGINJEC_T6_TRGO;
154154
#endif
155155
#ifdef TIM7 // if defined timer 7
156-
else if(timer->getHandle()->Instance == TIM7)
156+
else if(timer->Instance == TIM7)
157157
return ADC_EXTERNALTRIGINJEC_T7_TRGO;
158158
#endif
159159
#ifdef TIM8 // if defined timer 8
160-
else if(timer->getHandle()->Instance == TIM8)
160+
else if(timer->Instance == TIM8)
161161
return ADC_EXTERNALTRIGINJEC_T8_TRGO;
162162
#endif
163163
#ifdef TIM15 // if defined timer 15
164-
else if(timer->getHandle()->Instance == TIM15)
164+
else if(timer->Instance == TIM15)
165165
return ADC_EXTERNALTRIGINJEC_T15_TRGO;
166166
#endif
167167
#ifdef TIM20 // if defined timer 15
168-
else if(timer->getHandle()->Instance == TIM20)
168+
else if(timer->Instance == TIM20)
169169
return ADC_EXTERNALTRIGINJEC_T20_TRGO;
170170
#endif
171171
else
@@ -174,39 +174,39 @@ uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
174174

175175
// timer to regular TRGO
176176
// https://github.com/stm32duino/Arduino_Core_STM32/blob/6588dee03382e73ed42c4a5e473900ab3b79d6e4/system/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_adc.h#L519
177-
uint32_t _timerToRegularTRGO(HardwareTimer* timer){
178-
if(timer->getHandle()->Instance == TIM1)
177+
uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer){
178+
if(timer->Instance == TIM1)
179179
return ADC_EXTERNALTRIG_T1_TRGO;
180180
#ifdef TIM2 // if defined timer 2
181-
else if(timer->getHandle()->Instance == TIM2)
181+
else if(timer->Instance == TIM2)
182182
return ADC_EXTERNALTRIG_T2_TRGO;
183183
#endif
184184
#ifdef TIM3 // if defined timer 3
185-
else if(timer->getHandle()->Instance == TIM3)
185+
else if(timer->Instance == TIM3)
186186
return ADC_EXTERNALTRIG_T3_TRGO;
187187
#endif
188188
#ifdef TIM4 // if defined timer 4
189-
else if(timer->getHandle()->Instance == TIM4)
189+
else if(timer->Instance == TIM4)
190190
return ADC_EXTERNALTRIG_T4_TRGO;
191191
#endif
192192
#ifdef TIM6 // if defined timer 6
193-
else if(timer->getHandle()->Instance == TIM6)
193+
else if(timer->Instance == TIM6)
194194
return ADC_EXTERNALTRIG_T6_TRGO;
195195
#endif
196196
#ifdef TIM7 // if defined timer 7
197-
else if(timer->getHandle()->Instance == TIM7)
197+
else if(timer->Instance == TIM7)
198198
return ADC_EXTERNALTRIG_T7_TRGO;
199199
#endif
200200
#ifdef TIM8 // if defined timer 8
201-
else if(timer->getHandle()->Instance == TIM8)
201+
else if(timer->Instance == TIM8)
202202
return ADC_EXTERNALTRIG_T7_TRGO;
203203
#endif
204204
#ifdef TIM15 // if defined timer 15
205-
else if(timer->getHandle()->Instance == TIM15)
205+
else if(timer->Instance == TIM15)
206206
return ADC_EXTERNALTRIG_T15_TRGO;
207207
#endif
208208
#ifdef TIM20 // if defined timer 15
209-
else if(timer->getHandle()->Instance == TIM20)
209+
else if(timer->Instance == TIM20)
210210
return ADC_EXTERNALTRIG_T20_TRGO;
211211
#endif
212212
else

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ uint32_t _getADCChannel(PinName pin);
1919

2020
// timer to injected TRGO
2121
// https://github.com/stm32duino/Arduino_Core_STM32/blob/6588dee03382e73ed42c4a5e473900ab3b79d6e4/system/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_adc_ex.h#L217
22-
uint32_t _timerToInjectedTRGO(HardwareTimer* timer);
22+
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer);
2323

2424
// timer to regular TRGO
2525
// https://github.com/stm32duino/Arduino_Core_STM32/blob/6588dee03382e73ed42c4a5e473900ab3b79d6e4/system/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_adc.h#L519
26-
uint32_t _timerToRegularTRGO(HardwareTimer* timer);
26+
uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer);
2727

2828
// function returning index of the ADC instance
2929
int _adcToIndex(ADC_HandleTypeDef *AdcHandle);

src/drivers/hardware_specific/esp32/esp32_ledc_mcu.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ typedef struct ESP32LEDCDriverParams {
5454
} ESP32LEDCDriverParams;
5555

5656

57+
58+
int esp32_gpio_nr(int pin) {
59+
#if defined(BOARD_HAS_PIN_REMAP) && !defined(BOARD_USES_HW_GPIO_NUMBERS)
60+
return digitalPinToGPIONumber(pin);
61+
#else
62+
return pin;
63+
#endif
64+
}
65+
66+
5767
/*
5868
Function to attach a channel to a pin with advanced settings
5969
- freq - pwm frequency
@@ -96,7 +106,7 @@ bool _ledcAttachChannelAdvanced(uint8_t pin, int _channel, int _group, uint32_t
96106
ledc_channel.channel = channel;
97107
ledc_channel.timer_sel = LEDC_TIMER_0;
98108
ledc_channel.intr_type = LEDC_INTR_DISABLE;
99-
ledc_channel.gpio_num = pin;
109+
ledc_channel.gpio_num = esp32_gpio_nr(pin);
100110
ledc_channel.duty = duty;
101111
ledc_channel.hpoint = 0;
102112
ledc_channel.flags.output_invert = pin_high_level; // 0 is active high, 1 is active low

0 commit comments

Comments
 (0)