You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.9 KiB
61 lines
1.9 KiB
#include "zpwm_generator.hpp"
|
|
|
|
#include "zirq_dispatcher.hpp"
|
|
using namespace iflytop;
|
|
|
|
void ZPWMGenerator::initialize(zchip_tim_t *htim, uint32_t channel, float freq, bool polarity) {
|
|
m_htim = htim;
|
|
m_polarity = polarity;
|
|
m_channel = channel;
|
|
m_freq = freq;
|
|
|
|
ZEARLY_ASSERT(m_htim->Init.AutoReloadPreload == TIM_AUTORELOAD_PRELOAD_ENABLE);
|
|
ZEARLY_ASSERT(m_htim->Init.CounterMode == TIM_COUNTERMODE_UP);
|
|
}
|
|
|
|
/******************************************************
|
|
* kTimMode_timer *
|
|
******************************************************/
|
|
void ZPWMGenerator::startPWM(float duty) { startPWM(m_freq, duty); }
|
|
|
|
void ZPWMGenerator::startPWM(float freq, float duty) {
|
|
if (!m_polarity) {
|
|
duty = 100.0 - duty;
|
|
}
|
|
|
|
m_freq = freq;
|
|
|
|
uint32_t prescaler = 0;
|
|
uint32_t autoreload = 0;
|
|
|
|
uint32_t timClkFreq = chip_get_timer_clock_sorce_freq(m_htim);
|
|
ZEARLY_ASSERT(chip_calculate_prescaler_and_autoreload_by_expect_freq(timClkFreq, freq, &prescaler, &autoreload));
|
|
__HAL_TIM_SET_AUTORELOAD(m_htim, autoreload);
|
|
__HAL_TIM_SET_PRESCALER(m_htim, prescaler);
|
|
|
|
// printf("auto reload %d %f %f\n", autoreload, duty, duty / 100.0 * __HAL_TIM_GET_AUTORELOAD(m_htim));
|
|
|
|
/**
|
|
* @brief
|
|
*/
|
|
TIM_OC_InitTypeDef sConfigOC = {0};
|
|
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
|
sConfigOC.Pulse = duty / 100.0 * __HAL_TIM_GET_AUTORELOAD(m_htim);
|
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
|
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
|
|
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
|
|
|
|
if (HAL_TIM_PWM_ConfigChannel(m_htim, &sConfigOC, m_channel) != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
HAL_TIM_PWM_Stop(m_htim, m_channel);
|
|
__HAL_TIM_SET_COUNTER(m_htim, 0);
|
|
if (HAL_TIM_PWM_Start(m_htim, m_channel) != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
}
|
|
void ZPWMGenerator::stopPWM() {
|
|
float duty = 0;
|
|
startPWM(duty);
|
|
}
|