skullc-peripherals/Peripherals/Src/peripherals_pwm_channel.cpp
2021-03-12 17:07:18 +02:00

64 lines
1.1 KiB
C++

/*
* peripherals_pwm_channel.cpp
*
* Created on: Feb 24, 2021
* Author: erki
*/
#include "peripherals_pwm_channel.hpp"
namespace Peripherals
{
PwmChannel::PwmChannel(TIM_HandleTypeDef* timer,
const std::uint32_t channel,
const std::uint32_t timer_code,
const IO& pin)
: timer(timer)
, channel(channel)
, timer_code(timer_code)
, pin(pin)
{ }
void PwmChannel::PinToPwm()
{
GPIO_InitTypeDef gpio_init = { 0 };
gpio_init.Pin = pin.pin;
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Pull = GPIO_NOPULL;
gpio_init.Speed = GPIO_SPEED_FREQ_LOW;
gpio_init.Alternate = timer_code;
HAL_GPIO_Init(pin.port, &gpio_init);
}
void PwmChannel::PinToGpio()
{
GPIO_InitTypeDef gpio_config = { 0 };
gpio_config.Pin = pin.pin;
gpio_config.Mode = GPIO_MODE_OUTPUT_PP;
gpio_config.Pull = GPIO_NOPULL;
gpio_config.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(pin.port, &gpio_config);
}
void PwmChannel::Enable()
{
HAL_TIM_PWM_Start(timer, channel);
}
void PwmChannel::Disable()
{
HAL_TIM_PWM_Stop(timer, channel);
}
void PwmChannel::SetCompare(const std::uint32_t compare)
{
__HAL_TIM_SET_COMPARE(timer, channel, compare);
}
}