1
0
forked from me/IronOS

Fix simple mode crash / lockup from buffer overflow

This commit is contained in:
Ben V. Brown
2019-01-01 17:49:11 +11:00
parent f023761545
commit 6389c00e1d
6 changed files with 29 additions and 18 deletions

View File

@@ -18,7 +18,7 @@ void setupPower(uint8_t resistance);
int32_t tempToMilliWatts(int32_t rawTemp, uint16_t mass, uint8_t rawC);
void setTipMilliWatts(int32_t mw);
uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor);
uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor,uint8_t sample=0);
int32_t PWMToMilliWatts(uint8_t pwm, uint8_t divisor);
#endif /* POWER_HPP_ */

View File

@@ -29,12 +29,13 @@ void FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t MemAddress,
// Wait up to 1 second for the mutex
if (xSemaphoreTake(I2CSemaphore, (TickType_t)50) == pdTRUE) {
#ifdef I2CUSESDMA
if (HAL_I2C_Mem_Read_DMA(i2c, DevAddress, MemAddress, MemAddSize,
pData, Size) != HAL_OK) {
if (HAL_I2C_Mem_Read(i2c, DevAddress, MemAddress, MemAddSize,
pData, Size,500) != HAL_OK) {
I2C1_ClearBusyFlagErratum();
xSemaphoreGive(I2CSemaphore);
}
xSemaphoreGive(I2CSemaphore);
#else
HAL_I2C_Mem_Read(i2c, DevAddress, MemAddress, MemAddSize, pData, Size,
@@ -68,12 +69,13 @@ void FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
// Wait up to 1 second for the mutex
if (xSemaphoreTake(I2CSemaphore, (TickType_t)50) == pdTRUE) {
#ifdef I2CUSESDMA
if (HAL_I2C_Mem_Write_DMA(i2c, DevAddress, MemAddress, MemAddSize,
pData, Size) != HAL_OK) {
if (HAL_I2C_Mem_Write(i2c, DevAddress, MemAddress, MemAddSize,
pData, Size,500) != HAL_OK) {
I2C1_ClearBusyFlagErratum();
xSemaphoreGive(I2CSemaphore);
}
xSemaphoreGive(I2CSemaphore);
#else
if (HAL_I2C_Mem_Write(i2c, DevAddress, MemAddress, MemAddSize, pData,
Size, 5000) != HAL_OK) {

View File

@@ -356,7 +356,7 @@ void OLED::drawHeatSymbol(uint8_t state) {
// Draw symbol 14
// Then draw over it, the bottom 5 pixels always stay. 8 pixels above that are
// the levels masks the symbol nicely
state /= 12; // 0-> 8 range
state /= 31; // 0-> 8 range
// Then we want to draw down (16-(5+state)
uint8_t cursor_x_temp = cursor_x;
drawSymbol(14);

View File

@@ -420,10 +420,10 @@ int16_t calculateMaxVoltage(uint8_t useHP) {
}
#endif
volatile uint32_t pendingPWM = 0;
volatile uint8_t pendingPWM = 0;
void setTipPWM(uint8_t pulse) {
PWMSafetyTimer = 50; // This is decremented in the handler for PWM so that the tip pwm is
PWMSafetyTimer = 10; // This is decremented in the handler for PWM so that the tip pwm is
// disabled if the PID task is not scheduled often enough.
pendingPWM = pulse;

View File

@@ -73,12 +73,17 @@ int main(void) {
GUITaskHandle = osThreadCreate(osThread(GUITask), NULL);
/* definition and creation of PIDTask */
osThreadDef(PIDTask, startPIDTask, osPriorityRealtime, 0, 2 * 1024 / 4);
osThreadDef(PIDTask, startPIDTask, osPriorityRealtime, 0, 3 * 1024 / 4);
PIDTaskHandle = osThreadCreate(osThread(PIDTask), NULL);
if (PCBVersion < 3) {
/* definition and creation of MOVTask */
osThreadDef(MOVTask, startMOVTask, osPriorityNormal, 0, 3 * 1024 / 4);
osThreadDef(MOVTask, startMOVTask, osPriorityNormal, 0, 4 * 1024 / 4);
MOVTaskHandle = osThreadCreate(osThread(MOVTask), NULL);
#ifdef LOCAL_BUILD
//Test that there was enough ram in the FreeRToS pool to allocate all the tasks
if (MOVTaskHandle == 0)
asm("bkpt");
#endif
}
/* Start scheduler */
@@ -100,7 +105,7 @@ void GUIDelay() {
// This limits the re-draw rate to the LCD and also lets the DMA run
// As the gui task can very easily fill this bus with transactions, which will
// prevent the movement detection from running
osDelay(50);
osDelay(75);
}
void gui_drawTipTemp(bool symbol) {
// Draw tip temp handling unit conversion & tolerance near setpoint
@@ -265,7 +270,7 @@ static void gui_drawBatteryIcon() {
// User is on a lithium battery
// we need to calculate which of the 10 levels they are on
uint8_t cellCount = systemSettings.cutoutSetting + 2;
uint16_t cellV = getInputVoltageX10(systemSettings.voltageDiv, 0)
uint32_t cellV = getInputVoltageX10(systemSettings.voltageDiv, 0)
/ cellCount;
// Should give us approx cell voltage X10
// Range is 42 -> 33 = 9 steps therefore we will use battery 1-10
@@ -1025,6 +1030,8 @@ void startPIDTask(void const *argument __unused) {
HAL_IWDG_Refresh(&hiwdg);
} else {
asm("bkpt");
//ADC interrupt timeout
setTipMilliWatts(0);
setTipPWM(0);
@@ -1196,7 +1203,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c __unused) {
FRToSI2C::CpltCallback();
}
void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c __unused) {
asm("bkpt");
//asm("bkpt");
FRToSI2C::CpltCallback();
}

View File

@@ -28,7 +28,7 @@ int32_t tempToMilliWatts(int32_t rawTemp, uint16_t mass, uint8_t rawC) {
}
void setTipMilliWatts(int32_t mw) {
int32_t output = milliWattsToPWM(mw, systemSettings.voltageDiv / 10);
int32_t output = milliWattsToPWM(mw, systemSettings.voltageDiv / 10,1);
setTipPWM(output);
uint16_t actualMilliWatts = PWMToMilliWatts(output,
systemSettings.voltageDiv / 10);
@@ -36,12 +36,14 @@ void setTipMilliWatts(int32_t mw) {
milliWattHistory.update(actualMilliWatts);
}
uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor) {
uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor, uint8_t sample) {
//P = V^2 / R, v*v = v^2 * 100
// R = R*10
// P therefore is in V^2*10/R = W*10.
// Scale input milliWatts to the pwm rate
int32_t v = getInputVoltageX10(divisor, 1); // 100 = 10v
if (milliWatts == 0)
return 0;
int32_t v = getInputVoltageX10(divisor, sample); // 100 = 10v
int32_t availableMilliWatts = v * v / tipResistance;
//int32_t pwm = ((powerPWM * totalPWM / powerPWM) * milliWatts) / availableMilliWatts;
@@ -51,8 +53,8 @@ uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor) {
} else if (pwm < 0) {
pwm = 0;
}
if (milliWatts == 0)
pwm = 0;
return pwm;
}