5
5
#include " ../../../../communication/SimpleFOCDebug.h"
6
6
#define _TRGO_NOT_AVAILABLE 12345
7
7
8
+ #define SIMPLEFOC_STM32_DEBUG
8
9
9
10
// timer to injected TRGO
10
11
// https://github.com/stm32duino/Arduino_Core_STM32/blob/6588dee03382e73ed42c4a5e473900ab3b79d6e4/system/Drivers/STM32G4xx_HAL_Driver/Inc/stm32g4xx_hal_adc_ex.h#L217
@@ -16,7 +17,7 @@ uint32_t _timerToInjectedTRGO(HardwareTimer* timer){
16
17
return ADC_EXTERNALTRIGINJEC_T2_TRGO;
17
18
#endif
18
19
#ifdef TIM3 // if defined timer 3
19
- else if (timer->getHandle ()->Instance == TIM2 )
20
+ else if (timer->getHandle ()->Instance == TIM3 )
20
21
return ADC_EXTERNALTRIGINJEC_T3_TRGO;
21
22
#endif
22
23
#ifdef TIM4 // if defined timer 4
@@ -57,7 +58,7 @@ uint32_t _timerToRegularTRGO(HardwareTimer* timer){
57
58
return ADC_EXTERNALTRIG_T2_TRGO;
58
59
#endif
59
60
#ifdef TIM3 // if defined timer 3
60
- else if (timer->getHandle ()->Instance == TIM2 )
61
+ else if (timer->getHandle ()->Instance == TIM3 )
61
62
return ADC_EXTERNALTRIG_T3_TRGO;
62
63
#endif
63
64
#ifdef TIM4 // if defined timer 4
@@ -96,8 +97,10 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
96
97
/* *Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
97
98
*/
98
99
hadc.Instance = (ADC_TypeDef *)pinmap_peripheral (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC);
99
-
100
+ SIMPLEFOC_DEBUG (" here adc_init!" );
101
+
100
102
if (hadc.Instance == ADC1) {
103
+ SIMPLEFOC_DEBUG (" adc1" );
101
104
#ifdef __HAL_RCC_ADC1_CLK_ENABLE
102
105
__HAL_RCC_ADC1_CLK_ENABLE ();
103
106
#endif
@@ -107,6 +110,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
107
110
}
108
111
#ifdef ADC2
109
112
else if (hadc.Instance == ADC2) {
113
+ SIMPLEFOC_DEBUG (" adc2" );
110
114
#ifdef __HAL_RCC_ADC2_CLK_ENABLE
111
115
__HAL_RCC_ADC2_CLK_ENABLE ();
112
116
#endif
@@ -117,6 +121,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
117
121
#endif
118
122
#ifdef ADC3
119
123
else if (hadc.Instance == ADC3) {
124
+ SIMPLEFOC_DEBUG (" adc3" );
120
125
#ifdef __HAL_RCC_ADC3_CLK_ENABLE
121
126
__HAL_RCC_ADC3_CLK_ENABLE ();
122
127
#endif
@@ -135,18 +140,27 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
135
140
return -1 ; // error not a valid ADC instance
136
141
}
137
142
138
- hadc.Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
143
+ SIMPLEFOC_DEBUG (" here configure start!" );
144
+ hadc.Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
139
145
hadc.Init .Resolution = ADC_RESOLUTION_12B;
140
- hadc.Init .ScanConvMode = ENABLE;
141
- hadc.Init .ContinuousConvMode = ENABLE;
142
- hadc.Init .DiscontinuousConvMode = DISABLE;
143
- hadc.Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
144
- hadc.Init .ExternalTrigConv = ADC_SOFTWARE_START; // for now
145
- hadc.Init .DataAlign = ADC_DATAALIGN_RIGHT;
146
- hadc.Init .NbrOfConversion = 1 ;
147
- hadc.Init .DMAContinuousRequests = DISABLE;
148
- hadc.Init .EOCSelection = ADC_EOC_SINGLE_CONV;
149
- HAL_ADC_Init (&hadc);
146
+ // hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
147
+ // hadc.Init.ContinuousConvMode = DISABLE;
148
+ // hadc.Init.LowPowerAutoWait = DISABLE;
149
+ // hadc.Init.GainCompensation = 0;
150
+ // hadc.Init.DiscontinuousConvMode = DISABLE;
151
+ // hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
152
+ // hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; // for now
153
+ // hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
154
+ // hadc.Init.NbrOfConversion = 1;
155
+ // hadc.Init.DMAContinuousRequests = DISABLE;
156
+ // hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
157
+ // hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
158
+ if ( HAL_ADC_Init (&hadc) != HAL_OK){
159
+ #ifdef SIMPLEFOC_STM32_DEBUG
160
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init ADC!" );
161
+ #endif
162
+ return -1 ;
163
+ }
150
164
/* *Configure for the selected ADC regular channel to be converted.
151
165
*/
152
166
@@ -155,10 +169,13 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
155
169
sConfigInjected .InjectedNbrOfConversion = _isset (cs_params->pins [2 ]) ? 3 : 2 ;
156
170
sConfigInjected .InjectedSamplingTime = ADC_SAMPLETIME_2CYCLES_5;
157
171
sConfigInjected .ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONV_EDGE_RISING;
158
- sConfigInjected .AutoInjectedConv = DISABLE;
159
- sConfigInjected .InjectedDiscontinuousConvMode = DISABLE;
160
- sConfigInjected .InjectedOffset = 0 ;
172
+ // sConfigInjected.AutoInjectedConv = DISABLE;
173
+ // sConfigInjected.InjectedSingleDiff = ADC_SINGLE_ENDED;
174
+ // sConfigInjected.InjectedDiscontinuousConvMode = DISABLE;
175
+ // sConfigInjected.InjectedOffsetNumber = ADC_OFFSET_NONE;
176
+ // sConfigInjected.InjectedOffset = 0;
161
177
178
+ SIMPLEFOC_DEBUG (" here timer search start!" );
162
179
// automating TRGO flag finding - hardware specific
163
180
uint8_t tim_num = 0 ;
164
181
while (driver_params->timers [tim_num] != NP && tim_num < 6 ){
@@ -176,33 +193,51 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
176
193
}
177
194
if ( cs_params->timer_handle == NP ){
178
195
// not possible to use these timers for low-side current sense
179
- #ifdef SIMPLEFOC_STM32_DEBUG
196
+ #ifdef SIMPLEFOC_STM32_DEBUG
180
197
SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot sync any timer to injected channels!" );
181
- #endif
198
+ #endif
182
199
return -1 ;
183
200
}
184
201
// display which timer is being used
185
- #ifdef SIMPLEFOC_STM32_DEBUG
202
+ #ifdef SIMPLEFOC_STM32_DEBUG
186
203
// it would be better to use the getTimerNumber from driver
187
204
SIMPLEFOC_DEBUG (" STM32-CS: injected trigger for timer index: " , get_timer_index (cs_params->timer_handle ->getHandle ()->Instance ) + 1 );
188
- #endif
205
+ #endif
206
+
207
+ SIMPLEFOC_DEBUG (" here timer search done!" );
208
+ SIMPLEFOC_DEBUG (" here injected config done!" );
189
209
190
210
// first channel
191
- sConfigInjected .InjectedRank = 1 ;
211
+ sConfigInjected .InjectedRank = ADC_INJECTED_RANK_1 ;
192
212
sConfigInjected .InjectedChannel = STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC));
193
- HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected );
213
+ if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
214
+ #ifdef SIMPLEFOC_STM32_DEBUG
215
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC)) );
216
+ #endif
217
+ return -1 ;
218
+ }
194
219
// second channel
195
- sConfigInjected .InjectedRank = 2 ;
220
+ sConfigInjected .InjectedRank = ADC_INJECTED_RANK_2 ;
196
221
sConfigInjected .InjectedChannel = STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [1 ]), PinMap_ADC));
197
- HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected );
198
-
222
+ if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
223
+ #ifdef SIMPLEFOC_STM32_DEBUG
224
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [1 ]), PinMap_ADC)) );
225
+ #endif
226
+ return -1 ;
227
+ }
199
228
// third channel - if exists
200
229
if (_isset (cs_params->pins [2 ])){
201
- sConfigInjected .InjectedRank = 3 ;
230
+ sConfigInjected .InjectedRank = ADC_INJECTED_RANK_3 ;
202
231
sConfigInjected .InjectedChannel = STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [2 ]), PinMap_ADC));
203
- HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected );
232
+ if (HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected ) != HAL_OK){
233
+ #ifdef SIMPLEFOC_STM32_DEBUG
234
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot init injected channel: " , (int )STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [2 ]), PinMap_ADC)) );
235
+ #endif
236
+ return -1 ;
237
+ }
204
238
}
205
239
240
+ SIMPLEFOC_DEBUG (" here injected config done!" );
206
241
// enable interrupt
207
242
HAL_NVIC_SetPriority (ADC1_2_IRQn, 0 , 0 );
208
243
HAL_NVIC_EnableIRQ (ADC1_2_IRQn);
@@ -229,7 +264,7 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
229
264
}
230
265
231
266
extern " C" {
232
- void ADC_IRQHandler (void )
267
+ void ADC1_2_IRQHandler (void )
233
268
{
234
269
HAL_ADC_IRQHandler (&hadc);
235
270
}
0 commit comments