Skip to content

Commit

Permalink
Start v4.3, increasing the degree of averaging of the sent values ove…
Browse files Browse the repository at this point in the history
…r the battery (x 64)
  • Loading branch information
pvvx committed Mar 18, 2023
1 parent 8863530 commit 0e4e05d
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 22 deletions.
Binary file removed ATC_v41.bin
Binary file not shown.
Binary file added ATC_v43.bin
Binary file not shown.
Binary file added BTH_v43.bin
Binary file not shown.
Binary file removed CGDK2_v41.bin
Binary file not shown.
Binary file added CGDK2_v43.bin
Binary file not shown.
Binary file removed CGG1M_v41.bin
Binary file not shown.
Binary file added CGG1M_v43.bin
Binary file not shown.
Binary file removed CGG1_v41.bin
Binary file not shown.
Binary file added CGG1_v43.bin
Binary file not shown.
Binary file added MHO_C401N_v43.bin
Binary file not shown.
Binary file removed MHO_C401_v41.bin
Binary file not shown.
Binary file added MHO_C401_v43.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion MakeAll.cmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@Path=E:\Telink\SDK;E:\Telink\SDK\jre\bin;E:\Telink\SDK\opt\tc32\tools;E:\Telink\SDK\opt\tc32\bin;E:\Telink\SDK\usr\bin;E:\Telink\SDK\bin;%PATH%
@set SWVER=_v42
@set SWVER=_v43
@del /Q "ATC%SWVER%.bin"
make -s -j PROJECT_NAME=ATC%SWVER% POJECT_DEF="-DDEVICE_TYPE=DEVICE_LYWSD03MMC"
@if not exist "ATC%SWVER%.bin" goto :error
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ In case you want to go back to the original firmware, you can download them here
| 4.0 | Improved stability in connected mode. I2C bus CLK and PullUp correction.|
| 4.1 | Changed "connection latency" for [stability in connected](https://github.com/pvvx/ATC_MiThermometer/issues/265#issuecomment-1431495494).|
| 4.2 | Option Increasing Communication Distance: Bluetooth 5.0 LE Long Range (Advertising Extensions: primary and secondary Coded PHY S=8, Connectable). Added support [MHO-C401 (2022.11)](https://pvvx.github.io/MHO_C401N) and [MJWSD05MMC](https://pvvx.github.io/MJWSD05MMC). Added function key: Connect |
| 4.3 | Increasing the degree of averaging of the sent values over the battery. |

## Applications

Expand Down
50 changes: 33 additions & 17 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,6 @@ void WakeupLowPowerCb(int par) {
}
timer_measure_cb = 0;
bls_pm_setAppWakeupLowPower(0, 0); // clear callback

if (measured_data.battery_mv < END_VBAT_MV) // It is not recommended to write Flash below 2V
low_vbat();
}

_attribute_ram_code_
Expand Down Expand Up @@ -433,30 +430,46 @@ static void suspend_enter_cb(u8 e, u8 *p, int n) {
#endif // GPIO_KEY2 || (USE_WK_RDS_COUNTER)

//--- check battery
#define BAT_AVERAGE_SHL 4 // 2..7
#define BAT_AVERAGE_COUNT (1 << BAT_AVERAGE_SHL) // 8,16,32, ...
#define BAT_AVERAGE_SHL 4 // 16*16 = 256 ( 256*10/60 = 42.7 min)
#define BAT_AVERAGE_COUNT (1 << BAT_AVERAGE_SHL) // 8
RAM struct {
uint16_t buf[BAT_AVERAGE_COUNT];
uint8_t index;
uint32_t buf2[BAT_AVERAGE_COUNT];
uint16_t buf1[BAT_AVERAGE_COUNT];
uint8_t index1;
uint8_t index2;
} bat_average;

_attribute_ram_code_
__attribute__((optimize("-Os")))
void check_battery(void) {
uint32_t i, summ = 0;
uint32_t i;
uint32_t summ = 0;
measured_data.battery_mv = get_battery_mv();
bat_average.index++;
bat_average.index &= (BAT_AVERAGE_COUNT - 1);
bat_average.buf[bat_average.index] = measured_data.battery_mv;
if (measured_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] = measured_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.buf[i];
measured_data.average_battery_mv = summ >> BAT_AVERAGE_SHL;
summ += bat_average.buf2[i];
measured_data.average_battery_mv = summ >> (2*BAT_AVERAGE_SHL);
measured_data.battery_level = get_battery_level(measured_data.average_battery_mv);
}

__attribute__((optimize("-Os")))
static void start_tst_battery(void) {
int i;
measured_data.battery_mv = get_battery_mv();
if (measured_data.battery_mv < MIN_VBAT_MV) { // 2.2V
uint16_t avr_mv = get_battery_mv();
measured_data.battery_mv = avr_mv;
if (avr_mv < MIN_VBAT_MV) { // 2.2V
#if (DEVICE_TYPE == DEVICE_LYWSD03MMC)
// Set LYWSD03MMC sleep < 1 uA
init_i2c();
Expand All @@ -466,9 +479,12 @@ static void start_tst_battery(void) {
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + 120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes
}
measured_data.average_battery_mv = measured_data.battery_mv;
measured_data.average_battery_mv = avr_mv;
for(i = 0; i < BAT_AVERAGE_COUNT; i++)
bat_average.buf1[i] = avr_mv;
avr_mv <<= BAT_AVERAGE_SHL;
for(i = 0; i < BAT_AVERAGE_COUNT; i++)
bat_average.buf[i] = measured_data.battery_mv;
bat_average.buf2[i] = avr_mv;
}


Expand Down
2 changes: 1 addition & 1 deletion src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern "C" {
#endif

#define VERSION 0x42 // BCD format (0x34 -> '3.4')
#define VERSION 0x43 // BCD format (0x34 -> '3.4')
#define EEP_SUP_VER 0x09 // EEP data minimum supported version

#define DEVICE_LYWSD03MMC 0x055B // LCD display LYWSD03MMC
Expand Down
9 changes: 6 additions & 3 deletions src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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) {
_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);
Expand All @@ -22,7 +23,8 @@ _attribute_ram_code_ static void adc_channel_init(ADC_InputPchTypeDef p_ain) {
}

// Process takes about 260 μs at CPU CLK 24Mhz.
_attribute_ram_code_ uint16_t get_adc_mv(uint32_t p_ain) { // ADC_InputPchTypeDef
_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;
Expand Down Expand Up @@ -82,7 +84,8 @@ _attribute_ram_code_ uint16_t get_adc_mv(uint32_t p_ain) { // ADC_InputPchTypeDe
}

// 2200..3100 mv - 0..100%
_attribute_ram_code_ uint8_t get_battery_level(uint16_t battery_mv) {
_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
Expand Down

0 comments on commit 0e4e05d

Please sign in to comment.