Skip to content

Commit 162322f

Browse files
committed
giMerge branch '317-feature-foc_current-mode-and-current-feed-forward' into feat_h7_current_sensing
2 parents 2857d3e + b9b9cf3 commit 162322f

File tree

4 files changed

+59
-54
lines changed

4 files changed

+59
-54
lines changed

src/current_sense/hardware_specific/stm32/stm32_adc_utils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "stm32_adc_utils.h"
22

3-
#if defined(_STM32_DEF_)
3+
#if defined(_STM32_DEF_)
44

55
// for searching the best ADCs, we need to know the number of ADCs
66
// it might be better to use some HAL variable for example ADC_COUNT

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

+13-40
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,9 @@
33
#if defined(STM32F1xx)
44

55
#include "../../../../communication/SimpleFOCDebug.h"
6+
#include "../stm32_adc_utils.h"
67
#define _TRGO_NOT_AVAILABLE 12345
78

8-
// timer to injected TRGO
9-
// 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(TIM_HandleTypeDef* timer){
11-
if(timer->Instance == TIM1)
12-
return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
13-
#ifdef TIM2 // if defined timer 2
14-
else if(timer->Instance == TIM2)
15-
return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
16-
#endif
17-
#ifdef TIM4 // if defined timer 4
18-
else if(timer->Instance == TIM4)
19-
return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
20-
#endif
21-
#ifdef TIM5 // if defined timer 5
22-
else if(timer->Instance == TIM5)
23-
return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
24-
#endif
25-
else
26-
return _TRGO_NOT_AVAILABLE;
27-
}
28-
29-
// timer to regular TRGO
30-
// 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(TIM_HandleTypeDef* timer){
32-
if(timer->Instance == TIM3)
33-
return ADC_EXTERNALTRIGCONV_T3_TRGO;
34-
#ifdef TIM8 // if defined timer 8
35-
else if(timer->Instance == TIM8)
36-
return ADC_EXTERNALTRIGCONV_T8_TRGO;
37-
#endif
38-
else
39-
return _TRGO_NOT_AVAILABLE;
40-
}
41-
429
ADC_HandleTypeDef hadc;
4310

4411
/**
@@ -55,7 +22,8 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
5522

5623
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
5724
*/
58-
hadc.Instance = (ADC_TypeDef *)pinmap_peripheral(analogInputToPinName(cs_params->pins[0]), PinMap_ADC);
25+
hadc.Instance = _findBestADCForPins(3, cs_params->pins);
26+
5927
if(hadc.Instance == ADC1) __HAL_RCC_ADC1_CLK_ENABLE();
6028
#ifdef ADC2 // if defined ADC2
6129
else if(hadc.Instance == ADC2) __HAL_RCC_ADC2_CLK_ENABLE();
@@ -82,7 +50,12 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
8250

8351
/**Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
8452
*/
85-
sConfigInjected.InjectedNbrOfConversion = _isset(cs_params->pins[2]) ? 3 : 2;
53+
sConfigInjected.InjectedNbrOfConversion = 0;
54+
for(int pin_no=0; pin_no<3; pin_no++){
55+
if(_isset(cs_params->pins[pin_no])){
56+
sConfigInjected.InjectedNbrOfConversion++;
57+
}
58+
}
8659
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5;
8760
sConfigInjected.AutoInjectedConv = DISABLE;
8861
sConfigInjected.InjectedDiscontinuousConvMode = DISABLE;
@@ -123,16 +96,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
12396
}
12497

12598

126-
uint32_t ranks[4]= {ADC_INJECTED_RANK_1, ADC_INJECTED_RANK_2, ADC_INJECTED_RANK_3, ADC_INJECTED_RANK_4};
99+
uint8_t channel_no = 0;
127100
for(int i=0; i<3; i++){
128101
// skip if not set
129102
if (!_isset(cs_params->pins[i])) continue;
130103

131-
sConfigInjected.InjectedRank = ranks[i];
132-
sConfigInjected.InjectedChannel = STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[i]), PinMap_ADC));
104+
sConfigInjected.InjectedRank = _getADCInjectedRank(channel_no++);
105+
sConfigInjected.InjectedChannel = _getADCChannel(analogInputToPinName(cs_params->pins[i]), hadc.Instance);
133106
if (HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected) != HAL_OK){
134107
#ifdef SIMPLEFOC_STM32_DEBUG
135-
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[i]), PinMap_ADC)));
108+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init injected channel: ", (int)_getADCChannel(analogInputToPinName(cs_params->pins[i]) , hadc.Instance));
136109
#endif
137110
return -1;
138111
}

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

+5-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../../../../drivers/hardware_api.h"
66
#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
77
#include "../../../hardware_api.h"
8+
#include "../stm32_adc_utils.h"
89
#include "../stm32_mcu.h"
910
#include "stm32f1_hal.h"
1011
#include "Arduino.h"
@@ -25,17 +26,6 @@ uint8_t use_adc_interrupt = 1;
2526
uint8_t use_adc_interrupt = 0;
2627
#endif
2728

28-
int _adcToIndex(ADC_HandleTypeDef *AdcHandle){
29-
if(AdcHandle->Instance == ADC1) return 0;
30-
#ifdef ADC2 // if ADC2 exists
31-
else if(AdcHandle->Instance == ADC2) return 1;
32-
#endif
33-
#ifdef ADC3 // if ADC3 exists
34-
else if(AdcHandle->Instance == ADC3) return 2;
35-
#endif
36-
return 0;
37-
}
38-
3929
void* _configureADCLowSide(const void* driver_params, const int pinA, const int pinB, const int pinC){
4030

4131
Stm32CurrentSenseParams* cs_params= new Stm32CurrentSenseParams {
@@ -107,16 +97,18 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
10797

10898
// function reading an ADC value and returning the read voltage
10999
float _readADCVoltageLowSide(const int pin, const void* cs_params){
100+
uint8_t channel_no = 0;
110101
for(int i=0; i < 3; i++){
111102
if( pin == ((Stm32CurrentSenseParams*)cs_params)->pins[i]){ // found in the buffer
112103
if (use_adc_interrupt){
113-
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][i] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
104+
return adc_val[_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle)][channel_no] * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
114105
}else{
115106
// an optimized way to go from i to the channel i=0 -> channel 1, i=1 -> channel 2, i=2 -> channel 3
116-
uint32_t channel = (i == 0) ? ADC_INJECTED_RANK_1 : (i == 1) ? ADC_INJECTED_RANK_2 : ADC_INJECTED_RANK_3;;
107+
uint32_t channel = _getADCInjectedRank(channel_no);
117108
return HAL_ADCEx_InjectedGetValue(((Stm32CurrentSenseParams*)cs_params)->adc_handle, channel) * ((Stm32CurrentSenseParams*)cs_params)->adc_voltage_conv;
118109
}
119110
}
111+
if(_isset(((Stm32CurrentSenseParams*)cs_params)->pins[i])) channel_no++;
120112
}
121113
return 0;
122114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "../stm32_adc_utils.h"
2+
3+
#if defined(STM32F1xx)
4+
5+
6+
// timer to injected TRGO
7+
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_adc_ex.h#L215
8+
uint32_t _timerToInjectedTRGO(TIM_HandleTypeDef* timer){
9+
if(timer->Instance == TIM1)
10+
return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
11+
#ifdef TIM2 // if defined timer 2
12+
else if(timer->Instance == TIM2)
13+
return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
14+
#endif
15+
#ifdef TIM4 // if defined timer 4
16+
else if(timer->Instance == TIM4)
17+
return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
18+
#endif
19+
#ifdef TIM5 // if defined timer 5
20+
else if(timer->Instance == TIM5)
21+
return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
22+
#endif
23+
else
24+
return _TRGO_NOT_AVAILABLE;
25+
}
26+
27+
// timer to regular TRGO
28+
// https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_adc_ex.h#L215
29+
uint32_t _timerToRegularTRGO(TIM_HandleTypeDef* timer){
30+
if(timer->Instance == TIM3)
31+
return ADC_EXTERNALTRIGCONV_T3_TRGO;
32+
#ifdef TIM8 // if defined timer 8
33+
else if(timer->Instance == TIM8)
34+
return ADC_EXTERNALTRIGCONV_T8_TRGO;
35+
#endif
36+
else
37+
return _TRGO_NOT_AVAILABLE;
38+
}
39+
40+
#endif

0 commit comments

Comments
 (0)