ESP32 sleep_us periodic fail

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
cable134
Posts: 28
Joined: Sun Aug 20, 2017 10:51 pm

ESP32 sleep_us periodic fail

Post by cable134 » Tue Jul 02, 2019 12:55 pm

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)
Attachments
Screenshot 2019-07-02 at 15.38.12.png
Screenshot 2019-07-02 at 15.38.12.png (160.02 KiB) Viewed 2161 times

cable134
Posts: 28
Joined: Sun Aug 20, 2017 10:51 pm

Re: ESP32 sleep_us periodic fail

Post by cable134 » Thu Jul 04, 2019 1:02 pm

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.

cable134
Posts: 28
Joined: Sun Aug 20, 2017 10:51 pm

Re: ESP32 sleep_us periodic fail

Post by cable134 » Fri Jul 05, 2019 11:59 pm

The secret was in txbuf.

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

Post Reply