AnsweredAssumed Answered

USART with data parity

Question asked by mbavaro on May 6, 2012
Latest reply on May 11, 2012 by siqueira.erisson
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

Outcomes