Fixed PWM mode

This commit is contained in:
Ea-r-th
2025-11-06 00:41:46 -08:00
parent 12aedf1ff9
commit a1458de235
6 changed files with 93 additions and 28 deletions

View File

@@ -19,6 +19,7 @@ void Timer::start() {
auto event_generation_reg = getTimerEventGenerationRegister(m_key);
SHAL_apply_bitmask(control_reg.reg, control_reg.counter_enable_mask); //Enable counter
SHAL_apply_bitmask(control_reg.reg, control_reg.auto_reload_preload_enable_mask); //Preload enable (buffer)
SHAL_apply_bitmask(event_generation_reg.reg, event_generation_reg.update_generation_mask);
enableInterrupt();
@@ -54,19 +55,42 @@ void Timer::init(uint32_t prescaler, uint32_t autoReload) {
setARR(autoReload);
}
void Timer::setPWMMode(SHAL_Timer_Channel channel, SHAL_Timer_Channel_Main_Output_Mode mainOutputMode,
void Timer::setPWMMode(SHAL_Timer_Channel channel, SHAL_TIM_Output_Compare_Mode outputCompareMode, SHAL_Timer_Channel_Main_Output_Mode mainOutputMode,
SHAL_Timer_Channel_Complimentary_Output_Mode complimentaryOutputMode) {
uint8_t fullModeMask = static_cast<uint8_t>(mainOutputMode) | (static_cast<uint8_t>(complimentaryOutputMode) << 2);
uint32_t offset = static_cast<uint8_t>(channel) * 4;
auto ccer = getTimerCaptureCompareEnableRegister(m_key);
auto ccmr1 = getTimerCaptureCompareModeRegistersOutput(m_key);
auto bdtr = getTimerBreakDeadTimeRegister(m_key);
if(static_cast<uint8_t>(m_key) > 3){
fullModeMask &= (0b0011); //Clear bits for complimentary output since channels 4,5,6 don't support it
uint8_t fullChannelModeMask = static_cast<uint8_t>(mainOutputMode) | (static_cast<uint8_t>(complimentaryOutputMode) << 2);
uint8_t channelNum = static_cast<uint8_t>(channel);
if (channelNum <= 3) {
uint32_t regNum = channelNum / 2; //TODO change later for support for channels 5 and 6
if (channelNum % 2 == 1) {
SHAL_set_bits(ccmr1.regs[regNum], 4, static_cast<uint8_t>(outputCompareMode),
ccmr1.output_compare_2_mode_offset);
} else {
SHAL_set_bits(ccmr1.regs[regNum], 4, static_cast<uint8_t>(outputCompareMode),
ccmr1.output_compare_1_mode_offset);
}
}
SHAL_set_bits(ccer.reg,4,fullModeMask,offset);
uint32_t offset = channelNum * 4;
if (static_cast<uint8_t>(m_key) > 3) {
fullChannelModeMask &= (0b0011); //Clear bits for complimentary output since channels 4,5,6 don't support it
}
SHAL_set_bits(ccer.reg, 4, fullChannelModeMask, offset);
SHAL_apply_bitmask(bdtr.reg, bdtr.main_output_enable_mask);
}
void Timer::setPWMDutyCycle(uint32_t dutyCycle) {
auto reg = getTimerCaptureCompareRegister(m_key);
SHAL_set_bits(reg.reg,16,dutyCycle,0);
}

View File

@@ -10,6 +10,11 @@ GPIO_Key gpios[6] = {
GPIO_Key::A7,
};
bool isAlarmBeeping = false;
bool isCalibrateButtonHigh = false;
uint16_t sensorThresholds[6] = {4096,4096,4096,4096,4096,4096};
void timer2callback(){
uint16_t val[6];
@@ -26,8 +31,24 @@ void timer2callback(){
}
void b0PWM(){
PIN(B0).toggle();
void PWMToggle(){
if(!isAlarmBeeping){
SHAL_TIM1.start();
}
else{
SHAL_TIM1.stop();
}
isAlarmBeeping = !isAlarmBeeping;
}
void buttonCallback(){
}
void calibrateSensors(){
PIN(B3).setHigh();
SHAL_TIM7.stop();
}
int main() {
@@ -44,7 +65,10 @@ int main() {
PIN(A6).setPinMode(PinMode::ANALOG_MODE);
PIN(A7).setPinMode(PinMode::ANALOG_MODE);
PIN(B0).setPinMode(PinMode::OUTPUT_MODE);
PIN(B0).setAlternateFunction(GPIO_Alternate_Function_Mapping::B0_TIM1CH2N);
PIN(B6).setPinMode(PinMode::INPUT_MODE);
PIN(B6).useAsExternalInterrupt(TriggerMode::RISING_FALLING_EDGE,buttonCallback);
SHAL_TIM2.init(4000000,400);
@@ -52,11 +76,23 @@ int main() {
SHAL_TIM2.enableInterrupt();
SHAL_TIM2.start();
SHAL_TIM6.init(4000000,1);
SHAL_TIM6.setCallbackFunc(b0PWM);
SHAL_TIM1.init(300,999);
SHAL_TIM1.setPWMMode(SHAL_Timer_Channel::CH2,SHAL_TIM_Output_Compare_Mode::PWMMode1,SHAL_Timer_Channel_Main_Output_Mode::Disabled,SHAL_Timer_Channel_Complimentary_Output_Mode::Polarity_Reversed);
SHAL_TIM1.setPWMDutyCycle(499);
SHAL_TIM1.start();
SHAL_TIM6.init(4000000,400);
SHAL_TIM6.setCallbackFunc(PWMToggle);
SHAL_TIM6.enableInterrupt();
SHAL_TIM6.start();
SHAL_TIM7.init(4000000,3000);
SHAL_TIM7.setCallbackFunc(calibrateSensors);
SHAL_TIM7.enableInterrupt();
PIN(B3).setPinMode(PinMode::OUTPUT_MODE); //Test
while (true) {
SHAL_UART2.sendString("HELLO\r\n");
}
}