Configure PWM switch driver via DTS
Driver now moved to separate .c file to allow ISR handling in Zephyr.
This commit is contained in:
+1
-1
@@ -68,7 +68,7 @@ endchoice
|
||||
|
||||
choice
|
||||
prompt "PWM terminal"
|
||||
depends on $(dt_compat_enabled,pwm-switch)
|
||||
depends on $(dt_node_has_bool_prop,/outputs/pwm_switch,kconfig-flag)
|
||||
help
|
||||
Select the device connected to the PWM switch (currently
|
||||
only solar panel possible)
|
||||
|
||||
@@ -40,7 +40,7 @@ enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
|
||||
extern const char *led_ports[NUM_LED_PINS];
|
||||
extern const int led_pins[NUM_LED_PINS];
|
||||
|
||||
const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
{ PIN_HIGH }, // LED1
|
||||
};
|
||||
|
||||
@@ -85,7 +85,7 @@ enum {
|
||||
#define NUM_ADC_1_CH 7
|
||||
#define NUM_ADC_2_CH 3
|
||||
|
||||
const uint32_t adc_1_sequence[] = {
|
||||
static const uint32_t adc_1_sequence[] = {
|
||||
LL_ADC_CHANNEL_12, // V_LOW
|
||||
LL_ADC_CHANNEL_15, // V_HIGH
|
||||
LL_ADC_CHANNEL_11, // V_SC_SENSE
|
||||
@@ -95,7 +95,7 @@ const uint32_t adc_1_sequence[] = {
|
||||
LL_ADC_CHANNEL_TEMPSENSOR_ADC1, // TEMP_MCU
|
||||
};
|
||||
|
||||
const uint32_t adc_2_sequence[] = {
|
||||
static const uint32_t adc_2_sequence[] = {
|
||||
LL_ADC_CHANNEL_1, // I_DCDC
|
||||
LL_ADC_CHANNEL_2, // I_LOAD
|
||||
LL_ADC_CHANNEL_5, // I_PWM
|
||||
|
||||
@@ -29,12 +29,6 @@
|
||||
current-max = <20>;
|
||||
};
|
||||
|
||||
pwm-switch {
|
||||
compatible = "pwm-switch";
|
||||
pwm-frequency = <50>;
|
||||
current-max = <20>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
zephyr,console = &usart1;
|
||||
zephyr,shell-uart = &usart1;
|
||||
@@ -52,6 +46,12 @@
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
pwm_switch {
|
||||
label = "PWM power switch (solar panel)";
|
||||
pwms = <&pwm3 2 (20*1000*1000)>; /* 20 ms period = 50 Hz */
|
||||
current-max = <20>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
load {
|
||||
label = "Load output";
|
||||
gpios = <&gpiob 15 GPIO_ACTIVE_LOW>;
|
||||
|
||||
@@ -27,14 +27,6 @@
|
||||
internal-tref-max = <70>;
|
||||
};
|
||||
|
||||
pwm-switch {
|
||||
compatible = "pwm-switch";
|
||||
pwm-frequency = <50>;
|
||||
// Current reduced to 15A. Increase to 20A PCB max values
|
||||
// only if attached to a big heat sink.
|
||||
current-max = <15>;
|
||||
};
|
||||
|
||||
chosen {
|
||||
zephyr,console = &usart1;
|
||||
zephyr,shell-uart = &usart1;
|
||||
@@ -60,10 +52,18 @@
|
||||
|
||||
outputs {
|
||||
compatible = "outputs";
|
||||
pwm_switch {
|
||||
label = "PWM power switch (solar panel)";
|
||||
pwms = <&pwm3 4 (20*1000*1000)>; /* 20 ms period = 50 Hz */
|
||||
// Current reduced to 15A. Increase to 20A PCB max values
|
||||
// only if attached to a big heat sink.
|
||||
current-max = <15>;
|
||||
kconfig-flag; /* make node visible in Kconfig */
|
||||
};
|
||||
load {
|
||||
label = "Load output";
|
||||
gpios = <&gpiob 2 GPIO_ACTIVE_LOW>;
|
||||
current-max = <20>;
|
||||
current-max = <15>;
|
||||
};
|
||||
usb-pwr {
|
||||
label = "USB charging port disable";
|
||||
@@ -81,6 +81,25 @@
|
||||
spi-uext = &spi1;
|
||||
i2c-uext = &i2c1;
|
||||
};
|
||||
|
||||
// this should normally go to Zephyr repo
|
||||
soc {
|
||||
timers3: timers@40000400 {
|
||||
compatible = "st,stm32-timers";
|
||||
reg = <0x40000400 0x400>;
|
||||
clocks = <&rcc STM32_CLOCK_BUS_APB1 0x00000002>;
|
||||
status = "okay";
|
||||
label = "TIMERS_3";
|
||||
|
||||
pwm3: pwm {
|
||||
compatible = "st,stm32-pwm";
|
||||
status = "okay";
|
||||
st,prescaler = <10000>;
|
||||
label = "PWM_3";
|
||||
#pwm-cells = <2>;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
&usart1 {
|
||||
|
||||
+6
-6
@@ -21,7 +21,7 @@
|
||||
#if DT_COMPAT_DCDC
|
||||
static float dcdc_current_offset;
|
||||
#endif
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
static float pwm_current_offset;
|
||||
#endif
|
||||
static float load_current_offset;
|
||||
@@ -95,7 +95,7 @@ void calibrate_current_sensors()
|
||||
#if DT_COMPAT_DCDC
|
||||
dcdc_current_offset = -adc_scaled(ADC_POS_I_DCDC, vref, ADC_GAIN_I_DCDC);
|
||||
#endif
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
pwm_current_offset = -adc_scaled(ADC_POS_I_PWM, vref, ADC_GAIN_I_PWM);
|
||||
#endif
|
||||
load_current_offset = -adc_scaled(ADC_POS_I_LOAD, vref, ADC_GAIN_I_LOAD);
|
||||
@@ -107,7 +107,7 @@ void adc_update_value(unsigned int pos)
|
||||
// y(n) = c * x(n) + (c - 1) * y(n-1)
|
||||
// see also here: http://techteach.no/simview/lowpass_filter/doc/filter_algorithm.pdf
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
if (pos == ADC_POS_V_PWM || pos == ADC_POS_I_PWM) {
|
||||
// only read input voltage and current when switch is on or permanently off
|
||||
if (pwm_switch.signal_high() || pwm_switch.active() == false) {
|
||||
@@ -162,14 +162,14 @@ void daq_update()
|
||||
hv_bus.voltage = adc_scaled(ADC_POS_V_HIGH, vref, ADC_GAIN_V_HIGH);
|
||||
#endif
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
pwm_switch.ext_voltage = lv_bus.voltage - vref * (ADC_OFFSET_V_PWM / 1000.0) -
|
||||
adc_scaled(ADC_POS_V_PWM, vref, ADC_GAIN_V_PWM);
|
||||
#endif
|
||||
|
||||
load.current = adc_scaled(ADC_POS_I_LOAD, vref, ADC_GAIN_I_LOAD) + load_current_offset;
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power
|
||||
// calculation
|
||||
pwm_switch.current = -pwm_switch.get_duty_cycle() * (
|
||||
@@ -236,7 +236,7 @@ void high_voltage_alert()
|
||||
#if DT_COMPAT_DCDC
|
||||
dcdc.emergency_stop();
|
||||
#endif
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
pwm_switch.emergency_stop();
|
||||
#endif
|
||||
// do not use enter_state function, as we don't want to wait entire recharge delay
|
||||
|
||||
@@ -199,7 +199,7 @@ const data_object_t data_objects[] = {
|
||||
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
|
||||
#endif
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
TS_DATA_OBJ_BOOL(0x63, "PwmEn", &pwm_switch.enable,
|
||||
TS_INPUT, TS_READ_ALL | TS_WRITE_ALL),
|
||||
#endif
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
|
||||
#include "device_status.h"
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
#include <zephyr.h>
|
||||
#endif
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
+3
-3
@@ -106,8 +106,8 @@ void control_thread()
|
||||
|
||||
lv_terminal.update_bus_current_margins();
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
//pwm_switch.control();
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
pwm_switch.control();
|
||||
leds_set_charging(pwm_switch.active());
|
||||
#endif
|
||||
|
||||
@@ -134,7 +134,7 @@ void control_thread()
|
||||
hv_terminal.energy_balance();
|
||||
#endif
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
pwm_switch.energy_balance();
|
||||
#endif
|
||||
|
||||
|
||||
+8
-132
@@ -14,140 +14,16 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "board.h"
|
||||
#include "mcu.h"
|
||||
#include "daq.h"
|
||||
#include "debug.h"
|
||||
#include "helper.h"
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
|
||||
static bool _pwm_active;
|
||||
|
||||
#if defined(__MBED__) && !defined(UNIT_TEST)
|
||||
|
||||
static int _pwm_resolution;
|
||||
|
||||
void pwm_signal_init_registers(int freq_Hz)
|
||||
{
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
|
||||
// Enable TIM3 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
|
||||
// Select alternate function mode on PB1 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1;
|
||||
|
||||
// Select AF2 on PB1
|
||||
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFRL1_Pos;
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
TIM3->PSC = SystemCoreClock / 10000 - 1;
|
||||
|
||||
// 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_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_CC4P); // PB1 / TIM3_CH4: high-side
|
||||
|
||||
// Interrupt on timer update
|
||||
TIM3->DIER |= TIM_DIER_UIE;
|
||||
|
||||
// Auto Reload Register sets interrupt frequency
|
||||
TIM3->ARR = 10000 / freq_Hz - 1;
|
||||
|
||||
// 1 = second-highest priority of STM32L0/F0
|
||||
NVIC_SetPriority(TIM3_IRQn, 1);
|
||||
NVIC_EnableIRQ(TIM3_IRQn);
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
|
||||
// set PWM frequency and resolution
|
||||
_pwm_resolution = 10000 / freq_Hz;
|
||||
|
||||
// Auto Reload Register
|
||||
// Period goes from 0 to ARR (including ARR value), so substract 1 clock cycle
|
||||
TIM3->ARR = _pwm_resolution - 1;
|
||||
}
|
||||
|
||||
extern "C" void TIM3_IRQHandler(void)
|
||||
{
|
||||
TIM3->SR &= ~TIM_SR_UIF; // clear update interrupt flag
|
||||
|
||||
if ((int)TIM3->CCR4 < _pwm_resolution) {
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms
|
||||
// at each rising edge if switch is not continuously on
|
||||
adc_upper_alert_inhibit(ADC_POS_V_LOW, 10);
|
||||
}
|
||||
}
|
||||
|
||||
void pwm_signal_set_duty_cycle(float duty)
|
||||
{
|
||||
TIM3->CCR4 = _pwm_resolution * duty;
|
||||
}
|
||||
|
||||
void pwm_signal_duty_cycle_step(int delta)
|
||||
{
|
||||
if ((int)TIM3->CCR4 + delta <= _pwm_resolution && (int)TIM3->CCR4 + delta >= 0) {
|
||||
TIM3->CCR4 += delta;
|
||||
}
|
||||
}
|
||||
|
||||
float pwm_signal_get_duty_cycle()
|
||||
{
|
||||
return (float)(TIM3->CCR4) / _pwm_resolution;
|
||||
}
|
||||
|
||||
void pwm_signal_start(float pwm_duty)
|
||||
{
|
||||
pwm_signal_set_duty_cycle(pwm_duty);
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CEN; // edge-aligned mode
|
||||
|
||||
// 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_CC4E;
|
||||
|
||||
_pwm_active = true;
|
||||
}
|
||||
|
||||
void pwm_signal_stop()
|
||||
{
|
||||
TIM3->CR1 &= ~(TIM_CR1_CEN);
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4E);
|
||||
TIM3->CCR4 = 0;
|
||||
_pwm_active = false;
|
||||
}
|
||||
|
||||
bool pwm_signal_high()
|
||||
{
|
||||
return (GPIOB->IDR & GPIO_PIN_1);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// dummy functions for unit tests
|
||||
float pwm_signal_get_duty_cycle() { return 0; }
|
||||
void pwm_signal_set_duty_cycle(float duty) {;}
|
||||
void pwm_signal_duty_cycle_step(int delta) {;}
|
||||
void pwm_signal_init_registers(int freq_Hz) {;}
|
||||
void pwm_signal_start(float pwm_duty) {;}
|
||||
void pwm_signal_stop() {;}
|
||||
bool pwm_signal_high() { return false; }
|
||||
|
||||
#endif
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
|
||||
bool PwmSwitch::active()
|
||||
{
|
||||
return _pwm_active;
|
||||
return pwm_active();
|
||||
}
|
||||
|
||||
bool PwmSwitch::signal_high()
|
||||
@@ -164,15 +40,15 @@ PwmSwitch::PwmSwitch(DcBus *dc_bus) :
|
||||
offset_voltage_start = 2.0; // V charging switched on if Vsolar > Vbat + offset
|
||||
restart_interval = 60; // s When should we retry to start charging after low solar power cut-off?
|
||||
|
||||
pwm_signal_init_registers(DT_INST_0_PWM_SWITCH_PWM_FREQUENCY);
|
||||
// period stored in nanoseconds
|
||||
pwm_signal_init_registers(1000*1000*1000/DT_OUTPUTS_PWM_SWITCH_PWMS_PERIOD);
|
||||
|
||||
enable = true; // enable charging in general
|
||||
_pwm_active = false; // still disable actual switch
|
||||
}
|
||||
|
||||
void PwmSwitch::control()
|
||||
{
|
||||
if (_pwm_active) {
|
||||
if (pwm_active()) {
|
||||
if (neg_current_limit == 0
|
||||
|| current > 0 // discharging battery into solar panel --> stop
|
||||
|| bus->voltage < 9.0 // not enough voltage for MOSFET drivers anymore
|
||||
@@ -184,7 +60,7 @@ void PwmSwitch::control()
|
||||
}
|
||||
else if (bus->voltage > bus->sink_control_voltage() // bus voltage above target
|
||||
|| current < neg_current_limit // port current limit exceeded
|
||||
|| current < -DT_INST_0_PWM_SWITCH_CURRENT_MAX) // PCB current limit exceeded
|
||||
|| current < -DT_OUTPUTS_LOAD_CURRENT_MAX) // PCB current limit exceeded
|
||||
{
|
||||
// decrease power, as limits were reached
|
||||
|
||||
@@ -242,4 +118,4 @@ float PwmSwitch::get_duty_cycle()
|
||||
return pwm_signal_get_duty_cycle();
|
||||
}
|
||||
|
||||
#endif /* DT_COMPAT_PWM_SWITCH */
|
||||
#endif /* DT_OUTPUTS_PWM_SWITCH_PRESENT */
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
/** PWM charger type
|
||||
@@ -61,5 +63,23 @@ public:
|
||||
int restart_interval; ///< Interval to wait before retrying charging after low solar power cut-off (s)
|
||||
time_t off_timestamp; ///< Time when charger was switched off last time
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
float pwm_signal_get_duty_cycle();
|
||||
void pwm_signal_set_duty_cycle(float duty);
|
||||
void pwm_signal_duty_cycle_step(int delta);
|
||||
void pwm_signal_init_registers(int freq_Hz);
|
||||
void pwm_signal_start(float pwm_duty);
|
||||
void pwm_signal_stop();
|
||||
bool pwm_signal_high();
|
||||
bool pwm_active();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* PWM_SWITCH_H */
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "pwm_switch.h"
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
#include <zephyr.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "board.h"
|
||||
#include "daq.h"
|
||||
#include "debug.h"
|
||||
#include "helper.h"
|
||||
|
||||
#ifdef CONFIG_SOC_STM32L073XX
|
||||
#include "stm32l073xx.h"
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SOC_STM32G431XX
|
||||
#include "stm32g431xx.h"
|
||||
#endif
|
||||
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
|
||||
#if !defined(UNIT_TEST) && !defined(CONFIG_SOC_STM32G431XX) // not yet working for STM32G431
|
||||
|
||||
static bool _pwm_active;
|
||||
static int _pwm_resolution;
|
||||
|
||||
static void TIM3_IRQHandler(void *args)
|
||||
{
|
||||
TIM3->SR &= ~TIM_SR_UIF; // clear update interrupt flag
|
||||
|
||||
if ((int)TIM3->CCR4 < _pwm_resolution) {
|
||||
// turning the PWM switch on creates a short voltage rise, so inhibit alerts by 10 ms
|
||||
// at each rising edge if switch is not continuously on
|
||||
adc_upper_alert_inhibit(ADC_POS_V_LOW, 10);
|
||||
}
|
||||
}
|
||||
|
||||
void pwm_signal_init_registers(int freq_Hz)
|
||||
{
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
|
||||
// Enable TIM3 clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
|
||||
// Select alternate function mode on PB1 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE1)) | GPIO_MODER_MODE1_1;
|
||||
|
||||
// Select AF2 on PB1
|
||||
GPIOB->AFR[0] |= 0x2 << GPIO_AFRL_AFSEL1_Pos;
|
||||
|
||||
// Set timer clock to 10 kHz
|
||||
TIM3->PSC = SystemCoreClock / 10000 - 1;
|
||||
|
||||
// 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_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_CC4P); // PB1 / TIM3_CH4: high-side
|
||||
|
||||
// Interrupt on timer update
|
||||
TIM3->DIER |= TIM_DIER_UIE;
|
||||
|
||||
// Auto Reload Register sets interrupt frequency
|
||||
TIM3->ARR = 10000 / freq_Hz - 1;
|
||||
|
||||
// 1 = second-highest priority of STM32L0/F0
|
||||
IRQ_CONNECT(TIM3_IRQn, 1, TIM3_IRQHandler, 0, 0);
|
||||
irq_enable(TIM3_IRQn);
|
||||
|
||||
// Force update generation (UG = 1)
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
|
||||
// set PWM frequency and resolution
|
||||
_pwm_resolution = 10000 / freq_Hz;
|
||||
|
||||
// Auto Reload Register
|
||||
// Period goes from 0 to ARR (including ARR value), so substract 1 clock cycle
|
||||
TIM3->ARR = _pwm_resolution - 1;
|
||||
}
|
||||
|
||||
void pwm_signal_set_duty_cycle(float duty)
|
||||
{
|
||||
TIM3->CCR4 = _pwm_resolution * duty;
|
||||
}
|
||||
|
||||
void pwm_signal_duty_cycle_step(int delta)
|
||||
{
|
||||
if ((int)TIM3->CCR4 + delta <= _pwm_resolution && (int)TIM3->CCR4 + delta >= 0) {
|
||||
TIM3->CCR4 += delta;
|
||||
}
|
||||
}
|
||||
|
||||
float pwm_signal_get_duty_cycle()
|
||||
{
|
||||
return (float)(TIM3->CCR4) / _pwm_resolution;
|
||||
}
|
||||
|
||||
void pwm_signal_start(float pwm_duty)
|
||||
{
|
||||
pwm_signal_set_duty_cycle(pwm_duty);
|
||||
|
||||
// Control Register 1
|
||||
// TIM_CR1_CEN = 1: Counter enable
|
||||
TIM3->CR1 |= TIM_CR1_CEN; // edge-aligned mode
|
||||
|
||||
// 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_CC4E;
|
||||
|
||||
_pwm_active = true;
|
||||
}
|
||||
|
||||
void pwm_signal_stop()
|
||||
{
|
||||
TIM3->CR1 &= ~(TIM_CR1_CEN);
|
||||
TIM3->CCER &= ~(TIM_CCER_CC4E);
|
||||
TIM3->CCR4 = 0;
|
||||
_pwm_active = false;
|
||||
}
|
||||
|
||||
bool pwm_signal_high()
|
||||
{
|
||||
return (GPIOB->IDR & GPIO_IDR_ID1);
|
||||
}
|
||||
|
||||
bool pwm_active()
|
||||
{
|
||||
return _pwm_active;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// dummy functions for unit tests
|
||||
float pwm_signal_get_duty_cycle() { return 0; }
|
||||
void pwm_signal_set_duty_cycle(float duty) {;}
|
||||
void pwm_signal_duty_cycle_step(int delta) {;}
|
||||
void pwm_signal_init_registers(int freq_Hz) {;}
|
||||
void pwm_signal_start(float pwm_duty) {;}
|
||||
void pwm_signal_stop() {;}
|
||||
bool pwm_signal_high() { return false; }
|
||||
bool pwm_active() { return false; }
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* DT_OUTPUTS_PWM_SWITCH_PRESENT */
|
||||
+1
-1
@@ -45,7 +45,7 @@ Dcdc dcdc(&hv_terminal, &dcdc_lv_port, MODE_MPPT_BUCK);
|
||||
#endif // CONFIG_HV_TERMINAL
|
||||
#endif
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
PwmSwitch pwm_switch(&lv_bus);
|
||||
#endif
|
||||
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ extern PowerPort dcdc_lv_port;
|
||||
extern Dcdc dcdc;
|
||||
#endif
|
||||
|
||||
#if DT_COMPAT_PWM_SWITCH
|
||||
#if DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
extern PwmSwitch pwm_switch;
|
||||
#endif
|
||||
|
||||
|
||||
+5
-5
@@ -8,7 +8,7 @@
|
||||
#define PCB_STUB_H
|
||||
|
||||
// PWM charge controller
|
||||
#define CONFIG_PWM_TERMINAL_SOLAR DT_COMPAT_PWM_SWITCH
|
||||
#define CONFIG_PWM_TERMINAL_SOLAR DT_OUTPUTS_PWM_SWITCH_PRESENT
|
||||
|
||||
// MPPT buck/boost or nanogrid mode
|
||||
#define CONFIG_HV_TERMINAL_SOLAR DT_COMPAT_DCDC
|
||||
@@ -33,9 +33,9 @@
|
||||
#define DT_INST_0_DCDC_PWM_DEADTIME 300 // ns
|
||||
#define DT_INST_0_DCDC_CURRENT_MAX 20 // A
|
||||
|
||||
#define DT_COMPAT_PWM_SWITCH 1
|
||||
#define DT_INST_0_PWM_SWITCH_PWM_FREQUENCY 50 // Hz
|
||||
#define DT_INST_0_PWM_SWITCH_CURRENT_MAX 20 // A
|
||||
#define DT_OUTPUTS_PWM_SWITCH_PRESENT 1
|
||||
#define DT_OUTPUTS_PWM_SWITCH_PWMS_PERIOD 20*1000*1000
|
||||
#define DT_OUTPUTS_PWM_SWITCH_CURRENT_MAX 20 // A
|
||||
|
||||
#define DT_OUTPUTS_LOAD_PRESENT 1
|
||||
#define DT_OUTPUTS_LOAD_CURRENT_MAX 20 // PCB maximum load switch current
|
||||
@@ -83,7 +83,7 @@ static const int led_pins[NUM_LED_PINS] = {
|
||||
// A B C
|
||||
0, 0, 0
|
||||
};
|
||||
static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
static const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
{ PIN_HIGH, PIN_LOW, PIN_FLOAT }, // LED1
|
||||
{ PIN_LOW, PIN_HIGH, PIN_FLOAT }, // LED2
|
||||
{ PIN_HIGH, PIN_FLOAT, PIN_LOW }, // LED3
|
||||
|
||||
@@ -28,6 +28,7 @@ target_sources(app PRIVATE
|
||||
../src/load_drv.cpp
|
||||
../src/main.cpp
|
||||
../src/power_port.cpp
|
||||
../src/pwm_switch_driver.c
|
||||
../src/pwm_switch.cpp
|
||||
../src/setup.cpp
|
||||
../src/ext/ext.cpp
|
||||
|
||||
@@ -24,3 +24,6 @@ child-binding:
|
||||
present:
|
||||
type: int
|
||||
default: 1
|
||||
kconfig-flag:
|
||||
type: boolean
|
||||
description: This value can be checked by Kconfig parser using dt_node_has_bool_prop function
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
|
||||
description: MOSFETs of PWM charge controllers
|
||||
|
||||
compatible: "pwm-switch"
|
||||
|
||||
properties:
|
||||
pwm-frequency:
|
||||
type: int
|
||||
required: true
|
||||
description: PWM frequency in Hz
|
||||
current-max:
|
||||
type: int
|
||||
required: true
|
||||
description: Maximum inductor current in amps
|
||||
Reference in New Issue
Block a user