cancel
Showing results for 
Search instead for 
Did you mean: 

Mark and Space parity on UART?

byrned
Associate II
Posted on November 07, 2013 at 00:26

Does anyone know if you can set Mark or Space parity on the STM32 USARTS?

I only found that you can set Even or Odd parity so far. I need to configure an STM32 for hardware that uses Mark and Space parity bits so any help would be appreciated. 

Thanks,

Devyn

#uart-mark-space-parity
8 REPLIES 8
Posted on November 07, 2013 at 00:59

Does anyone know if you can set Mark or Space parity on the STM32 USARTS? I only found that you can set Even or Odd parity so far. I need to configure an STM32 for hardware that uses Mark and Space parity bits so any help would be appreciated.

Set it to 9-bit mode, no parity, and send whatever data patterns you want.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
byrned
Associate II
Posted on November 07, 2013 at 21:15

Thanks for replying, Clive, but how would I send a 9 bit character through the UART? Maybe I'm missing something that's really easy, but I can't think of it.

What I need is to send a message like this:

Hex, Binary, Decimal equivalents:

0x115 0b100010101 277 (this is really 21 with a mark parity set)

0x002 0b000000010 2

0x000 0b000000000 0

0x03b 0b000111011 59

0x000 0b000000000 0

0x000 0b000000000 0

0x000 0b000000000 0

0x052 0b001010010 82

so you see, first byte has mark parity(MSB) while all others are space. Just before sending the first byte, I tried to set the UART to 8N2 which should be the equivalent of a mark parity, then set it back to 9N1 before sending the rest of the bytes, but it doesn't seem to change anything. It still sends without making the MSB a 1 for the first byte. Here is my code:

uint8_t Message[0] = 21;
uint8_t Message[1] = 2;
uint8_t Message[2] = 0;
uint8_t Message[3] = 59; 
uint8_t Message[4] = 0;
uint8_t Message[5] = 0;
uint8_t Message[6] = 0;
uint8_t Message[7] = 82
//set USART3 to 8N2 before sending first byte
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_2;
USART_Init(USART3, &USART_InitStructure); 
//send first byte 
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) ==RESET); 
USART_SendData(USART3, Message[0]);
//set USART3 back to 9N1 
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_Init(USART3, &USART_InitStructure); 
for (i = 1; i < 8;i++) 
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); 
USART_SendData(USART3, From_MATE_Message[i]);
}

Thanks, Devyn
Posted on November 07, 2013 at 21:29

You'd want to wait on TC before shifting gears.

Or frankly just use 9N1 with

USART_SendData(USART1, 0x100 | FirstByte);

and

USART_SendData(USART1, SubsequentBytes & 0xFF);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
byrned
Associate II
Posted on November 07, 2013 at 21:43

Clive,

Just tried what you said. Works great! Thank you so much for your help! I thought that I had actually tried that before, but here's what I did wrong:

Message[0] |= 0x100; 
USART_SendData(USART3, Message[0]);

I tried ORing then setting it back into the 8 bit character which didn't do anything, because it only allows 8 bits, but if you do the calculation in the function it will send that 9th bit along with the 8 bit character. Anyways, thanks Clive! Was working on this issue for a while trying to figure it out. Devyn
STerb
Associate

With STM32CubeMX, under UART configuration. Why is there no mark parity option available.

Has there been any other workarounds other than above. Above method works though, but is rather troublesome.

> Has there been any other workarounds other than above.

Using an external UART.

JW

>>Why is there no mark parity option available.

Because the hardware doesn't commit gates to implementing niche functionality.

Use the 9-bit mode and send/receive/check whatever patterns you want.

Why is this troublesome/complex? You're not having to implement a soft-uart.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

... as if soft-UART would be troublesome or complex... ;)

JW