From 25ed1bd135ef18a162b2c4cc933479ebe70dfed6 Mon Sep 17 00:00:00 2001 From: openshwprojects <85486843+openshwprojects@users.noreply.github.com> Date: Thu, 19 Jun 2025 00:55:08 +0200 Subject: [PATCH] merge old Max31855 driver (#1403) * MAX31855 * tabl * fx * fx * test * try * fx * statics * defines * aa * dd * fx * fixces * Update obk_config.h --- sdk/OpenW600 | 2 +- src/driver/drv_local.h | 3 ++ src/driver/drv_main.c | 7 +++ src/driver/drv_max31855.c | 98 +++++++++++++++++++++++++++++++++++++++ src/driver/drv_max6675.c | 8 ++-- src/obk_config.h | 2 + 6 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 src/driver/drv_max31855.c diff --git a/sdk/OpenW600 b/sdk/OpenW600 index 8111457a6..ad8f51d06 160000 --- a/sdk/OpenW600 +++ b/sdk/OpenW600 @@ -1 +1 @@ -Subproject commit 8111457a6f988e80a9801772d244d1c586590292 +Subproject commit ad8f51d064a78465ec1845287cf19c3a24c655b8 diff --git a/src/driver/drv_local.h b/src/driver/drv_local.h index 4bbcf4666..aa17c2c6a 100644 --- a/src/driver/drv_local.h +++ b/src/driver/drv_local.h @@ -168,6 +168,9 @@ void RN8029_RunEverySecond(void); void MAX6675_Init(void); void MAX6675_RunEverySecond(void); +void MAX31855_Init(); +void MAX31855_RunEverySecond(); + void PWMG_Init(); void Freeze_Init(); diff --git a/src/driver/drv_main.c b/src/driver/drv_main.c index b5184a7b7..23e5c6145 100644 --- a/src/driver/drv_main.c +++ b/src/driver/drv_main.c @@ -222,6 +222,13 @@ static driver_t g_drivers[] = { //drvdetail:"requires":""} { "MAX6675", MAX6675_Init, MAX6675_RunEverySecond, NULL, NULL, NULL, NULL, NULL, false }, #endif +#if ENABLE_DRIVER_MAX31855 + //drvdetail:{"name":"MAX31855", + //drvdetail:"title":"TODO", + //drvdetail:"descr":"T", + //drvdetail:"requires":""} + { "MAX31855", MAX31855_Init, MAX31855_RunEverySecond, NULL, NULL, NULL, NULL, false }, +#endif #if ENABLE_DRIVER_PT6523 //drvdetail:{"name":"PT6523", //drvdetail:"title":"TODO", diff --git a/src/driver/drv_max31855.c b/src/driver/drv_max31855.c new file mode 100644 index 000000000..1c4c52735 --- /dev/null +++ b/src/driver/drv_max31855.c @@ -0,0 +1,98 @@ +// NOTE: based on https://github.com/adafruit/MAX31855-library/blob/master/MAX31855.cpp public domain +#include "../new_common.h" +#include "../new_pins.h" +#include "../new_cfg.h" +// Commands register, execution API and cmd tokenizer +#include "../cmnds/cmd_public.h" +#include "../mqtt/new_mqtt.h" +#include "../logging/logging.h" +#include "drv_local.h" +#include "../hal/hal_pins.h" + +static int port_cs = 24; +static int sclk = 26; +static int miso = 6; +static int targetChannel = -1; + +#define MAX31855_THERMOCOUPLE_RESOLUTION 0.25 //in °C per dac step +#define MAX31855_COLD_JUNCTION_RESOLUTION 0.0625 //in °C per dac step + +static int stage = 0; +int MAX31855_ReadRaw(void) { + int i; + int d = 0; + + // delay_ms(100); + HAL_PIN_SetOutputValue(port_cs, 0); + usleep(10); + for (i = 31; i >= 0; i--) { + HAL_PIN_SetOutputValue(sclk, 1); + usleep(10); + if (HAL_PIN_ReadDigitalInput(miso)) { + d |= (1 << i); + } + HAL_PIN_SetOutputValue(sclk, 0); + usleep(10); + } + HAL_PIN_SetOutputValue(port_cs, 1); + + return d; +} + +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) + +void MAX31855_ReadTemperature() { + int raw; + + stage = !stage; + + // request data + if (stage) { + HAL_PIN_SetOutputValue(port_cs, 0); + usleep(10); + HAL_PIN_SetOutputValue(port_cs, 1); + return; + } + // read data + raw = MAX31855_ReadRaw(); + + // print it like 0xFFAABBCC + //addLogAdv(LOG_INFO, LOG_FEATURE_MAIN, "0x%08X", raw); + + if (raw == 0) { + addLogAdv(LOG_INFO, LOG_FEATURE_MAIN, "MAX31855 read fail"); + return; + } + + if (bitRead(raw, 17) == 0 && bitRead(raw, 3) == 0) { + + } + else { + addLogAdv(LOG_INFO, LOG_FEATURE_MAIN, "MAX31855 bad ID"); + return; + } + + // clear D17..D0 bits + float temp = (float)(raw >> 18) * MAX31855_THERMOCOUPLE_RESOLUTION; + float cjTemp = (float)((raw & 0x0000FFFF) >> 4) * MAX31855_COLD_JUNCTION_RESOLUTION; + + addLogAdv(LOG_INFO, LOG_FEATURE_MAIN, "T %f, cjT %f", temp, cjTemp); +} +void MAX31855_RunEverySecond() { + MAX31855_ReadTemperature(); +} +// startDriver MAX31855 24 26 6 1 +void MAX31855_Init() { + port_cs = Tokenizer_GetArgIntegerDefault(1, port_cs); + sclk = Tokenizer_GetArgIntegerDefault(2, sclk); + miso = Tokenizer_GetArgIntegerDefault(3, miso); + targetChannel = Tokenizer_GetArgIntegerDefault(4, targetChannel); + + // define pin modes + HAL_PIN_Setup_Output(port_cs); + HAL_PIN_Setup_Output(sclk); + HAL_PIN_Setup_Input(miso); + + HAL_PIN_SetOutputValue(port_cs, 1); +} + diff --git a/src/driver/drv_max6675.c b/src/driver/drv_max6675.c index cdbf5680c..93b0ca72b 100644 --- a/src/driver/drv_max6675.c +++ b/src/driver/drv_max6675.c @@ -9,10 +9,10 @@ #include "drv_local.h" #include "../hal/hal_pins.h" -int port_cs = 24; -int sclk = 26; -int miso = 6; -int targetChannel = -1; +static int port_cs = 24; +static int sclk = 26; +static int miso = 6; +static int targetChannel = -1; byte MAX6675_ReadByte(void) { int i; diff --git a/src/obk_config.h b/src/obk_config.h index f69b5ce70..482d4b10d 100644 --- a/src/obk_config.h +++ b/src/obk_config.h @@ -243,6 +243,8 @@ //#define ENABLE_I2C_MCP23017 1 //#define ENABLE_I2C_LCD_PCF8574 1 + + #elif PLATFORM_LN882H