Update master branch with new features from development branch (#20)

* Major changes in DC/DC control and board support + ThingSet library added

* Small platformio.ini fixes
- added libignore for mbed-USBDevice to all STM32F072 builds
- add map file generation. Use text editor or AMAP (http://www.sikorskiy.net/prj/amap/)
to view and learn where the precious flash and ram goes to.
- fixed board define for LS 0.10 board

* Travis fix and more
- Now runs only a selected subset of all configurations (all STM32F072/L073)
- Added a src_filter to the cloudsolar_03 since L073 does not have CAN.
  Maybe there is a better way to do this. Also added libignore for USB
  to this environment.
- Added memory.map to .gitignore

* STML0 support improved + small changes

- STM32L0 now widely supported: PWM signal generation, ADC via DMA and display via I2C are working

- Old files for USB support (only F0) included into repository, as files in original mbed repository don't compile.

- CAN bus support only for F0 (added header defines to allow compilation)

* Cell & Pack Config refactoring: First work on #13.
Reorganized battery init a bit to get a better idea of how to split things.
All known chemistries are now supported with safe values (please check the values).

Added data for NMC (aka "normal" LiIo cells such as most 18650)
A configuration now declares itself to be valid or not after battery_init.

BAT_TYPE_NONE (formerly BAT_TYPE_DEFAULT, which equals 0) will not pass check
and thus is an invalid configuration, as a result an unconfigured
battery_t struct will not be considered valid by accident.

We should not continue to start normal operation if a battery configuration is not valid.
However, this is not yet implemented.

Also slightly changed interface to battery_init, now we pass the number of cells.

Moved the static pub_enabled into main.cpp to get ride of a ton of "unused static variable"
warnings.

* ThingSet moved to separate library

* Removed serious bug in PWM signal generation resulting in PWM with 0% duty cycle

* LED control via PWM for STM32L0 board added

* Gets rid of USB deprecated warnings in application code
This simply disables any warning regarding the use
of deprecated internals in mbed for now.
Since the "new" USBDevice code does not run on F0 or L0, we use old
one which works but generates warnings a lot.
If they ever remove the deprecated interface, we'll know sure enough
since then code will no longer compile.

* More battery init refactoring

In addition to the mandatory arguments cell type, number in series and capacity,
the code now accepts a partially configure battery_t structure (you provide
various voltage levels and charge currents) for Lithium based cells, checks the configuration values for sanity
and tries to adjust if outside valid operational limits. The defaults try
to balance between charging stress and capacity loss.
Charge is by default to about 90% maximum full charge, and discharge stops around
20%-30%.

For lead acid cells all voltage levels and currents are set / calculated based
on the mandatory parameters, no configuration of voltage levels possible.

We may change that, but I don't have enough no knowledge about valid ranges
etc.

I added a high voltage lithium chemistry for cells which have typically
4.35 voltage limit (vs. 4.2 normally).

* Added user configuration for battery and system operation mode
Copy the most relevant template to config.cpp and then compile.
config.cpp is intentionally not part of the git managed source code
since it represents the user specific configuration part
which varies across different firmware builds.

* Turn off MBED Deprecated Warning for Old USB Stack attempt 2
This time cross platform compatible...
And also fix the travis build issue

* Boost mode added and first updates for timer-based DC/DC control

- system_mode now distinguishes between MPPT_BOOST and MPPT_BUCK
- new function system_control() which does all important and fast control
stuff necessary for the DC/DC (will be included into interrupt in the
future)
- small correction in _dcdc_output_control() algorithm after test with e-bike
battery solar charging

* Naming of structs and enums adjusted

* Documentation amended and separate docs folder created.

* UNIT_TEST defines removed

* Timer for DC/DC control implemented, ADC timer updated

* Files restructured / renamed

- config.h is now pcb.h and contains only hardware-specific settings
- anything which communicates to the outside world (display, serial interface, etc)
  is now renamed to more generic "interface" instead of "output" and "communication".
  Use interface_<DEVICE>.cpp for implementations of specific interfaces.

* Updates in user configuration and interfaces

- Battery configuration can now be adjusted in battery_init_user in config.cpp
- New function battery_update_settings implemented to check new battery configuration for plausibility (also needed for later user-configuration via serial interface)
- Templates for GSM and LoRa interfaces created
- Restructuring of main to allow different interface configurations

* Travis CI adjusted to new config.h

* Load output templates added

* ThingSet library updated, interface processing updated, publication functions added

* Load output implementation updated and moved to load.cpp/h

* Energy calculation updated and eTotal changed to uint32_t

* Update firmware.md

* Issue monitor_speed typing mistake

We found a typing issue: "monitor_speed" is mistyped as "monitor_port".

* Major updates of recent months
This commit is contained in:
Martin Jäger
2019-02-16 09:53:36 +01:00
committed by GitHub
parent d1e5dd32c0
commit 112f36b08b
94 changed files with 14306 additions and 1859 deletions
+282
View File
@@ -0,0 +1,282 @@
#include "hardware.h"
#include "pcb.h"
#include "mbed.h"
#include "half_bridge.h"
#include "us_ticker_data.h"
extern bool led_states[];
#ifdef PIN_LOAD_EN
DigitalOut load_enable(PIN_LOAD_EN);
#else
DigitalOut load_disable(PIN_LOAD_DIS);
#endif
#ifdef PIN_USB_PWR_EN
DigitalOut usb_pwr_en(PIN_USB_PWR_EN);
#endif
#ifdef PIN_UEXT_DIS
DigitalOut uext_dis(PIN_UEXT_DIS);
#endif
//----------------------------------------------------------------------------
void init_load()
{
#ifdef PIN_UEXT_DIS
uext_dis = 0;
#endif
#ifdef PIN_USB_PWR_EN
usb_pwr_en = 1;
#endif
}
//----------------------------------------------------------------------------
void hw_load_switch(bool enabled)
{
#ifdef PIN_LOAD_EN
if (enabled) load_enable = 1;
else load_enable = 0;
#else
if (enabled) load_disable = 0;
else load_disable = 1;
#endif
#ifdef LED_LOAD
if (enabled) led_states[LED_LOAD] = 1;
else led_states[LED_LOAD] = 0;
#endif
}
//----------------------------------------------------------------------------
void hw_usb_out(bool enabled)
{
#ifdef PIN_USB_PWR_EN
if (enabled) usb_pwr_en = 1;
else usb_pwr_en = 0;
#endif
}
#if defined(STM32F0)
void control_timer_start(int freq_Hz) // max. 10 kHz
{
// Enable TIM16 clock
RCC->APB2ENR |= RCC_APB2ENR_TIM16EN;
// Set timer clock to 10 kHz
TIM16->PSC = SystemCoreClock / 10000 - 1;
// Interrupt on timer update
TIM16->DIER |= TIM_DIER_UIE;
// Auto Reload Register sets interrupt frequency
TIM16->ARR = 10000 / freq_Hz - 1;
// 1 = second-highest priority of STM32L0/F0
NVIC_SetPriority(TIM16_IRQn, 1);
NVIC_EnableIRQ(TIM16_IRQn);
// Control Register 1
// TIM_CR1_CEN = 1: Counter enable
TIM16->CR1 |= TIM_CR1_CEN;
}
extern "C" void TIM16_IRQHandler(void)
{
TIM16->SR &= ~TIM_SR_UIF; // clear update interrupt flag to restart timer
system_control();
}
#elif defined(STM32L0)
void control_timer_start(int freq_Hz) // max. 10 kHz
{
// Enable TIM7 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM7EN;
// Set timer clock to 10 kHz
TIM7->PSC = SystemCoreClock / 10000 - 1;
// Interrupt on timer update
TIM7->DIER |= TIM_DIER_UIE;
// Auto Reload Register sets interrupt frequency
TIM7->ARR = 10000 / freq_Hz - 1;
// 1 = second-highest priority of STM32L0/F0
NVIC_SetPriority(TIM7_IRQn, 1);
NVIC_EnableIRQ(TIM7_IRQn);
// Control Register 1
// TIM_CR1_CEN = 1: Counter enable
TIM7->CR1 |= TIM_CR1_CEN;
}
extern "C" void TIM7_IRQHandler(void)
{
TIM7->SR &= ~(1 << 0);
system_control();
}
#endif
// Reset the watchdog timer
void feed_the_dog()
{
IWDG->KR = 0xAAAA;
}
// configures IWDG (timeout in seconds)
void init_watchdog(float timeout) {
// TODO for STM32L0
#define LSI_FREQ 40000 // approx. 40 kHz according to RM0091
uint16_t prescaler;
IWDG->KR = 0x5555; // Disable write protection of IWDG registers
// set prescaler
if ((timeout * (LSI_FREQ/4)) < 0x7FF) {
IWDG->PR = IWDG_PRESCALER_4;
prescaler = 4;
}
else if ((timeout * (LSI_FREQ/8)) < 0xFF0) {
IWDG->PR = IWDG_PRESCALER_8;
prescaler = 8;
}
else if ((timeout * (LSI_FREQ/16)) < 0xFF0) {
IWDG->PR = IWDG_PRESCALER_16;
prescaler = 16;
}
else if ((timeout * (LSI_FREQ/32)) < 0xFF0) {
IWDG->PR = IWDG_PRESCALER_32;
prescaler = 32;
}
else if ((timeout * (LSI_FREQ/64)) < 0xFF0) {
IWDG->PR = IWDG_PRESCALER_64;
prescaler = 64;
}
else if ((timeout * (LSI_FREQ/128)) < 0xFF0) {
IWDG->PR = IWDG_PRESCALER_128;
prescaler = 128;
}
else {
IWDG->PR = IWDG_PRESCALER_256;
prescaler = 256;
}
// set reload value (between 0 and 0x0FFF)
IWDG->RLR = (uint32_t)(timeout * (LSI_FREQ/prescaler));
//float calculated_timeout = ((float)(prescaler * IWDG->RLR)) / LSI_FREQ;
//printf("WATCHDOG set with prescaler:%d reload value: 0x%X - timeout:%f\n", prescaler, (unsigned int)IWDG->RLR, calculated_timeout);
IWDG->KR = 0xAAAA; // reload
IWDG->KR = 0xCCCC; // start
feed_the_dog();
}
// this function is called by mbed if a serious error occured: error() function called
void mbed_die(void)
{
half_bridge_stop();
hw_load_switch(false);
hw_usb_out(false);
for (int i = 0; i < NUM_LEDS; i++) {
led_states[i] = 0;
}
int led_blink = 1;
while (1) {
for (int led = 0; led < NUM_LEDS; led++) {
led_states[led] = (led % 2) ^ led_blink;
}
led_blink = !led_blink;
wait_ms(100);
// stay here to indicate something was wrong
feed_the_dog();
}
}
#if defined(STM32F0)
#define SYS_MEM_START 0x1FFFC800
#define SRAM_END 0x20003FFF // 16k
#elif defined(STM32L0)
#define SYS_MEM_START 0x1FF00000
#define SRAM_END 0X20004FFF // 20k
#endif
#define MAGIC_CODE 0xDEADBEEF
void start_dfu_bootloader()
{
// place magic code at end of RAM and initiate reset
*((unsigned long *)(SRAM_END - 0xF)) = MAGIC_CODE;
NVIC_SystemReset();
}
// https://stackoverflow.com/questions/42020893/stm32l073rz-rev-z-iap-jump-to-bootloader-system-memory
// This function should be called at the beginning of SystemInit in system_clock.c (mbed target directory)
// As mbed does not provide this functionality, you have to patch the file manually
extern "C" void system_init_hook()
{
if (*((unsigned long *)(SRAM_END - 0xF)) == MAGIC_CODE) {
*((unsigned long *)(SRAM_END - 0xF)) = 0xCAFEFEED; // reset the trigger
// jump function pointer to be initialized and called later
void (*SysMemJump)(void);
// TODO for STM32L0
/*
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.PageAddress = Data_Address;
EraseInitStruct.NbPages = 1;
First_jump = *(__IO uint32_t *)(Data_Address);
if (First_jump == 0) {
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Data_Address, 0xAAAAAAAA);
HAL_FLASH_Lock();
// Reinitialize the Stack pointer and jump to application address
//JumpAddress = *(__IO uint32_t *)(0x1FF00004);
SysMemJump = (void (*)(void)) (*((uint32_t *) (SYS_MEM_START + 4)));
}
else {
HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError);
HAL_FLASH_Lock();
// Reinitialize the Stack pointer and jump to application address
//JumpAddress = (0x1FF00369);
SysMemJump = (void (*)(void)) (*((uint32_t *) (0x1FF00369)));
}
*/
//__set_MSP(0x20002250); // set stack pointer for STM32F0
__set_MSP(SYS_MEM_START);
//__set_MSP(*(__IO uint32_t *)(0x1FF00000));
// point the PC to the System Memory reset vector (+4)
SysMemJump = (void (*)(void)) (*((uint32_t *) (SYS_MEM_START + 4))); // for STM32F0
SysMemJump();
while (42);
}
}