From 287d309f0c5f6e175eaadf4bb80459eaa14f48c7 Mon Sep 17 00:00:00 2001 From: tommy Date: Thu, 19 Oct 2023 20:42:09 -0400 Subject: [PATCH 1/5] Serial Synchronization #24 --- components/bm1397/bm1366.c | 19 +------------- components/bm1397/bm1397.c | 24 +----------------- components/bm1397/include/serial.h | 5 +++- components/bm1397/serial.c | 40 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/components/bm1397/bm1366.c b/components/bm1397/bm1366.c index daec0212..c04f0fc5 100644 --- a/components/bm1397/bm1366.c +++ b/components/bm1397/bm1366.c @@ -633,24 +633,7 @@ void BM1366_send_work(void * pvParameters, bm_job * next_bm_job) asic_result * BM1366_receive_work(void) { - // wait for a response, wait time is pretty arbitrary - int received = SERIAL_rx(asic_response_buffer, 11, 60000); - - if (received < 0) { - ESP_LOGI(TAG, "Error in serial RX"); - return NULL; - } else if (received == 0) { - // Didn't find a solution, restart and try again - return NULL; - } - - if (received != 11 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) { - ESP_LOGI(TAG, "Serial RX invalid %i", received); - ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); - return NULL; - } - - return (asic_result *) asic_response_buffer; + return SERIAL_rx_aa55(asic_response_buffer,11); } uint16_t reverse_uint16(uint16_t num) diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index b3dc8921..badba315 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -378,29 +378,7 @@ void BM1397_send_work(void *pvParameters, bm_job *next_bm_job) asic_result *BM1397_receive_work(void) { - - // wait for a response, wait time is pretty arbitrary - int received = SERIAL_rx(asic_response_buffer, 9, 60000); - - if (received < 0) - { - ESP_LOGI(TAG, "Error in serial RX"); - return NULL; - } - else if (received == 0) - { - // Didn't find a solution, restart and try again - return NULL; - } - - if (received != 9 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) - { - ESP_LOGI(TAG, "Serial RX invalid %i", received); - ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); - return NULL; - } - - return (asic_result *)asic_response_buffer; + return SERIAL_rx_aa55(asic_response_buffer,9); } task_result *BM1397_proccess_work(void *pvParameters) diff --git a/components/bm1397/include/serial.h b/components/bm1397/include/serial.h index ae165c13..6f833cf7 100644 --- a/components/bm1397/include/serial.h +++ b/components/bm1397/include/serial.h @@ -10,4 +10,7 @@ int16_t SERIAL_rx(uint8_t *, uint16_t, uint16_t); void SERIAL_clear_buffer(void); void SERIAL_set_baud(int baud); -#endif /* SERIAL_H_ */ \ No newline at end of file +// recieve packet with 0xaa 0x55 header +void *SERIAL_rx_aa55(uint8_t *data,const int length); + +#endif /* SERIAL_H_ */ diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index dcc1b9a6..8898678a 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -94,3 +94,43 @@ void SERIAL_clear_buffer(void) { uart_flush(UART_NUM_1); } + +/** + * @brief recieve packet with 0xaa 0x55 header + * @param data - buffer for received serial data + * @param length - length of expected packet + */ +void *SERIAL_rx_aa55(uint8_t *data,const int length) { + for(int len=0,cnt=0;len2) { + // valid start + if(data[0] == 0xAA && data[1] == 0x55) { + len+=received; + } + else { + for(int count=1; count < len+received; ++count) { + if(*(data+count) == 0xAA) { + // move to head and adjust read length + memmove(data, data+count, len+received-count); + len+=received-count; + break; + } + } + } + } + else { + len+=received; + } + } + return data; +} From 21ab604bd03df5806ac0cc688e655d78813b8171 Mon Sep 17 00:00:00 2001 From: tommy Date: Thu, 19 Oct 2023 21:12:22 -0400 Subject: [PATCH 2/5] Fix formatting --- components/bm1397/serial.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index 8898678a..46d5fb44 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -101,9 +101,9 @@ void SERIAL_clear_buffer(void) * @param length - length of expected packet */ void *SERIAL_rx_aa55(uint8_t *data,const int length) { - for(int len=0,cnt=0;len2) { + if (len+received > 2) { // valid start - if(data[0] == 0xAA && data[1] == 0x55) { + if (data[0] == 0xAA && data[1] == 0x55) { len+=received; - } - else { + } else { for(int count=1; count < len+received; ++count) { if(*(data+count) == 0xAA) { // move to head and adjust read length @@ -127,8 +126,7 @@ void *SERIAL_rx_aa55(uint8_t *data,const int length) { } } } - } - else { + } else { len+=received; } } From 981ae9a431516e941e7ab0ac1b1c98c3f885b4fb Mon Sep 17 00:00:00 2001 From: tommywatson Date: Thu, 19 Oct 2023 21:15:30 -0400 Subject: [PATCH 3/5] Update components/bm1397/serial.c Co-authored-by: Johnny --- components/bm1397/serial.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index 46d5fb44..2449eb45 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -117,7 +117,7 @@ void *SERIAL_rx_aa55(uint8_t *data,const int length) { if (data[0] == 0xAA && data[1] == 0x55) { len+=received; } else { - for(int count=1; count < len+received; ++count) { + for(int count = 1; count < len + received; ++count) { if(*(data+count) == 0xAA) { // move to head and adjust read length memmove(data, data+count, len+received-count); From 6fc8878282d6ef49f92913bf3f62ea65418b8eb6 Mon Sep 17 00:00:00 2001 From: tommywatson Date: Thu, 19 Oct 2023 21:15:38 -0400 Subject: [PATCH 4/5] Update components/bm1397/serial.c Co-authored-by: Johnny --- components/bm1397/serial.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index 2449eb45..bcbeb393 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -115,7 +115,7 @@ void *SERIAL_rx_aa55(uint8_t *data,const int length) { if (len+received > 2) { // valid start if (data[0] == 0xAA && data[1] == 0x55) { - len+=received; + len += received; } else { for(int count = 1; count < len + received; ++count) { if(*(data+count) == 0xAA) { From e2bb38fa9b323d93369cf2e6c48baabe4844979b Mon Sep 17 00:00:00 2001 From: tommy Date: Thu, 19 Oct 2023 23:03:09 -0400 Subject: [PATCH 5/5] serial: resync serial data after invalid read User reported bm1397 received an invalid packet and was unable to continue without manually reset the device. This patch enables the serial comms to resync when invalid/short data is received issue #24 --- components/bm1397/bm1366.c | 19 +-------------- components/bm1397/bm1397.c | 24 +------------------ components/bm1397/include/serial.h | 5 +++- components/bm1397/serial.c | 38 ++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 42 deletions(-) diff --git a/components/bm1397/bm1366.c b/components/bm1397/bm1366.c index daec0212..c04f0fc5 100644 --- a/components/bm1397/bm1366.c +++ b/components/bm1397/bm1366.c @@ -633,24 +633,7 @@ void BM1366_send_work(void * pvParameters, bm_job * next_bm_job) asic_result * BM1366_receive_work(void) { - // wait for a response, wait time is pretty arbitrary - int received = SERIAL_rx(asic_response_buffer, 11, 60000); - - if (received < 0) { - ESP_LOGI(TAG, "Error in serial RX"); - return NULL; - } else if (received == 0) { - // Didn't find a solution, restart and try again - return NULL; - } - - if (received != 11 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) { - ESP_LOGI(TAG, "Serial RX invalid %i", received); - ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); - return NULL; - } - - return (asic_result *) asic_response_buffer; + return SERIAL_rx_aa55(asic_response_buffer,11); } uint16_t reverse_uint16(uint16_t num) diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index b3dc8921..badba315 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -378,29 +378,7 @@ void BM1397_send_work(void *pvParameters, bm_job *next_bm_job) asic_result *BM1397_receive_work(void) { - - // wait for a response, wait time is pretty arbitrary - int received = SERIAL_rx(asic_response_buffer, 9, 60000); - - if (received < 0) - { - ESP_LOGI(TAG, "Error in serial RX"); - return NULL; - } - else if (received == 0) - { - // Didn't find a solution, restart and try again - return NULL; - } - - if (received != 9 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) - { - ESP_LOGI(TAG, "Serial RX invalid %i", received); - ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); - return NULL; - } - - return (asic_result *)asic_response_buffer; + return SERIAL_rx_aa55(asic_response_buffer,9); } task_result *BM1397_proccess_work(void *pvParameters) diff --git a/components/bm1397/include/serial.h b/components/bm1397/include/serial.h index ae165c13..6f833cf7 100644 --- a/components/bm1397/include/serial.h +++ b/components/bm1397/include/serial.h @@ -10,4 +10,7 @@ int16_t SERIAL_rx(uint8_t *, uint16_t, uint16_t); void SERIAL_clear_buffer(void); void SERIAL_set_baud(int baud); -#endif /* SERIAL_H_ */ \ No newline at end of file +// recieve packet with 0xaa 0x55 header +void *SERIAL_rx_aa55(uint8_t *data,const int length); + +#endif /* SERIAL_H_ */ diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index dcc1b9a6..bcbeb393 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -94,3 +94,41 @@ void SERIAL_clear_buffer(void) { uart_flush(UART_NUM_1); } + +/** + * @brief recieve packet with 0xaa 0x55 header + * @param data - buffer for received serial data + * @param length - length of expected packet + */ +void *SERIAL_rx_aa55(uint8_t *data,const int length) { + for(int len=0; len < length;) { + // wait for a response, wait time is pretty arbitrary + int received = SERIAL_rx(data+len, length-len, 60000); + if (received < 0) { + ESP_LOGI(TAG, "Error in serial RX"); + return NULL; + } else if (received == 0) { + // Didn't find a solution, restart and try again + return NULL; + } + + if (len+received > 2) { + // valid start + if (data[0] == 0xAA && data[1] == 0x55) { + len += received; + } else { + for(int count = 1; count < len + received; ++count) { + if(*(data+count) == 0xAA) { + // move to head and adjust read length + memmove(data, data+count, len+received-count); + len+=received-count; + break; + } + } + } + } else { + len+=received; + } + } + return data; +}