Abstracted registers for PWM and other timer modes

This commit is contained in:
Ea-r-th
2025-11-05 18:15:32 -08:00
parent aa7a041946
commit 12aedf1ff9
8 changed files with 198 additions and 9 deletions

View File

@@ -109,6 +109,12 @@ uint16_t SHAL_GPIO::analogRead(SHAL_ADC_SampleTime sampleTime) {
return GPIOManager::getGPIOADC().singleConvertSingle(channel,sampleTime);
}
void SHAL_GPIO::setAlternateFunction(GPIO_Alternate_Function_Mapping AF) volatile {
setPinMode(PinMode::ALTERNATE_FUNCTION_MODE);
auto alternateFunctionReg = getGPIOAlternateFunctionRegister(m_GPIO_KEY);
SHAL_set_bits(alternateFunctionReg.reg,4,static_cast<uint8_t>(AF),alternateFunctionReg.offset);
}
SHAL_GPIO& GPIOManager::get(GPIO_Key key) {

View File

@@ -54,6 +54,21 @@ 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,
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);
if(static_cast<uint8_t>(m_key) > 3){
fullModeMask &= (0b0011); //Clear bits for complimentary output since channels 4,5,6 don't support it
}
SHAL_set_bits(ccer.reg,4,fullModeMask,offset);
}
Timer &TimerManager::get(Timer_Key timer_key) {

View File

@@ -26,6 +26,10 @@ void timer2callback(){
}
void b0PWM(){
PIN(B0).toggle();
}
int main() {
SHAL_init();
@@ -33,8 +37,6 @@ int main() {
SHAL_UART2.init(UART_Pair_Key::Tx2A2_Rx2A3);
SHAL_UART2.begin(115200);
SHAL_UART2.sendString("Begin\r\n");
PIN(A0).setPinMode(PinMode::ANALOG_MODE);
PIN(A1).setPinMode(PinMode::ANALOG_MODE);
PIN(A4).setPinMode(PinMode::ANALOG_MODE);
@@ -42,7 +44,7 @@ int main() {
PIN(A6).setPinMode(PinMode::ANALOG_MODE);
PIN(A7).setPinMode(PinMode::ANALOG_MODE);
SHAL_UART2.sendString("Hello\r\n");
PIN(B0).setPinMode(PinMode::OUTPUT_MODE);
SHAL_TIM2.init(4000000,400);
@@ -50,7 +52,10 @@ int main() {
SHAL_TIM2.enableInterrupt();
SHAL_TIM2.start();
SHAL_UART2.sendString("Hello\r\n");
SHAL_TIM6.init(4000000,1);
SHAL_TIM6.setCallbackFunc(b0PWM);
SHAL_TIM6.enableInterrupt();
SHAL_TIM6.start();
while (true) {
}