Skip to content

Commit 4d1ee20

Browse files
committed
Complimentary outputs..
1 parent 220a0ca commit 4d1ee20

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

src/drivers/hardware_specific/stm32/stm32_mcu.cpp

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ void* _configure8PWM(long pwm_frequency, float dead_zone)
686686
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
687687
GPIO_InitStruct.Pull = GPIO_NOPULL;
688688
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
689-
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
689+
GPIO_InitStruct.Alternate = GPIO_AF2_TIM1;
690690
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
691691

692692
// Configure PA14 and PB0 as TIM8 channels
@@ -696,18 +696,18 @@ void* _configure8PWM(long pwm_frequency, float dead_zone)
696696
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
697697
GPIO_InitStruct.Pull = GPIO_NOPULL;
698698
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
699-
GPIO_InitStruct.Alternate = GPIO_AF3_TIM8;
699+
GPIO_InitStruct.Alternate = GPIO_AF5_TIM8;
700700
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
701701
GPIO_InitStruct.Pin = GPIO_PIN_0;
702702
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
703703
GPIO_InitStruct.Pull = GPIO_NOPULL;
704704
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
705-
GPIO_InitStruct.Alternate = GPIO_AF3_TIM8;
705+
GPIO_InitStruct.Alternate = GPIO_AF4_TIM8;
706706
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
707707

708708

709709

710-
// Set TIM1 and TIM8 dead time values to 50 ns
710+
//Set initial values
711711
TIM_MasterConfigTypeDef sMasterConfig = {0};
712712
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
713713
htim1.Instance = TIM1;
@@ -730,6 +730,8 @@ void* _configure8PWM(long pwm_frequency, float dead_zone)
730730
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
731731
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
732732

733+
734+
// Set TIM1 dead time values to 50 ns
733735
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
734736
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
735737
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
@@ -750,23 +752,30 @@ void* _configure8PWM(long pwm_frequency, float dead_zone)
750752
sBreakDeadTimeConfigTIM8.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
751753
HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfigTIM8);
752754

755+
// Configure TIM1 channels 1-6 for PWM output
756+
TIM_OC_InitTypeDef sConfigOC1 = {0};
757+
sConfigOC1.OCMode = TIM_OCMODE_PWM1;
758+
sConfigOC1.Pulse = 0;
759+
sConfigOC1.OCPolarity = TIM_OCPOLARITY_HIGH;
760+
sConfigOC1.OCFastMode = TIM_OCFAST_DISABLE;
761+
762+
TIM_OC_InitTypeDef sConfigOC2 = {0};
763+
sConfigOC2.OCMode = TIM_OCMODE_PWM2;
764+
sConfigOC2.Pulse = 0;
765+
sConfigOC2.OCPolarity = TIM_OCPOLARITY_HIGH;
766+
sConfigOC2.OCFastMode = TIM_OCFAST_DISABLE;
753767

754-
// Configure TIM1 channels 1-6 for PWM output
755-
TIM_OC_InitTypeDef sConfigOC = {0};
756-
sConfigOC.OCMode = TIM_OCMODE_PWM1;
757-
sConfigOC.Pulse = 0;
758-
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
759-
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
760-
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LL_TIM_CHANNEL_CH1);
761-
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LL_TIM_CHANNEL_CH1N);
762-
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LL_TIM_CHANNEL_CH2);
763-
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LL_TIM_CHANNEL_CH2N);
764-
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LL_TIM_CHANNEL_CH3);
765-
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, LL_TIM_CHANNEL_CH3N);
768+
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC1, LL_TIM_CHANNEL_CH1);
769+
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC2, LL_TIM_CHANNEL_CH1N);
770+
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC1, LL_TIM_CHANNEL_CH2);
771+
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC2, LL_TIM_CHANNEL_CH2N);
772+
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC1, LL_TIM_CHANNEL_CH3);
773+
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC2, LL_TIM_CHANNEL_CH3N);
774+
775+
// Configure PWM output on TIM8 channel 1 and additional channel
776+
HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC1, LL_TIM_CHANNEL_CH2);
777+
HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC2, LL_TIM_CHANNEL_CH2N);
766778

767-
// Configure PWM output on TIM8 channel 1 and additional channel
768-
HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, LL_TIM_CHANNEL_CH2);
769-
HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, LL_TIM_CHANNEL_CH2N);
770779

771780

772781
// Enable PWM outputs
@@ -883,28 +892,15 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, vo
883892

884893

885894

886-
class EightPWMParams {
887-
public:
888-
int period;
889-
TIM_HandleTypeDef tim1_handle;
890-
TIM_HandleTypeDef tim8_handle;
891-
892-
// Public members and methods go here
893-
};
894895

895-
struct EightPWMConfig {
896-
uint16_t freq; // PWM frequency in Hz
897-
float duty_cycle_max; // Maximum duty cycle as a fraction of the period (0.0 to 1.0)
898-
};
899896

900897

901-
// Scale duty cycles to the PWM period
902-
class EightPWM {
903-
public:
904-
EightPWM(int period, TIM_HandleTypeDef tim1_handle, TIM_HandleTypeDef tim8_handle)
905-
: period(period), tim1_handle(tim1_handle), tim8_handle(tim8_handle) {}
906898

907-
void writeDutyCycle(float duty_cycle1_h1, float duty_cycle1_h2, float duty_cycle2_h1, float duty_cycle2_h2,
899+
900+
int period;
901+
902+
903+
void _writeDutyCycle8PWM(float duty_cycle1_h1, float duty_cycle1_h2, float duty_cycle2_h1, float duty_cycle2_h2,
908904
float duty_cycle3_h1, float duty_cycle3_h2, float duty_cycle4_h1, float duty_cycle4_h2) {
909905

910906
// Scale duty cycles to the PWM period
@@ -918,26 +914,26 @@ struct EightPWMConfig {
918914
uint16_t duty4_h2 = (uint16_t)(duty_cycle4_h2 * period);
919915

920916
// Set duty cycles for half-bridge driver 1
921-
__HAL_TIM_SET_COMPARE(&tim1_handle, LL_TIM_CHANNEL_CH1, duty1_h1);
922-
__HAL_TIM_SET_COMPARE(&tim1_handle, LL_TIM_CHANNEL_CH1N, duty1_h2);
917+
__HAL_TIM_SET_COMPARE(&htim1, LL_TIM_CHANNEL_CH1, duty1_h1);
918+
__HAL_TIM_SET_COMPARE(&htim1, LL_TIM_CHANNEL_CH1N, duty1_h2);
923919

924920
// Set duty cycles for half-bridge driver 2
925-
__HAL_TIM_SET_COMPARE(&tim1_handle, LL_TIM_CHANNEL_CH2, duty2_h1);
926-
__HAL_TIM_SET_COMPARE(&tim1_handle, LL_TIM_CHANNEL_CH2N, duty2_h2);
921+
__HAL_TIM_SET_COMPARE(&htim1, LL_TIM_CHANNEL_CH2, duty2_h1);
922+
__HAL_TIM_SET_COMPARE(&htim1, LL_TIM_CHANNEL_CH2N, duty2_h2);
927923

928924
// Set duty cycles for half-bridge driver 3
929-
__HAL_TIM_SET_COMPARE(&tim1_handle, LL_TIM_CHANNEL_CH3, duty3_h1);
930-
__HAL_TIM_SET_COMPARE(&tim1_handle, LL_TIM_CHANNEL_CH3N, duty3_h2);
925+
__HAL_TIM_SET_COMPARE(&htim1, LL_TIM_CHANNEL_CH3, duty3_h1);
926+
__HAL_TIM_SET_COMPARE(&htim1, LL_TIM_CHANNEL_CH3N, duty3_h2);
931927

932928
// Set duty cycles for half-bridge driver 4
933-
__HAL_TIM_SET_COMPARE(&tim8_handle, LL_TIM_CHANNEL_CH2, duty4_h1);
934-
__HAL_TIM_SET_COMPARE(&tim8_handle, LL_TIM_CHANNEL_CH2N, duty4_h2);
929+
__HAL_TIM_SET_COMPARE(&htim8, LL_TIM_CHANNEL_CH2, duty4_h1);
930+
__HAL_TIM_SET_COMPARE(&htim8, LL_TIM_CHANNEL_CH2N, duty4_h2);
931+
932+
935933
}
936934

937-
private:
938-
int period;
939-
TIM_HandleTypeDef tim1_handle;
940-
TIM_HandleTypeDef tim8_handle;
935+
936+
941937

942938
/*
943939
@@ -959,7 +955,7 @@ simply call "__HAL_TIM_SET_COMPARE" to update the compare value and the timer wi
959955
*/
960956

961957

962-
};
958+
963959

964960
// Configuring PWM frequency, resolution and alignment
965961
// - BLDC driver - 6PWM setting

0 commit comments

Comments
 (0)