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- 
429ADC_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    }
0 commit comments