2
2
3
3
#if defined(STM32F4xx)
4
4
5
+ // #define SIMPLEFOC_STM32_DEBUG
6
+
5
7
#include " ../../../../communication/SimpleFOCDebug.h"
6
8
#define _TRGO_NOT_AVAILABLE 12345
7
9
8
-
9
- // timer to injected TRGO
10
- // https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h#L179
11
- uint32_t _timerToInjectedTRGO (HardwareTimer* timer){
12
- if (timer->getHandle ()->Instance == TIM1)
13
- return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
14
- #ifdef TIM2 // if defined timer 2
15
- else if (timer->getHandle ()->Instance == TIM2)
16
- return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
17
- #endif
18
- #ifdef TIM4 // if defined timer 4
19
- else if (timer->getHandle ()->Instance == TIM4)
20
- return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
21
- #endif
22
- #ifdef TIM5 // if defined timer 5
23
- else if (timer->getHandle ()->Instance == TIM5)
24
- return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
25
- #endif
26
- else
27
- return _TRGO_NOT_AVAILABLE;
28
- }
29
-
30
- // timer to regular TRGO
31
- // https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h#L331
32
- uint32_t _timerToRegularTRGO (HardwareTimer* timer){
33
- if (timer->getHandle ()->Instance == TIM2)
34
- return ADC_EXTERNALTRIGCONV_T2_TRGO;
35
- #ifdef TIM3 // if defined timer 3
36
- else if (timer->getHandle ()->Instance == TIM3)
37
- return ADC_EXTERNALTRIGCONV_T3_TRGO;
38
- #endif
39
- #ifdef TIM8 // if defined timer 8
40
- else if (timer->getHandle ()->Instance == TIM8)
41
- return ADC_EXTERNALTRIGCONV_T8_TRGO;
42
- #endif
43
- else
44
- return _TRGO_NOT_AVAILABLE;
45
- }
46
-
47
10
ADC_HandleTypeDef hadc;
48
11
49
12
int _adc_init (Stm32CurrentSenseParams* cs_params, const STM32DriverParams* driver_params)
50
13
{
51
14
ADC_InjectionConfTypeDef sConfigInjected ;
15
+
16
+ // check if all pins belong to the same ADC
17
+ ADC_TypeDef* adc_pin1 = (ADC_TypeDef*)pinmap_peripheral (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC);
18
+ ADC_TypeDef* adc_pin2 = (ADC_TypeDef*)pinmap_peripheral (analogInputToPinName (cs_params->pins [1 ]), PinMap_ADC);
19
+ ADC_TypeDef* adc_pin3 = _isset (cs_params->pins [2 ]) ? (ADC_TypeDef*)pinmap_peripheral (analogInputToPinName (cs_params->pins [2 ]), PinMap_ADC) : nullptr ;
20
+ if ( (adc_pin1 != adc_pin2) || ( (adc_pin3) && (adc_pin1 != adc_pin3) )){
21
+ #ifdef SIMPLEFOC_STM32_DEBUG
22
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: Analog pins dont belong to the same ADC!" );
23
+ #endif
24
+ return -1 ;
25
+ }
26
+
27
+
52
28
/* *Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
53
29
*/
54
30
hadc.Instance = (ADC_TypeDef *)pinmap_peripheral (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC);
@@ -67,6 +43,10 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
67
43
return -1 ; // error not a valid ADC instance
68
44
}
69
45
46
+ #ifdef SIMPLEFOC_STM32_DEBUG
47
+ SIMPLEFOC_DEBUG (" STM32-CS: Using ADC: " , _adcToIndex (&hadc)+1 );
48
+ #endif
49
+
70
50
hadc.Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
71
51
hadc.Init .Resolution = ADC_RESOLUTION_12B;
72
52
hadc.Init .ScanConvMode = ENABLE;
@@ -79,13 +59,11 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
79
59
hadc.Init .DMAContinuousRequests = DISABLE;
80
60
hadc.Init .EOCSelection = ADC_EOC_SINGLE_CONV;
81
61
if ( HAL_ADC_Init (&hadc) != HAL_OK){
82
- #ifdef SIMPLEFOC_STM32_DEBUG
62
+ #ifdef SIMPLEFOC_STM32_DEBUG
83
63
SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init ADC!" );
84
- #endif
64
+ #endif
85
65
return -1 ;
86
66
}
87
- /* *Configure for the selected ADC regular channel to be converted.
88
- */
89
67
90
68
/* *Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
91
69
*/
@@ -124,33 +102,35 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
124
102
SIMPLEFOC_DEBUG (" STM32-CS: injected trigger for timer index: " , get_timer_index (cs_params->timer_handle ->getHandle ()->Instance ) + 1 );
125
103
#endif
126
104
105
+
127
106
// first channel
128
- sConfigInjected .InjectedRank = 1 ;
129
- sConfigInjected .InjectedChannel = STM_PIN_CHANNEL ( pinmap_function ( analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC ));
107
+ sConfigInjected .InjectedRank = ADC_INJECTED_RANK_1 ;
108
+ sConfigInjected .InjectedChannel = _getADCChannel ( analogInputToPinName (cs_params->pins [0 ]));
130
109
if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
131
- #ifdef SIMPLEFOC_STM32_DEBUG
132
- SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )STM_PIN_CHANNEL ( pinmap_function ( analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC )) );
133
- #endif
110
+ #ifdef SIMPLEFOC_STM32_DEBUG
111
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )_getADCChannel ( analogInputToPinName (cs_params->pins [0 ])) );
112
+ #endif
134
113
return -1 ;
135
114
}
115
+
136
116
// second channel
137
- sConfigInjected .InjectedRank = 2 ;
138
- sConfigInjected .InjectedChannel = STM_PIN_CHANNEL ( pinmap_function ( analogInputToPinName (cs_params->pins [1 ]), PinMap_ADC ));
117
+ sConfigInjected .InjectedRank = ADC_INJECTED_RANK_2 ;
118
+ sConfigInjected .InjectedChannel = _getADCChannel ( analogInputToPinName (cs_params->pins [1 ]));
139
119
if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
140
- #ifdef SIMPLEFOC_STM32_DEBUG
141
- SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )STM_PIN_CHANNEL ( pinmap_function ( analogInputToPinName (cs_params->pins [1 ]), PinMap_ADC )) ) ;
142
- #endif
120
+ #ifdef SIMPLEFOC_STM32_DEBUG
121
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )_getADCChannel ( analogInputToPinName (cs_params->pins [1 ]))) ;
122
+ #endif
143
123
return -1 ;
144
124
}
145
125
146
126
// third channel - if exists
147
127
if (_isset (cs_params->pins [2 ])){
148
- sConfigInjected .InjectedRank = 3 ;
149
- sConfigInjected .InjectedChannel = STM_PIN_CHANNEL ( pinmap_function ( analogInputToPinName (cs_params->pins [2 ]), PinMap_ADC ));
128
+ sConfigInjected .InjectedRank = ADC_INJECTED_RANK_3 ;
129
+ sConfigInjected .InjectedChannel = _getADCChannel ( analogInputToPinName (cs_params->pins [2 ]));
150
130
if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
151
- #ifdef SIMPLEFOC_STM32_DEBUG
152
- SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )STM_PIN_CHANNEL ( pinmap_function ( analogInputToPinName (cs_params->pins [2 ]), PinMap_ADC )) ) ;
153
- #endif
131
+ #ifdef SIMPLEFOC_STM32_DEBUG
132
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )_getADCChannel ( analogInputToPinName (cs_params->pins [2 ]))) ;
133
+ #endif
154
134
return -1 ;
155
135
}
156
136
}
0 commit comments