UART automatic baud rate detection

Discussion related to "under the hood" OpenMV topics.
mirkwood
Posts: 12
Joined: Tue Mar 10, 2020 4:08 am

UART automatic baud rate detection

Postby mirkwood » Fri Mar 20, 2020 5:40 am

Hi,

I can't find how to set uart automatic baud rate detection in the docs.
If it isn't supported, can you point me in the right direction in the code to implement it?
I've had a look and I can see it in the HAL, but I'm not sure which file in the open MV source implements the uart library.

Cheers
User avatar
iabdalkader
Posts: 1168
Joined: Sun May 24, 2015 3:53 pm

Re: UART automatic baud rate detection

Postby iabdalkader » Fri Mar 20, 2020 6:35 am

We use MicroPython's drivers, you need to edit MicroPython UART driver.
User avatar
kwagyeman
Posts: 4006
Joined: Sun May 24, 2015 2:10 pm

Re: UART automatic baud rate detection

Postby kwagyeman » Fri Mar 20, 2020 1:58 pm

Hi, you can use the stm module to access registers and memory directly in MicroPython. This will allow you to turn on this feature in the ST HAL. That said, its literally going to involve you manually accessing memory addresses and read/writing register bits.
Nyamekye,
mirkwood
Posts: 12
Joined: Tue Mar 10, 2020 4:08 am

Re: UART automatic baud rate detection

Postby mirkwood » Sun Mar 22, 2020 7:09 pm

If anyone else is interested this should work.

Code: Select all

# Automatic baud rate detection
import stm
from pyb import UART

USART_BASE_ADDRESS = 0x40011000

USART2_ADDRESS = USART_BASE_ADDRESS  + 4

USART2_ABRMODE_SHIFT = 21
USART2_ABRMODE_STARTBIT = 0 << USART2_ABRMODE_SHIFT
USART2_ABRMODE_FALLINGEDGE = 1 << USART2_ABRMODE_SHIFT
USART2_ABRMODE_0x7F = 2 << USART2_ABRMODE_SHIFT
USART2_ABRMODE_0x55 = 3 << USART2_ABRMODE_SHIFT

USART2_ABRMODE_MASK = 3 << USART2_ABRMODE_SHIFT
USART2_ABREN = 1 << 20

USART_ISR_ADDRESS = USART_BASE_ADDRESS + 0x1C
USART_RQR_ADDRESS = USART_BASE_ADDRESS + 0x18


def initUart():
    uart = UART(1, 115200)
    # change USART2_ABRMODE_STARTBIT to whatever mode you want here
    stm.mem32[USART2_ADDRESS] = USART2_ABREN | USART2_ABRMODE_STARTBIT | (stm.mem32[USART2_ADDRESS] & ~USART2_ABRMODE_MASK)
    return uart

def isAutoBaudRateDetected():
    return stm.mem32[USART_ISR_ADDRESS] & (1<<15)

def retryAutomaticBaudRateDetection():
    stm.mem32[USART_RQR_ADDRESS] |= 1


uart = initUart()

while(True):
    try:
        if (isAutoBaudRateDetected()):
            uart.write('Hello!')
    except OSError as error:
        # should really be more specific here about which OSError it is
        # if baud rate is wrong you'll get an [Errno 110] ETIMEDOUT
        print(error)
        retryAutomaticBaudRateDetection()
User avatar
kwagyeman
Posts: 4006
Joined: Sun May 24, 2015 2:10 pm

Re: UART automatic baud rate detection

Postby kwagyeman » Sun Mar 22, 2020 8:38 pm

Thanks for posting!
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 7 guests