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:
@@ -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
@@ -37,7 +37,7 @@ int main()
|
||||
adc_tests();
|
||||
bat_charger_tests();
|
||||
dc_bus_tests();
|
||||
|
||||
half_brigde_tests();
|
||||
// TODO
|
||||
//battery_tests();
|
||||
//load_tests();
|
||||
|
||||
@@ -6,3 +6,5 @@ void bat_charger_tests();
|
||||
void adc_tests();
|
||||
|
||||
void dc_bus_tests();
|
||||
|
||||
void half_brigde_tests();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user