Skip to content

Commit

Permalink
Wakeup HX71x
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Nov 5, 2024
1 parent c76a62e commit 03094b7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
21 changes: 16 additions & 5 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ void test_config(void) {
}

void low_vbat(void) {
#if (DEV_SERVICES & RDS)
#if USE_SENSOR_HX71X && (DEV_SERVICES & SERVICE_PRESSURE)
hx711_go_sleep();
#endif
#if (DEV_SERVICES & RDS)
rds1_input_off();
#endif
#if (DEV_SERVICES & SERVICE_SCREEN)
Expand Down Expand Up @@ -454,7 +457,7 @@ _attribute_ram_code_
void read_sensors(void) {
#endif
#if (DEV_SERVICES & SERVICE_RDS)
rds_input_on();
rds_input_on();
#endif
#if (defined(CHL_ADC1) || defined(CHL_ADC1))
if (1)
Expand Down Expand Up @@ -528,7 +531,7 @@ static void suspend_exit_cb(u8 e, u8 *p, int n) {
rf_set_power_level_index(cfg.rf_tx_power);
}

#if (DEV_SERVICES & SERVICE_KEY) || (DEV_SERVICES & SERVICE_RDS) // || (USE_SENSOR_HX71X)
#if (DEV_SERVICES & SERVICE_KEY) || (DEV_SERVICES & SERVICE_RDS) || (USE_SENSOR_HX71X && SENSOR_HX71X_WAKEAP)
_attribute_ram_code_
static void suspend_enter_cb(u8 e, u8 *p, int n) {
(void) e; (void) p; (void) n;
Expand All @@ -544,6 +547,9 @@ static void suspend_enter_cb(u8 e, u8 *p, int n) {
// cpu_set_gpio_wakeup(GPIO_RDS2, Level_Low, 0); // pad wakeup deepsleep disable
#endif
#endif
#if (USE_SENSOR_HX71X && SENSOR_HX71X_WAKEAP)
cpu_set_gpio_wakeup(GPIO_HX71X_DOUT, Level_Low, 1); // pad wakeup deepsleep enable
#endif
#if (DEV_SERVICES & SERVICE_KEY)
cpu_set_gpio_wakeup(GPIO_KEY2, BM_IS_SET(reg_gpio_in(GPIO_KEY2), GPIO_KEY2 & 0xff)? Level_Low : Level_High, 1); // pad wakeup deepsleep enable
#endif // (DEV_SERVICES & SERVICE_KEY)
Expand Down Expand Up @@ -595,6 +601,9 @@ static void start_tst_battery(void) {
uint16_t avr_mv = get_battery_mv();
measured_data.battery_mv = avr_mv;
if (avr_mv < MIN_VBAT_MV) { // 2.2V
#if USE_SENSOR_HX71X && (DEV_SERVICES & SERVICE_PRESSURE)
hx711_go_sleep();
#endif
#if USE_SENSOR_SHTC3
send_i2c_word(0x70 << 1, 0x98b0); // SHTC3 go SLEEP: Sleep command of the sensor
#endif // USE_SENSOR_SHTC3
Expand Down Expand Up @@ -659,6 +668,9 @@ void user_init_normal(void) {//this will get executed one time after power up
#if defined(MI_HW_VER_FADDR) && (MI_HW_VER_FADDR)
uint32_t hw_ver = get_mi_hw_version();
#endif // (DEVICE_TYPE == DEVICE_LYWSD03MMC) || (DEVICE_TYPE == DEVICE_MJWSD05MMC)
#if USE_SENSOR_HX71X && (DEV_SERVICES & SERVICE_PRESSURE)
hx711_gpio_wakeup();
#endif
// Read config
if(flash_read_cfg(&old_ver, EEP_ID_VER, sizeof(old_ver)) != sizeof(old_ver))
old_ver = 0;
Expand Down Expand Up @@ -765,8 +777,7 @@ void user_init_normal(void) {//this will get executed one time after power up
init_sensor();
#endif
#if USE_SENSOR_HX71X && (DEV_SERVICES & SERVICE_PRESSURE)
hx711_gpio_wakeup();
hx71x_get_data(HX71XMODE_A128);
hx71x_get_data(HX71XMODE_A128); // Start measure
#endif
#if (DEV_SERVICES & SERVICE_PLM)
calibrate_rh();
Expand Down
69 changes: 40 additions & 29 deletions src/hx71x.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int hx71x_get_data(hx71x_mode_t mode) {

u8 r = irq_disable();

write_reg8(pcClkReg, read_reg8(pcClkReg) & (~tmp_s));
write_reg8(pcClkReg, read_reg8(pcClkReg) & (~tmp_s)); // preset PD_SCK output 0

reg_gpio_oen(GPIO_HX71X_SCK) &= ~tmp_s; // Enable PD_SCK output

Expand All @@ -43,21 +43,32 @@ int hx71x_get_data(hx71x_mode_t mode) {

u32 pcRxReg = (0x580+((GPIO_HX71X_DOUT>>8)<<3)); // reg_gpio_in() register GPIO input

u32 dout = 256; // HX71XMODE_A128 - Period: 94 ms, Pulse (1): 81.5 us

// sleep_us(1);

while((read_reg8(pcRxReg) & tmp_d) != 0 && dout-- != 0);
// HX71x выдает сигнал занятости при каждом авто-измерении 10 или 40Hz.
// Пока идет измерение - считываемые данные не верны!
// Решение только одно - читать сразу по фронту готовности (set SENSOR_HX71X_WAKEAP = 1), пока идет пауза до следующего измерения
// Иначе будут сбои в показаниях, которые не отследить

u32 dout = 256; // HX71XMODE_A128 - Period: 94 ms, Pulse (1): 81.5 us
while(dout-- != 0) {
if((read_reg8(pcRxReg) & tmp_d) == 0) {
break;
} else {
x |= 1;
}
}

dout = 0;
while(i--) {
sleep_us(1);
write_reg8(pcClkReg, read_reg8(pcClkReg) | tmp_s);
write_reg8(pcClkReg, read_reg8(pcClkReg) | tmp_s); // PD_SCK set "1"
sleep_us(1);
dout <<= 1;
if(read_reg8(pcRxReg) & tmp_d)
dout |= x;
write_reg8(pcClkReg, read_reg8(pcClkReg) & (~tmp_s));
write_reg8(pcClkReg, read_reg8(pcClkReg) & (~tmp_s)); // PD_SCK set "0"
}
reg_gpio_oen(GPIO_HX71X_SCK) |= tmp_s; // Disable PD_SCK output
// reg_gpio_ie(GPIO_HX71X_DOUT) &= ~tmp_d; // Disable PD_DOUT input
Expand All @@ -67,13 +78,6 @@ int hx71x_get_data(hx71x_mode_t mode) {
return dout;
}

/*
_attribute_ram_code_
void hx71x_suspend(void) {
cpu_set_gpio_wakeup(GPIO_HX71X_DOUT, Level_Low, 1); // pad wakeup deepsleep enable
bls_pm_setWakeupSource(PM_WAKEUP_PAD | PM_WAKEUP_TIMER); // gpio pad wakeup suspend/deepsleep
}
*/

_attribute_ram_code_
uint16_t hx71x_get_volume(void) { // in 10 milliliters
Expand All @@ -89,7 +93,7 @@ uint16_t hx71x_get_volume(void) { // in 10 milliliters

// volume in the tank when the overflow sensor is triggered (in 10 milliliters)
void hx71x_calibration(void) {
if(hx71x.cfg.volume_10ml) {
if(hx71x.cfg.volume_10ml && hx71x.value) {
uint32_t coef = hx71x.value / hx71x.cfg.volume_10ml;
uint32_t delta = hx71x.cfg.coef >> 4; // div 16 -> 6.25%
if(coef < hx71x.cfg.coef + delta && coef > hx71x.cfg.coef - delta) {
Expand All @@ -106,27 +110,34 @@ _attribute_ram_code_
void hx71x_task(void) {
uint32_t value;
if(BM_IS_SET(reg_gpio_in(GPIO_HX71X_DOUT), GPIO_HX71X_DOUT & 0xff) == 0) {
value = hx71x_get_data(HX71XMODE_A128) + 0x80000000;
hx71x.adc = value;
if(value > hx71x.cfg.zero) {
value -= hx71x.cfg.zero;
hx71x.value = value;
value /= hx71x.cfg.coef; // in 10 milliliters
if(value > MAX_TANK_VOLUME_10ML)
value = MAX_TANK_VOLUME_10ML-1;
} else {
value = 0;
// hx71x.value = value;
// HX71X_DOUT = "0"
value = hx71x_get_data(HX71XMODE_A128);
if((value & 1) == 0) {
value += 0x80000000;
hx71x.adc = value;
if(value > hx71x.cfg.zero) {
value -= hx71x.cfg.zero;
hx71x.value = value;
value /= hx71x.cfg.coef; // in 10 milliliters
if(value > MAX_TANK_VOLUME_10ML)
value = MAX_TANK_VOLUME_10ML-1;
} else {
value = 0;
hx71x.value = value;
}
hx71x.summator += value;
hx71x.count++;
}
hx71x.summator += value;
hx71x.count++;
}
}

void hx71x_init(void) {
// cpu_set_gpio_wakeup(GPIO_HX71X_DOUT, Level_Low, 1); // pad wakeup deepsleep enable
// bls_pm_setWakeupSource(PM_WAKEUP_PAD | PM_WAKEUP_TIMER); // gpio pad wakeup suspend/deepsleep
/*
_attribute_ram_code_
void hx71x_suspend(void) {
cpu_set_gpio_wakeup(GPIO_HX71X_DOUT, Level_Low, 1); // pad wakeup deepsleep enable
bls_pm_setWakeupSource(PM_WAKEUP_PAD | PM_WAKEUP_TIMER); // gpio pad wakeup suspend/deepsleep
}
*/

#endif // USE_SENSOR_HX71X

8 changes: 7 additions & 1 deletion src/hx71x.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

#define MAX_TANK_VOLUME_10ML 32768 // 327 liters

#define SENSOR_HX71X_WAKEAP 1 // =0 - wakeap HX71X disable (LOW POWER), =1 - wakeap enable (HX71X read measure: 10 Hz)
// HX71x выдает сигнал занятости при каждом авто-измерении 10 или 40Hz.
// Пока идет измерение - считываемые данные не верны!
// Решение только одно - читать сразу по фронту готовности (set SENSOR_HX71X_WAKEAP = 1), пока идет пауза до следующего измерения
// Иначе будут сбои в показаниях, которые не отследить

typedef struct _hx71x_cfg_t {
uint32_t zero;
uint32_t coef;
Expand Down Expand Up @@ -50,7 +56,7 @@ extern hx71x_cfg_t def_hx71x_cfg;
int hx71x_get_data(hx71x_mode_t mode);
void hx71x_calibration(void);
uint16_t hx71x_get_volume(void); // in 10 milliliters
//void hx71x_suspend(void);
// void hx71x_suspend(void);
void hx71x_task(void);

inline void hx711_go_sleep(void) {
Expand Down

0 comments on commit 03094b7

Please sign in to comment.