Page 1 of 1

ESP32 sleep_us periodic fail

Posted: Tue Jul 02, 2019 12:55 pm
by cable134
Hi everyone.
Hope someone will help me with the problem I face.

Now I'm working on Modbus RTU tests and faced that sometimes time.sleep_us provides a wrong sleep period.
Please look at code and diagram from my Logic.
The idea of the test example is to send via UART 255 symbols and rise up an additional pin during the transmission.

If you look at the diagram you will see that time to time my UPes pin going Down with the delay (marked red).
I do not understand what is the reason.
I'm testing it on esp32-20190529-v1.11.bin

Will be glad for any ideas.
Thank you.

Code: Select all

import machine
import time

tx = 17
rx = 2
ctrl = 23
baudrate = 300

ctrlPin = machine.Pin(ctrl, mode=machine.Pin.OUT)
symbol_us = (10 * 1000000) / baudrate # time we need to send 1 symbol 

bin_arr = bytearray(b'') # Payload for test
for i in range(1,256): 
    bin_arr.append(i)
 
ctl_up_us = int( symbol_us * len(bin_arr) ) # Time of expected TX
print ('Length of payload: {}'.format(len(bin_arr)))
print ('Ctl Pin UP, us: {}'.format(ctl_up_us))

# timeout_char=10
uart = machine.UART( 0x02, baudrate=baudrate, bits=8, parity=None, stop=1, tx=tx, rx=rx)
                          
while True:
    ctrlPin(1)
    uart.write(bin_arr)
    time.sleep_us( ctl_up_us ) # !!! Problematic place
    ctrlPin(0)    
    time.sleep_us(300000)

Re: ESP32 sleep_us periodic fail

Posted: Thu Jul 04, 2019 1:02 pm
by cable134
An update.

If I comment out (disable) uart.write operation, sleep work fine.
Timings are beautiful.

So seems that UART operation does something wrong with sleep command.

Re: ESP32 sleep_us periodic fail

Posted: Fri Jul 05, 2019 11:59 pm
by cable134
The secret was in txbuf.

If you are going to send via UART a big array you should increase txbuf to get stable timing.