Skip to main content
Marco Zulli
Associate II
May 7, 2018
Question

9-bit UART receive STM32F103

  • May 7, 2018
  • 7 replies
  • 5373 views
Posted on May 07, 2018 at 11:34

Hello all,

I am really new at all this, I hope someone has a simple example to help.

I am trying to receive 9-bit data using the STM32-F103 in normal polling mode example code below

uint16_t

messages[30];

HAL_UART_Receive(&huart2, (

uint8_t

*)messages,

sizeof

(messages),1000);

I thought a 16 bit register should be used to get all 9 bits, however looking at the data that is contained in the messages array, they are all over the place and I can't make sense of it and in most cases all 16 bits are populated when I'd expect only 9 bits populated in each element of the array.

Anybody got a simple solution for me?

Thank you all in advance

    This topic has been closed for replies.

    7 replies

    Tesla DeLorean
    Guru
    May 7, 2018
    Posted on May 07, 2018 at 14:50

    I wouldn't use sizeof() in this context, use 30 if you configured the USART in 9-bit.

    Mask the content if required.

    Provide example data, and minimally illustrative complete example if the problem persists.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Pavel A.
    May 7, 2018
    Posted on May 07, 2018 at 15:14

    I thought a 16 bit register should be used to get all 9 bits

    Yes it  is indeed. USART_DR[8:0] is 9 bits

    The question is, how you are going to store 9 bits. STM32 does not have bit-oriented memory like 8051. So it won't fit in one byte, and two bytes is a bit excessive.

    -- pa

    John Craven
    Senior
    May 7, 2018
    Posted on May 07, 2018 at 19:34

    If you using 9bit with no parity, you just read the receive register as uint16 and use the lower 9bits.

    Parity is included in the STM32 data bit length.

    If you need 9 data bits with parity, your going to need to use software serial.

    Not sure if there is an equivalent package for F1...

    But i was able to adapt this uart emulation package for F4, so i could work with a funky device that used 9-odd-2 serial.

    http://www.st.com/en/embedded-software/stsw-stm32156.html

     
    Andrei Chichak
    Lead
    May 7, 2018
    Posted on May 07, 2018 at 22:32

    Back in Uni, one of the networking guys gave a lecture on RS232 (way back). When asked about 2 stop bits, that is a holdover from teletypes that had a mechanical rotor to commutate the bits. 7 data bits, one parity bit, and 2 stop bits at 10 characters per second gave 110 baud. 2 stop bits wasn't used anywhere else, just for 110 baud on TTYs.

    A

    Tesla DeLorean
    Guru
    May 7, 2018
    Posted on May 07, 2018 at 23:27

    It increases the inter-symbol gap, which can be helpful for synchronization in systems where the USART implementation is super aggressive in the bit timing, and packing symbols tightly.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Marco Zulli
    Associate II
    May 8, 2018
    Posted on May 08, 2018 at 11:23

    Thanks for all the replies,

    this is what I have so far, the messages array is set to zero to eliminate any previous data, then the RX is called where I would expect the data in the RX buffer to be stored into the messages array, then buzz through a mask

    void

    UART_RXTX(

    void

    )

    {

    int

    i;

    int

    a;

    char

    message[]=

    'start\r\n'

    ;

    uint16_t

    messages[10];

    for

    (i=0;i<10;i++)

    {

    messages[i]=0;

    }

    HAL_UART_Receive(&huart2, (

    uint8_t

    *)messages, 10,1000);

    for

    (i=0;i<10;i++)

    {

    a=messages[i] & 0x01FF;

    messages[i]=a;

    }

    }

    in the uart.c file, it is configured as such'

    else if

    (uartHandle->

    Instance

    ==USART2)

    {

    /* USER CODE BEGIN USART2_MspInit 0 */

    /* USER CODE END USART2_MspInit 0 */

    /* USART2 clock enable */

    __HAL_RCC_USART2_CLK_ENABLE();

    /**USART2 GPIO Configuration

    PA2 ------> USART2_TX

    PA3 ------> USART2_RX

    */

    GPIO_InitStruct.

    Pin

    = GPIO_PIN_2;

    GPIO_InitStruct.

    Mode

    = GPIO_MODE_AF_PP;

    GPIO_InitStruct.

    Speed

    = GPIO_SPEED_FREQ_HIGH;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.

    Pin

    = GPIO_PIN_3;

    GPIO_InitStruct.

    Mode

    = GPIO_MODE_INPUT;

    GPIO_InitStruct.

    Pull

    = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    and

    else if

    (uartHandle->

    Instance

    ==USART2)

    {

    /* USER CODE BEGIN USART2_MspDeInit 0 */

    /* USER CODE END USART2_MspDeInit 0 */

    /* Peripheral clock disable */

    __HAL_RCC_USART2_CLK_DISABLE();

    /**USART2 GPIO Configuration

    PA2 ------> USART2_TX

    PA3 ------> USART2_RX

    */

    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

    So the final result is that there is nothing in the messages array, the connections were checked and all wiring is ok, however the messages array is always 0

    any one have any ideas on what it could be?

    Marco Zulli
    Associate II
    May 8, 2018
    Posted on May 08, 2018 at 11:50

    So I got it to work using UART1, same configuration (different ports obviously), why does it work with UART1 and not UART2?

    Tesla DeLorean
    Guru
    May 8, 2018
    Posted on May 08, 2018 at 15:25

    Let's not play these 'guess what's wrong' games. If you think it is a software side issue, ZIP up the minimal project, if you think it is the circuit, provide the relevant portion of the schematic.

    Check obvious things like clocks, GPIO settings/clocks, and on F1 pin remapping and peripheral clashing issues.

    USART1 and USART2 should use the same design, consider external connectivity.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Marco Zulli
    Associate II
    May 30, 2018
    Posted on May 30, 2018 at 03:00

    Thanks for all the support. What I didn't mention was that I'm running a nucleo, turns out that uart2 is configured to support the serial programming- resoldered the links and now works. Was in the nucleo instructions....