Skip to content

Commit

Permalink
Temperature var (#1127)
Browse files Browse the repository at this point in the history
* s

* Update cmd_if.c

* header

* bl602 fix
  • Loading branch information
openshwprojects committed Mar 12, 2024
1 parent 2d6f4e6 commit a9f702a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 83 deletions.
11 changes: 11 additions & 0 deletions src/cmnds/cmd_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
86 changes: 4 additions & 82 deletions src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,68 +50,6 @@ static char SUBMIT_AND_END_FORM[] = "<br><input type=\"submit\" value=\"Submit\"
#if defined(PLATFORM_BK7231T) || defined(PLATFORM_BK7231N)
int tuya_os_adapt_wifi_all_ap_scan(AP_IF_S** ap_ary, unsigned int* num);
int tuya_os_adapt_wifi_release_ap(AP_IF_S* ap);
#elif 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

unsigned char hexdigit(char hex) {
Expand Down Expand Up @@ -818,27 +756,11 @@ int http_fn_index(http_request_t* request) {

// 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);
hprintf255(request, "<h5>Internal temperature: %.1f°C</h5>",
temperature/10.0f);
#elif PLATFORM_BL602
float wifi_temperature;
get_tsen_adc(&wifi_temperature, 0);
hprintf255(request, "<h5>Internal Temperature: %.1f°C</h5>",
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, "<h5>Internal temperature: %.1f°C</h5>",
g_wifi_temperature);
#if PLATFORM_BK7231T
hprintf255(request, "<h5>Internal temperature: [IGNORE IT - yet to be calibrated] %.1f°C</h5>", g_wifi_temperature);
#else
hprintf255(request, "<h5>Internal temperature: %.1f°C</h5>", g_wifi_temperature);
#endif
}

inputName = CFG_GetPingHost();
if (inputName && *inputName && CFG_GetPingDisconnectedSecondsToRestart()) {
Expand Down
1 change: 1 addition & 0 deletions src/new_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
95 changes: 94 additions & 1 deletion src/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@
#include "driver/drv_ssdp.h"
#include "driver/drv_uart.h"

#ifdef PLATFORM_BEKEN
#if PLATFORM_BEKEN
#include <mcu_ps.h>
#include <fake_clock_pub.h>
#include <BkDriverWdg.h>
#include "temp_detect_pub.h"
void bg_register_irda_check_func(FUNCPTR func);
#elif PLATFORM_BL602
#include <bl_sys.h>
#include <bl_adc.h> // For BL602 ADC HAL
#include <bl602_adc.h> // For BL602 ADC Standard Driver
#include <bl602_glb.h> // For BL602 Global Register Standard Driver
#endif


int g_secondsElapsed = 0;
// open access point after this number of seconds
int g_openAP = 0;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
{
Expand All @@ -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) {
Expand Down

0 comments on commit a9f702a

Please sign in to comment.