diff --git a/src/app_cfg.h b/src/app_cfg.h index b8cbb3a..36076ca 100644 --- a/src/app_cfg.h +++ b/src/app_cfg.h @@ -117,17 +117,21 @@ extern "C" { /* Voltage detect module */ -/* If you want to define the VOLTAGE_DETECT_ENABLE to 1, - * and the model of the development board is B91 evk or dongle, - * be sure to connect GPIO_PB0 to VCC. +/* If VOLTAGE_DETECT_ENABLE is set, + * 1) if MCU_CORE_826x is defined, the DRV_ADC_VBAT_MODE mode is used by default, + * and there is no need to configure the detection IO port; + * 2) if MCU_CORE_8258 or MCU_CORE_8278 is defined, the DRV_ADC_VBAT_MODE mode is used by default, + * we need to configure the detection IO port, and the IO must be in a floating state. + * 3) if MCU_CORE_B91 is defined, the DRV_ADC_BASE_MODE mode is used by default, + * we need to configure the detection IO port, and the IO must be connected to the target under test, + * such as VCC. */ -#define VOLTAGE_DETECT_ENABLE 0 - +#define VOLTAGE_DETECT_ENABLE 1 #if defined(MCU_CORE_826x) #define VOLTAGE_DETECT_ADC_PIN 0 #elif defined(MCU_CORE_8258) || defined(MCU_CORE_8278) - #define VOLTAGE_DETECT_ADC_PIN GPIO_PB5 + #define VOLTAGE_DETECT_ADC_PIN GPIO_PC5 #elif defined(MCU_CORE_B91) #define VOLTAGE_DETECT_ADC_PIN ADC_GPIO_PB0 #endif @@ -171,7 +175,6 @@ extern "C" { */ typedef enum{ EV_POLL_ED_DETECT, - EV_POLL_PM, EV_POLL_HCI, EV_POLL_IDLE, EV_POLL_MAX, diff --git a/src/app_i2c.c b/src/app_i2c.c index adaed4e..c182c05 100644 --- a/src/app_i2c.c +++ b/src/app_i2c.c @@ -1,7 +1,6 @@ -#include "compiler.h" +#include "tl_common.h" #include "types.h" #include "app_cfg.h" -#include "chip_8258/i2c.h" _attribute_data_retention_ bool i2c_sending; @@ -19,11 +18,11 @@ void send_i2c(u8 device_id, const u8 *buffer, int dataLen){ } u8 test_i2c_device(u8 address){ - reg_i2c_id = address<<1; + reg_i2c_id = address << 1; reg_i2c_ctrl = FLD_I2C_CMD_START | FLD_I2C_CMD_ID; while(reg_i2c_status & FLD_I2C_CMD_BUSY); reg_i2c_ctrl = FLD_I2C_CMD_STOP; while(reg_i2c_status & FLD_I2C_CMD_BUSY ); -return (reg_i2c_status & FLD_I2C_NAK)?0:1; + return (reg_i2c_status & FLD_I2C_NAK) ? 0 : 1; } \ No newline at end of file diff --git a/src/app_ui.c b/src/app_ui.c index 9b60e1f..b837e23 100644 --- a/src/app_ui.c +++ b/src/app_ui.c @@ -7,6 +7,7 @@ * @date 2021 * * @par Copyright (c) 2021, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +20,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * *******************************************************************************************************/ /********************************************************************** @@ -32,7 +34,6 @@ #include "shtv3_sensor.h" #include "lcd.h" -#include "device.h" /********************************************************************** * LOCAL CONSTANTS @@ -147,20 +148,20 @@ void buttonKeepPressed(u8 btNum){ void buttonShortPressed(u8 btNum){ if(btNum == VK_SW1){ if(zb_isDeviceJoinedNwk()){ - epInfo_t dstEpInfo; - memset((u8 *)&dstEpInfo, 0, sizeof(epInfo_t)); - - dstEpInfo.dstAddrMode = APS_SHORT_DSTADDR_WITHEP; - dstEpInfo.dstEp = SENSOR_DEVICE_ENDPOINT; - dstEpInfo.dstAddr.shortAddr = 0x0000; - dstEpInfo.profileId = HA_PROFILE_ID; - - zoneStatusChangeNoti_t statusChangeNotification; - - statusChangeNotification.zoneStatus = ZONE_STATUS_TEST; - statusChangeNotification.extStatus = 0; - statusChangeNotification.zoneId = ZCL_ZONE_ID_INVALID; - statusChangeNotification.delay = 0; +// epInfo_t dstEpInfo; +// memset((u8 *)&dstEpInfo, 0, sizeof(epInfo_t)); +// +// dstEpInfo.dstAddrMode = APS_SHORT_DSTADDR_WITHEP; +// dstEpInfo.dstEp = SENSOR_DEVICE_ENDPOINT; +// dstEpInfo.dstAddr.shortAddr = 0x0000; +// dstEpInfo.profileId = HA_PROFILE_ID; + +// zoneStatusChangeNoti_t statusChangeNotification; +// +// statusChangeNotification.zoneStatus = ZONE_STATUS_TEST; +// statusChangeNotification.extStatus = 0; +// statusChangeNotification.zoneId = ZCL_ZONE_ID_INVALID; +// statusChangeNotification.delay = 0; // zcl_iasZone_statusChangeNotificationCmd(SENSOR_DEVICE_ENDPOINT, &dstEpInfo, TRUE, &statusChangeNotification); } diff --git a/src/app_ui.h b/src/app_ui.h index 3a5c315..0b9cba8 100644 --- a/src/app_ui.h +++ b/src/app_ui.h @@ -7,6 +7,7 @@ * @date 2021 * * @par Copyright (c) 2021, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +20,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * *******************************************************************************************************/ #ifndef _APP_UI_H_ diff --git a/src/battery.c b/src/battery.c deleted file mode 100644 index eb08228..0000000 --- a/src/battery.c +++ /dev/null @@ -1,134 +0,0 @@ -#include "compiler.h" -#include "tl_common.h" - -#include "battery.h" - -uint8_t adc_hw_initialized = 0; -#define ADC_BUF_COUNT 8 - -// Process takes about 120 μs at CPU CLK 24Mhz. -_attribute_ram_code_ -static void adc_channel_init(ADC_InputPchTypeDef p_ain) { - adc_set_sample_clk(5); - adc_set_left_right_gain_bias(GAIN_STAGE_BIAS_PER100, GAIN_STAGE_BIAS_PER100); - adc_set_chn_enable_and_max_state_cnt(ADC_MISC_CHN, 2); - adc_set_state_length(240, 0, 10); - analog_write(anareg_adc_res_m, RES14 | FLD_ADC_EN_DIFF_CHN_M); - adc_set_ain_chn_misc(p_ain, GND); - adc_set_ref_voltage(ADC_MISC_CHN, ADC_VREF_1P2V); - adc_set_tsample_cycle_chn_misc(SAMPLING_CYCLES_6); - adc_set_ain_pre_scaler(ADC_PRESCALER_1F8); -} - -// Process takes about 260 μs at CPU CLK 24Mhz. -_attribute_ram_code_ -uint16_t get_adc_mv(uint32_t p_ain) { // ADC_InputPchTypeDef - volatile unsigned int adc_dat_buf[ADC_BUF_COUNT]; - uint16_t temp; - int i, j; - if (adc_hw_initialized != p_ain) { - adc_hw_initialized = p_ain; - adc_power_on_sar_adc(0); -#if 0 // gpio set in board_8258_03mmc.h - if(p_ain == SHL_ADC_VBAT) { - // Set missing pin on case TLSR8251F512ET24/TLSR8253F512ET32 - gpio_set_output_en(GPIO_VBAT, 1); - gpio_set_input_en(GPIO_VBAT, 0); - gpio_write(GPIO_VBAT, 1); - } -#endif - adc_channel_init(p_ain); - } - adc_power_on_sar_adc(1); // + 0.4 mA - adc_reset_adc_module(); - u32 t0 = clock_time(); - - uint16_t adc_sample[ADC_BUF_COUNT]; // = { 0 }; - u32 adc_average; - for (i = 0; i < ADC_BUF_COUNT; i++) { - adc_dat_buf[i] = 0; - } - while (!clock_time_exceed(t0, 25)); //wait at least 2 sample cycle(f = 96K, T = 10.4us) - adc_config_misc_channel_buf((uint16_t *) adc_dat_buf, sizeof(adc_dat_buf)); - dfifo_enable_dfifo2(); - sleep_us(20); - for (i = 0; i < ADC_BUF_COUNT; i++) { - while (!adc_dat_buf[i]); - if (adc_dat_buf[i] & BIT(13)) { - adc_sample[i] = 0; - } else { - adc_sample[i] = ((uint16_t) adc_dat_buf[i] & 0x1FFF); - } - if (i) { - if (adc_sample[i] < adc_sample[i - 1]) { - temp = adc_sample[i]; - adc_sample[i] = adc_sample[i - 1]; - for (j = i - 1; j >= 0 && adc_sample[j] > temp; j--) { - adc_sample[j + 1] = adc_sample[j]; - } - adc_sample[j + 1] = temp; - } - } - } - dfifo_disable_dfifo2(); - adc_power_on_sar_adc(0); // - 0.4 mA - adc_average = (adc_sample[2] + adc_sample[3] + adc_sample[4] - + adc_sample[5]) / 4; -#if DEVICE_TYPE == DEVICE_MJWSD05MMC - return (adc_average * 1686) >> 10; // adc_vref default: 1175 (mV) -#else - return (adc_average * 1175) >> 10; // adc_vref default: 1175 (mV) -#endif -} - -// 2200..3100 mv - 0..100% -_attribute_ram_code_ -uint8_t get_battery_level(uint16_t battery_mv) { - uint8_t battery_level = 0; - if (battery_mv > MIN_VBAT_MV) { - battery_level = (battery_mv - MIN_VBAT_MV) / ((MAX_VBAT_MV - - MIN_VBAT_MV) / 100); - if (battery_level > 100) - battery_level = 100; - } - return battery_level; -} - - -_attribute_data_retention_ struct { - uint32_t buf2[BAT_AVERAGE_COUNT]; - uint16_t buf1[BAT_AVERAGE_COUNT]; - uint8_t index1; - uint8_t index2; -} bat_average; - -_attribute_data_retention_ battery_data_t battery_data; - -void low_vbat(void) { - // go_sleep(120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes -} - -//__attribute__((optimize("-Os"))) -_attribute_ram_code_ -void check_battery(void) { - uint32_t i; - uint32_t summ = 0; - battery_data.battery_mv = get_battery_mv(); - if (battery_data.battery_mv < END_VBAT_MV) // It is not recommended to write Flash below 2V - low_vbat(); - bat_average.index1++; - bat_average.index1 &= BAT_AVERAGE_COUNT - 1; - if(bat_average.index1 == 0) { - bat_average.index2++; - bat_average.index2 &= BAT_AVERAGE_COUNT - 1; - } - bat_average.buf1[bat_average.index1] = battery_data.battery_mv; - for(i = 0; i < BAT_AVERAGE_COUNT; i++) - summ += bat_average.buf1[i]; - bat_average.buf2[bat_average.index2] = summ; - summ = 0; - for(i = 0; i < BAT_AVERAGE_COUNT; i++) - summ += bat_average.buf2[i]; - battery_data.average_battery_mv = summ >> (2*BAT_AVERAGE_SHL); - battery_data.battery_level = get_battery_level(battery_data.battery_mv); -} \ No newline at end of file diff --git a/src/battery.h b/src/battery.h deleted file mode 100644 index f16d8aa..0000000 --- a/src/battery.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include - -#define SHL_ADC_VBAT 1 // "B0P" in sdk/platform/adc.h - -#define MAX_VBAT_MV 2950 // 3100 mV - > battery = 100% no load, 2950 at load (during measurement) -#define MIN_VBAT_MV 2200 // 2200 mV - > battery = 0% - -#define LOW_VBAT_MV 2800 // level set LOW_CONNECT_LATENCY -#define END_VBAT_MV 2000 // It is not recommended to write Flash below 2V, go to deep-sleep - -uint16_t get_adc_mv(uint32_t p_ain); - -#define get_battery_mv() get_adc_mv(SHL_ADC_VBAT) // Channel B0P/B5P - -#define BAT_AVERAGE_SHL 4 // 16*16 = 256 ( 256*10/60 = 42.7 min) -#define BAT_AVERAGE_COUNT (1 << BAT_AVERAGE_SHL) // 8 - -typedef struct _battery_data_t { - uint16_t average_battery_mv; // mV - uint16_t battery_mv; // mV - uint8_t battery_level; // 0..100% (average_battery_mv) -} battery_data_t; - -void check_battery(void); diff --git a/src/board_8258_03mmc.h b/src/board_8258_03mmc.h index a6d34b5..599392e 100644 --- a/src/board_8258_03mmc.h +++ b/src/board_8258_03mmc.h @@ -12,13 +12,6 @@ extern "C" { #endif -// BATTERY -#define GPIO_VBAT GPIO_PB0 // missing pin on case TLSR8253F512ET32 -#define PB0_FUNC AS_GPIO -#define PB0_INPUT_ENABLE 1 -#define PB0_DATA_OUT 1 -#define PB0_OUTPUT_ENABLE 1 - // BUTTON #define BUTTON1 GPIO_PA5 // reset test point #define PA5_FUNC AS_GPIO diff --git a/src/device.c b/src/device.c index 32465e6..ab27013 100644 --- a/src/device.c +++ b/src/device.c @@ -15,7 +15,6 @@ #include "zcl_relative_humidity.h" #include "app_i2c.h" #include "shtv3_sensor.h" -#include "battery.h" #include "lcd.h" @@ -33,7 +32,6 @@ * GLOBAL VARIABLES */ app_ctx_t g_sensorAppCtx; -extern battery_data_t battery_data; #ifdef ZCL_OTA @@ -181,16 +179,24 @@ u8 is_comfort(s16 t, u16 h) { void read_sensor_and_save() { s16 temp = 0; u16 humi = 0; + u16 voltage, percentage; + u8 converted_voltage, percentage2; read_sensor(&temp,&humi); // printf("Temp: %d.%d, humid: %d\r\n", temp/10, temp % 10, humi); g_zcl_temperatureAttrs.measuredValue = temp * 10; g_zcl_relHumidityAttrs.measuredValue = humi * 100; - check_battery(); - // printf("battery %d mv, %d %%\r\n", battery_data.battery_mv, battery_data.battery_level); - g_zcl_powerAttrs.batteryVoltage = battery_data.battery_mv / 100; - g_zcl_powerAttrs.batteryPercentage = battery_data.battery_level * 2; + voltage = drv_get_adc_data(); + converted_voltage = (u8)(voltage / 100); + percentage = ((voltage - BATTERY_SAFETY_THRESHOLD) / 4); + if (percentage > 0xc8) percentage = 0xc8; + percentage2 = (u8)percentage; + + // printf("converted voltage %d diff %d", converted_voltage, (voltage - BATTERY_SAFETY_THRESHOLD)); + //printf(" , percentage2 %d\r\n", percentage2); + g_zcl_powerAttrs.batteryVoltage = converted_voltage; + g_zcl_powerAttrs.batteryPercentage = percentage2; // update lcd show_temp_symbol(1); @@ -301,7 +307,6 @@ void user_init(bool isRetention) drv_pm_wakeupPinConfig(g_sensorPmCfg, sizeof(g_sensorPmCfg)/sizeof(drv_pm_pinCfg_t)); #endif - random_generator_init(); //must init_i2c(); init_sensor(); init_lcd(!isRetention);