3
3
#if defined(STM32F1xx)
4
4
5
5
#include " ../../../../communication/SimpleFOCDebug.h"
6
+ #include " ../stm32_adc_utils.h"
6
7
#define _TRGO_NOT_AVAILABLE 12345
7
8
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
-
42
9
ADC_HandleTypeDef hadc;
43
10
44
11
/* *
@@ -55,7 +22,8 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
55
22
56
23
/* *Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
57
24
*/
58
- hadc.Instance = (ADC_TypeDef *)pinmap_peripheral (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC);
25
+ hadc.Instance = _findBestADCForPins (3 , cs_params->pins );
26
+
59
27
if (hadc.Instance == ADC1) __HAL_RCC_ADC1_CLK_ENABLE ();
60
28
#ifdef ADC2 // if defined ADC2
61
29
else if (hadc.Instance == ADC2) __HAL_RCC_ADC2_CLK_ENABLE ();
@@ -82,7 +50,12 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
82
50
83
51
/* *Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
84
52
*/
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
+ }
86
59
sConfigInjected .InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5;
87
60
sConfigInjected .AutoInjectedConv = DISABLE;
88
61
sConfigInjected .InjectedDiscontinuousConvMode = DISABLE;
@@ -123,16 +96,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
123
96
}
124
97
125
98
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 ;
127
100
for (int i=0 ; i<3 ; i++){
128
101
// skip if not set
129
102
if (!_isset (cs_params->pins [i])) continue ;
130
103
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 );
133
106
if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
134
107
#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 ));
136
109
#endif
137
110
return -1 ;
138
111
}
0 commit comments