Skip to content

Commit

Permalink
serial: resync serial data after invalid read
Browse files Browse the repository at this point in the history
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 skot#24
  • Loading branch information
tommy committed Oct 20, 2023
1 parent a4da6e2 commit e2bb38f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 42 deletions.
19 changes: 1 addition & 18 deletions components/bm1397/bm1366.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 1 addition & 23 deletions components/bm1397/bm1397.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion components/bm1397/include/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
// recieve packet with 0xaa 0x55 header
void *SERIAL_rx_aa55(uint8_t *data,const int length);

#endif /* SERIAL_H_ */
38 changes: 38 additions & 0 deletions components/bm1397/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e2bb38f

Please sign in to comment.