From e0283812418d409e927d7dc8aedaad8751e82bda Mon Sep 17 00:00:00 2001 From: zhji Date: Sun, 11 Aug 2024 20:29:57 +0800 Subject: [PATCH] [update] match new image --- stm32f4/src/dwin.c | 189 +++++++++++++++++++++++++++----------------- stm32f4/src/dwin.h | 4 +- stm32f4/src/rs485.c | 61 +++++++++----- stm32f4/src/rs485.h | 17 ++-- 4 files changed, 173 insertions(+), 98 deletions(-) diff --git a/stm32f4/src/dwin.c b/stm32f4/src/dwin.c index bae7988..da570ed 100644 --- a/stm32f4/src/dwin.c +++ b/stm32f4/src/dwin.c @@ -15,6 +15,7 @@ uint8_t dwin_buff_rx[DWIN_BUFF_RX_LEN] __attribute__((aligned(16))); static int __attribute__((section(".bss_ccm"))) sm = 0; extern uint32_t system_tick_cnt; +extern uint8_t cali_succeed; void dwin_init(void) { @@ -107,42 +108,79 @@ static void dwin_endian_reverse_2byte(uint8_t *buffer, uint16_t val) buffer[1] = (uint8_t)(val & 0xFF); } +static void dwin_endian_reverse_4byte_float(uint8_t *buffer, float val) +{ + uint32_t u32; + + u32 = *((uint32_t *)(&val)); + buffer[0] = (uint8_t)((u32 >> 24) & 0xFF); + buffer[1] = (uint8_t)((u32 >> 16) & 0xFF); + buffer[2] = (uint8_t)((u32 >> 8) & 0xFF); + buffer[3] = (uint8_t)(u32 & 0xFF); +} + void dwin_rx_parse(uint8_t *buffer, uint16_t len) { uint32_t u32; uint8_t *p = buffer; - if (p[0] != 0x5A || p[1] != 0xA5 || p[2] != 0x12 || p[3] != 0x83 || p[4] != 0x51 || p[5] != 0x00 || p[6] != 0x07) { + if (p[0] != 0x5A || p[1] != 0xA5 || p[2] != (DWIN_DATA_CTRL_LENG * 2 + 4) || p[3] != 0x83 || p[4] != 0x51 || p[5] != 0x00 || p[6] != DWIN_DATA_CTRL_LENG) { + /* read data error or other commands */ + if (cali_succeed) { + cali_succeed = 0; + sm = 7; + return; + } sm++; if (sm > 1) { sm = 0; } return; } - rs485_cfg_dwin.address = p[16]; - u32 = (uint32_t)p[20]; - u32 |= (uint32_t)p[19] << 8; - u32 |= (uint32_t)p[18] << 16; - u32 |= (uint32_t)p[17] << 24; + /* read data from dwin screen */ + p += 7; + rs485_cfg_dwin.init = p[0 * 2 + 1]; + rs485_cfg_dwin.address = p[1 * 2 + 1]; + rs485_cfg_dwin.confirm = p[4 * 2 + 1]; + rs485_cfg_dwin.cali_zero = p[5 * 2 + 1]; + rs485_cfg_dwin.cali_span = p[6 * 2 + 1]; + rs485_cfg_dwin.cali_restore = p[9 * 2 + 1]; + u32 = (uint32_t)p[7]; + u32 |= (uint32_t)p[6] << 8; + u32 |= (uint32_t)p[5] << 16; + u32 |= (uint32_t)p[4] << 24; rs485_cfg_dwin.baudrate = u32; - u32 = (uint32_t)p[14]; - u32 |= (uint32_t)p[13] << 8; - u32 |= (uint32_t)p[12] << 16; - u32 |= (uint32_t)p[11] << 24; - rs485_cfg_dwin.concentration = u32; - rs485_cfg_dwin.restore = p[10]; - rs485_cfg_dwin.cali = p[8]; + u32 = (uint32_t)p[17]; + u32 |= (uint32_t)p[16] << 8; + u32 |= (uint32_t)p[15] << 16; + u32 |= (uint32_t)p[14] << 24; + rs485_cfg_dwin.cali_span_concentration = u32; - if (rs485_cfg_dwin.restore) { - rs485_cfg_dwin.restore_bak = 1; + if (rs485_cfg_dwin.cali_restore) { + rs485_cfg_dwin.cali_restore_bak = 1; sm = 2; return; } - if (rs485_cfg_dwin.cali) { - rs485_cfg_dwin.cali_bak = 1; + if (rs485_cfg_dwin.cali_span) { + rs485_cfg_dwin.cali_span_bak = 1; sm = 3; return; } + if (rs485_cfg_dwin.cali_zero) { + rs485_cfg_dwin.cali_zero_bak = 1; + sm = 4; + return; + } + if (rs485_cfg_dwin.confirm) { + rs485_cfg_dwin.confirm_bak = 1; + sm = 5; + return; + } + if (rs485_cfg_dwin.cali_span_concentration == 0) { + rs485_cfg_dwin.cali_span_concentration = lark1s_gas_info.range / 2; + sm = 6; + return; + } sm++; if (sm > 1) { sm = 0; @@ -189,10 +227,12 @@ void dwin_loop(void) for (uint16_t j = 0; j < 16 - (12 - i); j++) { /* dwin support max 16 byte */ dwin_buff_tx[len++] = ' '; } - /* sn */ - for (uint16_t j = 0; j < 16; j++) { - dwin_buff_tx[len++] = sn[j]; - } + /* range */ + dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_info.range); + len += 4; + /* detector temperature */ + dwin_endian_reverse_4byte_float(dwin_buff_tx + len, ((float)(lark1s_data.det_temp - 27300)) / 100.0f); + len += 4; /* concentration */ dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_data.gas3_reading); len += 4; @@ -208,9 +248,10 @@ void dwin_loop(void) for (uint16_t j = 0; j < 16 - (8 - i); j++) { /* dwin support max 16 byte */ dwin_buff_tx[len++] = ' '; } - /* detector temperature */ - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_data.det_temp); - len += 4; + /* sn */ + for (uint16_t j = 0; j < 16; j++) { + dwin_buff_tx[len++] = sn[j]; + } /* sig CTS */ dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_data.cts_gas3); len += 4; @@ -218,50 +259,7 @@ void dwin_loop(void) dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_data.cts_ref); len += 4; /* air pressure */ - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_data.air_pressure); - len += 4; - /* range */ - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_info.range); - len += 4; - /* min calibration */ - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_info.min_cali); - len += 4; - /* status */ - dwin_endian_reverse_2byte(dwin_buff_tx + len, lark1s_status.zero_record[2]); - len += 2; - dwin_endian_reverse_2byte(dwin_buff_tx + len, lark1s_status.span_record[2]); - len += 2; - dwin_endian_reverse_2byte(dwin_buff_tx + len, lark1s_status.active); - len += 2; - dwin_endian_reverse_2byte(dwin_buff_tx + len, lark1s_status.restore); - len += 2; - /* zero calibration */ - if (lark1s_gas_cali_data.available & 1) { - dwin_buff_tx[len++] = 'N'; - } else { - dwin_buff_tx[len++] = 'Y'; - } - dwin_buff_tx[len++] = ' '; - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_cali_data.zero_det_temp); - len += 4; - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_cali_data.zero_sig_cts); - len += 4; - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_cali_data.zero_ref_cts); - len += 4; - /* span calibration */ - if (lark1s_gas_cali_data.available & 2) { - dwin_buff_tx[len++] = 'N'; - } else { - dwin_buff_tx[len++] = 'Y'; - } - dwin_buff_tx[len++] = ' '; - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_cali_data.span_concentration); - len += 4; - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_cali_data.span_det_temp); - len += 4; - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_cali_data.span_sig_cts); - len += 4; - dwin_endian_reverse_4byte(dwin_buff_tx + len, lark1s_gas_cali_data.span_ref_cts); + dwin_endian_reverse_4byte_float(dwin_buff_tx + len, ((float)(lark1s_data.air_pressure)) / 100.0f); len += 4; } else if (sm == 2) { /* clear restore request data */ @@ -269,20 +267,65 @@ void dwin_loop(void) dwin_buff_tx[len++] = 0xA5; /* frame head */ dwin_buff_tx[len++] = 0x05; /* length */ dwin_buff_tx[len++] = 0x82; /* write operation */ - dwin_buff_tx[len++] = ((DWIN_DATA_CTRL_ADDR + 1) >> 8) & 0xFF; /* address high byte */ - dwin_buff_tx[len++] = (DWIN_DATA_CTRL_ADDR + 1) & 0xFF; /* address low byte */ + dwin_buff_tx[len++] = ((DWIN_DATA_CTRL_ADDR + 9) >> 8) & 0xFF; /* address high byte */ + dwin_buff_tx[len++] = (DWIN_DATA_CTRL_ADDR + 9) & 0xFF; /* address low byte */ dwin_buff_tx[len++] = 0x00; /* data */ dwin_buff_tx[len++] = 0x00; /* data */ } else if (sm == 3) { - /* clear restore request data */ + /* clear cali span request data */ dwin_buff_tx[len++] = 0x5A; dwin_buff_tx[len++] = 0xA5; /* frame head */ dwin_buff_tx[len++] = 0x05; /* length */ dwin_buff_tx[len++] = 0x82; /* write operation */ - dwin_buff_tx[len++] = ((DWIN_DATA_CTRL_ADDR + 0) >> 8) & 0xFF; /* address high byte */ - dwin_buff_tx[len++] = (DWIN_DATA_CTRL_ADDR + 0) & 0xFF; /* address low byte */ + dwin_buff_tx[len++] = ((DWIN_DATA_CTRL_ADDR + 6) >> 8) & 0xFF; /* address high byte */ + dwin_buff_tx[len++] = (DWIN_DATA_CTRL_ADDR + 6) & 0xFF; /* address low byte */ dwin_buff_tx[len++] = 0x00; /* data */ dwin_buff_tx[len++] = 0x00; /* data */ + } else if (sm == 4) { + /* clear cali zero request data */ + dwin_buff_tx[len++] = 0x5A; + dwin_buff_tx[len++] = 0xA5; /* frame head */ + dwin_buff_tx[len++] = 0x05; /* length */ + dwin_buff_tx[len++] = 0x82; /* write operation */ + dwin_buff_tx[len++] = ((DWIN_DATA_CTRL_ADDR + 5) >> 8) & 0xFF; /* address high byte */ + dwin_buff_tx[len++] = (DWIN_DATA_CTRL_ADDR + 5) & 0xFF; /* address low byte */ + dwin_buff_tx[len++] = 0x00; /* data */ + dwin_buff_tx[len++] = 0x00; /* data */ + } else if (sm == 5) { + /* clear confirm request data */ + dwin_buff_tx[len++] = 0x5A; + dwin_buff_tx[len++] = 0xA5; /* frame head */ + dwin_buff_tx[len++] = 0x05; /* length */ + dwin_buff_tx[len++] = 0x82; /* write operation */ + dwin_buff_tx[len++] = ((DWIN_DATA_CTRL_ADDR + 4) >> 8) & 0xFF; /* address high byte */ + dwin_buff_tx[len++] = (DWIN_DATA_CTRL_ADDR + 4) & 0xFF; /* address low byte */ + dwin_buff_tx[len++] = 0x00; /* data */ + dwin_buff_tx[len++] = 0x00; /* data */ + } else if (sm == 6) { + /* set default span concentration */ + uint32_t range = lark1s_gas_info.range / 2; + dwin_buff_tx[len++] = 0x5A; + dwin_buff_tx[len++] = 0xA5; /* frame head */ + dwin_buff_tx[len++] = 0x07; /* length */ + dwin_buff_tx[len++] = 0x82; /* write operation */ + dwin_buff_tx[len++] = ((DWIN_DATA_CTRL_ADDR + 7) >> 8) & 0xFF; /* address high byte */ + dwin_buff_tx[len++] = (DWIN_DATA_CTRL_ADDR + 7) & 0xFF; /* address low byte */ + dwin_buff_tx[len++] = (uint8_t)((range >> 24) & 0xFF); /* data */ + dwin_buff_tx[len++] = (uint8_t)((range >> 16) & 0xFF); /* data */ + dwin_buff_tx[len++] = (uint8_t)((range >> 8) & 0xFF); /* data */ + dwin_buff_tx[len++] = (uint8_t)((range >> 0) & 0xFF); /* data */ + } else if (sm == 7) { + /* pop one window for cali succeed */ + dwin_buff_tx[len++] = 0x5A; + dwin_buff_tx[len++] = 0xA5; /* frame head */ + dwin_buff_tx[len++] = 0x07; /* length */ + dwin_buff_tx[len++] = 0x82; /* write operation */ + dwin_buff_tx[len++] = 0x00; + dwin_buff_tx[len++] = 0x84; /* cmd: picture set */ + dwin_buff_tx[len++] = 0x5A; /* must */ + dwin_buff_tx[len++] = 0x01; /* must */ + dwin_buff_tx[len++] = 0x00; + dwin_buff_tx[len++] = 0x02; /* picture id */ } dwin_send(len); } diff --git a/stm32f4/src/dwin.h b/stm32f4/src/dwin.h index ce11a2e..bcab58a 100644 --- a/stm32f4/src/dwin.h +++ b/stm32f4/src/dwin.h @@ -13,9 +13,9 @@ #define DWIN_BAUDRATE (115200) #define DWIN_DATA_DISP_ADDR (0x5000) -#define DWIN_DATA_DISP_LENG (0x3A) +#define DWIN_DATA_DISP_LENG (0x24) #define DWIN_DATA_CTRL_ADDR (0x5100) -#define DWIN_DATA_CTRL_LENG (0x07) +#define DWIN_DATA_CTRL_LENG (0x0A) extern char sn[16]; extern struct lark1s_gas_info_s lark1s_gas_info; diff --git a/stm32f4/src/rs485.c b/stm32f4/src/rs485.c index 3f141e6..eac2237 100644 --- a/stm32f4/src/rs485.c +++ b/stm32f4/src/rs485.c @@ -20,6 +20,8 @@ struct lark1s_gas_info_s lark1s_gas_info __attribute__((section(".bss_ccm"))); struct lark1s_gas_cali_data_s lark1s_gas_cali_data __attribute__((section(".bss_ccm"))); struct lark1s_data_s lark1s_data __attribute__((section(".bss_ccm"))); struct lark1s_status_s lark1s_status __attribute__((section(".bss_ccm"))); +uint8_t cali_type __attribute__((section(".bss_ccm"))); /* 0:none, 1:zero, 2:span, 3:restore */ +uint8_t cali_succeed __attribute__((section(".bss_ccm"))); extern uint32_t system_tick_cnt; @@ -130,7 +132,7 @@ void rs485_state_machine_tx(void) case 4: len = lark1s_req_status(rs485_buff_tx); break; case 5: len = lark1s_req_gas3_cali_zero_record(rs485_buff_tx, LARK1S_CALI_ZERO); break; case 6: len = lark1s_req_gas3_cali_active(rs485_buff_tx, LARK1S_CALI_ZERO); break; - case 7: len = lark1s_req_gas3_cali_span_record(rs485_buff_tx, rs485_cfg_dwin.concentration); break; + case 7: len = lark1s_req_gas3_cali_span_record(rs485_buff_tx, rs485_cfg_dwin.cali_span_concentration); break; case 8: len = lark1s_req_gas3_cali_active(rs485_buff_tx, LARK1S_CALI_SPAN); break; case 9: len = lark1s_req_gas3_cali_restore(rs485_buff_tx); break; default: len = 0; break; @@ -155,6 +157,34 @@ void rs485_state_machine_rx(int len) case 9: ret = lark1s_parse_gas3_cali_restore(rs485_buff_rx, len); break; default: break; } + switch (sm) { + case 6: cali_type = 1; break; + case 8: cali_type = 2; break; + case 9: cali_type = 3; break; + default: break; + } + /* check cali status */ + if (sm == 4) { + if (cali_type == 1) { + /* cali zero */ + cali_type = 0; + if (lark1s_status.zero_record[2] == 0 && lark1s_status.active == 0) { + cali_succeed = 1; + } + } else if (cali_type == 2) { + /* cali span */ + cali_type = 0; + if (lark1s_status.span_record[2] == 0 && lark1s_status.active == 0) { + cali_succeed = 1; + } + } else if (cali_type == 3) { + /* cali restore */ + cali_type = 0; + if (lark1s_status.restore == 0) { + cali_succeed = 1; + } + } + } if ((ret != LARK1S_PARSE_OK) && (sm >= 5)) { sm = 0; return; @@ -168,16 +198,15 @@ void rs485_state_machine_rx(int len) sm = 0; } if (sm < 4) { - if (rs485_cfg_dwin.restore_bak) { - rs485_cfg_dwin.restore_bak = 0; + if (rs485_cfg_dwin.cali_restore_bak) { + rs485_cfg_dwin.cali_restore_bak = 0; sm = 9; - } else if (rs485_cfg_dwin.cali_bak) { - rs485_cfg_dwin.cali_bak = 0; - if (rs485_cfg_dwin.concentration == 0) { - sm = 5; - } else { - sm = 7; - } + } else if (rs485_cfg_dwin.cali_span_bak) { + rs485_cfg_dwin.cali_span_bak = 0; + sm = 7; + } else if (rs485_cfg_dwin.cali_zero_bak) { + rs485_cfg_dwin.cali_zero_bak = 0; + sm = 5; } } } @@ -185,20 +214,16 @@ void rs485_state_machine_rx(int len) void rs485_loop(void) { static uint32_t ms = 0; - static uint32_t baudrate = 0; - static uint8_t addr = 0; if (system_tick_cnt < (ms + 200)) { return; } ms = system_tick_cnt; - if (baudrate != rs485_cfg_dwin.baudrate) { - baudrate = rs485_cfg_dwin.baudrate; + + if (rs485_cfg_dwin.confirm_bak) { + rs485_cfg_dwin.confirm_bak = 0; rs485_init(); - } - if (addr != rs485_cfg_dwin.address) { - addr = rs485_cfg_dwin.address; - lark1s_set_mb_address(addr); + lark1s_set_mb_address(rs485_cfg_dwin.address); } rs485_state_machine_tx(); } diff --git a/stm32f4/src/rs485.h b/stm32f4/src/rs485.h index 4a3b7c3..967f034 100644 --- a/stm32f4/src/rs485.h +++ b/stm32f4/src/rs485.h @@ -14,12 +14,19 @@ struct rs485_cfg_dwin { uint32_t baudrate; - uint32_t concentration; - uint8_t cali; - uint8_t cali_bak; - uint8_t restore; - uint8_t restore_bak; + uint32_t cali_span_concentration; + uint8_t cali_zero; + uint8_t cali_zero_bak; + uint8_t cali_span; + uint8_t cali_span_bak; + uint8_t cali_restore; + uint8_t cali_restore_bak; uint8_t address; + uint8_t confirm; + uint8_t confirm_bak; + uint8_t init; + uint8_t init_bak; + uint8_t cali_succeed; }; extern struct rs485_cfg_dwin rs485_cfg_dwin;