Skip to content

Commit 4ac6923

Browse files
committed
Configure deadtime by just defining it in nanoseconds. Firmware will calculate the required DTG register value.
Signed-off-by: Marcos Chaparro <[email protected]>
1 parent 9652231 commit 4ac6923

7 files changed

+42
-7
lines changed

conf_general.c

+34
Original file line numberDiff line numberDiff line change
@@ -928,3 +928,37 @@ bool conf_general_measure_flux_linkage(float current, float duty,
928928
return true;
929929
}
930930
#endif
931+
932+
/* Calculate DTG register */
933+
uint8_t conf_general_calculate_deadtime(float deadtime_ns, float core_clock_freq) {
934+
uint8_t DTG = 0;
935+
float timebase = 1/(core_clock_freq/1000000.0)*1000.0;
936+
937+
if (deadtime_ns <= (timebase * 127.0) )
938+
DTG = deadtime_ns / timebase;
939+
else {
940+
if (deadtime_ns <= ((63.0 + 64.0)*2.0*timebase) ) {
941+
DTG = deadtime_ns / (2.0*timebase) - 64.0;
942+
DTG |= 0x80;
943+
}
944+
else {
945+
if (deadtime_ns <= ((31.0 + 32.0)*8.0*timebase) ) {
946+
DTG = deadtime_ns / (8.0*timebase) - 32.0;
947+
DTG |= 0xC0;
948+
}
949+
else {
950+
if (deadtime_ns <= ((31.0 + 32)*16*timebase) ) {
951+
DTG = deadtime_ns / (16.0*timebase) - 32.0;
952+
DTG |= 0xE0;
953+
}
954+
else {
955+
// Deadtime requested is longer than max achievable. Set deadtime at
956+
// longest possible value
957+
DTG = 0xFF;
958+
assert_param(1); //catch this
959+
}
960+
}
961+
}
962+
}
963+
return DTG;
964+
}

hwconf/hw_75_300.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
#define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3)
231231

232232
// Override dead time. See the stm32f4 reference manual for calculating this value.
233-
#define HW_DEAD_TIME_VALUE 110
233+
#define HW_DEAD_TIME_NSEC 660.0
234234

235235
// Default setting overrides
236236
#ifndef MCCONF_L_MAX_VOLTAGE

hwconf/hw_mini4.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
#define READ_HALL2() palReadPad(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2)
211211
#define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3)
212212

213-
#define HW_DEAD_TIME_VALUE 20
213+
#define HW_DEAD_TIME_NSEC 120.0
214214

215215
// Default setting overrides
216216
#ifndef MCCONF_DEFAULT_MOTOR_TYPE

hwconf/hw_palta.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
#define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3)
208208

209209
// Override dead time. See the stm32f4 reference manual for calculating this value.
210-
#define HW_DEAD_TIME_VALUE 181
210+
#define HW_DEAD_TIME_NSEC 1400.0
211211
#define HW_GATE_DRIVER_SUPPLY_MAX_VOLTAGE 16.0
212212
#define HW_GATE_DRIVER_SUPPLY_MIN_VOLTAGE 14.0
213213

mc_interface.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ extern volatile uint16_t ADC_Value[];
8787
extern volatile int ADC_curr_norm_value[];
8888

8989
// Common fixed parameters
90-
#ifndef HW_DEAD_TIME_VALUE
91-
#define HW_DEAD_TIME_VALUE 60 // Dead time
90+
#ifndef HW_DEAD_TIME_NSEC
91+
#define HW_DEAD_TIME_NSEC 360.0 // Dead time
9292
#endif
9393

94+
9495
#endif /* MC_INTERFACE_H_ */

mcpwm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ void mcpwm_init(volatile mc_configuration *configuration) {
258258
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
259259
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
260260
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
261-
TIM_BDTRInitStructure.TIM_DeadTime = HW_DEAD_TIME_VALUE;
261+
TIM_BDTRInitStructure.TIM_DeadTime = conf_general_calculate_deadtime(HW_DEAD_TIME_NSEC, SYSTEM_CORE_CLOCK);
262262
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
263263
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
264264
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;

mcpwm_foc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void mcpwm_foc_init(volatile mc_configuration *configuration) {
277277
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
278278
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
279279
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
280-
TIM_BDTRInitStructure.TIM_DeadTime = HW_DEAD_TIME_VALUE;
280+
TIM_BDTRInitStructure.TIM_DeadTime = conf_general_calculate_deadtime(HW_DEAD_TIME_NSEC, SYSTEM_CORE_CLOCK);
281281
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
282282
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
283283
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;

0 commit comments

Comments
 (0)