Add temperature to BL602 (#1067)

Co-authored-by: openshwprojects <85486843+openshwprojects@users.noreply.github.com>
This commit is contained in:
giedriuslt
2024-02-09 15:32:02 +02:00
committed by GitHub
parent cedc4be125
commit a1decd3427

View File

@ -25,6 +25,9 @@ static char SUBMIT_AND_END_FORM[] = "<br><input type=\"submit\" value=\"Submit\"
// nothing
#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
#elif PLATFORM_W600 || PLATFORM_W800
#elif PLATFORM_XR809
@ -47,6 +50,68 @@ 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) {
@ -751,22 +816,29 @@ int http_fn_index(http_request_t* request) {
hprintf255(request, "<h5>Cfg size: %i, change counter: %i, ota counter: %i, boot incompletes %i (might change to 0 if you wait to 30 sec)!</h5>",
sizeof(g_cfg), g_cfg.changeCounter, g_cfg.otaCounter, g_bootFailures);
#if PLATFORM_BEKEN
if(!bSafeMode && g_bootFailures <= 1) // only in Normal mode, and if boot is not failing
// 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: %f°C</h5>",
hprintf255(request, "<h5>Internal temperature: %.1f°C</h5>",
g_wifi_temperature);
#endif
}
inputName = CFG_GetPingHost();
if (inputName && *inputName && CFG_GetPingDisconnectedSecondsToRestart()) {