Skip to content

Commit 0c86169

Browse files
authored
Merge pull request #482 from simplefoc/feat_bg341_sync_lost_fix
Feat bg341 sync lost fix
2 parents fd066fa + 1c584c3 commit 0c86169

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/current_sense/hardware_specific/stm32/b_g431/b_g431_mcu.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../stm32_mcu.h"
88
#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
99
#include "communication/SimpleFOCDebug.h"
10+
#include "../stm32_adc_utils.h"
1011

1112
#define _ADC_VOLTAGE 3.3f
1213
#define _ADC_RESOLUTION 4096.0f
@@ -25,6 +26,16 @@ static DMA_HandleTypeDef hdma_adc2;
2526
volatile uint16_t adcBuffer1[ADC_BUF_LEN_1] = {0}; // Buffer for store the results of the ADC conversion
2627
volatile uint16_t adcBuffer2[ADC_BUF_LEN_2] = {0}; // Buffer for store the results of the ADC conversion
2728

29+
30+
// structure containing the configuration of the adc interrupt
31+
Stm32AdcInterruptConfig adc_interrupt_config[5] = {
32+
{0, 0, 0}, // ADC1
33+
{0, 0, 0}, // ADC2
34+
{0, 0, 0}, // ADC3
35+
{0, 0, 0}, // ADC4
36+
{0, 0, 0} // ADC5
37+
};
38+
2839
// function reading an ADC value and returning the read voltage
2940
// As DMA is being used just return the DMA result
3041
float _readADCVoltageLowSide(const int pin, const void* cs_params){
@@ -155,14 +166,16 @@ void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
155166
// stop all the timers for the driver
156167
stm32_pause(driver_params);
157168

158-
// if timer has repetition counter - it will downsample using it
159-
// and it does not need the software downsample
160-
if( IS_TIM_REPETITION_COUNTER_INSTANCE(cs_params->timer_handle->Instance) ){
161-
// adjust the initial timer state such that the trigger for DMA transfer aligns with the pwm peaks instead of throughs.
162-
// only necessary for the timers that have repetition counters
163-
cs_params->timer_handle->Instance->CR1 |= TIM_CR1_DIR;
164-
cs_params->timer_handle->Instance->CNT = cs_params->timer_handle->Instance->ARR;
169+
// get the index of the adc
170+
int adc_index = _adcToIndex(cs_params->adc_handle);
171+
172+
bool tim_interrupt = _initTimerInterruptDownsampling(cs_params, driver_params, adc_interrupt_config[adc_index]);
173+
if(tim_interrupt) {
174+
// error in the timer interrupt initialization
175+
SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt not supported for B-G431");
176+
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
165177
}
178+
166179
// set the trigger output event
167180
LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
168181

0 commit comments

Comments
 (0)