uart write slows down thread

  • I have the following code on a LoPy4. Firmware version 1.20.2.r4

    def toggleRGB(delay,color,count):
        for j in range(count):
    def uart_tx_rx():
        while True:
            # uart.write("some data here\n")
            # time.sleep_ms(30)
            # uart.write("some data here\n")
            # time.sleep_ms(30)
            toggleRGB(5,0xFF69B4,1) #led flash means we are alive
    _thread.start_new_thread(uart_tx_rx, ())

    As shown, the RGB led flashes every 2.5 seconds. But when I comment out the uart.write(), it becomes very slow and indeterminate. Sometimes taking 14 seconds, other times 30 seconds. The uart object has been declared earlier and works well.
    Any idea what's going on?

  • @jcaron No it isn't. The only printout i have no control over is the boot printout. Nothing else is using it.

  • @mbyamukama That's strange. I do not expect P23 to have any side effect. Some crosstalk between P2 and P3 may be more a problem. Since you have already a sleep_ms(30) after each UART write to wait until the data has been sent, can you increase that delay (especially the second one) longer to ensure, that all data has been sent before you flash the LED.

  • @mbyamukama Since you only mention 2 pins, I suppose you don't use flow control (RTS/CTS)? It shouldn't matter anyway as IIRC write is non-blocking, but you never know...

    Which UART bus are you using (the number in the constructor)?

    Can you add logs with timestamps?

  • @robert-hh It flashes well WITHOUT the uart write and takes a long time otherwise. I am writing very little data. About 150-300 bytes at 115200bps. I am using uart1 on pins P3 and P4. One thing I did not show, because I didn't think it's an issue, is that before the uart write, I pull pin P23 low. This pin controls a fast SPDT analog switch that enables me to multiplex the LoPy4's uart between two different devices. I do not have any reason to believe that is an issue because the unit tests work well.

  • @jcaron Yes. I mean, when I uncomment. I can't correct it unfortunately as the 3600s has already elapsed.

  • @jcaron That's what I was unclear about too, so I asked more specifically. @mbyamukama Which pins do you use for the UART?

  • @robert-hh I believe it's the opposite. The "comment out" puzzled me as well, I admit, I think OP meant "when I uncomment".

  • @mbyamukama Is the UART used by other things (including any debug info from the OS)? How much data are you writing? What are the UART settings (speed, mostly)?

    14 to 30 seconds seems quite extreme for it to be related to the time needed to actually send the data (at a relatively slow 9600 bps that equates to about 15 to 30 Kbytes of data), but maybe you're actually sending that much or using an even slower speed, or you have something else spewing logs like crazy?

  • @mbyamukama So with UART write if flashes well, and without UART write it does not. What happens if you increase the delay parameter, lets say from 5 to 50?

Log in to reply

Pycom on Twitter