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; +}