Initial support of MPPT 2420 HPX board

- Some flags changed to make the firmware compile for new STM32G431 MCU
- Zephyr board definition added
- Serial, OLED display and LED working
- ADC compiling, but not yet working
This commit is contained in:
Martin Jaeger
2020-01-23 14:41:44 +01:00
parent d5ce578aeb
commit 24f751b2e5
20 changed files with 600 additions and 27 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ const uint16_t eeprom_data_objects[] = {
0xA6 // day count
};
#ifndef UNIT_TEST
#if !defined(UNIT_TEST) && (defined(STM32F0) || defined(STM32L0))
uint32_t _calc_crc(const uint8_t *buf, size_t len)
{
+18 -25
View File
@@ -10,15 +10,16 @@
#include "pcb.h"
#include "config.h"
#include <stdio.h>
static int _pwm_resolution;
static float _min_duty;
static float _max_duty;
static uint8_t _deadtime_clocks;
static bool _enabled;
#ifndef UNIT_TEST
#if defined(STM32L0)
class PWM_TIM3
{
@@ -27,23 +28,14 @@ class PWM_TIM3
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
#ifdef __MBED__
@@ -114,7 +106,8 @@ class PWM_TIM3
}
};
#if defined(STM32F0)
#elif defined(STM32F0)
class PWM_TIM1
{
public:
@@ -200,17 +193,12 @@ class PWM_TIM1
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 */
#else /* UNIT_TEST */
#ifdef UNIT_TEST
const uint32_t SystemCoreClock = 24000000;
#endif
class PWM_UT
{
@@ -249,10 +237,16 @@ 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 */
#if (PWM_TIM == 1)
typedef PWM_TIM1 PWM_TIM_HW;
#elif (PWM_TIM == 3)
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
@@ -265,9 +259,8 @@ void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_d
// be handled nicely, parentheses make this more explicit
_deadtime_clocks = ((SystemCoreClock / (1000000)) * deadtime_ns) / 1000;
// set PWM frequency and resolution (in fact half the resolution)
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;
@@ -294,7 +287,7 @@ void half_bridge_set_duty_cycle(float duty)
{
_half_bridge_set_duty_cycle(_pwm_resolution * duty);
}
#include <stdio.h>
void half_bridge_duty_cycle_step(int delta)
{
_half_bridge_set_duty_cycle(PWM_TIM_HW::get_ccr() + delta);
+2
View File
@@ -208,7 +208,9 @@ void LoadOutput::stop(uint32_t error_flag)
// flicker the load LED if failure was most probably caused by the user
if (error_flag & (ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) {
#ifdef LED_LOAD
leds_flicker(LED_LOAD);
#endif
oc_timestamp = uptime();
}
}
+2
View File
@@ -155,7 +155,9 @@ extern "C" void ADC1_COMP_IRQHandler(void)
void LoadOutput::switch_set(bool status)
{
pgood = status;
#ifdef LED_LOAD
leds_set(LED_LOAD, status);
#endif
#if defined(__MBED__) && defined(PIN_LOAD_EN)
DigitalOut load_enable(PIN_LOAD_EN);
+16
View File
@@ -19,6 +19,11 @@
#define STM32F072
#endif
#ifdef CONFIG_SOC_STM32G431XX
#define STM32G4
#define STM32G431
#endif
#ifdef STM32L073
#include "stm32l073xx.h"
#endif
@@ -27,6 +32,10 @@
#include "stm32f072xx.h"
#endif
#ifdef STM32G431
#include "stm32g431xx.h"
#endif
// factory calibration values for internal voltage reference and temperature sensor
// (see MCU datasheet, not Reference Manual)
#if defined(STM32F0)
@@ -43,6 +52,13 @@
const uint16_t TSENSE_CAL2 = *((uint16_t *)0x1FF8007E);
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
#define TSENSE_CAL2_VALUE 130.0 // temperature of second calibration point
#elif defined(STM32G4)
const uint16_t VREFINT_CAL = *((uint16_t *)0x1FFF75AA); // VREFINT @3.0V/30°C
#define VREFINT_VALUE 3000 // mV
const uint16_t TSENSE_CAL1 = *((uint16_t *)0x1FFF75A8);
const uint16_t TSENSE_CAL2 = *((uint16_t *)0x1FFF75CA);
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
#define TSENSE_CAL2_VALUE 110.0 // temperature of second calibration point
#elif defined(UNIT_TEST)
#define VREFINT_CAL (4096 * 1.224 / 3.0) // VREFINT @3.0V/25°C
#define VREFINT_VALUE 3000 // mV
+2
View File
@@ -24,6 +24,8 @@
#include "pcbs/mppt_1210_hus_0v4.h"
#elif defined(CONFIG_BOARD_MPPT_1210_HUS_0V7)
#include "pcbs/mppt_1210_hus_0v7.h"
#elif defined(CONFIG_BOARD_MPPT_2420_HPX)
#include "pcbs/mppt_2420_hpx_0v1.h"
#elif defined(CONFIG_BOARD_PWM_2420_LUS_0V2) || defined(CONFIG_BOARD_PWM_2420_LUS_0V3)
#include "pcbs/pwm_2420_lus.h"
#elif defined(UNIT_TEST)
+107
View File
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2020 Martin Jäger / Libre Solar
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef MPPT_1210_HUS_0V4_H
#define MPPT_1210_HUS_0V4_H
#ifdef __MBED__
#include "mbed.h"
#elif defined(__ZEPHYR__)
#include <zephyr.h>
#endif
#define DEVICE_TYPE "MPPT-1210-HUS"
#define HARDWARE_VERSION "v0.4"
// specify features of charge controller
#define CONFIG_HAS_DCDC_CONVERTER 1
#define CONFIG_HAS_PWM_SWITCH 0
#define CONFIG_HAS_LOAD_OUTPUT 1
// DC/DC converter settings
#define PWM_FREQUENCY 50 // kHz 50 = better for cloud solar to increase efficiency
#define PWM_DEADTIME 230 // ns
//#define PWM_TIM 1 // use TIM1 timer
#define DCDC_CURRENT_MAX 10 // PCB maximum DCDC output current
#define LOAD_CURRENT_MAX 10 // PCB maximum load switch current
#define LOW_SIDE_VOLTAGE_MAX 16 // Maximum voltage at battery port (V)
#define HIGH_SIDE_VOLTAGE_MAX 55 // Maximum voltage at PV input port (V)
#define PIN_UEXT_TX PA_2
#define PIN_UEXT_RX PA_3
#define PIN_UEXT_SCL PB_6
#define PIN_UEXT_SDA PB_7
#define PIN_UEXT_MISO PB_4
#define PIN_UEXT_MOSI PB_5
#define PIN_UEXT_SCK PB_3
#define PIN_UEXT_SSEL PA_15
#define PIN_SWD_TX PA_9
#define PIN_SWD_RX PA_10
#define PIN_LOAD_EN PC_13
#define PIN_USB_PWR_EN PB_12
#define PIN_REF_I_DCDC PA_4
enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
// assignment LED numbers on PCB to their meaning
#define NUM_LEDS 1
#define LED_PWR 0 // LED1
// LED pins and pin state configuration to switch above LEDs on
#define NUM_LED_PINS 1
// defined in board definition pinmux.c
extern const char *led_ports[CONFIG_NUM_LED_PINS];
extern const int led_pins[CONFIG_NUM_LED_PINS];
const enum pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
{ PIN_HIGH }, // LED1
};
// pin definition only needed in adc_dma.cpp to detect if they are present on the PCB
#define PIN_ADC_TEMP_FETS PA_5
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
#define NTC_SERIES_RESISTOR 10000.0
#define ADC_GAIN_V_BAT (105.6 / 5.6) // both voltage dividers: 100k + 5.6k
#define ADC_GAIN_V_SOLAR (105.6 / 5.6)
#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm
#define ADC_GAIN_I_DCDC (1000 / 4 / 50)
// position in the array written by the DMA controller
enum {
ADC_POS_V_BAT, // ADC 0 (PA_0)
ADC_POS_V_SOLAR, // ADC 1 (PA_1)
ADC_POS_TEMP_FETS, // ADC 5 (PA_5)
ADC_POS_I_DCDC, // ADC 7 (PA_7)
ADC_POS_I_LOAD, // ADC 6 (PA_6)
ADC_POS_VREF_MCU, // ADC 17
ADC_POS_TEMP_MCU, // ADC 16
NUM_ADC_CH // trick to get the number of elements
};
// selected ADC 1 channels (has to match with above enum)
#define NUM_ADC_1_CH 5
const uint32_t adc_1_sequence[] = {
LL_ADC_CHANNEL_12, // V_BAT
LL_ADC_CHANNEL_15, // V_SOLAR
LL_ADC_CHANNEL_6, // TEMP_FETS
LL_ADC_CHANNEL_1, // I_DCDC
LL_ADC_CHANNEL_2, // I_LOAD
LL_ADC_CHANNEL_VREFINT, // VREF_MCU
LL_ADC_CHANNEL_TEMPSENSOR_ADC1, // TEMP_MCU
};
#endif
-1
View File
@@ -17,7 +17,6 @@
#define PWM_FREQUENCY 50 // kHz
#define PWM_DEADTIME 230 // ns
#define PWM_TIM 3 // use TIM3 timer
#define PWM_CURRENT_MAX 15 // PCB maximum PWM switch (solar) current
#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current