Cleanup of half_bridge driver
Variables are now named same as registers to avoid confusion between duty cycle as a value between 0 and 1 or as the capture/compare register value.
This commit is contained in:
+190
-219
@@ -11,322 +11,293 @@
|
||||
#include "mcu.h"
|
||||
#include "board.h"
|
||||
|
||||
static int _pwm_resolution;
|
||||
static float _min_duty;
|
||||
static float _max_duty;
|
||||
static uint8_t _deadtime_clocks;
|
||||
static uint16_t tim_arr; // auto-reload register (defines PWM frequency)
|
||||
static uint16_t tim_ccr_min; // capture/compare register min/max
|
||||
static uint16_t tim_ccr_max;
|
||||
static uint16_t tim_dt_clocks;
|
||||
|
||||
static bool _enabled;
|
||||
static bool pwm_enabled;
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
|
||||
class PWM_TIM3
|
||||
static void pwm_init_registers(uint16_t arr)
|
||||
{
|
||||
public:
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
|
||||
static void _init_registers(uint32_t pwm_resolution)
|
||||
{
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
// Enable TIM3 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
|
||||
// Enable TIM3 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
// Select alternate function mode on PB0 and PB1 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE0)) | GPIO_MODER_MODE0_1;
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1;
|
||||
|
||||
// Select alternate function mode on PB0 and PB1 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE0)) | GPIO_MODER_MODE0_1;
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1;
|
||||
// Select AF2 on PB0 and PB1
|
||||
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL0_Pos;
|
||||
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL1_Pos;
|
||||
|
||||
// Select AF2 on PB0 and PB1
|
||||
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL0_Pos;
|
||||
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL1_Pos;
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM3->PSC = 0;
|
||||
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM3->PSC = 0;
|
||||
// Capture/Compare Mode Register 1
|
||||
// OCxM = 110: Select PWM mode 1 on OCx
|
||||
// OCxPE = 1: Enable preload register on OCx (reset value)
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3PE;
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4PE;
|
||||
|
||||
// Capture/Compare Mode Register 1
|
||||
// OCxM = 110: Select PWM mode 1 on OCx
|
||||
// OCxPE = 1: Enable preload register on OCx (reset value)
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC3PE;
|
||||
TIM3->CCMR2 |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4PE;
|
||||
// Capture/Compare Enable Register
|
||||
// CCxP: Active high polarity on OCx (default = 0)
|
||||
TIM3->CCER &= ~(TIM_CCER_CC3P); // PB0 / TIM3_CH3: high-side
|
||||
TIM3->CCER |= TIM_CCER_CC4P; // PB1 / TIM3_CH4: low-side
|
||||
|
||||
// Capture/Compare Enable Register
|
||||
// CCxP: Active high polarity on OCx (default = 0)
|
||||
TIM3->CCER &= ~(TIM_CCER_CC3P); // PB0 / TIM3_CH3: high-side
|
||||
TIM3->CCER |= TIM_CCER_CC4P; // PB1 / TIM3_CH4: low-side
|
||||
// Control Register 1
|
||||
// TIM_CR1_CMS = 01: Select center-aligned mode 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN;
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CMS = 01: Select center-aligned mode 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN;
|
||||
// Control Register 2
|
||||
// OIS1 = OIS1N = 0: Output Idle State is set to off
|
||||
//TIM3->CR2 |= ;
|
||||
|
||||
// Control Register 2
|
||||
// OIS1 = OIS1N = 0: Output Idle State is set to off
|
||||
//TIM3->CR2 |= ;
|
||||
// Force update generation (UG = 1)
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
// Auto Reload Register
|
||||
// center-aligned mode --> resolution is double
|
||||
TIM3->ARR = arr;
|
||||
}
|
||||
|
||||
// Auto Reload Register
|
||||
// center-aligned mode --> resolution is double
|
||||
TIM3->ARR = pwm_resolution;
|
||||
}
|
||||
static void pwm_start()
|
||||
{
|
||||
// Capture/Compare Enable Register
|
||||
// CCxE = 1: Enable the output on OCx
|
||||
// CCxP = 0: Active high polarity on OCx (default)
|
||||
// CCxNE = 1: Enable the output on OC1N
|
||||
// CCxNP = 0: Active high polarity on OC1N (default)
|
||||
TIM3->CCER |= TIM_CCER_CC3E;
|
||||
TIM3->CCER |= TIM_CCER_CC4E;
|
||||
}
|
||||
|
||||
static void start()
|
||||
{
|
||||
// Capture/Compare Enable Register
|
||||
// CCxE = 1: Enable the output on OCx
|
||||
// CCxP = 0: Active high polarity on OCx (default)
|
||||
// CCxNE = 1: Enable the output on OC1N
|
||||
// CCxNP = 0: Active high polarity on OC1N (default)
|
||||
TIM3->CCER |= TIM_CCER_CC3E;
|
||||
TIM3->CCER |= TIM_CCER_CC4E;
|
||||
}
|
||||
static void pwm_stop()
|
||||
{
|
||||
TIM3->CCER &= ~(TIM_CCER_CC3E);
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4E);
|
||||
}
|
||||
|
||||
static void stop()
|
||||
{
|
||||
TIM3->CCER &= ~(TIM_CCER_CC3E);
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4E);
|
||||
}
|
||||
static uint16_t pwm_get_ccr()
|
||||
{
|
||||
return TIM3->CCR3;
|
||||
}
|
||||
|
||||
static int32_t get_ccr()
|
||||
{
|
||||
return TIM3->CCR3;
|
||||
}
|
||||
|
||||
static void set_ccr(uint32_t val)
|
||||
{
|
||||
TIM3->CCR3 = val; // high-side
|
||||
TIM3->CCR4 = val + _deadtime_clocks; // low-side
|
||||
}
|
||||
};
|
||||
static void pwm_set_ccr(uint16_t ccr)
|
||||
{
|
||||
TIM3->CCR3 = ccr; // high-side
|
||||
TIM3->CCR4 = ccr + tim_dt_clocks; // low-side
|
||||
}
|
||||
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
|
||||
class PWM_TIM1
|
||||
static void pwm_init_registers(uint16_t arr)
|
||||
{
|
||||
public:
|
||||
|
||||
static void _init_registers(uint32_t pwm_resolution)
|
||||
{
|
||||
// Enable peripheral clock of GPIOA and GPIOB
|
||||
// Enable peripheral clock of GPIOA and GPIOB
|
||||
#ifdef CONFIG_SOC_SERIES_STM32F0X
|
||||
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
|
||||
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
|
||||
#else // CONFIG_SOC_SERIES_STM32G4X
|
||||
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN | RCC_AHB2ENR_GPIOBEN;
|
||||
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN | RCC_AHB2ENR_GPIOBEN;
|
||||
#endif
|
||||
|
||||
// Enable TIM1 clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
|
||||
// Enable TIM1 clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
|
||||
|
||||
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) | GPIO_MODER_MODER8_1;
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER13)) | GPIO_MODER_MODER13_1;
|
||||
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) | GPIO_MODER_MODER8_1;
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER13)) | GPIO_MODER_MODER13_1;
|
||||
|
||||
// Select AF2 on PA8 (TIM1_CH1) and PB13 (TIM1_CH1N)
|
||||
// Select AF2 on PA8 (TIM1_CH1) and PB13 (TIM1_CH1N)
|
||||
#ifdef CONFIG_SOC_SERIES_STM32F0X
|
||||
GPIOA->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL8_Pos;
|
||||
GPIOB->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL13_Pos;
|
||||
GPIOA->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL8_Pos;
|
||||
GPIOB->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL13_Pos;
|
||||
#else // CONFIG_SOC_SERIES_STM32G4X
|
||||
GPIOA->AFR[1] |= 0x6 << GPIO_AFRH_AFSEL8_Pos;
|
||||
GPIOB->AFR[1] |= 0x6 << GPIO_AFRH_AFSEL13_Pos;
|
||||
GPIOA->AFR[1] |= 0x6 << GPIO_AFRH_AFSEL8_Pos;
|
||||
GPIOB->AFR[1] |= 0x6 << GPIO_AFRH_AFSEL13_Pos;
|
||||
#endif
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM1->PSC = 0;
|
||||
// No prescaler --> timer frequency == SystemClock
|
||||
TIM1->PSC = 0;
|
||||
|
||||
// Capture/Compare Mode Register 1
|
||||
// OC1M = 110: Select PWM mode 1 on OC1
|
||||
// OC1PE = 1: Enable preload register on OC1 (reset value)
|
||||
TIM1->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
||||
// Capture/Compare Mode Register 1
|
||||
// OC1M = 110: Select PWM mode 1 on OC1
|
||||
// OC1PE = 1: Enable preload register on OC1 (reset value)
|
||||
TIM1->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
||||
|
||||
// Capture/Compare Enable Register
|
||||
// CC1E = 1: Enable the output on OC1
|
||||
// CC1P = 0: Active high polarity on OC1 (default)
|
||||
// CC1NE = 1: Enable the output on OC1N
|
||||
// CC1NP = 0: Active high polarity on OC1N (default)
|
||||
TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE; // | TIM_CCER_CC1NP;
|
||||
// Capture/Compare Enable Register
|
||||
// CC1E = 1: Enable the output on OC1
|
||||
// CC1P = 0: Active high polarity on OC1 (default)
|
||||
// CC1NE = 1: Enable the output on OC1N
|
||||
// CC1NP = 0: Active high polarity on OC1N (default)
|
||||
TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE; // | TIM_CCER_CC1NP;
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CMS = 01: Select center-aligned mode 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM1->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN;
|
||||
// Control Register 1
|
||||
// TIM_CR1_CMS = 01: Select center-aligned mode 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM1->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN;
|
||||
|
||||
// Control Register 2
|
||||
// OIS1 = OIS1N = 0: Output Idle State is set to off
|
||||
//TIM1->CR2 |= ;
|
||||
// Control Register 2
|
||||
// OIS1 = OIS1N = 0: Output Idle State is set to off
|
||||
//TIM1->CR2 |= ;
|
||||
|
||||
#ifdef CONFIG_SOC_SERIES_STM32G4X
|
||||
// Boards with STM32G4 MCU use ADC2 for synchronized ADC sampling. We use OC2
|
||||
// for triggering, but but don't configure any pin for PWM mode
|
||||
TIM1->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2PE;
|
||||
TIM1->CCER |= TIM_CCER_CC2E | TIM_CCER_CC2NE;
|
||||
// Trigger ADC via TIM1_TRGO2 on OC2ref signal event
|
||||
TIM1->CR2 |= TIM_CR2_MMS2_2 | TIM_CR2_MMS2_0;
|
||||
// Boards with STM32G4 MCU use ADC2 for synchronized ADC sampling. We use OC2
|
||||
// for triggering, but but don't configure any pin for PWM mode
|
||||
TIM1->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2PE;
|
||||
TIM1->CCER |= TIM_CCER_CC2E | TIM_CCER_CC2NE;
|
||||
// Trigger ADC via TIM1_TRGO2 on OC2ref signal event
|
||||
TIM1->CR2 |= TIM_CR2_MMS2_2 | TIM_CR2_MMS2_0;
|
||||
#endif
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM1->EGR |= TIM_EGR_UG;
|
||||
// Force update generation (UG = 1)
|
||||
TIM1->EGR |= TIM_EGR_UG;
|
||||
|
||||
// Auto Reload Register
|
||||
// center-aligned mode --> frequency is half the pwm_resolution;
|
||||
TIM1->ARR = pwm_resolution;
|
||||
// Auto Reload Register
|
||||
// center-aligned mode --> frequency is half the pwm_resolution;
|
||||
TIM1->ARR = arr;
|
||||
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
// OSSR = 0: Off-state selection for Run mode -> OC/OCN = 0
|
||||
// OSSI = 0: Off-state selection for Idle mode -> OC/OCN = 0
|
||||
TIM1->BDTR |= (_deadtime_clocks & (uint32_t)0x7F); // ensure that only the last 7 bits are changed
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
// OSSR = 0: Off-state selection for Run mode -> OC/OCN = 0
|
||||
// OSSI = 0: Off-state selection for Idle mode -> OC/OCN = 0
|
||||
TIM1->BDTR |= (tim_dt_clocks & (uint32_t)0x7F); // ensure that only the last 7 bits are changed
|
||||
|
||||
// Lock Break and Dead-Time Register
|
||||
// TODO: does not work properly... maybe HW bug?
|
||||
TIM1->BDTR |= TIM_BDTR_LOCK_1 | TIM_BDTR_LOCK_0;
|
||||
}
|
||||
// Lock Break and Dead-Time Register
|
||||
// TODO: does not work properly... maybe HW bug?
|
||||
TIM1->BDTR |= TIM_BDTR_LOCK_1 | TIM_BDTR_LOCK_0;
|
||||
}
|
||||
|
||||
static void start()
|
||||
{
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
TIM1->BDTR |= TIM_BDTR_MOE;
|
||||
}
|
||||
|
||||
static void stop()
|
||||
{
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
TIM1->BDTR &= ~(TIM_BDTR_MOE);
|
||||
}
|
||||
|
||||
static int32_t get_ccr()
|
||||
{
|
||||
return TIM1->CCR1;
|
||||
}
|
||||
|
||||
static void set_ccr(uint32_t val)
|
||||
{
|
||||
TIM1->CCR1 = val;
|
||||
|
||||
// Trigger ADC for current measurement in the middle of the cycle.
|
||||
TIM1->CCR2 = 1;
|
||||
}
|
||||
};
|
||||
|
||||
#else /* UNIT_TEST */
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
const uint32_t SystemCoreClock = 24000000;
|
||||
#endif
|
||||
|
||||
class PWM_UT
|
||||
static void pwm_start()
|
||||
{
|
||||
public:
|
||||
static uint32_t m_pwm_resolution;
|
||||
static bool m_started;
|
||||
static uint32_t m_ccr;
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
TIM1->BDTR |= TIM_BDTR_MOE;
|
||||
}
|
||||
|
||||
static void _init_registers(uint32_t pwm_resolution)
|
||||
{
|
||||
m_pwm_resolution = pwm_resolution;
|
||||
}
|
||||
static void pwm_stop()
|
||||
{
|
||||
// Break and Dead-Time Register
|
||||
// MOE = 1: Main output enable
|
||||
TIM1->BDTR &= ~(TIM_BDTR_MOE);
|
||||
}
|
||||
|
||||
static void start()
|
||||
{
|
||||
m_started = true;
|
||||
}
|
||||
static uint32_t pwm_get_ccr()
|
||||
{
|
||||
return TIM1->CCR1;
|
||||
}
|
||||
|
||||
static void stop()
|
||||
{
|
||||
m_started = false;
|
||||
}
|
||||
static void pwm_set_ccr(uint32_t ccr)
|
||||
{
|
||||
TIM1->CCR1 = ccr;
|
||||
|
||||
static int32_t get_ccr()
|
||||
{
|
||||
return m_ccr;
|
||||
}
|
||||
// Trigger ADC for current measurement in the middle of the cycle.
|
||||
TIM1->CCR2 = 1;
|
||||
}
|
||||
|
||||
static void set_ccr(uint32_t val)
|
||||
{
|
||||
m_ccr = val; // high-side
|
||||
}
|
||||
};
|
||||
#elif defined(UNIT_TEST)
|
||||
|
||||
uint32_t PWM_UT::m_pwm_resolution = 0;
|
||||
bool PWM_UT::m_started = false;
|
||||
uint32_t PWM_UT::m_ccr = 0;
|
||||
const uint32_t SystemCoreClock = 24000000;
|
||||
|
||||
// dummy registers
|
||||
uint32_t _tim_ccr = 0;
|
||||
|
||||
static void pwm_init_registers(uint16_t arr)
|
||||
{
|
||||
tim_arr = arr;
|
||||
}
|
||||
|
||||
static void pwm_start()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
static void pwm_stop()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
static uint16_t pwm_get_ccr()
|
||||
{
|
||||
return _tim_ccr;
|
||||
}
|
||||
|
||||
static void pwm_set_ccr(uint16_t ccr)
|
||||
{
|
||||
_tim_ccr = ccr; // high-side
|
||||
}
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32F0X) || defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
typedef PWM_TIM1 PWM_TIM_HW;
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
typedef PWM_TIM3 PWM_TIM_HW;
|
||||
#else
|
||||
typedef PWM_UT PWM_TIM_HW;
|
||||
#endif
|
||||
|
||||
void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty)
|
||||
{
|
||||
// timers are counting at system clock
|
||||
// we are using a mode which counts up and down in each period
|
||||
// so we need half the impluses as resolution for the given frequency.
|
||||
_pwm_resolution = SystemCoreClock / (freq_kHz * 1000) / 2;
|
||||
// The timer runs at system clock speed.
|
||||
// We are using a mode which counts up and down in each period,
|
||||
// so we need half the clocks as resolution for the given frequency.
|
||||
tim_arr = SystemCoreClock / (freq_kHz * 1000) / 2;
|
||||
|
||||
// (clocks per ms * deadtime in ns) / 1000 == (clocks per ms * deadtime in ms)
|
||||
// although the C operator precedence does the "right thing" to allow deadtime_ns < 1000 to
|
||||
// be handled nicely, parentheses make this more explicit
|
||||
_deadtime_clocks = ((SystemCoreClock / (1000000)) * deadtime_ns) / 1000;
|
||||
tim_dt_clocks = ((SystemCoreClock / (1000000)) * deadtime_ns) / 1000;
|
||||
|
||||
// set PWM frequency and resolution (in fact half the resolution)
|
||||
PWM_TIM_HW::_init_registers(_pwm_resolution);
|
||||
pwm_init_registers(tim_arr);
|
||||
|
||||
_min_duty = min_duty * _pwm_resolution;
|
||||
_max_duty = max_duty * _pwm_resolution;
|
||||
tim_ccr_min = min_duty * tim_arr;
|
||||
tim_ccr_max = max_duty * tim_arr;
|
||||
|
||||
half_bridge_set_duty_cycle(max_duty); // init with allowed value
|
||||
|
||||
_enabled = false;
|
||||
pwm_enabled = false;
|
||||
}
|
||||
|
||||
static void _half_bridge_set_duty_cycle(int duty_target)
|
||||
static void _half_bridge_set_ccr(int ccr_target)
|
||||
{
|
||||
// protection against wrong settings which could destroy the hardware
|
||||
if (duty_target < _min_duty) {
|
||||
duty_target = _min_duty;
|
||||
if (ccr_target < tim_ccr_min) {
|
||||
ccr_target = tim_ccr_min;
|
||||
}
|
||||
else if (duty_target > _max_duty) {
|
||||
duty_target = _max_duty;
|
||||
else if (ccr_target > tim_ccr_max) {
|
||||
ccr_target = tim_ccr_max;
|
||||
}
|
||||
|
||||
PWM_TIM_HW::set_ccr(duty_target);
|
||||
pwm_set_ccr(ccr_target);
|
||||
}
|
||||
|
||||
void half_bridge_set_duty_cycle(float duty)
|
||||
{
|
||||
_half_bridge_set_duty_cycle(_pwm_resolution * duty);
|
||||
_half_bridge_set_ccr(tim_arr * duty);
|
||||
}
|
||||
|
||||
void half_bridge_duty_cycle_step(int delta)
|
||||
{
|
||||
_half_bridge_set_duty_cycle(PWM_TIM_HW::get_ccr() + delta);
|
||||
_half_bridge_set_ccr(pwm_get_ccr() + delta);
|
||||
}
|
||||
|
||||
float half_bridge_get_duty_cycle()
|
||||
{
|
||||
return (float)(PWM_TIM_HW::get_ccr()) / _pwm_resolution;
|
||||
return (float)(pwm_get_ccr()) / tim_arr;
|
||||
}
|
||||
|
||||
void half_bridge_start(float pwm_duty)
|
||||
{
|
||||
half_bridge_set_duty_cycle(pwm_duty);
|
||||
|
||||
PWM_TIM_HW::start();
|
||||
_enabled = true;
|
||||
pwm_start();
|
||||
pwm_enabled = true;
|
||||
}
|
||||
|
||||
void half_bridge_stop()
|
||||
{
|
||||
PWM_TIM_HW::stop();
|
||||
_enabled = false;
|
||||
pwm_stop();
|
||||
pwm_enabled = false;
|
||||
}
|
||||
|
||||
bool half_bridge_enabled()
|
||||
{
|
||||
return _enabled;
|
||||
return pwm_enabled;
|
||||
}
|
||||
|
||||
+24
-14
@@ -1,19 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2016 Martin Jäger / Libre Solar
|
||||
*/
|
||||
|
||||
#ifndef HALF_BRIDGE_H
|
||||
#define HALF_BRIDGE_H
|
||||
#ifndef HALF_BRIDGE_H_
|
||||
#define HALF_BRIDGE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
/** @file
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief PWM timer functions for half bridge of DC/DC converter
|
||||
*/
|
||||
|
||||
/** Initiatializes the registers to generate the PWM signal and sets duty
|
||||
* cycle limits
|
||||
/**
|
||||
* Initiatializes the registers to generate the PWM signal and sets duty
|
||||
* cycle limits
|
||||
*
|
||||
* @param freq_kHz Switching frequency in kHz
|
||||
* @param deadtime_ns Deadtime in ns between switching the two FETs on/off
|
||||
@@ -22,38 +26,44 @@
|
||||
*/
|
||||
void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty);
|
||||
|
||||
/** Start the PWM generation
|
||||
/**
|
||||
* Start the PWM generation
|
||||
*
|
||||
* @param pwm_duty Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
void half_bridge_start(float pwm_duty);
|
||||
|
||||
/** Stop the PWM generation
|
||||
/**
|
||||
* Stop the PWM generation
|
||||
*/
|
||||
void half_bridge_stop();
|
||||
|
||||
/** Get status of the PWM output
|
||||
/**
|
||||
* Get status of the PWM output
|
||||
*
|
||||
* @returns True if PWM output enabled
|
||||
*/
|
||||
bool half_bridge_enabled();
|
||||
|
||||
/** Set the duty cycle of the PWM signal
|
||||
/**
|
||||
* Set the duty cycle of the PWM signal
|
||||
*
|
||||
* @param duty Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
void half_bridge_set_duty_cycle(float duty);
|
||||
|
||||
/** Adjust the duty cycle with minimum step size
|
||||
/**
|
||||
* Adjust the duty cycle with minimum step size
|
||||
*
|
||||
* @param delta Number of steps (positive or negative)
|
||||
*/
|
||||
void half_bridge_duty_cycle_step(int delta);
|
||||
|
||||
/** Read the currently set duty cycle
|
||||
/**
|
||||
* Read the currently set duty cycle
|
||||
*
|
||||
* @returns Duty cycle between 0.0 and 1.0
|
||||
*/
|
||||
float half_bridge_get_duty_cycle();
|
||||
|
||||
#endif /* HALF_BRIDGE_H */
|
||||
#endif /* HALF_BRIDGE_H_ */
|
||||
|
||||
Reference in New Issue
Block a user