Zephyr: Load output added
Overcurrent protection in PWM charge controller not yet working for Zephyr.
This commit is contained in:
+8
-168
@@ -14,176 +14,8 @@
|
||||
#include "debug.h"
|
||||
#include "helper.h"
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
#endif
|
||||
|
||||
extern DeviceStatus dev_stat;
|
||||
|
||||
extern LoadOutput load; // necessary to call emergency stop function
|
||||
|
||||
#if defined(__MBED__) && defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2
|
||||
static void lptim_init()
|
||||
{
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
|
||||
// Enable LPTIM clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_LPTIM1EN;
|
||||
|
||||
// Select alternate function mode on PB2 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE2)) | GPIO_MODER_MODE2_1;
|
||||
|
||||
// Select AF2 (LPTIM_OUT) on PB2
|
||||
GPIOB->AFR[0] |= 0x2U << GPIO_AFRL_AFRL2_Pos;
|
||||
|
||||
// Set prescaler to 32 (resulting in 1 MHz timer frequency)
|
||||
LPTIM1->CFGR |= 0x5U << LPTIM_CFGR_PRESC_Pos;
|
||||
|
||||
// Enable trigger (rising edge)
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_TRIGEN_0;
|
||||
|
||||
// Select trigger 7 (COMP2_OUT)
|
||||
LPTIM1->CFGR |= 0x7U << LPTIM_CFGR_TRIGSEL_Pos;
|
||||
|
||||
//LPTIM1->CFGR |= LPTIM_CFGR_WAVPOL;
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_PRELOAD;
|
||||
|
||||
// Glitch filter of 8 cycles
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_TRGFLT_0 | LPTIM_CFGR_TRGFLT_1;
|
||||
|
||||
// Enable set-once mode
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_WAVE;
|
||||
|
||||
// Enable timer (must be done *before* changing ARR or CMP, but *after*
|
||||
// changing CFGR)
|
||||
LPTIM1->CR |= LPTIM_CR_ENABLE;
|
||||
|
||||
// Auto Reload Register
|
||||
LPTIM1->ARR = 1000;
|
||||
|
||||
// Set load switch-off delay in microseconds
|
||||
// (actually takes approx. 4 us longer than this setting)
|
||||
LPTIM1->CMP = 10;
|
||||
|
||||
// Continuous mode
|
||||
//LPTIM1->CR |= LPTIM_CR_CNTSTRT;
|
||||
//LPTIM1->CR |= LPTIM_CR_SNGSTRT;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void short_circuit_comp_init()
|
||||
{
|
||||
#if defined(__MBED__) && defined(PIN_I_LOAD_COMP)
|
||||
MBED_ASSERT(PIN_I_LOAD_COMP == PB_4);
|
||||
|
||||
// set GPIO pin to analog
|
||||
RCC->IOPENR |= RCC_IOPENR_GPIOBEN;
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODE4);
|
||||
|
||||
// enable SYSCFG clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
|
||||
|
||||
// enable VREFINT buffer
|
||||
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP;
|
||||
|
||||
// select PB4 as positive input
|
||||
COMP2->CSR |= COMP_INPUT_PLUS_IO2;
|
||||
|
||||
// select VREFINT divider as negative input
|
||||
COMP2->CSR |= COMP_INPUT_MINUS_1_4VREFINT;
|
||||
|
||||
// propagate comparator value to LPTIM input
|
||||
COMP2->CSR |= COMP_CSR_COMP2LPTIM1IN1;
|
||||
|
||||
// normal polarity
|
||||
//COMP2->CSR |= COMP_CSR_COMP2POLARITY;
|
||||
|
||||
// set high-speed mode (1.2us instead of 2.5us propagation delay, but 3.5uA instead of 0.5uA current consumption)
|
||||
//COMP2->CSR |= COMP_CSR_COMP2SPEED;
|
||||
|
||||
// enable COMP2
|
||||
COMP2->CSR |= COMP_CSR_COMP2EN;
|
||||
|
||||
// enable EXTI software interrupt / event
|
||||
EXTI->IMR |= EXTI_IMR_IM22;
|
||||
EXTI->EMR |= EXTI_EMR_EM22;
|
||||
EXTI->RTSR |= EXTI_RTSR_RT22;
|
||||
EXTI->FTSR |= EXTI_FTSR_FT22;
|
||||
EXTI->SWIER |= EXTI_SWIER_SWI22;
|
||||
|
||||
// 1 = second-highest priority of STM32L0/F0
|
||||
NVIC_SetPriority(ADC1_COMP_IRQn, 1);
|
||||
NVIC_EnableIRQ(ADC1_COMP_IRQn);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef PIN_I_LOAD_COMP
|
||||
|
||||
extern "C" void ADC1_COMP_IRQHandler(void)
|
||||
{
|
||||
// interrupt called because of COMP2?
|
||||
if (COMP2->CSR & COMP_CSR_COMP2VALUE) {
|
||||
// Load should be switched off by LPTIM trigger already. This interrupt
|
||||
// is mainly used to indicate the failure.
|
||||
load.stop(ERR_LOAD_SHORT_CIRCUIT);
|
||||
}
|
||||
|
||||
// clear interrupt flag
|
||||
EXTI->PR |= EXTI_PR_PIF22;
|
||||
}
|
||||
|
||||
#endif // PIN_I_LOAD_COMP
|
||||
|
||||
|
||||
void LoadOutput::switch_set(bool status)
|
||||
{
|
||||
pgood = status;
|
||||
leds_set(LED_LOAD, status);
|
||||
|
||||
#if defined(__MBED__) && defined(PIN_LOAD_EN)
|
||||
DigitalOut load_enable(PIN_LOAD_EN);
|
||||
if (status == true) {
|
||||
load_enable = 1;
|
||||
}
|
||||
else {
|
||||
load_enable = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__MBED__) && defined(PIN_LOAD_DIS)
|
||||
DigitalOut load_disable(PIN_LOAD_DIS);
|
||||
if (status == true) {
|
||||
#ifdef PIN_I_LOAD_COMP
|
||||
lptim_init();
|
||||
#else
|
||||
load_disable = 0;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
load_disable = 1;
|
||||
}
|
||||
#endif
|
||||
print_info("Load pgood = %d, state = %u\n", status, state);
|
||||
}
|
||||
|
||||
void LoadOutput::usb_set(bool status)
|
||||
{
|
||||
usb_pgood = status;
|
||||
|
||||
#if defined(__MBED__) && defined(PIN_USB_PWR_EN)
|
||||
DigitalOut usb_pwr_en(PIN_USB_PWR_EN);
|
||||
if (status == true) usb_pwr_en = 1;
|
||||
else usb_pwr_en = 0;
|
||||
#endif
|
||||
#if defined(__MBED__) && defined(PIN_USB_PWR_DIS)
|
||||
DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS);
|
||||
if (status == true) usb_pwr_dis = 0;
|
||||
else usb_pwr_dis = 1;
|
||||
#endif
|
||||
print_info("USB pgood = %d, state = %u, en = %d\n", status, usb_state, usb_enable);
|
||||
}
|
||||
|
||||
LoadOutput::LoadOutput(PowerPort *pwr_port) :
|
||||
port(pwr_port)
|
||||
{
|
||||
@@ -191,6 +23,11 @@ LoadOutput::LoadOutput(PowerPort *pwr_port) :
|
||||
usb_enable = true;
|
||||
state = LOAD_STATE_DISABLED;
|
||||
usb_state = LOAD_STATE_DISABLED;
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
get_bindings();
|
||||
#endif
|
||||
|
||||
switch_set(false);
|
||||
usb_set(false);
|
||||
junction_temperature = 25; // starting point: 25°C
|
||||
@@ -199,7 +36,10 @@ LoadOutput::LoadOutput(PowerPort *pwr_port) :
|
||||
ov_hysteresis = 0.3;
|
||||
|
||||
// analog comparator to detect short circuits and trigger immediate load switch-off
|
||||
#ifdef __MBED__
|
||||
// for Zephyr some interrupt conflicts have still to be resolved
|
||||
short_circuit_comp_init();
|
||||
#endif
|
||||
}
|
||||
|
||||
void LoadOutput::usb_state_machine()
|
||||
|
||||
@@ -102,9 +102,17 @@ public:
|
||||
///< temperature measurement (unit: °C)
|
||||
|
||||
float ov_hysteresis; ///< Hysteresis to switch back on after an overvoltage event
|
||||
|
||||
private:
|
||||
int determine_load_state();
|
||||
void short_circuit_comp_init();
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
void get_bindings();
|
||||
|
||||
struct device *dev_load;
|
||||
struct device *dev_usb;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif /* LOAD_H */
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Martin Jäger / Libre Solar
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "load.h"
|
||||
|
||||
#include "pcb.h"
|
||||
#include "config.h"
|
||||
#include "hardware.h"
|
||||
#include "leds.h"
|
||||
#include "device_status.h"
|
||||
#include "debug.h"
|
||||
|
||||
#if defined(__ZEPHYR__)
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <device.h>
|
||||
#include <drivers/gpio.h>
|
||||
|
||||
#if defined(CONFIG_SOC_SERIES_STM32L0X)
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
#elif defined(CONFIG_SOC_SERIES_STM32G4X)
|
||||
#include <stm32g4xx_ll_system.h>
|
||||
#endif
|
||||
|
||||
#endif // __ZEPHYR__
|
||||
|
||||
extern LoadOutput load; // necessary to call emergency stop function
|
||||
|
||||
#if defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2
|
||||
static void lptim_init()
|
||||
{
|
||||
// Enable peripheral clock of GPIOB
|
||||
RCC->IOPENR |= RCC_IOPENR_IOPBEN;
|
||||
|
||||
// Enable LPTIM clock
|
||||
RCC->APB1ENR |= RCC_APB1ENR_LPTIM1EN;
|
||||
|
||||
// Select alternate function mode on PB2 (first bit _1 = 1, second bit _0 = 0)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE2)) | GPIO_MODER_MODE2_1;
|
||||
|
||||
// Select AF2 (LPTIM_OUT) on PB2
|
||||
#ifdef __MBED__ // older version of STM32 Cube
|
||||
GPIOB->AFR[0] |= 0x2U << GPIO_AFRL_AFRL2_Pos;
|
||||
#else
|
||||
GPIOB->AFR[0] |= 0x2U << GPIO_AFRL_AFSEL2_Pos;
|
||||
#endif
|
||||
|
||||
// Set prescaler to 32 (resulting in 1 MHz timer frequency)
|
||||
LPTIM1->CFGR |= 0x5U << LPTIM_CFGR_PRESC_Pos;
|
||||
|
||||
// Enable trigger (rising edge)
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_TRIGEN_0;
|
||||
|
||||
// Select trigger 7 (COMP2_OUT)
|
||||
LPTIM1->CFGR |= 0x7U << LPTIM_CFGR_TRIGSEL_Pos;
|
||||
|
||||
//LPTIM1->CFGR |= LPTIM_CFGR_WAVPOL;
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_PRELOAD;
|
||||
|
||||
// Glitch filter of 8 cycles
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_TRGFLT_0 | LPTIM_CFGR_TRGFLT_1;
|
||||
|
||||
// Enable set-once mode
|
||||
LPTIM1->CFGR |= LPTIM_CFGR_WAVE;
|
||||
|
||||
// Enable timer (must be done *before* changing ARR or CMP, but *after*
|
||||
// changing CFGR)
|
||||
LPTIM1->CR |= LPTIM_CR_ENABLE;
|
||||
|
||||
// Auto Reload Register
|
||||
LPTIM1->ARR = 1000;
|
||||
|
||||
// Set load switch-off delay in microseconds
|
||||
// (actually takes approx. 4 us longer than this setting)
|
||||
LPTIM1->CMP = 10;
|
||||
|
||||
// Continuous mode
|
||||
//LPTIM1->CR |= LPTIM_CR_CNTSTRT;
|
||||
//LPTIM1->CR |= LPTIM_CR_SNGSTRT;
|
||||
}
|
||||
#endif
|
||||
|
||||
void LoadOutput::short_circuit_comp_init()
|
||||
{
|
||||
#if defined(PIN_I_LOAD_COMP)
|
||||
|
||||
#ifdef __MBED__
|
||||
MBED_ASSERT(PIN_I_LOAD_COMP == PB_4);
|
||||
#endif
|
||||
|
||||
// set GPIO pin to analog
|
||||
RCC->IOPENR |= RCC_IOPENR_GPIOBEN;
|
||||
GPIOB->MODER &= ~(GPIO_MODER_MODE4);
|
||||
|
||||
// enable SYSCFG clock
|
||||
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
|
||||
|
||||
// enable VREFINT buffer
|
||||
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP;
|
||||
|
||||
// select PB4 as positive input
|
||||
COMP2->CSR |= COMP_INPUT_PLUS_IO2;
|
||||
|
||||
// select VREFINT divider as negative input
|
||||
COMP2->CSR |= COMP_INPUT_MINUS_1_4VREFINT;
|
||||
|
||||
// propagate comparator value to LPTIM input
|
||||
COMP2->CSR |= COMP_CSR_COMP2LPTIM1IN1;
|
||||
|
||||
// normal polarity
|
||||
//COMP2->CSR |= COMP_CSR_COMP2POLARITY;
|
||||
|
||||
// set high-speed mode (1.2us instead of 2.5us propagation delay, but 3.5uA instead of 0.5uA current consumption)
|
||||
//COMP2->CSR |= COMP_CSR_COMP2SPEED;
|
||||
|
||||
// enable COMP2
|
||||
COMP2->CSR |= COMP_CSR_COMP2EN;
|
||||
|
||||
// enable EXTI software interrupt / event
|
||||
EXTI->IMR |= EXTI_IMR_IM22;
|
||||
EXTI->EMR |= EXTI_EMR_EM22;
|
||||
EXTI->RTSR |= EXTI_RTSR_RT22;
|
||||
EXTI->FTSR |= EXTI_FTSR_FT22;
|
||||
EXTI->SWIER |= EXTI_SWIER_SWI22;
|
||||
|
||||
// 1 = second-highest priority of STM32L0/F0
|
||||
#if defined(__MBED__)
|
||||
NVIC_SetPriority(ADC1_COMP_IRQn, 1);
|
||||
NVIC_EnableIRQ(ADC1_COMP_IRQn);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef PIN_I_LOAD_COMP
|
||||
|
||||
extern "C" void ADC1_COMP_IRQHandler(void)
|
||||
{
|
||||
// interrupt called because of COMP2?
|
||||
if (COMP2->CSR & COMP_CSR_COMP2VALUE) {
|
||||
// Load should be switched off by LPTIM trigger already. This interrupt
|
||||
// is mainly used to indicate the failure.
|
||||
load.stop(ERR_LOAD_SHORT_CIRCUIT);
|
||||
}
|
||||
|
||||
// clear interrupt flag
|
||||
EXTI->PR |= EXTI_PR_PIF22;
|
||||
}
|
||||
|
||||
#endif // PIN_I_LOAD_COMP
|
||||
|
||||
|
||||
void LoadOutput::switch_set(bool status)
|
||||
{
|
||||
pgood = status;
|
||||
leds_set(LED_LOAD, status);
|
||||
|
||||
#if defined(__MBED__) && defined(PIN_LOAD_EN)
|
||||
DigitalOut load_enable(PIN_LOAD_EN);
|
||||
if (status == true) {
|
||||
load_enable = 1;
|
||||
}
|
||||
else {
|
||||
load_enable = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__MBED__) && defined(PIN_LOAD_DIS)
|
||||
DigitalOut load_disable(PIN_LOAD_DIS);
|
||||
if (status == true) {
|
||||
#ifdef PIN_I_LOAD_COMP
|
||||
lptim_init();
|
||||
#else
|
||||
load_disable = 0;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
load_disable = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__ZEPHYR__) && defined(DT_SWITCH_LOAD_EN_GPIOS_CONTROLLER)
|
||||
gpio_pin_configure(dev_load, DT_SWITCH_LOAD_EN_GPIOS_PIN, GPIO_DIR_OUT);
|
||||
if (status == true) {
|
||||
gpio_pin_write(dev_load, DT_SWITCH_LOAD_EN_GPIOS_PIN, 1);
|
||||
}
|
||||
else {
|
||||
gpio_pin_write(dev_load, DT_SWITCH_LOAD_EN_GPIOS_PIN, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__ZEPHYR__) && defined(DT_SWITCH_LOAD_DIS_GPIOS_CONTROLLER)
|
||||
gpio_pin_configure(dev_load, DT_SWITCH_LOAD_DIS_GPIOS_PIN, GPIO_DIR_OUT);
|
||||
if (status == true) {
|
||||
#ifdef PIN_I_LOAD_COMP
|
||||
lptim_init();
|
||||
#else
|
||||
gpio_pin_write(dev_load, DT_SWITCH_LOAD_DIS_GPIOS_PIN, 0);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
gpio_pin_write(dev_load, DT_SWITCH_LOAD_DIS_GPIOS_PIN, 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
print_info("Load pgood = %d, state = %u\n", status, state);
|
||||
}
|
||||
|
||||
void LoadOutput::usb_set(bool status)
|
||||
{
|
||||
usb_pgood = status;
|
||||
|
||||
#if defined(__MBED__) && defined(PIN_USB_PWR_EN)
|
||||
DigitalOut usb_pwr_en(PIN_USB_PWR_EN);
|
||||
if (status == true) usb_pwr_en = 1;
|
||||
else usb_pwr_en = 0;
|
||||
#endif
|
||||
#if defined(__MBED__) && defined(PIN_USB_PWR_DIS)
|
||||
DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS);
|
||||
if (status == true) usb_pwr_dis = 0;
|
||||
else usb_pwr_dis = 1;
|
||||
#endif
|
||||
|
||||
#if defined(__ZEPHYR__) && defined(DT_SWITCH_USB_PWR_EN_GPIOS_CONTROLLER)
|
||||
gpio_pin_configure(dev_usb, DT_SWITCH_USB_PWR_EN_GPIOS_PIN, GPIO_DIR_OUT);
|
||||
if (status == true) {
|
||||
gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_EN_GPIOS_PIN, 1);
|
||||
}
|
||||
else {
|
||||
gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_EN_GPIOS_PIN, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__ZEPHYR__) && defined(DT_SWITCH_USB_PWR_DIS_GPIOS_CONTROLLER)
|
||||
gpio_pin_configure(dev_usb, DT_SWITCH_USB_PWR_DIS_GPIOS_PIN, GPIO_DIR_OUT);
|
||||
if (status == true) {
|
||||
gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_DIS_GPIOS_PIN, 0);
|
||||
}
|
||||
else {
|
||||
gpio_pin_write(dev_usb, DT_SWITCH_USB_PWR_DIS_GPIOS_PIN, 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
print_info("USB pgood = %d, state = %u, en = %d\n", status, usb_state, usb_enable);
|
||||
}
|
||||
|
||||
#ifdef __ZEPHYR__
|
||||
|
||||
void LoadOutput::get_bindings()
|
||||
{
|
||||
#if defined(DT_SWITCH_USB_PWR_EN_GPIOS_CONTROLLER)
|
||||
dev_usb = device_get_binding(DT_SWITCH_USB_PWR_EN_GPIOS_CONTROLLER);
|
||||
#elif defined(DT_SWITCH_USB_PWR_DIS_GPIOS_CONTROLLER)
|
||||
dev_usb = device_get_binding(DT_SWITCH_USB_PWR_DIS_GPIOS_CONTROLLER);
|
||||
#endif
|
||||
|
||||
#if defined(DT_SWITCH_LOAD_EN_GPIOS_CONTROLLER)
|
||||
dev_load = device_get_binding(DT_SWITCH_LOAD_EN_GPIOS_CONTROLLER);
|
||||
#elif defined(DT_SWITCH_LOAD_DIS_GPIOS_CONTROLLER)
|
||||
dev_load = device_get_binding(DT_SWITCH_LOAD_DIS_GPIOS_CONTROLLER);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
+80
-15
@@ -27,7 +27,6 @@
|
||||
#include "leds.h" // LED switching using charlieplexing
|
||||
#include "device_status.h" // log data (error memory, min/max measurements, etc.)
|
||||
#include "data_objects.h" // for access to internal data via ThingSet
|
||||
#include <stm32l0xx_ll_system.h>
|
||||
|
||||
PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT)
|
||||
|
||||
@@ -83,20 +82,24 @@ extern ThingSet ts; // defined in data_objects.cpp
|
||||
|
||||
time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable)
|
||||
|
||||
#define USB_PWR_EN_PORT DT_ALIAS_USB_PWR_EN_GPIOS_CONTROLLER
|
||||
#define USB_PWR_EN_PIN DT_ALIAS_USB_PWR_EN_GPIOS_PIN
|
||||
|
||||
void main(void)
|
||||
{
|
||||
u32_t cnt = 0;
|
||||
struct device *dev_usb_en;
|
||||
|
||||
dev_usb_en = device_get_binding(USB_PWR_EN_PORT);
|
||||
gpio_pin_configure(dev_usb_en, USB_PWR_EN_PIN, GPIO_DIR_OUT);
|
||||
gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, 1);
|
||||
|
||||
printf("Booting Libre Solar Charge Controller: %s\n", CONFIG_BOARD);
|
||||
|
||||
battery_conf_init(&bat_conf,
|
||||
BAT_TYPE_GEL, // temporary!!
|
||||
CONFIG_BAT_DEFAULT_NUM_CELLS,
|
||||
CONFIG_BAT_DEFAULT_CAPACITY_AH);
|
||||
battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values
|
||||
|
||||
// Configuration from EEPROM
|
||||
data_objects_read_eeprom();
|
||||
ts.set_conf_callback(data_objects_update_conf); // after each configuration change, data should be written back to EEPROM
|
||||
//ts.set_user_password(THINGSET_USER_PASSWORD); // passwords defined in config.h (see template)
|
||||
//ts.set_maker_password(THINGSET_MAKER_PASSWORD);
|
||||
|
||||
// ADC, DMA and sensor calibration
|
||||
adc_setup();
|
||||
dma_setup();
|
||||
@@ -112,22 +115,82 @@ void main(void)
|
||||
// initialize all extensions and external communication interfaces
|
||||
ext_mgr.enable_all();
|
||||
|
||||
#if CONFIG_HV_TERMINAL_SOLAR || CONFIG_LV_TERMINAL_SOLAR || CONFIG_PWM_TERMINAL_SOLAR
|
||||
solar_terminal.init_solar();
|
||||
#endif
|
||||
|
||||
#if CONFIG_HV_TERMINAL_NANOGRID
|
||||
grid_terminal.init_nanogrid();
|
||||
#endif
|
||||
|
||||
charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system
|
||||
battery_init_dc_bus(&bat_terminal, &bat_conf, charger.num_batteries);
|
||||
load_terminal.init_load(bat_conf.voltage_absolute_max * charger.num_batteries);
|
||||
|
||||
k_sleep(2000); // safety feature: be able to re-flash before starting
|
||||
|
||||
while (1) {
|
||||
//gpio_pin_write(dev_usb_en, USB_PWR_EN_PIN, cnt % 2);
|
||||
|
||||
leds_update_1s();
|
||||
leds_update_soc(charger.soc, dev_stat.has_error(ERR_LOAD_LOW_SOC));
|
||||
|
||||
update_measurements();
|
||||
printf("Vbat: %f\n", bat_terminal.voltage);
|
||||
|
||||
cnt++;
|
||||
k_sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void control_thread()
|
||||
{
|
||||
int counter = 0;
|
||||
while (1) {
|
||||
|
||||
// convert ADC readings to meaningful measurement values
|
||||
update_measurements();
|
||||
|
||||
// alerts should trigger only for transients, so update based on actual voltage
|
||||
adc_set_lv_alerts(lv_terminal.voltage * 1.2, lv_terminal.voltage * 0.8);
|
||||
|
||||
#if CONFIG_HAS_PWM_SWITCH
|
||||
ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal);
|
||||
//pwm_switch.control();
|
||||
leds_set_charging(pwm_switch.active());
|
||||
#endif
|
||||
|
||||
#if CONFIG_HAS_DCDC_CONVERTER
|
||||
ports_update_current_limits(&dcdc_lv_port, &bat_terminal, &load_terminal);
|
||||
dcdc.control(); // control of DC/DC including MPPT algorithm
|
||||
leds_set_charging(half_bridge_enabled());
|
||||
#endif
|
||||
|
||||
load.control();
|
||||
|
||||
if (counter % CONTROL_FREQUENCY == 0) {
|
||||
// called once per second (this timer is much more accurate than time(NULL) based on LSI)
|
||||
// see also here: https://github.com/ARMmbed/mbed-os/issues/9065
|
||||
timestamp++;
|
||||
counter = 0;
|
||||
|
||||
// energy + soc calculation must be called exactly once per second
|
||||
#if CONFIG_HAS_DCDC_CONVERTER
|
||||
hv_terminal.energy_balance();
|
||||
#endif
|
||||
|
||||
#if CONFIG_HAS_PWM_SWITCH
|
||||
pwm_terminal.energy_balance();
|
||||
#endif
|
||||
|
||||
lv_terminal.energy_balance();
|
||||
load_terminal.energy_balance();
|
||||
dev_stat.update_energy();
|
||||
dev_stat.update_min_max_values();
|
||||
charger.update_soc(&bat_conf);
|
||||
}
|
||||
|
||||
counter++;
|
||||
k_sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
void ext_mgr_thread()
|
||||
{
|
||||
u32_t cnt = 0;
|
||||
@@ -144,7 +207,9 @@ void ext_mgr_thread()
|
||||
}
|
||||
}
|
||||
|
||||
K_THREAD_DEFINE(leds_id, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, K_NO_WAIT);
|
||||
K_THREAD_DEFINE(control_thread_id, 1024, control_thread, NULL, NULL, NULL, 2, 0, 2000);
|
||||
|
||||
K_THREAD_DEFINE(leds_thread, 256, leds_update_thread, NULL, NULL, NULL, 4, 0, K_NO_WAIT);
|
||||
|
||||
K_THREAD_DEFINE(ext_thread, 1024, ext_mgr_thread, NULL, NULL, NULL, 6, 0, 1000);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user