Skip to content

Commit 4ef3738

Browse files
committed
added the trgo info to the ADC
1 parent 1c25100 commit 4ef3738

File tree

11 files changed

+56
-27
lines changed

11 files changed

+56
-27
lines changed

src/current_sense/hardware_specific/stm32/stm32_mcu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#define STM32_CURRENTSENSE_MCU_DEF
44
#include "../../hardware_api.h"
55
#include "../../../common/foc_utils.h"
6+
#include "../../../drivers/hardware_specific/stm32/stm32_mcu.h"
7+
#include "../../../drivers/hardware_specific/stm32/stm32_timerutils.h"
68

79
#if defined(_STM32_DEF_)
810

src/current_sense/hardware_specific/stm32/stm32f1/stm32f1_hal.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
8686
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers_handle[tim_num++]);
8787
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
8888

89+
// check if TRGO used already - if yes use the next timer
90+
if((driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_ENABLE) || // if used for timer sync
91+
(driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_UPDATE)) // if used for ADC sync
92+
{
93+
continue;
94+
}
95+
8996
// if the code comes here, it has found the timer available
9097
// timer does have trgo flag for injected channels
9198
sConfigInjected.ExternalTrigInjecConv = trigger_flag;
@@ -101,12 +108,11 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
101108
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot sync any timer to injected channels!");
102109
#endif
103110
return -1;
104-
}
105-
// display which timer is being used
111+
}else{
106112
#ifdef SIMPLEFOC_STM32_DEBUG
107-
// it would be better to use the getTimerNumber from driver
108-
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->Instance) + 1);
113+
SIMPLEFOC_DEBUG("STM32-CS: Using timer: ", stm32_getTimerNumber(cs_params->timer_handle->Instance));
109114
#endif
115+
}
110116

111117
// first channel
112118
sConfigInjected.InjectedRank = ADC_REGULAR_RANK_1;

src/current_sense/hardware_specific/stm32/stm32f1/stm32f1_hal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#if defined(STM32F1xx)
77
#include "stm32f1xx_hal.h"
8-
#include "../../../../common/foc_utils.h"
9-
#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
108
#include "../stm32_mcu.h"
119

1210
int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* driver_params);

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
8080
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers_handle[tim_num++]);
8181
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
8282

83+
// check if TRGO used already - if yes use the next timer
84+
if((driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_ENABLE) || // if used for timer sync
85+
(driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_UPDATE)) // if used for ADC sync
86+
{
87+
continue;
88+
}
89+
8390
// if the code comes here, it has found the timer available
8491
// timer does have trgo flag for injected channels
8592
sConfigInjected.ExternalTrigInjecConv = trigger_flag;
@@ -95,12 +102,11 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
95102
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot sync any timer to injected channels!");
96103
#endif
97104
return -1;
105+
}else{
106+
#ifdef SIMPLEFOC_STM32_DEBUG
107+
SIMPLEFOC_DEBUG("STM32-CS: Using timer: ", stm32_getTimerNumber(cs_params->timer_handle->Instance));
108+
#endif
98109
}
99-
// display which timer is being used
100-
#ifdef SIMPLEFOC_STM32_DEBUG
101-
// it would be better to use the getTimerNumber from driver
102-
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->Instance) + 1);
103-
#endif
104110

105111

106112
// first channel

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#if defined(STM32F4xx)
77
#include "stm32f4xx_hal.h"
8-
#include "../../../../common/foc_utils.h"
9-
#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
108
#include "../stm32_mcu.h"
119
#include "stm32f4_utils.h"
1210

src/current_sense/hardware_specific/stm32/stm32f7/stm32f7_hal.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
8080
TIM_HandleTypeDef *timer_to_check = driver_params->timers_handle[tim_num++];
8181
TIM_TypeDef *instance_to_check = timer_to_check->Instance;
8282

83-
// bool TRGO_already_configured = instance_to_check->CR2 & LL_TIM_TRGO_UPDATE;
84-
// if(TRGO_already_configured) continue;
85-
8683
uint32_t trigger_flag = _timerToInjectedTRGO(timer_to_check);
8784
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
8885

86+
// check if TRGO used already - if yes use the next timer
87+
if((timer_to_check->Instance->CR2 & LL_TIM_TRGO_ENABLE) || // if used for timer sync
88+
(timer_to_check->Instance->CR2 & LL_TIM_TRGO_UPDATE)) // if used for ADC sync
89+
{
90+
continue;
91+
}
92+
8993
// if the code comes here, it has found the timer available
9094
// timer does have trgo flag for injected channels
9195
sConfigInjected.ExternalTrigInjecConv = trigger_flag;
@@ -106,12 +110,11 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
106110
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot sync any timer to injected channels!");
107111
#endif
108112
return -1;
113+
}else{
114+
#ifdef SIMPLEFOC_STM32_DEBUG
115+
SIMPLEFOC_DEBUG("STM32-CS: Using timer: ", stm32_getTimerNumber(cs_params->timer_handle->Instance));
116+
#endif
109117
}
110-
// display which timer is being used
111-
#ifdef SIMPLEFOC_STM32_DEBUG
112-
// it would be better to use the getTimerNumber from driver
113-
SIMPLEFOC_DEBUG("STM32-CS: injected trigger for timer index: ", get_timer_index(cs_params->timer_handle->Instance) + 1);
114-
#endif
115118

116119

117120
// first channel

src/current_sense/hardware_specific/stm32/stm32f7/stm32f7_hal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#if defined(STM32F7xx)
66
#include "stm32f7xx_hal.h"
7-
#include "../../../../common/foc_utils.h"
8-
#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
97
#include "../stm32_mcu.h"
108
#include "stm32f7_utils.h"
119

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
130130
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers_handle[tim_num++]);
131131
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
132132

133+
// check if TRGO used already - if yes use the next timer
134+
if((driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_ENABLE) || // if used for timer sync
135+
(driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_UPDATE)) // if used for ADC sync
136+
{
137+
continue;
138+
}
139+
133140
// if the code comes here, it has found the timer available
134141
// timer does have trgo flag for injected channels
135142
sConfigInjected.ExternalTrigInjecConv = trigger_flag;
@@ -145,6 +152,10 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
145152
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot sync any timer to injected channels!");
146153
#endif
147154
return -1;
155+
}else{
156+
#ifdef SIMPLEFOC_STM32_DEBUG
157+
SIMPLEFOC_DEBUG("STM32-CS: Using timer: ", stm32_getTimerNumber(cs_params->timer_handle->Instance));
158+
#endif
148159
}
149160

150161

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#if defined(STM32G4xx) && !defined(ARDUINO_B_G431B_ESC1)
77

88
#include "stm32g4xx_hal.h"
9-
#include "../../../../common/foc_utils.h"
10-
#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
119
#include "../stm32_mcu.h"
1210
#include "stm32g4_utils.h"
1311

src/current_sense/hardware_specific/stm32/stm32l4/stm32l4_hal.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
128128
while(driver_params->timers_handle[tim_num] != NP && tim_num < 6){
129129
uint32_t trigger_flag = _timerToInjectedTRGO(driver_params->timers_handle[tim_num++]);
130130
if(trigger_flag == _TRGO_NOT_AVAILABLE) continue; // timer does not have valid trgo for injected channels
131+
132+
// check if TRGO used already - if yes use the next timer
133+
if((driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_ENABLE) || // if used for timer sync
134+
(driver_params->timers_handle[tim_num-1]->Instance->CR2 & LL_TIM_TRGO_UPDATE)) // if used for ADC sync
135+
{
136+
continue;
137+
}
131138

132139
// if the code comes here, it has found the timer available
133140
// timer does have trgo flag for injected channels
@@ -144,6 +151,10 @@ int _adc_init(Stm32CurrentSenseParams* cs_params, const STM32DriverParams* drive
144151
SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot sync any timer to injected channels!");
145152
#endif
146153
return -1;
154+
}else{
155+
#ifdef SIMPLEFOC_STM32_DEBUG
156+
SIMPLEFOC_DEBUG("STM32-CS: Using timer: ", stm32_getTimerNumber(cs_params->timer_handle->Instance));
157+
#endif
147158
}
148159

149160

0 commit comments

Comments
 (0)