* 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
173 lines
4.8 KiB
C++
173 lines
4.8 KiB
C++
/* mbed library for half bridge driver PWM generation
|
|
* Copyright (c) 2016-2017 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.
|
|
*/
|
|
|
|
#ifndef UNIT_TEST
|
|
|
|
#include "half_bridge.h"
|
|
#include "pcb.h"
|
|
|
|
#if defined(STM32F0) && (PWM_TIM == 1)
|
|
|
|
static int _pwm_resolution;
|
|
static float _min_duty;
|
|
static float _max_duty;
|
|
|
|
static bool _enabled;
|
|
|
|
void _init_registers(int freq_kHz, int deadtime_ns)
|
|
{
|
|
// Enable peripheral clock of GPIOA and GPIOB
|
|
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
|
|
|
|
// Enable TIM1 clock
|
|
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
|
|
|
|
// Select alternate function mode on PA8 und PB13
|
|
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) | GPIO_MODER_MODER8_1;
|
|
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER13)) | GPIO_MODER_MODER13_1;
|
|
|
|
// Select AF2 on PA8 (TIM1_CH1)
|
|
GPIOA->AFR[1] |= 0x2 << ((8 - 8)*4); // AFR[1] for pins 8-15
|
|
|
|
// Select AF2 on PB13 (TIM1_CH1N)
|
|
GPIOB->AFR[1] |= 0x2 << ((13 - 8)*4); // AFR[1] for pins 8-15
|
|
|
|
// No prescaler --> timer frequency = 48 MHz
|
|
TIM1->PSC = 0;
|
|
|
|
// Capture/Compare Mode Register 1
|
|
// OC1M = 110: Select PWM mode 1 on OC1
|
|
// OC1PE = 1: Enable preload register on OC1 (reset value)
|
|
TIM1->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
|
|
|
// Capture/Compare Enable Register
|
|
// CC1E = 1: Enable the output on OC1
|
|
// CC1P = 0: Active high polarity on OC1 (default)
|
|
// CC1NE = 1: Enable the output on OC1N
|
|
// CC1NP = 0: Active high polarity on OC1N (default)
|
|
TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE;// | TIM_CCER_CC1NP;
|
|
|
|
// Control Register 1
|
|
// TIM_CR1_CMS = 01: Select center-aligned mode 1
|
|
// TIM_CR1_CEN = 1: Counter enable
|
|
TIM1->CR1 |= TIM_CR1_CMS_0 | TIM_CR1_CEN;
|
|
|
|
// Control Register 2
|
|
// OIS1 = OIS1N = 0: Output Idle State is set to off
|
|
//TIM1->CR2 |= ;
|
|
|
|
// Force update generation (UG = 1)
|
|
TIM1->EGR |= TIM_EGR_UG;
|
|
|
|
// set PWM frequency and resolution
|
|
_pwm_resolution = SystemCoreClock / (freq_kHz * 1000);
|
|
|
|
// Auto Reload Register
|
|
// center-aligned mode --> divide resolution by 2
|
|
TIM1->ARR = _pwm_resolution / 2;
|
|
|
|
uint8_t deadtime_clocks = (SystemCoreClock / 1000 / 1000) * deadtime_ns / 1000;
|
|
|
|
// Break and Dead-Time Register
|
|
// MOE = 1: Main output enable
|
|
// OSSR = 0: Off-state selection for Run mode -> OC/OCN = 0
|
|
// OSSI = 0: Off-state selection for Idle mode -> OC/OCN = 0
|
|
TIM1->BDTR |= (deadtime_clocks & (uint32_t)0x7F); // ensure that only the last 7 bits are changed
|
|
|
|
// Lock Break and Dead-Time Register
|
|
// TODO: does not work properly... maybe HW bug?
|
|
TIM1->BDTR |= TIM_BDTR_LOCK_1 | TIM_BDTR_LOCK_0;
|
|
}
|
|
|
|
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;
|
|
// 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;
|
|
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 (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;
|
|
}
|
|
|
|
#endif /* PWM_TIM */
|
|
|
|
#endif /* UNIT_TEST */
|