@@ -22,7 +22,8 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
2222
2323 /* *Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
2424 */
25- hadc.Instance = (ADC_TypeDef *)pinmap_peripheral (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC);
25+ hadc.Instance = _findBestADCForPins (3 , cs_params->pins );
26+
2627 if (hadc.Instance == ADC1) __HAL_RCC_ADC1_CLK_ENABLE ();
2728#ifdef ADC2 // if defined ADC2
2829 else if (hadc.Instance == ADC2) __HAL_RCC_ADC2_CLK_ENABLE ();
@@ -49,7 +50,12 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
4950
5051 /* *Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
5152 */
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+ }
5359 sConfigInjected .InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5;
5460 sConfigInjected .AutoInjectedConv = DISABLE;
5561 sConfigInjected .InjectedDiscontinuousConvMode = DISABLE;
@@ -90,16 +96,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
9096 }
9197
9298
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 ;
94100 for (int i=0 ; i<3 ; i++){
95101 // skip if not set
96102 if (!_isset (cs_params->pins [i])) continue ;
97103
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 );
100106 if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
101107 #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 ));
103109 #endif
104110 return -1 ;
105111 }
0 commit comments