diff --git a/Core/Src/app_temp.c b/Core/Src/app_temp.c index 0bba73f..fbd0b9f 100644 --- a/Core/Src/app_temp.c +++ b/Core/Src/app_temp.c @@ -11,136 +11,162 @@ #include "adc.h" #include "snprintf.h" #include "app_safety.h" +#include "eeprom_emul_types.h" +#include "eeprom_emul.h" +#include "ee_addresses.h" +#include "transmute.h" /* DMA dest */ static volatile uint16_t adc_values[4]; const float V_REFINT = 1.23f; -#define AVERAGEBUF_DEPTH 8 -#define OVENTEMP_HISTORY_DEPTH 5 +#define AVERAGEBUF_DEPTH 16 +#define OVENTEMP_HISTORY_DEPTH 20 static struct App { float oven_temp; float soc_temp; float v_sensor; + float cal_a; + float cal_b; uint16_t adc_averagebuf[AVERAGEBUF_DEPTH * 4]; uint8_t averagebuf_ptr; float adc_averages[4]; float oventemp_history[OVENTEMP_HISTORY_DEPTH]; uint8_t oventemp_history_ptr; -} s_analog = {}; +} s_analog = { + .cal_a = 1.0f, + .cal_b = 0.0f, +}; #define TSENSE_LOOKUP_LEN 101 #define TSENSE_T_STEP 5.0f #define TSENSE_T_MIN 0.0f #define TSENSE_T_MAX 500.0f static const float TSENSE_LOOKUP[TSENSE_LOOKUP_LEN] = { - 0.092678405931418f, - 0.0943174479327356f, - 0.095948157844312f, - 0.0975706768542549f, - 0.0991848957506647f, - 0.100791037522732f, - 0.102388993070241f, - 0.103978983136042f, - 0.105560980458654f, - 0.107135039851509f, - 0.108701215616829f, - 0.110259642413441f, - 0.111810211533421f, - 0.113353137226489f, - 0.114888310929339f, - 0.11641594480226f, - 0.117936009906507f, - 0.119448557132363f, - 0.120953636903929f, - 0.122451377845456f, - 0.12394167187544f, - 0.125424725109556f, - 0.126900429638119f, - 0.128368989630084f, - 0.129830374697352f, - 0.131284632150064f, - 0.132731808872517f, - 0.134172027901771f, - 0.135605181883591f, - 0.13703146935069f, - 0.138450783142958f, - 0.139863319976468f, - 0.14126904821384f, - 0.142668011892657f, - 0.144060254660872f, - 0.145445894373796f, - 0.146824824486877f, - 0.14819723645253f, - 0.149563023938454f, - 0.150922376699229f, - 0.15227526202401f, - 0.153621720954182f, - 0.15496179417407f, - 0.156295594725426f, - 0.157623016940038f, - 0.158944245649448f, - 0.160259175412251f, - 0.16156798947087f, - 0.162870654195634f, - 0.164167207880495f, - 0.165457688491696f, - 0.166742204592451f, - 0.168020651444079f, - 0.169293207677971f, - 0.170559768793747f, - 0.171820511933356f, - 0.173075402684405f, - 0.174324476817747f, - 0.175567769803026f, - 0.176805386030345f, - 0.178037221732226f, - 0.179263449725904f, - 0.180483966491086f, - 0.181698943447122f, - 0.182908345518766f, - 0.184112206156428f, - 0.185310558533273f, - 0.186503503145257f, - 0.187690937227925f, - 0.188873028139146f, - 0.190049673368296f, - 0.191221038959601f, - 0.192387089280576f, - 0.193547855644572f, - 0.194703369109397f, - 0.195853726532112f, - 0.196998826174689f, - 0.19813883026229f, - 0.199273637315452f, - 0.200403408323351f, - 0.201528107189346f, - 0.20264776325594f, - 0.203762405629782f, - 0.204872127762998f, - 0.205976828960191f, - 0.207076666615101f, - 0.208171540293999f, - 0.209261606226334f, - 0.210346827933364f, - 0.211427232937629f, - 0.212502848543705f, - 0.213573765013592f, - 0.214639882704581f, - 0.215701354457324f, - 0.216758080892489f, - 0.217810213752734f, - 0.218857716249547f, - 0.219900614222686f, - 0.220938933310224f, - 0.221972760781578f, - 0.223001998051553f, + 0.0909090909090909f, + 0.0925200328471449f, + 0.0941228958173389f, + 0.0957178169362106f, + 0.0973046872002769f, + 0.0988837241910161f, + 0.100454819038946f, + 0.102018187184848f, + 0.103573800259031f, + 0.105121710606384f, + 0.106661970090864f, + 0.108194709632656f, + 0.109719820815089f, + 0.111237512887056f, + 0.112747677594865f, + 0.114250522193605f, + 0.115746016789507f, + 0.117234210034522f, + 0.118715150141415f, + 0.120188962295434f, + 0.121655538774297f, + 0.123115081061434f, + 0.124567481621389f, + 0.126012940077612f, + 0.127451425220842f, + 0.128882982328346f, + 0.13030765627567f, + 0.131725566931108f, + 0.133136607355006f, + 0.134540971782401f, + 0.135938553479917f, + 0.137329544944786f, + 0.138713913835562f, + 0.140091702340679f, + 0.141462952280536f, + 0.142827778585901f, + 0.144186075171443f, + 0.145538029496198f, + 0.146883535696824f, + 0.148222779606307f, + 0.149555727912261f, + 0.150882419971517f, + 0.152202894803216f, + 0.153517262745702f, + 0.154825418625535f, + 0.156127543558165f, + 0.157423532604634f, + 0.158713565356767f, + 0.159997607673187f, + 0.161275696311014f, + 0.162547867717066f, + 0.163814227951767f, + 0.165074672799026f, + 0.166329377427284f, + 0.167578237864685f, + 0.168821427850084f, + 0.170058912538152f, + 0.171290726295724f, + 0.172516903204089f, + 0.17373754533289f, + 0.174952549458501f, + 0.176162085166716f, + 0.177366049485545f, + 0.178564610657697f, + 0.179757733244091f, + 0.180945449410727f, + 0.182127791060477f, + 0.183304856534839f, + 0.184476543628915f, + 0.185643016681207f, + 0.186804173743842f, + 0.18796017789194f, + 0.18911099318983f, + 0.190256649774341f, + 0.191397177539504f, + 0.192532671339319f, + 0.193663030006298f, + 0.194788412940845f, + 0.195908719236171f, + 0.197024107102852f, + 0.198134540194308f, + 0.19924004677399f, + 0.200340654881021f, + 0.201436456102762f, + 0.202527350321362f, + 0.203613492284647f, + 0.204694782137667f, + 0.205771373506423f, + 0.206843229708988f, + 0.20791037727704f, + 0.208972842534733f, + 0.210030714005839f, + 0.211083892628833f, + 0.212132528763072f, + 0.213176523610881f, + 0.214216026472748f, + 0.215251000398025f, + 0.216281470315524f, + 0.21730746096184f, + 0.218329057984116f, + 0.219346163379138f, }; void app_analog_init() { + // read calibration constants + uint32_t c = 0; + if (EE_OK == EE_ReadVariable32bits(EE_ADDR_CAL_A, &c)) { + s_analog.cal_a = ((x32_t) { .u = c }).f; + PRINTF("ADC calib a read from EE: %f\r\n", s_analog.cal_a); + } + if (EE_OK == EE_ReadVariable32bits(EE_ADDR_CAL_B, &c)) { + s_analog.cal_b = ((x32_t) { .u = c }).f; + PRINTF("ADC calib b read from EE: %f\r\n", s_analog.cal_b); + } + + if (s_analog.cal_a == 0.0f || s_analog.cal_b == 0.0f) { + PRINTF("ADC invalid calib, reset\r\n"); + s_analog.cal_a = 1.0f; + s_analog.cal_b = 0.0f; + } + LL_ADC_Enable(ADC_TEMP); LL_ADC_StartCalibration(ADC_TEMP); @@ -203,6 +229,13 @@ void app_temp_sample() s_analog.adc_averages[2] = (float) sums[2] / count; s_analog.adc_averages[3] = (float) sums[3] / count; + PRINTF("%f\t%f\t%f\t%f\r\n", + s_analog.adc_averages[0], + s_analog.adc_averages[1], + s_analog.adc_averages[2], + s_analog.adc_averages[3] + ); + /* r_pt100, r_ref, internal_temp, v_ref_int */ float refint = s_analog.adc_averages[3]; float scale = V_REFINT / refint; @@ -217,7 +250,11 @@ void app_temp_sample() // using a voltage divider, so assuming the reference resistor is measured well, // we can just use the ratio and the exact voltage value is not important. - float actual_temp = val_to_c(s_analog.adc_averages[0] / s_analog.adc_averages[1]); + + float x = s_analog.adc_averages[0] / s_analog.adc_averages[1]; + float y = s_analog.cal_a * x + s_analog.cal_b; + float actual_temp = val_to_c(y); + PRINTF("Compensated x %f -> y %f, temp %f C\r\n", x, y, actual_temp); s_analog.oventemp_history[s_analog.oventemp_history_ptr] = actual_temp; s_analog.oventemp_history_ptr = (s_analog.oventemp_history_ptr + 1) % OVENTEMP_HISTORY_DEPTH; diff --git a/Core/Src/ee_addresses.h b/Core/Src/ee_addresses.h new file mode 100644 index 0000000..3417a13 --- /dev/null +++ b/Core/Src/ee_addresses.h @@ -0,0 +1,13 @@ +// +// Created by MightyPork on 2023/04/09. +// + +#ifndef TOASTER_OVEN_BLUEPILL_EE_ADDRESSES_H +#define TOASTER_OVEN_BLUEPILL_EE_ADDRESSES_H + +enum EEAddresses { + EE_ADDR_CAL_A = 1, + EE_ADDR_CAL_B = 2, +}; + +#endif //TOASTER_OVEN_BLUEPILL_EE_ADDRESSES_H diff --git a/Core/Src/transmute.h b/Core/Src/transmute.h new file mode 100644 index 0000000..27d10dc --- /dev/null +++ b/Core/Src/transmute.h @@ -0,0 +1,15 @@ +// +// Created by MightyPork on 2023/04/09. +// + +#ifndef TOASTER_OVEN_BLUEPILL_TRANSMUTE_H +#define TOASTER_OVEN_BLUEPILL_TRANSMUTE_H + +#include + +typedef union x32 { + uint32_t u; + float f; +} x32_t; + +#endif //TOASTER_OVEN_BLUEPILL_TRANSMUTE_H