Skip to content

Commit 4a8f8de

Browse files
committed
g4 trials
1 parent 11a2f12 commit 4a8f8de

File tree

3 files changed

+113
-50
lines changed

3 files changed

+113
-50
lines changed

src/current_sense/hardware_specific/stm32/stm32f4/stm32f4_hal.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
7878
hadc.Init.NbrOfConversion = 1;
7979
hadc.Init.DMAContinuousRequests = DISABLE;
8080
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
81-
HAL_ADC_Init(&hadc);
81+
if ( HAL_ADC_Init(&hadc) != HAL_OK){
82+
#ifdef SIMPLEFOC_STM32_DEBUG
83+
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot init ADC!");
84+
#endif
85+
return -1;
86+
}
8287
/**Configure for the selected ADC regular channel to be converted.
8388
*/
8489

@@ -122,17 +127,32 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
122127
// first channel
123128
sConfigInjected.InjectedRank = 1;
124129
sConfigInjected.InjectedChannel = STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[0]), PinMap_ADC));
125-
HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected);
130+
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
134+
return -1;
135+
}
126136
// second channel
127137
sConfigInjected.InjectedRank = 2;
128138
sConfigInjected.InjectedChannel = STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[1]), PinMap_ADC));
129-
HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected);
139+
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
143+
return -1;
144+
}
130145

131146
// third channel - if exists
132147
if(_isset(cs_params->pins[2])){
133148
sConfigInjected.InjectedRank = 3;
134149
sConfigInjected.InjectedChannel = STM_PIN_CHANNEL(pinmap_function(analogInputToPinName(cs_params->pins[2]), PinMap_ADC));
135-
HAL_ADCEx_InjectedConfigChannel(&hadc, &sConfigInjected);
150+
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
154+
return -1;
155+
}
136156
}
137157

138158
// enable interrupt

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_hal.cpp

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../../../../communication/SimpleFOCDebug.h"
66
#define _TRGO_NOT_AVAILABLE 12345
77

8+
#define SIMPLEFOC_STM32_DEBUG
89

910
// timer to injected TRGO
1011
// 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){
1617
return ADC_EXTERNALTRIGINJEC_T2_TRGO;
1718
#endif
1819
#ifdef TIM3 // if defined timer 3
19-
else if(timer->getHandle()->Instance == TIM2)
20+
else if(timer->getHandle()->Instance == TIM3)
2021
return ADC_EXTERNALTRIGINJEC_T3_TRGO;
2122
#endif
2223
#ifdef TIM4 // if defined timer 4
@@ -57,7 +58,7 @@ uint32_t _timerToRegularTRGO(HardwareTimer* timer){
5758
return ADC_EXTERNALTRIG_T2_TRGO;
5859
#endif
5960
#ifdef TIM3 // if defined timer 3
60-
else if(timer->getHandle()->Instance == TIM2)
61+
else if(timer->getHandle()->Instance == TIM3)
6162
return ADC_EXTERNALTRIG_T3_TRGO;
6263
#endif
6364
#ifdef TIM4 // if defined timer 4
@@ -96,8 +97,10 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
9697
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
9798
*/
9899
hadc.Instance = (ADC_TypeDef *)pinmap_peripheral(analogInputToPinName(cs_params->pins[0]), PinMap_ADC);
99-
100+
SIMPLEFOC_DEBUG("here adc_init!");
101+
100102
if(hadc.Instance == ADC1) {
103+
SIMPLEFOC_DEBUG("adc1");
101104
#ifdef __HAL_RCC_ADC1_CLK_ENABLE
102105
__HAL_RCC_ADC1_CLK_ENABLE();
103106
#endif
@@ -107,6 +110,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
107110
}
108111
#ifdef ADC2
109112
else if (hadc.Instance == ADC2) {
113+
SIMPLEFOC_DEBUG("adc2");
110114
#ifdef __HAL_RCC_ADC2_CLK_ENABLE
111115
__HAL_RCC_ADC2_CLK_ENABLE();
112116
#endif
@@ -117,6 +121,7 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
117121
#endif
118122
#ifdef ADC3
119123
else if (hadc.Instance == ADC3) {
124+
SIMPLEFOC_DEBUG("adc3");
120125
#ifdef __HAL_RCC_ADC3_CLK_ENABLE
121126
__HAL_RCC_ADC3_CLK_ENABLE();
122127
#endif
@@ -135,18 +140,27 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
135140
return -1; // error not a valid ADC instance
136141
}
137142

138-
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
143+
SIMPLEFOC_DEBUG("here configure start!");
144+
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
139145
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+
}
150164
/**Configure for the selected ADC regular channel to be converted.
151165
*/
152166

@@ -155,10 +169,13 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
155169
sConfigInjected.InjectedNbrOfConversion = _isset(cs_params->pins[2]) ? 3 : 2;
156170
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_2CYCLES_5;
157171
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;
161177

178+
SIMPLEFOC_DEBUG("here timer search start!");
162179
// automating TRGO flag finding - hardware specific
163180
uint8_t tim_num = 0;
164181
while(driver_params->timers[tim_num] != NP && tim_num < 6){
@@ -176,33 +193,51 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
176193
}
177194
if( cs_params->timer_handle == NP ){
178195
// not possible to use these timers for low-side current sense
179-
#ifdef SIMPLEFOC_STM32_DEBUG
196+
#ifdef SIMPLEFOC_STM32_DEBUG
180197
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot sync any timer to injected channels!");
181-
#endif
198+
#endif
182199
return -1;
183200
}
184201
// display which timer is being used
185-
#ifdef SIMPLEFOC_STM32_DEBUG
202+
#ifdef SIMPLEFOC_STM32_DEBUG
186203
// it would be better to use the getTimerNumber from driver
187204
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!");
189209

190210
// first channel
191-
sConfigInjected.InjectedRank = 1;
211+
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_1;
192212
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+
}
194219
// second channel
195-
sConfigInjected.InjectedRank = 2;
220+
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_2;
196221
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+
}
199228
// third channel - if exists
200229
if(_isset(cs_params->pins[2])){
201-
sConfigInjected.InjectedRank = 3;
230+
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_3;
202231
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+
}
204238
}
205239

240+
SIMPLEFOC_DEBUG("here injected config done!");
206241
// enable interrupt
207242
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
208243
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
@@ -229,7 +264,7 @@ void _adc_gpio_init(Stm32CurrentSenseParams* cs_params, const int pinA, const in
229264
}
230265

231266
extern "C" {
232-
void ADC_IRQHandler(void)
267+
void ADC1_2_IRQHandler(void)
233268
{
234269
HAL_ADC_IRQHandler(&hadc);
235270
}

src/current_sense/hardware_specific/stm32/stm32g4/stm32g4_mcu.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,37 @@ void _driverSyncLowSide(void* _driver_params, void* _cs_params){
4949
STM32DriverParams* driver_params = (STM32DriverParams*)_driver_params;
5050
Stm32CurrentSenseParams* cs_params = (Stm32CurrentSenseParams*)_cs_params;
5151

52+
53+
5254
// if compatible timer has not been found
5355
if (cs_params->timer_handle == NULL) return;
5456

55-
// stop all the timers for the driver
56-
_stopTimers(driver_params->timers, 6);
57-
58-
// if timer has repetition counter - it will downsample using it
59-
// and it does not need the software downsample
60-
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance) ){
61-
// adjust the initial timer state such that the trigger
62-
// - for DMA transfer aligns with the pwm peaks instead of throughs.
63-
// - for interrupt based ADC transfer
64-
// - only necessary for the timers that have repetition counters
65-
cs_params->timer_handle->getHandle()->Instance->CR1 |= TIM_CR1_DIR;
66-
cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
67-
// remember that this timer has repetition counter - no need to downasmple
68-
needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
69-
}
57+
// // stop all the timers for the driver
58+
// _stopTimers(driver_params->timers, 6);
59+
60+
// // if timer has repetition counter - it will downsample using it
61+
// // and it does not need the software downsample
62+
// if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->getHandle()->Instance) ){
63+
// // adjust the initial timer state such that the trigger
64+
// // - for DMA transfer aligns with the pwm peaks instead of throughs.
65+
// // - for interrupt based ADC transfer
66+
// // - only necessary for the timers that have repetition counters
67+
// cs_params->timer_handle->getHandle()->Instance->CR1 |= TIM_CR1_DIR;
68+
// cs_params->timer_handle->getHandle()->Instance->CNT = cs_params->timer_handle->getHandle()->Instance->ARR;
69+
// // remember that this timer has repetition counter - no need to downasmple
70+
// needs_downsample[_adcToIndex(cs_params->adc_handle)] = 0;
71+
// }
72+
7073
// set the trigger output event
7174
LL_TIM_SetTriggerOutput(cs_params->timer_handle->getHandle()->Instance, LL_TIM_TRGO_UPDATE);
7275
// start the adc
76+
// HAL_ADC_Start(cs_params->adc_handle);
7377
HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
7478

79+
SIMPLEFOC_DEBUG("here driver_sync!");
7580
// restart all the timers of the driver
76-
_startTimers(driver_params->timers, 6);
81+
// _startTimers(driver_params->timers, 6);
82+
pinMode(PB10,OUTPUT);
7783
}
7884

7985

@@ -89,6 +95,7 @@ float _readADCVoltageLowSide(const int pin, const void* cs_params){
8995

9096
extern "C" {
9197
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
98+
digitalWrite(PB10, 0);
9299
// calculate the instance
93100
int adc_index = _adcToIndex(AdcHandle);
94101

@@ -100,7 +107,8 @@ extern "C" {
100107

101108
adc_val[adc_index][0]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_1);
102109
adc_val[adc_index][1]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_2);
103-
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
110+
adc_val[adc_index][2]=HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_3);
111+
digitalWrite(PB10, 1);
104112
}
105113
}
106114

0 commit comments

Comments
 (0)