Skip to content

Commit

Permalink
v3.6a Additional parameters: Support BT5.0 PHY, Channel Selection Alg…
Browse files Browse the repository at this point in the history
…orithm 2
  • Loading branch information
pvvx committed Feb 18, 2022
1 parent 0c5528d commit 3a512c1
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 47 deletions.
Binary file removed ATC_V35f.bin
Binary file not shown.
Binary file added ATC_V36a.bin
Binary file not shown.
Binary file removed CGDK2_V35g.bin
Binary file not shown.
Binary file added CGDK2_V36a.bin
Binary file not shown.
Binary file removed CGG1_v35f.bin
Binary file not shown.
Binary file added CGG1_v36a.bin
Binary file not shown.
Binary file removed MHO_C401_v35f.bin
Binary file not shown.
Binary file added MHO_C401_v36a.bin
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -12,6 +12,7 @@ The custom firmware can be flashed _via a modern browser_ and _over-the-air (OTA
* Improved measurement accuracy and extended format in 0.01 units
* Full support in HA ['Passive BLE Monitor integration'](https://github.com/custom-components/ble_monitor)
* Supports [encrypted connections using **PIN-code**](https://github.com/pvvx/ATC_MiThermometer/issues/174#issuecomment-1003987084) and encrypted **bindkey** beacon
* Support BT5.0+ PHY, CSA1/CSA2
* **3 LCD Display Screens** (Looping): Temperature & Humidity & Comfort, Temperature & Battery Level, Clock
* **Measurement values recording** & Charting. See [Reading Measurements from Flash](#reading-measurements-from-flash)
* **Adjustable correction offsets** and **Comfort zones**
Expand Down Expand Up @@ -153,6 +154,7 @@ In case you want to go back to the original firmware, you can download them here
| 3.3 | Added autodetection for [LYWSD03MMC hardware versions B1.6 and B1.9](https://github.com/pvvx/ATC_MiThermometer/issues/125). For CGG1-M and MHO-C401 - autodetection of SHTV3 or SHT4x sensors |
| 3.4 | Correct Hardware Version Setting for [LYWSD03MMC B1.7](https://github.com/pvvx/ATC_MiThermometer/issues/145) |
| 3.5 | Correction of moisture readings for SHT4x sensors. [Rounding off sensor values on display.](https://github.com/pvvx/ATC_MiThermometer/issues/163). Saving HW string B2.0 on LYWSD03MMC. Eliminated [battery voltage noise](https://github.com/pvvx/ATC_MiThermometer/issues/180) in CGG1-M. CGG1 - correction of the battery charge display. Added CGDK2 and modified [(DIY) variant of CGDK2-2](https://pvvx.github.io/CGDK2/CGDK2-2/). |
| 3.6 | Additional parameters: Support BT5.0 PHY, Channel Selection Algorithm 2 |

## Applications

Expand Down
12 changes: 10 additions & 2 deletions SDK/components/application/print/putchar.c
Expand Up @@ -96,7 +96,7 @@ int swire_putc(int c) {
#if (UART_PRINT_DEBUG_ENABLE)

#ifndef BIT_INTERVAL
#define BIT_INTERVAL (16000000/PRINT_BAUD_RATE)
#define BIT_INTERVAL ((CLOCK_16M_SYS_TIMER_CLK_1S-120)/PRINT_BAUD_RATE) // todo calk real interval?
#endif

_attribute_ram_code_
Expand Down Expand Up @@ -137,6 +137,14 @@ int uart_putc(char byte) //GPIO simulate uart print func

return byte;
}

int puts(const char *s) {
while(*s) {
uart_putc(*s++);
}
return 0;
}

#endif

int putchar(int c){
Expand All @@ -153,5 +161,5 @@ int putchar(int c){
return 0;
}

#endif
#endif // ifndef WIN32

4 changes: 2 additions & 2 deletions SDK/components/application/print/u_printf.c
Expand Up @@ -226,9 +226,9 @@ int u_sprintf(char *out, const char *format, ...) {
void u_array_printf(unsigned char*data, unsigned int len) {
u_printf("{");
for(int i = 0; i < len; ++i){
u_printf("%X%s", data[i], i<(len)-1? ":":" ");
u_printf("%X%s", data[i], i<(len)-1? ":":"}");
}
u_printf("}\n");
u_printf("\n");
}

#endif
Expand Down
4 changes: 2 additions & 2 deletions firmware.json
@@ -1,3 +1,3 @@
{"version": 53,
"custom":["ATC_V35f.bin", "MHO_C401_v35f.bin", "CGG1_v35f.bin", "ATC_V35f.bin", "ATC_V35f.bin", "ATC_V35f.bin", "CGDK2_V35g.bin", "?"],
{"version": 54,
"custom":["ATC_V36a.bin", "MHO_C401_V36a.bin", "CGG1_V36a.bin", "ATC_V36a.bin", "ATC_V36a.bin", "ATC_V36a.bin", "CGDK2_V36a.bin", "?"],
"original":["Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin","Original_OTA_Xiaomi_MHO_C401_v1.0.0_0010.bin","Original_OTA_CGG1_v1.0.1_0093.bin","Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin","Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin","Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0130.bin", "Original_OTA_CGDK2_v1.1.1_0210.bin", "?"]}
7 changes: 7 additions & 0 deletions src/app.c
Expand Up @@ -73,18 +73,22 @@ const cfg_t def_cfg = {
.flg.tx_measures = false,
.advertising_interval = 40, // multiply by 62.5 ms = 2.5 sec
#if DEVICE_TYPE == DEVICE_LYWSD03MMC
.flg.comfort_smiley = true,
.measure_interval = 4, // * advertising_interval = 10 sec
.min_step_time_update_lcd = 49, //x0.05 sec, 2.45 sec
.hw_cfg.hwver = 0,
#elif DEVICE_TYPE == DEVICE_MHO_C401
.flg.comfort_smiley = true,
.measure_interval = 8, // * advertising_interval = 20 sec
.min_step_time_update_lcd = 199, //x0.05 sec, 9.95 sec
.hw_cfg.hwver = 1,
#elif DEVICE_TYPE == DEVICE_CGG1
.flg.comfort_smiley = true,
.measure_interval = 8, // * advertising_interval = 20 sec
.min_step_time_update_lcd = 199, //x0.05 sec, 9.95 sec
.hw_cfg.hwver = 2,
#elif DEVICE_TYPE == DEVICE_CGDK2
.flg.comfort_smiley = false,
.measure_interval = 4, // * advertising_interval = 10 sec
.min_step_time_update_lcd = 49, //x0.05 sec, 2.45 sec
.hw_cfg.hwver = 6,
Expand Down Expand Up @@ -530,6 +534,9 @@ _attribute_ram_code_ void main_loop(void) {
while(clock_time() - utc_time_sec_tick > utc_time_tick_step) {
utc_time_sec_tick += utc_time_tick_step;
utc_time_sec++; // + 1 sec
#if USE_EXTENDED_ADVERTISING
adv_send_count++; // todo: fix app_advertise_prepare_handler()
#endif
}
if (wrk_measure
&& timer_measure_cb
Expand Down
4 changes: 3 additions & 1 deletion src/app.h
Expand Up @@ -61,7 +61,9 @@ typedef struct __attribute__((packed)) _cfg_t {
uint8_t smiley : 3; // 0..7
uint8_t mi_beacon : 1; // advertising uses crypto beacon
uint8_t adv_flags : 1; // advertising add flags
uint8_t reserved : 3;
uint8_t bt5hgy : 1; // support BT5.0 Coded PHY
uint8_t chalg2 : 1; // ChannelSelectionAlgorithm 2
uint8_t reserved : 1;
} flg2;
int8_t temp_offset; // Set temp offset, -12,5 - +12,5 °C (-125..125)
int8_t humi_offset; // Set humi offset, -12,5 - +12,5 % (-125..125)
Expand Down
2 changes: 2 additions & 0 deletions src/app_config.h
Expand Up @@ -20,6 +20,8 @@ extern "C" {
// Set DIY_ADC_TO_TH_LYWSD03MMC 1
#define DIY_ADC_TO_TH_LYWSD03MMC 0

#define USE_EXTENDED_ADVERTISING 0 // debug, not completed!

#define BLE_SECURITY_ENABLE 1
#define BLE_HOST_SMP_ENABLE BLE_SECURITY_ENABLE

Expand Down
123 changes: 84 additions & 39 deletions src/ble.c
Expand Up @@ -55,8 +55,8 @@ void app_enter_ota_mode(void) {
bls_ota_setTimeout(45 * 1000000); // set OTA timeout 45 seconds
}

#define BLE_ADV_TIME 0 // Test Only!
#if BLE_ADV_TIME
#define BLE_ADV_RESP_TIME 0 // Test Only!
#if BLE_ADV_RESP_TIME
typedef struct __attribute__((packed)) _resp_time_t {
uint8_t len;
uint8_t type;
Expand Down Expand Up @@ -100,6 +100,9 @@ void ble_disconnect_callback(uint8_t e, uint8_t *p, int n) {
tx_measures = 0xff;
else
tx_measures = 0;
#if USE_EXTENDED_ADVERTISING
ev_adv_timeout(0,0,0);
#endif
}

void ble_connect_callback(uint8_t e, uint8_t *p, int n) {
Expand Down Expand Up @@ -145,15 +148,54 @@ _attribute_ram_code_ void user_set_rf_power(u8 e, u8 *p, int n) {
(void) e; (void) p; (void) n;
rf_set_power_level_index(cfg.rf_tx_power);
}

#if USE_EXTENDED_ADVERTISING
#define APP_ADV_SETS_NUMBER 1 // Number of Supported Advertising Sets
#define APP_MAX_LENGTH_ADV_DATA 64 // 1024 // Maximum Advertising Data Length, (if legacy ADV, max length 31 bytes is enough)
#define APP_MAX_LENGTH_SCAN_RESPONSE_DATA 31 // 1024 // Maximum Scan Response Data Length, (if legacy ADV, max length 31 bytes is enough)

RAM u8 app_adv_set_param[ADV_SET_PARAM_LENGTH * APP_ADV_SETS_NUMBER];
RAM u8 app_primary_adv_pkt[MAX_LENGTH_PRIMARY_ADV_PKT * APP_ADV_SETS_NUMBER];
RAM u8 app_secondary_adv_pkt[MAX_LENGTH_SECOND_ADV_PKT * APP_ADV_SETS_NUMBER];
RAM u8 app_advData[APP_MAX_LENGTH_ADV_DATA * APP_ADV_SETS_NUMBER];
RAM u8 app_scanRspData[APP_MAX_LENGTH_SCAN_RESPONSE_DATA * APP_ADV_SETS_NUMBER];
#endif

/*
* bls_app_registerEventCallback (BLT_EV_FLAG_ADV_DURATION_TIMEOUT, &ev_adv_timeout);
* blt_event_callback_t(): */
_attribute_ram_code_ void ev_adv_timeout(u8 e, u8 *p, int n) {
void ev_adv_timeout(u8 e, u8 *p, int n) {
(void) e; (void) p; (void) n;
#if USE_EXTENDED_ADVERTISING
blc_ll_setExtAdvParam(ADV_HANDLE0,
ADV_EVT_PROP_LEGACY_CONNECTABLE_SCANNABLE_UNDIRECTED,
adv_interval, adv_interval + 10,
BLT_ENABLE_ADV_ALL,
OWN_ADDRESS_PUBLIC,
BLE_ADDR_PUBLIC,
NULL,
ADV_FP_NONE,
cfg.rf_tx_power,
BLE_PHY_1M,
0,
BLE_PHY_CODED,
ADV_SID_0,
0);
//blc_ll_setExtAdvData(ADV_HANDLE0, DATA_OPER_COMPLETE, DATA_FRAGM_ALLOWED, adv_buf.data[0] + 4, (u8 *)&adv_buf);
blc_ll_setExtScanRspData(ADV_HANDLE0, DATA_OPER_COMPLETE, DATA_FRAGM_ALLOWED, ble_name[0]+1, (u8 *)&ble_name);
blc_ll_setExtAdvEnable_1(BLC_ADV_ENABLE, 1, ADV_HANDLE0, 0 , 0);
#else
bls_ll_setAdvParam(adv_interval, adv_interval + 10,
ADV_TYPE_CONNECTABLE_UNDIRECTED, OWN_ADDRESS_PUBLIC, 0, NULL,
BLT_ENABLE_ADV_ALL, ADV_FP_NONE);
bls_ll_setAdvEnable(1);
//bls_ll_setAdvData((u8 *)&adv_buf, adv_buf.data[0] + 4);
#if BLE_ADV_RESP_TIME
bls_ll_setScanRspData((uint8_t *) ble_name, ble_name[0] + 1 + ble_name[ble_name[0]+1] + 1);
#else // BLE_ADV_RESP_TIME
bls_ll_setScanRspData((uint8_t *) ble_name, ble_name[0]+1);
#endif // BLE_ADV_RESP_TIME
bls_ll_setAdvEnable(BLC_ADV_ENABLE); //ADV enable
#endif
}

#if BLE_SECURITY_ENABLE
Expand Down Expand Up @@ -229,7 +271,7 @@ void ble_get_name(void) {
ble_name[0] = (uint8_t)(len + 1);
}
ble_name[1] = 0x09;
#if BLE_ADV_TIME
#if BLE_ADV_RESP_TIME
add_resp_time();
#endif
}
Expand All @@ -242,11 +284,31 @@ __attribute__((optimize("-Os"))) void init_ble(void) {
////// Controller Initialization //////////
blc_ll_initBasicMCU(); //must
blc_ll_initStandby_module(mac_public); //must
#if USE_EXTENDED_ADVERTISING
// Extended ADV module:
blc_ll_initExtendedAdvertising_module(app_adv_set_param, app_primary_adv_pkt, APP_ADV_SETS_NUMBER);
blc_ll_initExtSecondaryAdvPacketBuffer(app_secondary_adv_pkt, MAX_LENGTH_SECOND_ADV_PKT);
blc_ll_initExtAdvDataBuffer(app_advData, APP_MAX_LENGTH_ADV_DATA);
blc_ll_initExtScanRspDataBuffer(app_scanRspData, APP_MAX_LENGTH_SCAN_RESPONSE_DATA);
blc_ll_setMaxAdvDelay_for_AdvEvent(0); //no ADV random delay, for debug
#else
blc_ll_initAdvertising_module(mac_public); // adv module: must for BLE slave,
#endif
blc_ll_initConnection_module(); // connection module must for BLE slave/master
blc_ll_initSlaveRole_module(); // slave module: must for BLE slave,
blc_ll_initPowerManagement_module(); //pm module: optional

#if (!USE_EXTENDED_ADVERTISING)
if(cfg.flg2.bt5hgy) // 2MPhy
#endif
{
blc_ll_init2MPhyCodedPhy_feature(); // mandatory for 2M/Coded PHY
//bls_app_registerEventCallback (BLT_EV_FLAG_PHY_UPDATE, &callback_phy_update_complete_event);
blc_ll_setDefaultPhy(PHY_TRX_PREFER, PHY_PREFER_1M, PHY_PREFER_1M);
//blc_ll_setDefaultPhy(PHY_TRX_PREFER, BLE_PHY_CODED, BLE_PHY_CODED);
blc_ll_setDefaultConnCodingIndication(CODED_PHY_PREFER_S8);
}
if(cfg.flg2.chalg2) // ChannelSelectionAlgorithm 2
blc_ll_initChannelSelectionAlgorithm_2_feature();
////// Host Initialization //////////
blc_gap_peripheral_init();
my_att_init(); //gatt initialization
Expand Down Expand Up @@ -307,17 +369,12 @@ __attribute__((optimize("-Os"))) void init_ble(void) {
blc_smp_setSecurityLevel(No_Security);

///////////////////// USER application initialization ///////////////////
#if BLE_ADV_TIME
bls_ll_setScanRspData((uint8_t *) ble_name, ble_name[0] + 1 + ble_name[ble_name[0]+1] + 1);
#else
bls_ll_setScanRspData((uint8_t *) ble_name, ble_name[0]+1);
#endif
rf_set_power_level_index(cfg.rf_tx_power);
bls_app_registerEventCallback(BLT_EV_FLAG_SUSPEND_EXIT, &user_set_rf_power);
bls_app_registerEventCallback(BLT_EV_FLAG_CONNECT, &ble_connect_callback);
bls_app_registerEventCallback(BLT_EV_FLAG_TERMINATE,
&ble_disconnect_callback);
#if BLE_ADV_TIME
bls_app_registerEventCallback(BLT_EV_FLAG_TERMINATE, &ble_disconnect_callback);

#if BLE_ADV_RESP_TIME
bls_app_registerEventCallback(BLT_EV_FLAG_SCAN_RSP, &ble_scan_response_callback);
#endif

Expand All @@ -332,37 +389,18 @@ __attribute__((optimize("-Os"))) void init_ble(void) {
#endif
bls_ota_registerStartCmdCb(app_enter_ota_mode);
blc_l2cap_registerConnUpdateRspCb(app_conn_param_update_response);
bls_set_advertise_prepare(app_advertise_prepare_handler);
#if USE_MIHOME_BEACON
mi_beacon_init();
#endif

#if 0 // BLE_SECURITY_ENABLE && DEVICE_TYPE != DEVICE_MHO_C401
if(pincode && *((u32 *)(CFG_ADR_BIND)) != 0xffffffff) {
smp_param_save_t bondInfo;
u8 bond_number = blc_smp_param_getCurrentBondingDeviceNumber(); //get bonded device number
if(bond_number) { // at least 1 bonding device exist
bls_smp_param_loadByIndex(bond_number - 1, &bondInfo); //get the latest bonding device (index: bond_number-1 )
//set direct adv
bls_ll_setAdvParam( adv_interval, adv_interval + 50,
ADV_TYPE_CONNECTABLE_DIRECTED_LOW_DUTY,
OWN_ADDRESS_PUBLIC,
bondInfo.peer_addr_type, bondInfo.peer_addr,
BLT_ENABLE_ADV_ALL,
ADV_FP_NONE);
//it is recommended that direct adv only last for several seconds, then switch to indirect adv
bls_ll_setAdvDuration(adv_interval*625*2, 1); // interval usec, duration enable
bls_app_registerEventCallback(BLT_EV_FLAG_ADV_DURATION_TIMEOUT, &ev_adv_timeout);
bls_ll_setAdvEnable(1);
}
} else
#if (!USE_EXTENDED_ADVERTISING)
bls_set_advertise_prepare(app_advertise_prepare_handler); // todo: not work if USE_EXTENDED_ADVERTISING
#endif
ev_adv_timeout(0,0,0);
}

_attribute_ram_code_
__attribute__((optimize("-Os")))
void set_adv_data() {
void set_adv_data(void) {
uint8_t adv_type = cfg.flg.advertising_type; // 0 - atc1441, 1 - pvvx, 2 - Mi, 3 - all
adv_old_count = adv_send_count;
if(adv_type == ADV_TYPE_ALL)
Expand Down Expand Up @@ -469,9 +507,18 @@ void set_adv_data() {
bit5..7: Reserved
*/
adv_buf.flag[2] = 0x06; // Flags
#if USE_EXTENDED_ADVERTISING
blc_ll_setExtAdvData(ADV_HANDLE0, DATA_OPER_COMPLETE, DATA_FRAGM_ALLOWED, adv_buf.data[0] + 4, (u8 *)&adv_buf);
#else
bls_ll_setAdvData((u8 *)&adv_buf, adv_buf.data[0] + 4);
} else
#endif
} else {
#if USE_EXTENDED_ADVERTISING
blc_ll_setExtAdvData(ADV_HANDLE0, DATA_OPER_COMPLETE, DATA_FRAGM_ALLOWED, adv_buf.data[0] + 1, (u8 *)&adv_buf.data);
#else
bls_ll_setAdvData((u8 *)&adv_buf.data, adv_buf.data[0] + 1);
#endif
}
}

_attribute_ram_code_ void ble_send_measures(void) {
Expand Down Expand Up @@ -532,5 +579,3 @@ __attribute__((optimize("-Os"))) void send_memo_blk(void) {
}
}
#endif


9 changes: 8 additions & 1 deletion src/cmd_parser.c
Expand Up @@ -280,16 +280,23 @@ __attribute__((optimize("-Os"))) void cmd_parser(void * p) {
ble_send_ext();
} else if (cmd == CMD_ID_CFG || cmd == CMD_ID_CFG_NS) { // Get/set config
if(--len > sizeof(cfg)) len = sizeof(cfg);
u8 x = ((u8 *)&cfg.flg2)[0];
if(len) {
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));
x ^= ((u8 *)&cfg.flg2)[0];
if(x & 0x60) // (cfg.flg2.bt5hgy || cfg.flg2.chalg2)
ble_connected |= 0x80; // reset device on disconnect
}
ble_send_cfg();
} else if (cmd == CMD_ID_CFG_DEF) { // Set default config
if(cfg.flg2.bt5hgy || cfg.flg2.chalg2)
ble_connected |= 0x80; // reset device on disconnect
memcpy(&cfg, &def_cfg, sizeof(cfg));
test_config();
set_hw_version();
Expand Down

0 comments on commit 3a512c1

Please sign in to comment.