Serial with parity

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

Serial with parity

Post by lilvinz »

Hi,

what is the right way to handle serial data with parity e.g. 7E1?
At least the STM32 UART peripherals don't mask out the parity bit of a received character
which means that i see "garbage" at the application level working on the BaseSequentialStream device.
When i do this on linux, the application doesn't see the parity bits. They are either not provided
by the hardware uart or masked out at the driver level.

A possible solution could look like this

Code: Select all

static void serve_interrupt(SerialDriver *sdp) {
  <snip>
  /* Data available.*/
  chSysLockFromIsr();
  while (sr & USART_SR_RXNE) {
    /* Error condition detection.*/
    if (sr & (USART_SR_ORE | USART_SR_NE | USART_SR_FE  | USART_SR_PE))
      set_error(sdp, sr);
    uint16_t c = u->DR;
    /* Mask parity bit according to configuration. */
    switch (u->CR1 & (USART_CR1_PCE | USART_CR1_M))
    {
    case USART_CR1_PCE:
        c &= 0x7f;
        break;
    case USART_CR1_PCE | USART_CR1_M:
        c &= 0xff;
        break;
    default:
        break;
    }
    sdIncomingDataI(sdp, c);
    sr = u->SR;
  }
  chSysUnlockFromIsr();
  <snip>
}


Thanks and cheers,

Vinz
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Serial with parity

Post by Giovanni »

Hi,

Good point, after thinking at it for a while my conclusion is that it is better to not break compatibility, somebody could be relying on the behavior.

You may create a subclass of SerialDriver, naming it for example FilteredSerialDriver, and perform the filtering on the high level instead than the ISR. A filtering stream could be an idea too.

Giovanni
User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

Re: Serial with parity

Post by lilvinz »

Some further thoughts:
Adding a higher level driver or stream filter leads to redundant configuration e.g.
the lower level driver has to configure the uart CRs but the higher level driver needs to
know the current frame size and parity settings as well.
Doesn't really look clean to me.
Breakage of compatibility could easily be avoided by adding a preprocessor option.
Also what possible scenario do you have in mind where the parity bit would be required by application
and why then is the behavior on e.g. linux to mask the parity bit?
Remember the uart already checks the parity of incoming data and raises an error if its invalid.

Cheers,

Vinz
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Serial with parity

Post by Giovanni »

For example, a while ago I designed a protocol over rs485 where parity errors were used for both detecting errors and detecting start of packets.

Giovanni
Post Reply