@@ -22,7 +22,8 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
22
22
23
23
/* *Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
24
24
*/
25
- hadc.Instance = (ADC_TypeDef *)pinmap_peripheral (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC);
25
+ hadc.Instance = _findBestADCForPins (3 , cs_params->pins );
26
+
26
27
if (hadc.Instance == ADC1) __HAL_RCC_ADC1_CLK_ENABLE ();
27
28
#ifdef ADC2 // if defined ADC2
28
29
else if (hadc.Instance == ADC2) __HAL_RCC_ADC2_CLK_ENABLE ();
@@ -49,7 +50,12 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
49
50
50
51
/* *Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
51
52
*/
52
- 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
+ }
53
59
sConfigInjected .InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5;
54
60
sConfigInjected .AutoInjectedConv = DISABLE;
55
61
sConfigInjected .InjectedDiscontinuousConvMode = DISABLE;
@@ -90,16 +96,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
90
96
}
91
97
92
98
93
- 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 ;
94
100
for (int i=0 ; i<3 ; i++){
95
101
// skip if not set
96
102
if (!_isset (cs_params->pins [i])) continue ;
97
103
98
- sConfigInjected .InjectedRank = ranks[i] ;
99
- 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 );
100
106
if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
101
107
#ifdef SIMPLEFOC_STM32_DEBUG
102
- 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 ));
103
109
#endif
104
110
return -1 ;
105
111
}
0 commit comments