Skip to content

Commit db09787

Browse files
committed
Merge branch 'stm32_hal_pwm' into dev
2 parents 20e4481 + 6473a17 commit db09787

29 files changed

+1828
-1034
lines changed

src/current_sense/hardware_specific/stm32/b_g431/b_g431_mcu.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
132132
Stm32CurrentSenseParams* params = new Stm32CurrentSenseParams {
133133
.pins = { pinA, pinB, pinC },
134134
.adc_voltage_conv = (_ADC_VOLTAGE) / (_ADC_RESOLUTION),
135-
.timer_handle = (HardwareTimer *)(HardwareTimer_Handle[get_timer_index(TIM1)]->__this)
135+
.timer_handle = ((STM32DriverParams*)driver_params)->timers_handle[0],
136136
};
137137

138138
return params;
@@ -153,21 +153,21 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
153153
Stm32CurrentSenseParams* cs_params = (Stm32CurrentSenseParams*)_cs_params;
154154

155155
// stop all the timers for the driver
156-
_stopTimers(driver_params->timers, 6);
156+
stm32_pause(driver_params);
157157

158158
// if timer has repetition counter - it will downsample using it
159159
// and it does not need the software downsample
160-
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance) ){
160+
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->Instance) ){
161161
// adjust the initial timer state such that the trigger for DMA transfer aligns with the pwm peaks instead of throughs.
162162
// only necessary for the timers that have repetition counters
163-
cs_params->timer_handle->getHandle()->Instance->CR1 |= TIM_CR1_DIR;
164-
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
163+
cs_params->timer_handle->Instance->CR1 |= TIM_CR1_DIR;
164+
cs_params->timer_handle->Instance->CNT = cs_params->timer_handle->Instance->ARR;
165165
}
166166
// set the trigger output event
167-
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
167+
LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
168168

169169
// restart all the timers of the driver
170-
_startTimers(driver_params->timers, 6);
170+
stm32_resume(driver_params);
171171

172172
// return the cs parameters
173173
// successfully initialized

src/current_sense/hardware_specific/stm32/stm32_mcu.h

+1-1
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/stm32f1/stm32f1_hal.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
// timer to injected TRGO
99
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_adc_ex.h#L215
10-
uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
11-
if(timer->getHandle()->Instance == TIM1)
10+
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer){
11+
if(timer->Instance == TIM1)
1212
return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
1313
#ifdef TIM2 // if defined timer 2
14-
else if(timer->getHandle()->Instance == TIM2)
14+
else if(timer->Instance == TIM2)
1515
return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
1616
#endif
1717
#ifdef TIM4 // if defined timer 4
18-
else if(timer->getHandle()->Instance == TIM4)
18+
else if(timer->Instance == TIM4)
1919
return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
2020
#endif
2121
#ifdef TIM5 // if defined timer 5
22-
else if(timer->getHandle()->Instance == TIM5)
22+
else if(timer->Instance == TIM5)
2323
return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
2424
#endif
2525
else
@@ -28,11 +28,11 @@ uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
2828

2929
// timer to regular TRGO
3030
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_adc_ex.h#L215
31-
uint32_t _timerToRegularTRGO(HardwareTimer* timer){
32-
if(timer->getHandle()->Instance == TIM3)
31+
uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer){
32+
if(timer->Instance == TIM3)
3333
return ADC_EXTERNALTRIGCONV_T3_TRGO;
3434
#ifdef TIM8 // if defined timer 8
35-
else if(timer->getHandle()->Instance == TIM8)
35+
else if(timer->Instance == TIM8)
3636
return ADC_EXTERNALTRIGCONV_T8_TRGO;
3737
#endif
3838
else
@@ -82,16 +82,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
8282

8383
// automating TRGO flag finding - hardware specific
8484
uint8_t tim_num = 0;
85-
while(driver_params->timers[tim_num] != NP && tim_num < 6){
86-
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers[tim_num++]);
85+
while(driver_params->timers_handle[tim_num] != NP && tim_num < 6){
86+
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers_handle[tim_num++]);
8787
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
8888

8989
// if the code comes here, it has found the timer available
9090
// timer does have trgo flag for injected channels
9191
sConfigInjected.ExternalTrigInjecConv = trigger_flag;
9292

9393
// this will be the timer with which the ADC will sync
94-
cs_params->timer_handle = driver_params->timers[tim_num-1];
94+
cs_params->timer_handle = driver_params->timers_handle[tim_num-1];
9595
// done
9696
break;
9797
}
@@ -105,7 +105,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
105105
// display which timer is being used
106106
#ifdef SIMPLEFOC_STM32_DEBUG
107107
// it would be better to use the getTimerNumber from driver
108-
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->getHandle()->Instance) + 1);
108+
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->Instance) + 1);
109109
#endif
110110

111111
// first channel

src/current_sense/hardware_specific/stm32/stm32f1/stm32f1_mcu.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
5656
if (cs_params->timer_handle == NULL) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
5757

5858
// stop all the timers for the driver
59-
_stopTimers(driver_params->timers, 6);
59+
stm32_pause(driver_params);
6060

6161
// if timer has repetition counter - it will downsample using it
6262
// and it does not need the software downsample
63-
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance) ){
63+
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->Instance) ){
6464
// adjust the initial timer state such that the trigger
6565
// - for DMA transfer aligns with the pwm peaks instead of throughs.
6666
// - for interrupt based ADC transfer
6767
// - only necessary for the timers that have repetition counters
68-
cs_params->timer_handle->getHandle()->Instance->CR1 |= TIM_CR1_DIR;
69-
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
68+
cs_params->timer_handle->Instance->CR1 |= TIM_CR1_DIR;
69+
cs_params->timer_handle->Instance->CNT = cs_params->timer_handle->Instance->ARR;
7070
// remember that this timer has repetition counter - no need to downasmple
7171
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
7272
}else{
@@ -79,7 +79,7 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
7979
}
8080
}
8181
// set the trigger output event
82-
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
82+
LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
8383

8484
// Start the adc calibration
8585
HAL_ADCEx_Calibration_Start(cs_params->adc_handle);
@@ -96,7 +96,7 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
9696

9797

9898
// restart all the timers of the driver
99-
_startTimers(driver_params->timers, 6);
99+
stm32_resume(driver_params);
100100

101101
// return the cs parameters
102102
// successfully initialized

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

+4-4
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

+6-6
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

+9-9
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

+2-2
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/stm32f7/stm32f7_hal.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
7777
// automating TRGO flag finding - hardware specific
7878
uint8_t tim_num = 0;
7979
for (size_t i=0; i<6; i++) {
80-
HardwareTimer *timer_to_check = driver_params->timers[tim_num++];
81-
TIM_TypeDef *instance_to_check = timer_to_check->getHandle()->Instance;
80+
TIM_HandleTypeDef *timer_to_check = driver_params->timers_handle[tim_num++];
81+
TIM_TypeDef *instance_to_check = timer_to_check->Instance;
8282

8383
// bool TRGO_already_configured = instance_to_check->CR2 & LL_TIM_TRGO_UPDATE;
8484
// if(TRGO_already_configured) continue;
@@ -110,7 +110,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
110110
// display which timer is being used
111111
#ifdef SIMPLEFOC_STM32_DEBUG
112112
// it would be better to use the getTimerNumber from driver
113-
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->getHandle()->Instance) + 1);
113+
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->Instance) + 1);
114114
#endif
115115

116116

src/current_sense/hardware_specific/stm32/stm32f7/stm32f7_mcu.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
4242
if (cs_params->timer_handle == NULL) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
4343

4444
// stop all the timers for the driver
45-
_stopTimers(driver_params->timers, 6);
45+
stm32_pause(driver_params);
4646

4747
// if timer has repetition counter - it will downsample using it
4848
// and it does not need the software downsample
49-
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance) ){
49+
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->Instance) ){
5050
// adjust the initial timer state such that the trigger
5151
// - for DMA transfer aligns with the pwm peaks instead of throughs.
5252
// - for interrupt based ADC transfer
5353
// - only necessary for the timers that have repetition counters
5454

55-
cs_params->timer_handle->getHandle()->Instance->CR1 |= TIM_CR1_DIR;
56-
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
55+
cs_params->timer_handle->Instance->CR1 |= TIM_CR1_DIR;
56+
cs_params->timer_handle->Instance->CNT = cs_params->timer_handle->Instance->ARR;
5757
// remember that this timer has repetition counter - no need to downasmple
5858
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
5959
}
6060
// set the trigger output event
61-
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
61+
LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
6262

6363
// start the adc
6464
#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
@@ -68,7 +68,7 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
6868
#endif
6969

7070
// restart all the timers of the driver
71-
_startTimers(driver_params->timers, 6);
71+
stm32_resume(driver_params);
7272

7373
// return the cs parameters
7474
// successfully initialized

0 commit comments

Comments
 (0)