1
0
forked from me/IronOS
Files
IronOS/source/Core/Src/power.cpp
Ben V. Brown 3594604efc Fixes for I2C on Pinecil + USB-PD stack (#1099)
* Remove unused includes

* Adding in submodule

* Move fusb functions to the BSP

* Remove old code

* Creating IronOS PD integration wrapper

* Redirect to wrapper

* pd lib updates

* fix Docker build

* Finish linking across

* Cleanup

* Update Makefile

* Update push.yml

* Update push.yml

* PD -> Compensate for different tick rates

* Update codeql-analysis.yml

* Fix PD #define for @Firebie

* Check irq low at start

* Update BSP.h

* Update main.cpp

* Closer delay

* Update OLED.cpp

* Bugfix trying to start QC too early

* Missing fusb shouldnt hang qc

* Update FreeRTOSConfig.h

* Update the GD drivers

* Update Pinecil IRQ setup

* Redirect printf() to uart

* Update Power.cpp

* Adding extras to PD state

* Update USBPD.cpp

* Delay in printf

* Iterate once before delay on start

* Update usb-pd

* master usb-pd now

* Format gd libs

* Update gd32vf103_bkp.c

* Guard with PD timeout

* Remove CodeQL

* Slow for testing, fix runt pulses at start

* Fix runt pulse in read size 1

* Cleaner probing setup

* Testing delay during stop gen in read 1

* Update I2C driver

* Update gd32vf103_i2c.c

* Cleaning up i2c wrapper a little, given up on dma for rx

* Update preRTOS.cpp

* Update Setup.cpp

* Update MOVThread.cpp

* Slow down UART to work with new clock config

* Better ack setup for 2 byte read

* Cleanup POW_PD so cant be lost in #includes

* tipResistance -> TIP_RESISTANCE

* handle NOP race on len==2

* Update configuration.h

* Dont use neg timeout to mask anymore

* Not required for MHP

* Fix up source display Miniware

* Fix race on PD init

* Update POWThread.cpp

* Update formatting

* MHP format

* Update push.yml

* Faster TS80P I2C

* Bugfix for IRQ handlers

* Correctly handle I2C race on PD access

* Fix CI error (unused var) and MHP IRQ

* Test Pinecil alt ADC mode
2021-10-02 14:48:58 +10:00

86 lines
2.9 KiB
C++

/*
* power.cpp
*
* Created on: 28 Oct, 2018
* Authors: Ben V. Brown, David Hilton <- Mostly David
*/
#include <BSP.h>
#include <Settings.h>
#include <power.hpp>
static int32_t PWMToX10Watts(uint8_t pwm, uint8_t sample);
const int fastPWMChangeoverPoint = 128;
const int fastPWMChangeoverTolerance = 16;
expMovingAverage<uint32_t, wattHistoryFilter> x10WattHistory = {0};
bool shouldBeUsingFastPWMMode(const uint8_t pwmTicks) {
// Determine if we should use slow or fast PWM mode
// Crossover between modes set around the midpoint of the PWM control point
static bool lastPWMWasFast = true;
if (pwmTicks > (fastPWMChangeoverPoint + fastPWMChangeoverTolerance) && lastPWMWasFast) {
lastPWMWasFast = false;
} else if (pwmTicks < (fastPWMChangeoverPoint - fastPWMChangeoverTolerance) && !lastPWMWasFast) {
lastPWMWasFast = true;
}
return lastPWMWasFast;
}
int32_t tempToX10Watts(int32_t rawTemp) {
// mass is in x10J/*C, rawC is raw per degree C
// returns x10Watts needed to raise/lower a mass by rawTemp
// degrees in one cycle.
int32_t x10Watts = TIP_THERMAL_MASS * rawTemp;
return x10Watts;
}
void setTipX10Watts(int32_t mw) {
int32_t outputPWMLevel = X10WattsToPWM(mw, 1);
const bool shouldUseFastPWM = shouldBeUsingFastPWMMode(outputPWMLevel);
setTipPWM(outputPWMLevel, shouldUseFastPWM);
uint32_t actualMilliWatts = PWMToX10Watts(outputPWMLevel, 0);
x10WattHistory.update(actualMilliWatts);
}
uint32_t availableW10(uint8_t sample) {
// P = V^2 / R, v*v = v^2 * 100
// R = R*10
// P therefore is in V^2*100/R*10 = W*10.
uint32_t v = getInputVoltageX10(getSettingValue(SettingsOptions::VoltageDiv), sample); // 100 = 10v
uint32_t availableWattsX10 = (v * v) / TIP_RESISTANCE;
// However, 100% duty cycle is not possible as there is a dead time while the ADC takes a reading
// Therefore need to scale available milliwats by this
// avMw=(AvMw*powerPWM)/totalPWM.
availableWattsX10 = availableWattsX10 * powerPWM;
availableWattsX10 /= totalPWM;
// availableMilliWattsX10 is now an accurate representation
return availableWattsX10;
}
uint8_t X10WattsToPWM(int32_t x10Watts, uint8_t sample) {
// Scale input x10Watts to the pwm range available
if (x10Watts < 0) {
// keep the battery voltage updating the filter
getInputVoltageX10(getSettingValue(SettingsOptions::VoltageDiv), sample);
return 0;
}
// Calculate desired x10Watts as a percentage of availableW10
uint32_t pwm;
pwm = (powerPWM * x10Watts) / availableW10(sample);
if (pwm > powerPWM) {
// constrain to max PWM counter
pwm = powerPWM;
}
return pwm;
}
static int32_t PWMToX10Watts(uint8_t pwm, uint8_t sample) {
uint32_t maxMW = availableW10(sample); // Get the milliwatts for the max pwm period
// Then convert pwm into percentage of powerPWM to get the percentage of the max mw
return (((uint32_t)pwm) * maxMW) / powerPWM;
}