diff --git a/src/cmnds/cmd_if.c b/src/cmnds/cmd_if.c index 4fda4f21b..a861e3c11 100644 --- a/src/cmnds/cmd_if.c +++ b/src/cmnds/cmd_if.c @@ -289,6 +289,11 @@ float getRebootReason(const char *s) { } +float getInternalTemperature(const char *s) { + return g_wifi_temperature; +} + + const constant_t g_constants[] = { //cnstdetail:{"name":"MQTTOn", //cnstdetail:"title":"MQTTOn", @@ -483,7 +488,13 @@ const constant_t g_constants[] = { //cnstdetail:"requires":""} { "$rebootReason", &getRebootReason }, #endif + //cnstdetail:{"name":"$intTemp", + //cnstdetail:"title":"$intTemp", + //cnstdetail:"descr":"Internal temperature (of WiFi module sensor)", + //cnstdetail:"requires":""} + { "$intTemp", &getInternalTemperature }, }; + static int g_totalConstants = sizeof(g_constants) / sizeof(g_constants[0]); // tries to expand a given string into a constant diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index e193f8104..14b4dd4b3 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -50,68 +50,6 @@ static char SUBMIT_AND_END_FORM[] = "
Internal temperature: %.1f°C", - temperature/10.0f); -#elif PLATFORM_BL602 - float wifi_temperature; - get_tsen_adc(&wifi_temperature, 0); - hprintf255(request, "
Internal Temperature: %.1f°C
", - wifi_temperature); -#elif PLATFORM_LN882H - // Quick hack to display LN-only temperature, - // we may improve it in the future - extern float g_wifi_temperature; - - hprintf255(request, "
Internal temperature: %.1f°C
", - g_wifi_temperature); +#if PLATFORM_BK7231T + hprintf255(request, "
Internal temperature: [IGNORE IT - yet to be calibrated] %.1f°C
", g_wifi_temperature); +#else + hprintf255(request, "
Internal temperature: %.1f°C
", g_wifi_temperature); #endif - } inputName = CFG_GetPingHost(); if (inputName && *inputName && CFG_GetPingDisconnectedSecondsToRestart()) { diff --git a/src/new_common.h b/src/new_common.h index e5828a859..1308cc5a7 100644 --- a/src/new_common.h +++ b/src/new_common.h @@ -485,6 +485,7 @@ extern int g_openAP; extern int g_bootFailures; extern int g_secondsElapsed; extern int g_rebootReason; +extern float g_wifi_temperature; typedef int(*jsonCb_t)(void *userData, const char *fmt, ...); int JSON_ProcessCommandReply(const char *cmd, const char *args, void *request, jsonCb_t printer, int flags); diff --git a/src/user_main.c b/src/user_main.c index 2f8b90115..13e9c372e 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -39,13 +39,20 @@ #include "driver/drv_ssdp.h" #include "driver/drv_uart.h" -#ifdef PLATFORM_BEKEN +#if PLATFORM_BEKEN #include #include #include +#include "temp_detect_pub.h" void bg_register_irda_check_func(FUNCPTR func); +#elif PLATFORM_BL602 +#include +#include // For BL602 ADC HAL +#include // For BL602 ADC Standard Driver +#include // For BL602 Global Register Standard Driver #endif + int g_secondsElapsed = 0; // open access point after this number of seconds int g_openAP = 0; @@ -93,6 +100,70 @@ size_t xPortGetFreeHeapSize() { } #endif +#if PLATFORM_BL602 +/// Read the Internal Temperature Sensor as Float. Returns 0 if successful. +/// Based on bl_tsen_adc_get in https://github.com/lupyuen/bl_iot_sdk/blob/master/components/hal_drv/bl602_hal/bl_adc.c#L224-L282 +static int get_tsen_adc( + float *temp, // Pointer to float to store the temperature + uint8_t log_flag // 0 to disable logging, 1 to enable logging +) { + static uint16_t tsen_offset = 0xFFFF; + float val = 0.0; + + // If the offset has not been fetched... + if (0xFFFF == tsen_offset) { + // Define the ADC configuration + tsen_offset = 0; + ADC_CFG_Type adcCfg = { + .v18Sel = ADC_V18_SEL_1P82V, /*!< ADC 1.8V select */ + .v11Sel = ADC_V11_SEL_1P1V, /*!< ADC 1.1V select */ + .clkDiv = ADC_CLK_DIV_32, /*!< Clock divider */ + .gain1 = ADC_PGA_GAIN_1, /*!< PGA gain 1 */ + .gain2 = ADC_PGA_GAIN_1, /*!< PGA gain 2 */ + .chopMode = ADC_CHOP_MOD_AZ_PGA_ON, /*!< ADC chop mode select */ + .biasSel = ADC_BIAS_SEL_MAIN_BANDGAP, /*!< ADC current form main bandgap or aon bandgap */ + .vcm = ADC_PGA_VCM_1V, /*!< ADC VCM value */ + .vref = ADC_VREF_2V, /*!< ADC voltage reference */ + .inputMode = ADC_INPUT_SINGLE_END, /*!< ADC input signal type */ + .resWidth = ADC_DATA_WIDTH_16_WITH_256_AVERAGE, /*!< ADC resolution and oversample rate */ + .offsetCalibEn = 0, /*!< Offset calibration enable */ + .offsetCalibVal = 0, /*!< Offset calibration value */ + }; + ADC_FIFO_Cfg_Type adcFifoCfg = { + .fifoThreshold = ADC_FIFO_THRESHOLD_1, + .dmaEn = DISABLE, + }; + + // Enable and reset the ADC + GLB_Set_ADC_CLK(ENABLE, GLB_ADC_CLK_96M, 7); + ADC_Disable(); + ADC_Enable(); + ADC_Reset(); + + // Configure the ADC and Internal Temperature Sensor + ADC_Init(&adcCfg); + ADC_Channel_Config(ADC_CHAN_TSEN_P, ADC_CHAN_GND, 0); + ADC_Tsen_Init(ADC_TSEN_MOD_INTERNAL_DIODE); + ADC_FIFO_Cfg(&adcFifoCfg); + + // Fetch the offset + BL_Err_Type rc = ADC_Trim_TSEN(&tsen_offset); + + // Must wait 100 milliseconds or returned temperature will be negative + rtos_delay_milliseconds(100); + } + // Read the temperature based on the offset + val = TSEN_Get_Temp(tsen_offset); + if (log_flag) { + printf("offset = %d\r\n", tsen_offset); + printf("temperature = %f Celsius\r\n", val); + } + // Return the temperature + *temp = val; + return 0; +} +#endif + #ifdef PLATFORM_BK7231T // this function waits for the extended app functions to finish starting. extern void extended_app_waiting_for_launch(void); @@ -370,6 +441,14 @@ bool Main_HasFastConnect() { } return false; } +#if PLATFORM_LN882H +// Quick hack to display LN-only temperature, +// we may improve it in the future +extern float g_wifi_temperature; +#else +float g_wifi_temperature = 0; +#endif + static byte g_secondsSpentInLowMemoryWarning = 0; void Main_OnEverySecond() { @@ -381,6 +460,20 @@ void Main_OnEverySecond() g_bHasWiFiConnected = 1; #endif + // display temperature - thanks to giedriuslt +// only in Normal mode, and if boot is not failing + if (!bSafeMode && g_bootFailures <= 1) + { +#if PLATFORM_BEKEN + UINT32 temperature; + temp_single_get_current_temperature(&temperature); + g_wifi_temperature = temperature / 10.0f; +#elif PLATFORM_BL602 + get_tsen_adc(&g_wifi_temperature, 0); +#elif PLATFORM_LN882H + // this is set externally, I am just leaving comment here +#endif + } // run_adc_test(); newMQTTState = MQTT_RunEverySecondUpdate(); if (newMQTTState != bMQTTconnected) {