pycom / pycom-micropython-sigfox

A fork of MicroPython with the ESP32 port customized to run on Pycom's IoT multi-network modules.
MIT License
199 stars 167 forks source link

UART receive buffer full #300

Closed davesto closed 5 years ago

davesto commented 5 years ago

(sysname='FiPy', nodename='FiPy', release='1.20.0.rc8', version='v1.9.4-7b83c6d on 2019-03-06', machine='FiPy with ESP32', lorawan='1.0.2', sigfox='1.0.1')

The UART receive buffer seems to fill after ~600 bytes of data have been received In machuart.c, MACHUART_RX_BUFFER_LEN is defined as 4096 but the buffer seems to run out of steam well before that.

Test configuration is an Expansion board with pin 9 wired to pin 10. Test code transmits N bytes, reads N bytes and bails when receive data is inconsistent.

Test code:

def uart_buffer_test():
    '''uart buffer test function'''

    uart = machine.UART(
        1, baudrate=9600, bits=8, parity=None, stop=1, pins=('P9', 'P10'))

    tx_start = 32
    tx_stop = 8192
    tx_step = 32
    tx_buffer = bytearray('')

    # create data block
    block_end = tx_stop / 2
    block = bytearray('')
    for num in range(1, block_end):
        block += num.to_bytes(2, 'big')

    # multiple blocks
    for num in range(tx_stop / len(block)):
        tx_buffer += block

    try:
        for size in range(tx_start, tx_stop, tx_step):
            rx_data = bytearray('')
            tx_data = bytearray('')
            # send data
            tx_data = tx_buffer[0:size]
            written = uart.write(tx_data)

            time.sleep(1)

            # receive data
            rx_data = uart.read(len(tx_data))
            #test
            #rx_data = tx_buffer[0:size]
            #rx_data = rx_data[0:3] + b'\00' + rx_data[4:]

            # compare data
            if not rx_data == tx_data:
                print('Error sending ', size, ' bytes')
                if rx_data:
                    print('Sent ', written, ' received ', len(rx_data),
                        ' bytes')
                    error = 0
                    for index, _ in enumerate(rx_data):
                        if rx_data[index] != tx_data[index]:
                            error = index
                            break
                    print('First error at offset ', error)
                    print('Tail expected \r\n', tx_data[error:])
                    print('Tail received \r\n', bytearray(rx_data[error:]))
                    break
                else:
                    print('Sent ', written, ' received no bytes')
            else:
                print('Sent and received ', size, ' bytes')

    except Exception as ex:  # pylint: disable=broad-except
        print('exception ', ex)

Test output:

Sent and received  32  bytes
Sent and received  64  bytes
[snip]
Sent and received  672  bytes
Sent and received  704  bytes
Error sending  736  bytes
Sent  736  received  607  bytes
First error at offset  600
Tail expected
bytearray(b'\x01-\x01.\x01/\x010\x011\x012\x013\x014\x015\x016\x017\x018\x019\x01:\x01;\x01<\x01=\x01>\x01?\x01@\x01A\x01B\x01C\x01D\x01E\x01F\x01G\x01H\x01I\x01J\x01K\x01L\x01M\x01N\x01O\x01P\x01Q\x01R\x01S\x01T\x01U\x01V\x01W\x01X\x01Y\x01Z\x01[\x01\\\x01]\x01^\x01_\x01`\x01a\x01b\x01c\x01d\x01e\x01f\x01g\x01h\x01i\x01j\x01k\x01l\x01m\x01n\x01o\x01p')
Tail received
bytearray(b'm\x01n\x01o\x01p')
iwahdan88 commented 5 years ago

@davesto The MACHUART_RX_BUFFER_LEN is set to 512 in the development firmware starting from v1.20.0.rc0 and you are using v1.20.0.rc8 so I believe the UART Rx buffer is filled at 512 , however as of v1.20.0.rc12, we made the rx buffer size configurable via Micropython see 81167ed. so please try testing with

uart = machine.UART(
        1, baudrate=9600, bits=8, parity=None, stop=1, pins=('P9', 'P10'), rx_buffer_size=<buff_size>)

on v1.20.0.rc12.1 and see if that fixes the problem.

Thanks