buserror / simavr

simavr is a lean, mean and hackable AVR simulator for linux & OSX
GNU General Public License v3.0
1.56k stars 365 forks source link

UART Parity bit support #13

Open computerlyrik opened 11 years ago

computerlyrik commented 11 years ago

I am trying to get 9N1 to work. [1]

Modifying the uart-loop-testcode to use the parity bit does not work.

On sending, TXB8 will be set.

Inspecting the gtkwave file, the parity bit will not be set during recieve from the loopback.

The project implementing the sending/receive can be found here: https://github.com/computerlyrik/haqbus-avr-lib

This is my first AVR-C project and under heavy development, so the code might be a little in chaos

[1] 9N1 is a mode using the parity bit as a 9th databit. Default mode is 8N1 where only UDR matters.

systemmind commented 5 years ago

Hi, @computerlyrik

I did not check whole functional of simavr, but I think you set wrong bit to reach your goal.

From the datasheet of atmega2561:

A frame starts with the start bit followed by the least significant data bit. Then the next data bits, up to a total of nine, are succeeding, ending with the most significant bit. If enabled, the parity bit is inserted after the data bits, before the stop bits.

The parity bit is used to check the parity of the frame to detect possible communication interference.

To enable parity bit generation you don't need set TXB8 bit. You need another one. This is what datasheet says:

The Parity Generator calculates the parity bit for the serial frame data. When parity bit is enabled (UPMn1 = 1), the transmitter control logic inserts the parity bit between the last data bit and the first stop bit of the frame that is sent.

So the UPMn1 is what you need.

But if your goal to send 9 data bits (parity bit may be enabled or disabled and it does not matter actually) you need enable 9 bit mode by settings UCSZn bit first and then set/clear TXB8 depending of your most significant (ninth) data bit value.

Was it helpful? Regards.

systemmind commented 5 years ago

@buserror, but I have some similar question. I want to make some integration tests of atmega2561 firmware where the 9 data bit usart communication is used. The chip sends 9 data bit by usart and it expects 9 data bit frame in answer.

Now I create 2 private interruption and connect them with uart input/output interruptions like uart_udp example does. But when chip's firmware sends 9 bit frame I am receiving 8 bit in my irq-handle.

Is there any way how I may detect that the frame contains 9 bit? And vice-verse, how I may emulate 9 bit frame sending in simavr?

Thanks.