Merge pull request #6 from shufps/non-blocking-rx

uses non-blocking read for asic response
This commit is contained in:
Thomas Shufps 2024-09-14 10:56:58 +02:00 committed by GitHub
commit a05f666031
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 28 deletions

View File

@ -362,24 +362,23 @@ bool BM1397_receive_work(uint16_t timeout, asic_result *result)
{
uint8_t *rcv_buf = (uint8_t*) result;
// wait for a response, wait time is pretty arbitrary
int received = SERIAL_rx(rcv_buf, 9, timeout);
// non blocking read
int received = SERIAL_rx_non_blocking(rcv_buf, 9);
if (received < 0)
{
Serial.println("Error in serial RX");
return false;
}
else if (received == 0)
else if (!received)
{
// Didn't find a solution, restart and try again
// we have not received any data
return false;
}
if (received != 9 || rcv_buf[0] != 0xAA || rcv_buf[1] != 0x55)
{
Serial.println("Serial RX invalid. Resetting receive buffer ...");
//ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received);
SERIAL_clear_buffer();
return false;
}

View File

@ -57,6 +57,28 @@ size_t SERIAL_check_for_data() {
return length;
}
/// @brief waits for a serial response from the device
/// @param buf buffer to read data into
/// @param buf number of ms to wait before timing out
/// @return number of bytes read, or -1 on error
int16_t SERIAL_rx_non_blocking(uint8_t *buf, uint16_t size) {
// check how much data we have
size_t available = SERIAL_check_for_data();
// no data available, return 0
if (!available) {
return 0;
}
// check for incomplete data
if (available && available < size) {
Serial.printf("not returning incomplete data ... %d vs %d\n", (int) available, (int) size);
return 0;
}
// timeout 0 means non_blocking read
return SERIAL_rx(buf, size, 0);
}
/// @brief waits for a serial response from the device
/// @param buf buffer to read data into
@ -64,13 +86,6 @@ size_t SERIAL_check_for_data() {
/// @return number of bytes read, or -1 on error
int16_t SERIAL_rx(uint8_t *buf, uint16_t size, uint16_t timeout_ms)
{
// don't return incomplete data
size_t available = SERIAL_check_for_data();
if (available && available < size) {
Serial.printf("not returning parts of data ... %d vs %d\n", (int) available, (int) size);
return 0;
}
int16_t bytes_read = uart_read_bytes(UART_NUM_1, buf, size, timeout_ms / portTICK_PERIOD_MS);
// if (bytes_read > 0) {
// printf("rx: ");
@ -83,21 +98,6 @@ int16_t SERIAL_rx(uint8_t *buf, uint16_t size, uint16_t timeout_ms)
return bytes_read;
}
void SERIAL_debug_rx(void)
{
int ret;
uint8_t buf[CHUNK_SIZE];
ret = SERIAL_rx(buf, 100, 20);
if (ret < 0)
{
fprintf(stderr, "unable to read data\n");
return;
}
memset(buf, 0, 1024);
}
void SERIAL_clear_buffer(void)
{
uart_flush(UART_NUM_1);

View File

@ -4,8 +4,8 @@
int SERIAL_send(uint8_t *, int, bool);
void SERIAL_init(void);
void SERIAL_debug_rx(void);
int16_t SERIAL_rx(uint8_t *, uint16_t, uint16_t);
int16_t SERIAL_rx_non_blocking(uint8_t *buf, uint16_t size);
void SERIAL_clear_buffer(void);
void SERIAL_set_baud(int baud);