pulkin / micropython

MicroPython implementation on Ai-Thinker GPRS module A9 (RDA8955)
https://micropython.org
MIT License
103 stars 30 forks source link

\xFF is missing from UART.read() #116

Closed garudaonekh closed 1 year ago

garudaonekh commented 1 year ago

Hi, I send data from a LORA RS485 via UART to another. All the 0xFF byte are missing. At first, I thought it maybe the LORA Module have problem, I try another pair from a different company. Exactly same result. I try with computer UART, the ff byte is received normally. For example:

uart.write(b'\xff\xff\x01\x02') On the receiver will receive as:

b'\x01\x02'

The problem here is with the read().

Thanks;

garudaonekh commented 1 year ago

if I put the board to sleep, all the data coming correctly into the buffer and I can read after wakeup from time.sleep(). but if data is coming while the board is running, all \xFF are missing.

robert-hh commented 1 year ago

The culprit is the function uart_rx_intr_handler() in uart.c, which suppresses all characters that happen to be the mp_interrupt_char(). A simple fix could be:

static void uart_rx_intr_handler(UART_Callback_Param_t param) {
    // handles rx interrupts
    ringbuf_t *ringbuf = uart_ringbuf + param.port - 1;
    int* to_dupterm = uart_attached_to_dupterm + param.port - 1;
    for (uint32_t i=0; i<param.length; i++) {
        if (*to_dupterm && param.buf[i] == mp_interrupt_char) {
            mp_keyboard_interrupt();
        }
        ringbuf_put(ringbuf, param.buf[i]);
    }
}

Besides that, the data type of param.buf in the type UART_Callback_Param_t must be set to unsigned char instead of signed char. Otherwise setting mp_interrupt_char to -1 will not have the intended effect of ignoring the interrupt.

garudaonekh commented 1 year ago

Thanks. I will update and recompile

robert-hh commented 1 year ago

Since this is a bug for everyone the issue better stays open to document it.