1
0
forked from me/IronOS
This commit is contained in:
Ben V. Brown
2021-04-28 21:08:42 +10:00
parent fe2469fdb5
commit 56a885ed42
4 changed files with 136 additions and 165 deletions

View File

@@ -3,41 +3,39 @@
#include "BSP.h" #include "BSP.h"
#include "I2C_Wrapper.hpp" #include "I2C_Wrapper.hpp"
#include "Model_Config.h" #include "Model_Config.h"
#include "Utils.h"
#include "Pins.h" #include "Pins.h"
#include "Setup.h" #include "Setup.h"
#include "Utils.h"
#include "history.hpp" #include "history.hpp"
#include "main.hpp" #include "main.hpp"
#include <IRQ.h> #include <IRQ.h>
volatile uint16_t PWMSafetyTimer = 0; volatile uint16_t PWMSafetyTimer = 0;
volatile uint8_t pendingPWM = 0; volatile uint8_t pendingPWM = 0;
uint16_t totalPWM = 255; uint16_t totalPWM = 255;
const uint16_t powerPWM = 255; const uint16_t powerPWM = 255;
history<uint16_t, PID_TIM_HZ> rawTempFilter = { { 0 }, 0, 0 }; history<uint16_t, PID_TIM_HZ> rawTempFilter = {{0}, 0, 0};
void resetWatchdog() { void resetWatchdog() { HAL_IWDG_Refresh(&hiwdg); }
HAL_IWDG_Refresh(&hiwdg);
}
#ifdef TEMP_NTC #ifdef TEMP_NTC
// Lookup table for the NTC // Lookup table for the NTC
// Stored as ADCReading,Temp in degC // Stored as ADCReading,Temp in degC
static const uint16_t NTCHandleLookup[] = { static const uint16_t NTCHandleLookup[] = {
// ADC Reading , Temp in C // ADC Reading , Temp in C
11292, 600, // 11292, 600, //
12782, 550, // 12782, 550, //
14380, 500, // 14380, 500, //
16061, 450, // 16061, 450, //
17793, 400, // 17793, 400, //
19541, 350, // 19541, 350, //
21261, 300, // 21261, 300, //
22915, 250, // 22915, 250, //
24465, 200, // 24465, 200, //
25882, 150, // 25882, 150, //
27146, 100, // 27146, 100, //
28249, 50, // 28249, 50, //
29189, 0, // 29189, 0, //
}; };
const int NTCHandleLookupItems = sizeof(NTCHandleLookup) / (2 * sizeof(uint16_t)); const int NTCHandleLookupItems = sizeof(NTCHandleLookup) / (2 * sizeof(uint16_t));
#endif #endif
@@ -45,153 +43,140 @@ const int NTCHandleLookupItems = sizeof(NTCHandleLookup) / (2 * sizeof(uint16_t)
// timers. // timers.
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
// Period has elapsed // Period has elapsed
if (htim->Instance == TIM1) { if (htim->Instance == TIM1) {
// STM uses this for internal functions as a counter for timeouts // STM uses this for internal functions as a counter for timeouts
HAL_IncTick(); HAL_IncTick();
} }
} }
uint16_t getHandleTemperature() { uint16_t getHandleTemperature() {
int32_t result = getADC(0); int32_t result = getADC(0);
return Utils::InterpolateLookupTable(NTCHandleLookup,NTCHandleLookupItems, result); return Utils::InterpolateLookupTable(NTCHandleLookup, NTCHandleLookupItems, result);
} }
uint16_t getTipInstantTemperature() { uint16_t getTipInstantTemperature() { return getADC(2); }
return getADC(2);
}
uint16_t getTipRawTemp(uint8_t refresh) { uint16_t getTipRawTemp(uint8_t refresh) {
if (refresh) { if (refresh) {
uint16_t lastSample = getTipInstantTemperature(); uint16_t lastSample = getTipInstantTemperature();
rawTempFilter.update(lastSample); rawTempFilter.update(lastSample);
return lastSample; return lastSample;
} else { } else {
return rawTempFilter.average(); return rawTempFilter.average();
} }
} }
uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) { uint16_t getInputVoltageX10(uint16_t divisor, uint8_t sample) {
// ADC maximum is 32767 == 3.3V at input == 28.05V at VIN // ADC maximum is 32767 == 3.3V at input == 28.05V at VIN
// Therefore we can divide down from there // Therefore we can divide down from there
// Multiplying ADC max by 4 for additional calibration options, // Multiplying ADC max by 4 for additional calibration options,
// ideal term is 467 // ideal term is 467
static uint8_t preFillneeded = 10; static uint8_t preFillneeded = 10;
static uint32_t samples[BATTFILTERDEPTH]; static uint32_t samples[BATTFILTERDEPTH];
static uint8_t index = 0; static uint8_t index = 0;
if (preFillneeded) { if (preFillneeded) {
for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) for (uint8_t i = 0; i < BATTFILTERDEPTH; i++)
samples[i] = getADC(1); samples[i] = getADC(1);
preFillneeded--; preFillneeded--;
} }
if (sample) { if (sample) {
samples[index] = getADC(1); samples[index] = getADC(1);
index = (index + 1) % BATTFILTERDEPTH; index = (index + 1) % BATTFILTERDEPTH;
} }
uint32_t sum = 0; uint32_t sum = 0;
for (uint8_t i = 0; i < BATTFILTERDEPTH; i++) for (uint8_t i = 0; i < BATTFILTERDEPTH; i++)
sum += samples[i]; sum += samples[i];
sum /= BATTFILTERDEPTH; sum /= BATTFILTERDEPTH;
if (divisor == 0) { if (divisor == 0) {
divisor = 1; divisor = 1;
} }
return sum * 4 / divisor; return sum * 4 / divisor;
} }
bool tryBetterPWM(uint8_t pwm) { bool tryBetterPWM(uint8_t pwm) {
// We dont need this for the MHP30 // We dont need this for the MHP30
return false; return false;
} }
void setTipPWM(uint8_t pulse) { void setTipPWM(uint8_t pulse) {
// We can just set the timer directly // We can just set the timer directly
htim3.Instance->CCR1 = pulse; htim3.Instance->CCR1 = pulse;
} }
void unstick_I2C() { void unstick_I2C() {
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
int timeout = 100; int timeout = 100;
int timeout_cnt = 0; int timeout_cnt = 0;
// 1. Clear PE bit. // 1. Clear PE bit.
hi2c1.Instance->CR1 &= ~(0x0001); hi2c1.Instance->CR1 &= ~(0x0001);
/**I2C1 GPIO Configuration /**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA PB7 ------> I2C1_SDA
*/ */
// 2. Configure the SCL and SDA I/Os as General Purpose Output Open-Drain, High level (Write 1 to GPIOx_ODR). // 2. Configure the SCL and SDA I/Os as General Purpose Output Open-Drain, High level (Write 1 to GPIOx_ODR).
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = SCL_Pin; GPIO_InitStruct.Pin = SCL_Pin;
HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET);
GPIO_InitStruct.Pin = SDA_Pin; GPIO_InitStruct.Pin = SDA_Pin;
HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET);
while (GPIO_PIN_SET != HAL_GPIO_ReadPin(SDA_GPIO_Port, SDA_Pin)) { while (GPIO_PIN_SET != HAL_GPIO_ReadPin(SDA_GPIO_Port, SDA_Pin)) {
// Move clock to release I2C // Move clock to release I2C
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_RESET);
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
asm("nop"); asm("nop");
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET);
timeout_cnt++; timeout_cnt++;
if (timeout_cnt > timeout) if (timeout_cnt > timeout)
return; return;
} }
// 12. Configure the SCL and SDA I/Os as Alternate function Open-Drain. // 12. Configure the SCL and SDA I/Os as Alternate function Open-Drain.
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = SCL_Pin; GPIO_InitStruct.Pin = SCL_Pin;
HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SDA_Pin; GPIO_InitStruct.Pin = SDA_Pin;
HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SCL_GPIO_Port, SCL_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(SDA_GPIO_Port, SDA_Pin, GPIO_PIN_SET);
// 13. Set SWRST bit in I2Cx_CR1 register. // 13. Set SWRST bit in I2Cx_CR1 register.
hi2c1.Instance->CR1 |= 0x8000; hi2c1.Instance->CR1 |= 0x8000;
asm("nop"); asm("nop");
// 14. Clear SWRST bit in I2Cx_CR1 register. // 14. Clear SWRST bit in I2Cx_CR1 register.
hi2c1.Instance->CR1 &= ~0x8000; hi2c1.Instance->CR1 &= ~0x8000;
asm("nop"); asm("nop");
// 15. Enable the I2C peripheral by setting the PE bit in I2Cx_CR1 register // 15. Enable the I2C peripheral by setting the PE bit in I2Cx_CR1 register
hi2c1.Instance->CR1 |= 0x0001; hi2c1.Instance->CR1 |= 0x0001;
// Call initialization function. // Call initialization function.
HAL_I2C_Init(&hi2c1); HAL_I2C_Init(&hi2c1);
} }
uint8_t getButtonA() { uint8_t getButtonA() { return HAL_GPIO_ReadPin(KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET ? 1 : 0; }
return HAL_GPIO_ReadPin(KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET ? uint8_t getButtonB() { return HAL_GPIO_ReadPin(KEY_B_GPIO_Port, KEY_B_Pin) == GPIO_PIN_RESET ? 1 : 0; }
1 : 0;
}
uint8_t getButtonB() {
return HAL_GPIO_ReadPin(KEY_B_GPIO_Port, KEY_B_Pin) == GPIO_PIN_RESET ?
1 : 0;
}
void BSPInit(void) { void BSPInit(void) {}
}
void reboot() { void reboot() { NVIC_SystemReset(); }
NVIC_SystemReset();
}
void delay_ms(uint16_t count) { void delay_ms(uint16_t count) { HAL_Delay(count); }
HAL_Delay(count);
}

View File

@@ -8,9 +8,9 @@
#include "TipThermoModel.h" #include "TipThermoModel.h"
#include "BSP.h" #include "BSP.h"
#include "Settings.h" #include "Settings.h"
#include "Utils.h"
#include "configuration.h" #include "configuration.h"
#include "main.hpp" #include "main.hpp"
#include "Utils.h"
#include "power.hpp" #include "power.hpp"
/* /*
* The hardware is laid out as a non-inverting op-amp * The hardware is laid out as a non-inverting op-amp
@@ -218,9 +218,7 @@ const uint16_t uVtoDegC[] = {
#endif #endif
const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uint16_t)); const int uVtoDegCItems = sizeof(uVtoDegC) / (2 * sizeof(uint16_t));
uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { uint32_t TipThermoModel::convertuVToDegC(uint32_t tipuVDelta) { return Utils::InterpolateLookupTable(uVtoDegC, uVtoDegCItems, tipuVDelta); }
return Utils::InterpolateLookupTable(uVtoDegC,uVtoDegCItems,tipuVDelta);
}
uint32_t TipThermoModel::convertuVToDegF(uint32_t tipuVDelta) { return convertCtoF(convertuVToDegC(tipuVDelta)); } uint32_t TipThermoModel::convertuVToDegF(uint32_t tipuVDelta) { return convertCtoF(convertuVToDegC(tipuVDelta)); }

View File

@@ -7,26 +7,17 @@
#include <Utils.h> #include <Utils.h>
int32_t Utils::InterpolateLookupTable(const uint16_t *lookupTable, int32_t Utils::InterpolateLookupTable(const uint16_t *lookupTable, const int noItems, const uint16_t value) {
const int noItems, const uint16_t value) { if (value) {
if (value) { for (int i = 1; i < (noItems - 1); i++) {
for (int i = 1; i < (noItems - 1); i++) { // If current tip temp is less than current lookup, then this current lookup is the higher point to interpolate
// If current tip temp is less than current lookup, then this current lookup is the higher point to interpolate if (value < lookupTable[i * 2]) {
if (value < lookupTable[i * 2]) { return LinearInterpolate(lookupTable[(i - 1) * 2], lookupTable[((i - 1) * 2) + 1], lookupTable[i * 2], lookupTable[(i * 2) + 1], value);
return LinearInterpolate(lookupTable[(i - 1) * 2], }
lookupTable[((i - 1) * 2) + 1], lookupTable[i * 2], }
lookupTable[(i * 2) + 1], value); return LinearInterpolate(lookupTable[(noItems - 2) * 2], lookupTable[((noItems - 2) * 2) + 1], lookupTable[(noItems - 1) * 2], lookupTable[((noItems - 1) * 2) + 1], value);
} }
} return 0;
return LinearInterpolate(lookupTable[(noItems - 2) * 2],
lookupTable[((noItems - 2) * 2) + 1],
lookupTable[(noItems - 1) * 2],
lookupTable[((noItems - 1) * 2) + 1], value);
}
return 0;
} }
int32_t Utils::LinearInterpolate(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t Utils::LinearInterpolate(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x) { return y1 + (((((x - x1) * 1000) / (x2 - x1)) * (y2 - y1))) / 1000; }
int32_t x) {
return y1 + (((((x - x1) * 1000) / (x2 - x1)) * (y2 - y1))) / 1000;
}

View File

@@ -10,11 +10,8 @@
#include <stdint.h> #include <stdint.h>
class Utils { class Utils {
public: public:
static int32_t InterpolateLookupTable(const uint16_t *lookupTable, static int32_t InterpolateLookupTable(const uint16_t *lookupTable, const int noItems, const uint16_t value);
const int noItems, const uint16_t value); static int32_t LinearInterpolate(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x);
static int32_t LinearInterpolate(int32_t x1, int32_t y1, int32_t x2,
int32_t y2, int32_t x);
}; };
#endif /* CORE_DRIVERS_UTILS_H_ */ #endif /* CORE_DRIVERS_UTILS_H_ */