cancel
Showing results for 
Search instead for 
Did you mean: 

USART with data parity

michele77
Associate II
Posted on May 06, 2012 at 19:45

Hi all,

I just wanted to share the surprise that I had today when, in order to have the USB CDC example virtualizing USART2 at 115200,8O1 on a STM32F102RB clocked with a 16MHz oscillator I had to configure like this:

USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Odd;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

And disable the USART_Config() function because it would break things when called by the application on the PC.

bool
USART_Config(
void
)
{
USART_InitTypeDef USART_InitStructure;
/* set the Stop bit*/
switch
(linecoding.format) {
case
0:
USART_InitStructure.USART_StopBits = USART_StopBits_1;
break
;
case
1:
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
break
;
case
2:
USART_InitStructure.USART_StopBits = USART_StopBits_2;
break
;
default
:
// USART_Config_Default();
return
(FALSE);
}
/* set the parity bit*/
switch
(linecoding.paritytype) {
case
0:
USART_InitStructure.USART_Parity = USART_Parity_No;
break
;
case
1:
USART_InitStructure.USART_Parity = USART_Parity_Even;
break
;
case
2:
USART_InitStructure.USART_Parity = USART_Parity_Odd;
break
;
default
:
return
(FALSE);
}
/*set the data type : only 8bits and 9bits is supported */
switch
(linecoding.datatype) {
case
0x07:
/* With this configuration a parity (Even or Odd) should be set */
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
break
;
case
0x08:
if
(USART_InitStructure.USART_Parity == USART_Parity_No) {
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
} 
else
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
}
break
;
default
:
return
(FALSE);
}
USART_InitStructure.USART_BaudRate = linecoding.bitrate;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure and enable the USART */
if
(eDevOnUSB==GPSRX1) {
USART_Init(GPS1UART_PORT, &USART_InitStructure);
USART_Cmd(GPS1UART_PORT, ENABLE);
} 
else
if
(eDevOnUSB==GPSRX2) {
// USART_Init(GPS2UART_PORT, &USART_InitStructure);
// USART_Cmd(GPS2UART_PORT, ENABLE);
} 
else
if
(eDevOnUSB==RS422) {
USART_Init(RS422_PORT, &USART_InitStructure);
USART_Cmd(RS422_PORT, ENABLE);
}
return
(TRUE);
}

I am wondering if anyone has also noticed that using USART_Parity_Odd you need to use USART_WordLength_9b for a normal 801 communication? Thanks in advance for your kind answers. Best regards, Michele #uart/usart-parity-and-wordlengt
3 REPLIES 3
alok472
Associate II
Posted on May 08, 2012 at 03:57

DId you try with 8b Wordlength ? I think it shall work without problem.

For your configuration, did you check the signals on Oscilloscope ?

Chris1
Senior III
Posted on May 08, 2012 at 23:13

If you want 8 data bits and a parity bit, you must configure the USART for 9-bit word length.

 

That may seem ''odd'', but it is how ST implemented the USARTs; their word length is the total number of bits between the Start and Stop bits...

This can be seen in the diagrams in the USART section of the Reference Manual (RM0008).

-Chris

esiqueira
Associate II
Posted on May 11, 2012 at 16:25

I agree with Chris,,,,

    Examples to help.....

 

 /*USART3 Structure Config*/

  USART_InitStructure.USART_BaudRate = 9600;

  USART_InitStructure.USART_WordLength = USART_WordLength_9b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_Even;      

// Even or Odd

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_Init(USART3, &USART_InitStructure);

    

  

/*USART2 Structure Config*/

  USART_InitStructure.USART_BaudRate = 9600;

  USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_No;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_Init(USART2, &USART_InitStructure);