Dear forum,
is there any way to enable advanced UART features e.g., (SwapTXRX, RXInvert, TXInvert) from within micropython or by changing certain flags in the source code (and then cross-compile and flash new firmware). Board OpenMV3 (STM32F7xxxx). I’ve seen that these flags are available in the HAL definitions, but I don’t know how to activate them. In previous firmware versions it seems that the MAPPING of UART3_TX to GPIO_10 was hard coded in some config file and was now refactored.
Kind regards
Chris
Yes, use the stm.mem32 feature. This lets you read/write SRAM memory locations directly.
e.g.
# djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
@micropython.viper
def _hash(self, data, size : int) -> uint: # private
h = 5381
d = ptr8(data)
for i in range(size):
h = ((h << 5) + h) ^ d[i]
return uint(h)
def __init__(self): # private
self.__crc_16 = self.__def_crc_16
if omv.board_type() == "H7":
stm.mem32[stm.RCC + stm.RCC_AHB4ENR] = stm.mem32[stm.RCC + stm.RCC_AHB4ENR] | (1 << 19)
stm.mem32[stm.CRC + stm.CRC_POL] = 0x1021
self.__crc_16 = self.__stm_crc_16
elif omv.board_type() == "F7":
stm.mem32[stm.RCC + stm.RCC_AHB1ENR] = stm.mem32[stm.RCC + stm.RCC_AHB1ENR] | (1 << 12)
stm.mem32[stm.CRC + stm.CRC_POL] = 0x1021
self.__crc_16 = self.__stm_crc_16
self._stream_writer_queue_depth_max = 255
Many thanks. That is helpful. It seems that I can’t write these registers. Is there something I need to enable first?
Test-Code:
def print_uart_registers(label):
print(label,
" CR1: ", stm.mem32[stm.USART3 + stm.USART_CR1],
" CR2: ", stm.mem32[stm.USART3 + stm.USART_CR2],
" BRR: ", stm.mem32[stm.USART3 + stm.USART_BRR])
def set_TXRX_swap():
stm.mem32[stm.USART3 + stm.USART_CR2] = stm.mem32[stm.USART3 + stm.USART_CR2] | (1 << 15)
print_uart_registers("UART not created ")
set_TXRX_swap()
print_uart_registers("VerifySetSwap ")
baud = 115200
uart = machine.UART(3, baud)
print_uart_registers("UART instance 115200 ")
set_TXRX_swap()
print_uart_registers("VerifySetSwap ")
uart.init(9600, timeout_char=100, rxbuf=2500, parity=None)
print_uart_registers("UART initialized 9600")
set_TXRX_swap()
print_uart_registers("VerifySetSwap ")
UART not created CR1: 0 CR2: 0 BRR: 0
VerifySetSwap CR1: 0 CR2: 0 BRR: 0
UART instance 115200 CR1: 45 CR2: 0 BRR: 469
VerifySetSwap CR1: 45 CR2: 0 BRR: 469
UART initialized 9600 CR1: 45 CR2: 0 BRR: 5625
VerifySetSwap CR1: 45 CR2: 0 BRR: 5625
No matter, when I set the register CR2, it keeps being at 0
I mean one could hard code this into:
But I would rather have it on micropython level and not “fork” the firmware
You need to enable the UART via the RCC reg.
I’d import the module first, init it, and then apply your custom changes. There are a lot of other registers you have to setup first.
Many thanks. Activating the register did the trick.
def set_TXRX_swap():
stm.mem32[stm.RCC + stm.RCC_APB1ENR] |= 0x00040000
stm.mem32[stm.USART3 + stm.USART_CR2] |= (1 << 15)