diff --git a/README.md b/README.md index 0f79f6ce..b90c8f72 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Xiaomi Mijia LYWSD03MMC HW: [B1.4](https://github.com/pvvx/ATC_MiThermometer/tree/master/BoardPinout), [B1.6](https://github.com/pvvx/ATC_MiThermometer/issues/125), [B1.7](https://github.com/pvvx/ATC_MiThermometer/issues/145), [B1.9](https://github.com/pvvx/ATC_MiThermometer/issues/125), [B2.0](https://github.com/pvvx/ATC_MiThermometer/issues/166) & Xiaomi Miaomiaoce [MHO-C401](https://pvvx.github.io/MHO_C401) & [CGG1](https://pvvx.github.io/CGG1) Mijia Bluetooth Thermometer Firmware & [CGDK2](https://pvvx.github.io/CGDK2) Qingping Temp & RH Monitor Lite -This repository contains custom firmware for several Bluetooth Thermometer & Hygrometer devices. +_This repository contains custom firmware for several Bluetooth Thermometer & Hygrometer devices. The download of the appropriate firmware for your device will be presented automatically in [TelinkMiFlasher.html](https://pvvx.github.io/ATC_MiThermometer/TelinkMiFlasher.html). Downloading files from this repository is not required. -Alternative firmware and configurator from ATC1441 (variant ATC_Thermometer.bin) can be found here: [atc1441/ATC_MiThermometer](https://github.com/atc1441/ATC_MiThermometer). +Alternative firmware and configurator from ATC1441 (variant ATC_Thermometer.bin) can be found here: [atc1441/ATC_MiThermometer](https://github.com/atc1441/ATC_MiThermometer)._ The custom firmware can be flashed _via a modern browser_ and _over-the-air (OTA)_ without opening the device and unlocks several customization options. You can go back to the original firmware at any time. OTA and configuration main page - [TelinkMiFlasher.html](https://pvvx.github.io/ATC_MiThermometer/TelinkMiFlasher.html). diff --git a/makefile b/makefile index e7cc1d90..f5aa32dc 100644 --- a/makefile +++ b/makefile @@ -65,6 +65,7 @@ LS_FLAGS := $(PROJECT_PATH)/boot.link #-include $(PROJECT_PATH)/make/common.mk #-include $(PROJECT_PATH)/make/vendor_common.mk #-include $(PROJECT_PATH)/make/tinyFlash.mk +-include $(PROJECT_PATH)/uprintf.mk -include $(PROJECT_PATH)/drivers_8258.mk -include $(PROJECT_PATH)/div_mod.mk diff --git a/src/app.c b/src/app.c index e5848d25..ea7d8ba0 100644 --- a/src/app.c +++ b/src/app.c @@ -90,10 +90,6 @@ const cfg_t def_cfg = { .hw_cfg.hwver = 6, #endif #if USE_FLASH_MEMO - .hw_cfg.clock = 1, -#endif -#if USE_FLASH_MEMO - .hw_cfg.memo = 1, #if (DEVICE_TYPE == DEVICE_LYWSD03MMC) || (DEVICE_TYPE == DEVICE_CGDK2) .averaging_measurements = 60, // * measure_interval = 10 * 60 = 600 sec = 10 minutes #else // DEVICE_TYPE == DEVICE_MHO_C401 & DEVICE_CGG1 @@ -107,7 +103,7 @@ RAM cfg_t cfg; static const external_data_t def_ext = { .big_number = 0, .small_number = 0, - .vtime_sec = 60 * 10, // 10 minutes + .vtime_sec = 60, // 1 minutes .flg.smiley = 7, // 7 = "(ooo)" .flg.percent_on = true, .flg.battery = false, @@ -119,7 +115,8 @@ RAM uint32_t pincode; #endif __attribute__((optimize("-Os"))) -static void set_hw_version(void) { +void set_hw_version(void) { + cfg.hw_cfg.reserved = 0; if(sensor_i2c_addr == (SHTC3_I2C_ADDR << 1)) cfg.hw_cfg.shtc3 = 1; // = 1 - sensor SHTC3 else @@ -156,7 +153,7 @@ static void set_hw_version(void) { #elif DEVICE_TYPE == DEVICE_CGDK2 cfg.hw_cfg.hwver = 6; #else - cfg.hw_cfg.hwver = 4; + cfg.hw_cfg.hwver = 15; #endif } @@ -208,9 +205,6 @@ __attribute__((optimize("-Os"))) void test_config(void) { cfg.min_step_time_update_lcd = 10; // min 10*0.05 = 0.5 sec min_step_time_update_lcd = cfg.min_step_time_update_lcd * (100 * CLOCK_16M_SYS_TIMER_CLK_1MS); - cfg.hw_cfg.clock = USE_CLOCK; - cfg.hw_cfg.memo = USE_FLASH_MEMO; - cfg.hw_cfg.trg = USE_TRIGGER_OUT; my_RxTx_Data[0] = CMD_ID_CFG; my_RxTx_Data[1] = VERSION; memcpy(&my_RxTx_Data[2], &cfg, sizeof(cfg)); @@ -325,6 +319,7 @@ uint32_t get_mi_hw_version(void) { #endif // DEVICE_TYPE == DEVICE_LYWSD03MMC //------------------ user_init_normal ------------------- void user_init_normal(void) {//this will get executed one time after power up + bool next_start = false; if (get_battery_mv() < MIN_VBAT_MV) // 2.2V cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER, clock_time() + 120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes @@ -333,7 +328,8 @@ void user_init_normal(void) {//this will get executed one time after power up uint32_t hw_ver = get_mi_hw_version(); #endif // Read config - if (flash_supported_eep_ver(EEP_SUP_VER, VERSION)) { + next_start = flash_supported_eep_ver(EEP_SUP_VER, VERSION); + if (next_start) { if(flash_read_cfg(&cfg, EEP_ID_CFG, sizeof(cfg)) != sizeof(cfg)) memcpy(&cfg, &def_cfg, sizeof(cfg)); if(flash_read_cfg(&cmf, EEP_ID_CMF, sizeof(cmf)) != sizeof(cmf)) @@ -387,6 +383,11 @@ void user_init_normal(void) {//this will get executed one time after power up #if (DEVICE_TYPE == DEVICE_LYWSD03MMC) || (DEVICE_TYPE == DEVICE_CGDK2) update_lcd(); #endif + if(!next_start) { // first start? + if(!cfg.hw_cfg.shtc3) // sensor SHT4x ? + cfg.flg.lp_measures = 1; + flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg)); + } start_measure = 1; } diff --git a/src/app.h b/src/app.h index a0f908a4..479d3834 100644 --- a/src/app.h +++ b/src/app.h @@ -70,14 +70,22 @@ typedef struct __attribute__((packed)) _cfg_t { uint8_t rf_tx_power; // RF_POWER_N25p18dBm .. RF_POWER_P3p01dBm (130..191) uint8_t connect_latency; // +1 x0.02 sec ( = connection interval), Tmin = 1*20 = 20 ms, Tmax = 256 * 20 = 5120 ms uint8_t min_step_time_update_lcd; // x0.05 sec, 0.5..12.75 sec (10..255) +#if 0 // old version < 3.6 struct __attribute__((packed)) { uint8_t hwver : 3; // 0 - LYWSD03MMC B1.4, 1 - MHO-C401, 2 - CGG1, 3 - LYWSD03MMC B1.6, 4 - LYWSD03MMC B1.9, 5 - LYWSD03MMC B1.7 uint8_t clock : 1; // uses clock (old version < 3.6) uint8_t memo : 1; // uses flash write measures uint8_t trg : 1; // uses trigger out - uint8_t mi_beacon : 1; // advertising uses mi crypto beacon + uint8_t reserved : 1; // reserved uint8_t shtc3 : 1; // =1 - sensor SHTC3, = 0 - sensor SHT4x } hw_cfg; // read only +#else + struct __attribute__((packed)) { + uint8_t hwver : 4; // 0 - LYWSD03MMC B1.4, 1 - MHO-C401, 2 - CGG1, 3 - LYWSD03MMC B1.6, 4 - LYWSD03MMC B1.9, 5 - LYWSD03MMC B1.7, 6 - CGDK2 + uint8_t reserved : 3; // reserved + uint8_t shtc3 : 1; // =1 - sensor SHTC3, = 0 - sensor SHT4x + } hw_cfg; // read only +#endif uint8_t averaging_measurements; // * measure_interval, 0 - off, 1..255 * measure_interval }cfg_t; extern cfg_t cfg; @@ -166,6 +174,7 @@ extern uint32_t connection_timeout; extern uint32_t measurement_step_time; void ev_adv_timeout(u8 e, u8 *p, int n); void test_config(void); +void set_hw_version(void); void reset_cache(void); void blc_newMacAddress(int flash_addr, u8 *mac_pub, u8 *mac_rand); diff --git a/src/app_config.h b/src/app_config.h index 626e4d0c..674734bb 100644 --- a/src/app_config.h +++ b/src/app_config.h @@ -4,7 +4,7 @@ extern "C" { #endif -#define VERSION 0x35 // BCD format (0x34 -> '3.4') +#define VERSION 0x36 // BCD format (0x34 -> '3.4') #define EEP_SUP_VER 0x09 // EEP data minimum supported version #define DEVICE_LYWSD03MMC 0x055B // LCD display LYWSD03MMC @@ -33,6 +33,8 @@ extern "C" { #define USE_MIHOME_BEACON 1 // = 1 Compatible with MiHome beacon encryption #define USE_NEW_OTA 0 // = 1 keeping the old firmware, erasing the region when updating (test version only!) +#define UART_PRINT_DEBUG_ENABLE 0 // =1 use u_printf() (PA7/SWS), source: SDK/components/application/print/u_printf.c + #if DEVICE_TYPE == DEVICE_MHO_C401 // TLSR8251F512ET24 @@ -279,6 +281,16 @@ extern "C" { #endif // DEVICE_TYPE == ? +#if UART_PRINT_DEBUG_ENABLE +#define PRINT_BAUD_RATE 1500000 // real 1000000 +#define DEBUG_INFO_TX_PIN GPIO_PA7 // SWS +#define PA7_DATA_OUT 1 +#define PA7_OUTPUT_ENABLE 1 +#define PULL_WAKEUP_SRC_PA7 PM_PIN_PULLUP_1M +#define PA7_FUNC AS_GPIO +#endif // UART_PRINT_DEBUG_ENABLE + + #define MODULE_WATCHDOG_ENABLE 0 #define WATCHDOG_INIT_TIMEOUT 250 //ms diff --git a/src/cmd_parser.c b/src/cmd_parser.c index cecefc7b..e925b848 100644 --- a/src/cmd_parser.c +++ b/src/cmd_parser.c @@ -284,13 +284,15 @@ __attribute__((optimize("-Os"))) void cmd_parser(void * p) { memcpy(&cfg, &req->dat[1], len); } test_config(); + set_hw_version(); ev_adv_timeout(0, 0, 0); - if(cmd != CMD_ID_CFG_NS) // Get/set config (not save to Flash) + if(cmd != CMD_ID_CFG_NS) // Get/set config (not save to Flash) flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg)); ble_send_cfg(); } else if (cmd == CMD_ID_CFG_DEF) { // Set default config memcpy(&cfg, &def_cfg, sizeof(cfg)); test_config(); + set_hw_version(); ev_adv_timeout(0, 0, 0); flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg)); ble_send_cfg(); diff --git a/src/uprintf.mk b/src/uprintf.mk new file mode 100644 index 00000000..917b8ecb --- /dev/null +++ b/src/uprintf.mk @@ -0,0 +1,11 @@ + +OUT_DIR += /application/print + +OBJS += \ +$(OUT_PATH)/application/print/putchar.o \ +$(OUT_PATH)/application/print/u_printf.o \ + +# Each subdirectory must supply rules for building sources it contributes +$(OUT_PATH)/application/%.o: $(TEL_PATH)/components/application/%.c + @echo 'Building file: $<' + @$(TC32_PATH)tc32-elf-gcc $(GCC_FLAGS) $(INCLUDE_PATHS) -c -o"$@" "$<"