Unified half bridge timer handling (#53)

* As much as possible code is shared between both timer abstractions.
* Unit tests added to main implementation file (stub removed)
This commit is contained in:
hogthrob
2019-10-15 10:34:53 +02:00
committed by Martin Jäger
parent fb11fb7cb4
commit 6d56003c8c
8 changed files with 405 additions and 455 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
#ifndef HALF_BRIDGE_H
#define HALF_BRIDGE_H
#include <stdint.h>
/** @file
*
* @brief PWM timer functions for half bridge of DC/DC converter
+329
View File
@@ -0,0 +1,329 @@
/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "half_bridge.h"
#include "pcb.h"
#include "config.h"
static int _pwm_resolution;
static float _min_duty;
static float _max_duty;
static uint8_t _deadtime_clocks;
static bool _enabled;
#ifndef UNIT_TEST
class PWM_TIM3
{
public:
static void _init_registers(uint32_t pwm_resolution)
{
// Enable peripheral clock of GPIOB
#if defined(STM32F0)
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
#elif defined(STM32L0)
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
#endif
// Enable TIM3 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
// Select alternate function mode on PB0 and PB1 (first bit _1 = 1, second bit _0 = 0)
#if defined(STM32F0)
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER0)) | GPIO_MODER_MODER0_1;
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER1)) | GPIO_MODER_MODER1_1;
#elif defined(STM32L0)
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE0)) | GPIO_MODER_MODE0_1;
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1;
#endif
// Select AF2 on PB0 and PB1
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL0_Pos;
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL1_Pos;
// 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 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 2
// OIS1 = OIS1N = 0: Output Idle State is set to off
//TIM3->CR2 |= ;
// Force update generation (UG = 1)
TIM3->EGR |= TIM_EGR_UG;
// Auto Reload Register
// center-aligned mode --> resolution is double
TIM3->ARR = pwm_resolution;
}
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 stop()
{
TIM3->CCER &= ~(TIM_CCER_CC3E);
TIM3->CCER &= ~(TIM_CCER_CC4E);
}
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
}
};
#if defined(STM32F0)
class PWM_TIM1
{
public:
static void _init_registers(uint32_t pwm_resolution)
{
// Enable peripheral clock of GPIOA and GPIOB
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
// Enable TIM1 clock
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
// Select AF2 on PA8 (TIM1_CH1) and PB13 (TIM1_CH1N)
GPIOB->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL8_Pos;
GPIOB->AFR[1] |= 0x2 << GPIO_AFRH_AFSEL13_Pos;
// 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 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 2
// OIS1 = OIS1N = 0: Output Idle State is set to off
//TIM1->CR2 |= ;
// 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;
// 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
// 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;
}
};
#endif
#if (PWM_TIM == 1)
typedef PWM_TIM1 PWM_TIM_HW;
#elif (PWM_TIM == 3)
typedef PWM_TIM3 PWM_TIM_HW;
#endif
#else /* #ifndef UNIT_TEST */
const uint32_t SystemCoreClock = 24000000;
class PWM_UT
{
public:
static uint32_t m_pwm_resolution;
static bool m_started;
static uint32_t m_ccr;
static void _init_registers(uint32_t pwm_resolution)
{
m_pwm_resolution = pwm_resolution;
}
static void start()
{
m_started = true;
}
static void stop()
{
m_started = false;
}
static int32_t get_ccr()
{
return m_ccr;
}
static void set_ccr(uint32_t val)
{
m_ccr = val; // high-side
}
};
uint32_t PWM_UT::m_pwm_resolution = 0;
bool PWM_UT::m_started = false;
uint32_t PWM_UT::m_ccr = 0;
typedef PWM_UT PWM_TIM_HW;
#endif /* UNIT_TEST */
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;
// (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;
PWM_TIM_HW::_init_registers(_pwm_resolution);
// set PWM frequency and resolution (in fact half the resolution)
_min_duty = min_duty * _pwm_resolution;
_max_duty = max_duty * _pwm_resolution;
half_bridge_set_duty_cycle(max_duty); // init with allowed value
_enabled = false;
}
static void _half_bridge_set_duty_cycle(int duty_target)
{
// protection against wrong settings which could destroy the hardware
if (duty_target < _min_duty) {
duty_target = _min_duty;
}
else if (duty_target > _max_duty) {
duty_target = _max_duty;
}
PWM_TIM_HW::set_ccr(duty_target);
}
void half_bridge_set_duty_cycle(float duty)
{
_half_bridge_set_duty_cycle(_pwm_resolution * duty);
}
void half_bridge_duty_cycle_step(int delta)
{
half_bridge_set_duty_cycle(((float)(PWM_TIM_HW::get_ccr() + delta))/_pwm_resolution);
}
float half_bridge_get_duty_cycle()
{
return (float)(PWM_TIM_HW::get_ccr()) / _pwm_resolution;
}
void half_bridge_start(float pwm_duty)
{
half_bridge_set_duty_cycle(pwm_duty);
PWM_TIM_HW::start();
_enabled = true;
}
void half_bridge_stop()
{
PWM_TIM_HW::stop();
_enabled = false;
}
bool half_bridge_enabled()
{
return _enabled;
}
-173
View File
@@ -1,173 +0,0 @@
/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef UNIT_TEST
#include "half_bridge.h"
#include "pcb.h"
#include "config.h"
#if defined(STM32F0) && (PWM_TIM == 1)
static int _pwm_resolution;
static float _min_duty;
static float _max_duty;
static bool _enabled;
void _init_registers(int freq_kHz, int deadtime_ns)
{
// Enable peripheral clock of GPIOA and GPIOB
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
// Enable TIM1 clock
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
// Select alternate function mode on PA8 und PB13
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)
GPIOA->AFR[1] |= 0x2 << ((8 - 8)*4); // AFR[1] for pins 8-15
// Select AF2 on PB13 (TIM1_CH1N)
GPIOB->AFR[1] |= 0x2 << ((13 - 8)*4); // AFR[1] for pins 8-15
// No prescaler --> timer frequency = 48 MHz
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 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 2
// OIS1 = OIS1N = 0: Output Idle State is set to off
//TIM1->CR2 |= ;
// Force update generation (UG = 1)
TIM1->EGR |= TIM_EGR_UG;
// set PWM frequency and resolution
_pwm_resolution = SystemCoreClock / (freq_kHz * 1000);
// Auto Reload Register
// center-aligned mode --> divide resolution by 2
TIM1->ARR = _pwm_resolution / 2;
uint8_t deadtime_clocks = (SystemCoreClock / 1000 / 1000) * deadtime_ns / 1000;
// 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
// Lock Break and Dead-Time Register
// TODO: does not work properly... maybe HW bug?
TIM1->BDTR |= TIM_BDTR_LOCK_1 | TIM_BDTR_LOCK_0;
}
void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty)
{
_init_registers(freq_kHz, deadtime_ns);
_min_duty = min_duty;
_max_duty = max_duty;
half_bridge_set_duty_cycle(_max_duty); // init with allowed value
_enabled = false;
}
void half_bridge_set_duty_cycle(float duty)
{
float duty_target;
// protection against wrong settings which could destroy the hardware
if (duty < _min_duty) {
duty_target = _min_duty;
}
else if (duty > _max_duty) {
duty_target = _max_duty;
}
else {
duty_target = duty;
}
TIM1->CCR1 = _pwm_resolution / 2 * duty_target;
}
void half_bridge_duty_cycle_step(int delta)
{
float duty_target;
duty_target = (float)(TIM1->CCR1 + delta) / (_pwm_resolution / 2);
// protection against wrong settings which could destroy the hardware
if (duty_target < _min_duty) {
half_bridge_set_duty_cycle(_min_duty);
}
else if (duty_target > _max_duty) {
half_bridge_set_duty_cycle(_max_duty);
}
else {
TIM1->CCR1 = TIM1->CCR1 + delta;
}
}
float half_bridge_get_duty_cycle()
{
return (float)(TIM1->CCR1) / (_pwm_resolution / 2);;
}
void half_bridge_start(float pwm_duty)
{
half_bridge_set_duty_cycle(pwm_duty);
// Break and Dead-Time Register
// MOE = 1: Main output enable
TIM1->BDTR |= TIM_BDTR_MOE;
_enabled = true;
}
void half_bridge_stop()
{
// Break and Dead-Time Register
// MOE = 1: Main output enable
TIM1->BDTR &= ~(TIM_BDTR_MOE);
_enabled = false;
}
bool half_bridge_enabled()
{
return _enabled;
}
#endif /* PWM_TIM */
#endif /* UNIT_TEST */
-175
View File
@@ -1,175 +0,0 @@
/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef UNIT_TEST
/* Generates PWM signal on PB0 (high-side) and PB1 (low-side) using timer TIM3
*/
#include "half_bridge.h"
#include "pcb.h"
#include "config.h"
// timer used for PWM generation has to be globally selected for the used PCB
#if (PWM_TIM == 3)
static int _pwm_resolution;
static float _min_duty;
static float _max_duty;
static uint8_t _deadtime_clocks;
static bool _enabled;
void _init_registers(int freq_kHz)
{
// Enable peripheral clock of GPIOB
#if defined(STM32F0)
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
#elif defined(STM32L0)
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
#endif
// Enable TIM3 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
// Select alternate function mode on PB0 and PB1 (first bit _1 = 1, second bit _0 = 0)
#if defined(STM32F0)
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER0)) | GPIO_MODER_MODER0_1;
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER1)) | GPIO_MODER_MODER1_1;
#elif defined(STM32L0)
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE0)) | GPIO_MODER_MODE0_1;
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1;
#endif
#if defined(STM32F0)
// Select AF1 on PB0 and PB1
GPIOB->AFR[0] |= 0x1 << GPIO_AFRL_AFSEL0_Pos;
GPIOB->AFR[0] |= 0x1 << GPIO_AFRL_AFSEL1_Pos;
#elif defined(STM32L0)
// Select AF2 on PB0 and PB1
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL0_Pos;
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL1_Pos;
#endif
// No prescaler --> timer frequency = 32/48 MHz (for L0/F0)
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 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 2
// OIS1 = OIS1N = 0: Output Idle State is set to off
//TIM3->CR2 |= ;
// Force update generation (UG = 1)
TIM3->EGR |= TIM_EGR_UG;
// set PWM frequency and resolution
_pwm_resolution = SystemCoreClock / (freq_kHz * 1000);
// Auto Reload Register
// center-aligned mode --> divide resolution by 2
TIM3->ARR = _pwm_resolution / 2;
}
void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty)
{
_init_registers(freq_kHz);
_deadtime_clocks = (SystemCoreClock / 1000 / 1000) * deadtime_ns / 1000;
_min_duty = min_duty;
_max_duty = max_duty;
half_bridge_set_duty_cycle(_max_duty); // init with allowed value
_enabled = false;
}
void half_bridge_set_duty_cycle(float duty)
{
float duty_target;
// protection against wrong settings which could destroy the hardware
if (duty < _min_duty) {
duty_target = _min_duty;
}
else if (duty > _max_duty) {
duty_target = _max_duty;
}
else {
duty_target = duty;
}
TIM3->CCR3 = _pwm_resolution / 2 * duty_target; // high-side
TIM3->CCR4 = _pwm_resolution / 2 * duty_target + _deadtime_clocks; // low-side
}
void half_bridge_duty_cycle_step(int delta)
{
float duty_target;
duty_target = (float)(TIM3->CCR3 + delta) / (_pwm_resolution / 2);
half_bridge_set_duty_cycle(duty_target);
}
float half_bridge_get_duty_cycle() {
return (float)(TIM3->CCR3) / (_pwm_resolution / 2);;
}
void half_bridge_start(float pwm_duty)
{
half_bridge_set_duty_cycle(pwm_duty);
// 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;
_enabled = true;
}
void half_bridge_stop()
{
TIM3->CCER &= ~(TIM_CCER_CC3E);
TIM3->CCER &= ~(TIM_CCER_CC4E);
_enabled = false;
}
bool half_bridge_enabled()
{
return _enabled;
}
#endif /* PWM_TIM */
#endif /* UNIT_TEST */
-105
View File
@@ -1,105 +0,0 @@
/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "half_bridge.h"
#include "pcb.h"
#include "config.h"
static int _pwm_resolution;
static float _min_duty;
static float _max_duty;
static bool _enabled;
void _init_registers(int freq_kHz, int deadtime_ns)
{
}
void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_duty)
{
_init_registers(freq_kHz, deadtime_ns);
_min_duty = min_duty;
_max_duty = max_duty;
half_bridge_set_duty_cycle(_max_duty); // init with allowed value
_enabled = false;
}
void half_bridge_set_duty_cycle(float duty)
{
float duty_target = 0; // dummy
// protection against wrong settings which could destroy the hardware
if (duty < _min_duty) {
duty_target = _min_duty;
}
else if (duty > _max_duty) {
duty_target = _max_duty;
}
else {
duty_target = duty;
}
//TIM1->CCR1 = _pwm_resolution / 2 * duty_target;
}
void half_bridge_duty_cycle_step(int delta)
{
float duty_target = 0; // dummy
//duty_target = (float)(TIM1->CCR1 + delta) / (_pwm_resolution / 2);
// protection against wrong settings which could destroy the hardware
if (duty_target < _min_duty) {
half_bridge_set_duty_cycle(_min_duty);
}
else if (duty_target > _max_duty) {
half_bridge_set_duty_cycle(_max_duty);
}
else {
//TIM1->CCR1 = TIM1->CCR1 + delta;
}
}
float half_bridge_get_duty_cycle()
{
return 0;//(float)(TIM1->CCR1) / (_pwm_resolution / 2);;
}
void half_bridge_start(float pwm_duty)
{
half_bridge_set_duty_cycle(pwm_duty);
// Break and Dead-Time Register
// MOE = 1: Main output enable
//TIM1->BDTR |= TIM_BDTR_MOE;
_enabled = true;
}
void half_bridge_stop()
{
// Break and Dead-Time Register
// MOE = 1: Main output enable
//TIM1->BDTR &= ~(TIM_BDTR_MOE);
_enabled = false;
}
bool half_bridge_enabled()
{
return _enabled;
}
+1 -1
View File
@@ -37,7 +37,7 @@ int main()
adc_tests();
bat_charger_tests();
dc_bus_tests();
half_brigde_tests();
// TODO
//battery_tests();
//load_tests();
+2
View File
@@ -6,3 +6,5 @@ void bat_charger_tests();
void adc_tests();
void dc_bus_tests();
void half_brigde_tests();
+72
View File
@@ -0,0 +1,72 @@
#include "tests.h"
#include "half_bridge.h"
#include <time.h>
#include <stdio.h>
#include <math.h>
#define MAX_PWM_DUTY 0.97
#define MIN_PWM_DUTY 0.1
#define MID_PWM_DUTY ((MIN_PWM_DUTY + MAX_PWM_DUTY)/2)
#define PWM_F_KHZ 70
#define PWM_DEADTIME_NS 300
const float duty_epsilon = 0.006;
// calculated duty cycle float may deviate +/- duty_epsilon from test target value
static void init_structs()
{
half_bridge_init(PWM_F_KHZ,PWM_DEADTIME_NS,MIN_PWM_DUTY,MAX_PWM_DUTY);
half_bridge_stop();
}
void half_brigde_set_duty_cycle_works()
{
half_bridge_set_duty_cycle(MID_PWM_DUTY);
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon,MID_PWM_DUTY, half_bridge_get_duty_cycle());
}
void half_brigde_not_steps_over_max()
{
half_bridge_set_duty_cycle(MAX_PWM_DUTY);
half_bridge_duty_cycle_step(1);
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon,MAX_PWM_DUTY, half_bridge_get_duty_cycle());
}
void half_brigde_not_steps_under_min()
{
half_bridge_set_duty_cycle(MIN_PWM_DUTY);
half_bridge_duty_cycle_step(-1);
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon,MIN_PWM_DUTY, half_bridge_get_duty_cycle());
}
void half_brigde_not_exceeds_max()
{
half_bridge_set_duty_cycle(1.0);
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon,MAX_PWM_DUTY,half_bridge_get_duty_cycle());
}
void half_brigde_not_exceeds_min()
{
half_bridge_set_duty_cycle(-1.0);
TEST_ASSERT_FLOAT_WITHIN(duty_epsilon,MIN_PWM_DUTY,half_bridge_get_duty_cycle());
}
void half_brigde_tests()
{
init_structs();
UNITY_BEGIN();
RUN_TEST(half_brigde_set_duty_cycle_works);
RUN_TEST(half_brigde_not_exceeds_max);
RUN_TEST(half_brigde_not_exceeds_min);
RUN_TEST(half_brigde_not_steps_over_max);
RUN_TEST(half_brigde_not_steps_under_min);
UNITY_END();
}