Uart word-size and parity

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Uart word-size and parity

Post by marfis » Fri Oct 31, 2014 10:57 am

Hi

I am using the uart module with 8bit data, even parity, 1 stop bit. (using the uPy Board as a bootrap loading device for MSP430 uPs).
Initalisation is as follows:

Code: Select all

uart = pyb.UART(4)
uart.init(9600, bits=8, parity = 0)

txdata = struct.pack('<BBB', 0x81, 0xaa,0x80)
uart.write(txdata)                      
uart.write successfully transmits 3 Bytes (double checked on the Logic Analyser). But the transmitted bytes did not include the correct parity or had framing errors.

I tried with different options (parity = 1 etc), checked the strmhal C sources and finally the STM4 family guide on this issue.
I was surprised to see that the guide states on page 957 that the parity bit is included in Bit 8 if the uart is configured in 8 Bit mode. So I tried it with bits=9 and here we go: all correct.

So as a summary: If parity bits are used, then the word-length must be 9 bits? If so a clarification in the code/examples might be helpful.

Googling around for a definition on this subject mainly describes the parity bit as an addition to the word-length (e.g. 8E1 means 8bit data, even parity, 1 stop bit where the parity bit is separate from the actual 8bit data).

BTW:
Really enjoying the boards - working flawlessly!

Martin

PS: maybe a small hint on the quick example page that the uart.write command is valid only since october might be helpful (previously it was uart.send).

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Uart word-size and parity

Post by dhylands » Fri Oct 31, 2014 2:45 pm


Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Uart word-size and parity

Post by Damien » Fri Oct 31, 2014 8:52 pm

Thanks for pointing this out, I agree it's pretty unconventional behaviour.

It is now fixed in the latest version: bits specifies the actual number of data bits, and if you enable parity then it adds an extra bit. So your original initialisation will now work.

BTW, you may be interested in this code snippet which makes a USB-UART pass through: http://docs.micropython.org/en/latest/t ... rough.html.

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: Uart word-size and parity

Post by marfis » Sat Nov 01, 2014 7:05 pm

Thanks for the fast fix. Appreciate it!

I'm aiming to use the pyboard as a standalone programmer board for MSP430 devices. The SD card holds the firmware file(s) and pressing the button would start the download to the target device. Such a programmer would work on any OS. Or it might even be used without a PC at all.

And - the open source tools for the MSP430 are already written in python, so the port to uPy should be straightforward.

Your Code is interesting though as it might help to implement an alternative "passthrough" mode for the PC to the target board. But for that purpose, RTS/CTS line information must also be accessible. Not sure if this is possible in uPy...

Martin

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Uart word-size and parity

Post by Damien » Sat Nov 01, 2014 11:28 pm

But for that purpose, RTS/CTS line information must also be accessible. Not sure if this is possible in uPy...
In a recent patch (yesterday) I added experimental RTS/CTS support. There are 2 uarts on the pyboard that have these lines available. The way it's implemented is that you say you want flow control on initialisation of the uart object, and then it "just works". Eg:

Code: Select all

# UART(2), RTS/CTS on X2/X1
uart = pyb.UART(2, 9600, flow=pyb.UART.RTS | pyb.UART.CTS)
This enables RTS and CTS, and you need to hook up the X2 and X1 lines respectively. Flow control for writing is such that uart.write(...) will pause if the line is blocked. You can use select to wait for it to become available:

Code: Select all

import select
select.select([], [uart], []) # wait for uart to become writable
Is there any other functionality that you would need for RTS/CTS?

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: Uart word-size and parity

Post by marfis » Sun Nov 02, 2014 11:02 am

This enables RTS and CTS, and you need to hook up the X2 and X1 lines respectively
That sounds pretty good.
Is there any other functionality that you would need for RTS/CTS?
Unfortunately I messed it up - for msp430-python-tools to work "out of the box" RTS and DTR lines are used to drive the RST/TCK pins of the MSP. So my wish would be to have DTR line information on a pin as well... sorry.

Martin

BTW:
The prospect of having a full featured, python controled USB-serial converter incl. all control lines is great! The protocol data might be logged to SD card for later introspection - so we would have an integrated serial spy as well..

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Uart word-size and parity

Post by Damien » Sun Nov 02, 2014 12:56 pm

Code: Select all

So my wish would be to have DTR line information on a pin as well
Unfortunately the hardware (the STM32F405 MCU) does not support DTR/DSR. But, from my understanding, you can just use RTS/CTS mode and connect them to DTR/DSR... would need to know your exact situation to understand if this is possible or not.

Post Reply