Skip to main content
Randy Nelson
Associate III
Feb 16, 2018 9:30 PM UTC
Solved

Need help using USUART2 on STM32F303

Posted on February 16, 2018 at 22:30

I'm trying to find instruction to code the usuart2 on this STM part. I using cmsis and the StdPeripheral lib. My end goal is to connect this to a virtual com port for debug. I'm using the Keil development environment.

I using the default HSI clock setup running at 8MHZ then -

RCC->AHBENR |= BIT17; //Enable portA clock

RCC->AHBENR |= BIT18; //Enable portB clock

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_Init(GPIOA, &GPIO_InitStructure);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

USART_InitTypeDef USART_InitStructure;

USART_InitStructure.USART_BaudRate = 115200;

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);

//send the data

USART_SendData(USART2, 0xaa);

I realize the baud may be wrong but right now I'm trying to see something on the PA2 pin 

with a scope. Nothing is changing.

I tried to make sure the tx register was empty before writing the data using:

if( ((USART2->ISR) & BIT7)) != 0) {

  USART_SendData(USART2, 0xaa);

}

Still nothing coming out.

Any suggestions. 

Thanks,

Randy

    This topic has been closed for replies.
    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    Feb 16, 2018 11:34 PM UTC
    Posted on February 16, 2018 at 23:34

    Data Sheet, Table 14

    http://www.st.com/content/ccc/resource/technical/document/datasheet/f2/1f/e1/41/ef/59/4d/50/DM00058181.pdf/files/DM00058181.pdf/jcr:content/translations/en.DM00058181.pdf

     0690X00000604J2QAI.jpg
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..

    13 replies

    Tesla DeLorean
    Guru
    Feb 16, 2018 9:33 PM UTC
    Posted on February 16, 2018 at 22:33

    For baud issues confirm HSE_VALUE define matches the hardware

    USART_Cmd(USART2, ENABLE);                    // Enable UART

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Randy Nelson
    Associate III
    Feb 16, 2018 10:37 PM UTC
    Posted on February 16, 2018 at 22:37

    Clive,

    I understand '

    For baud issues confirm HSE_VALUE define matches the hardware'

    but I do not understand it in reference to '

    USART_Cmd(USART2, ENABLE);'

    The USUART cmd simply sets the 'UE' bit in the register.

    Randy

    Randy Nelson
    Associate III
    Feb 16, 2018 10:41 PM UTC
    Posted on February 16, 2018 at 22:41

    My apologies. You are correct about the 

    USART_Cmd(USART2, ENABLE); cmd.

    I have it in my code but forgot to copy/paste it to my text. Sorry.

    Randy

    Randy Nelson
    Associate III
    Feb 16, 2018 10:01 PM UTC
    Posted on February 16, 2018 at 23:01

    I was able to verify the USUART is taking the time to send a byte. It looks like the problem may be getting the USUART's TX out through the GPIO PA2 pin.

    I feel like my pin setup flow is correct because I added a digital output test to toggle during transmit time and it works.This is how I verified the time for the 'send' matches the baud rate. 

    So to connect PA2 to USART2TX,

    Randy Nelson
    Associate III
    Feb 16, 2018 11:04 PM UTC
    Posted on February 16, 2018 at 23:04

    Sent to early ...

    PA2 according the manuals is connected to UART2TX. 

    Is this all I need to make sure the GPIO pin is connected to the UART2TX?

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    Randy

    Tesla DeLorean
    Guru
    Feb 16, 2018 11:16 PM UTC
    Posted on February 16, 2018 at 23:16

    Also needs mapping through the mux

    /* Connect PA2 to USART2_Tx */

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7); // USART2 MUX AF7
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Andrew Neil
    Super User
    Feb 17, 2018 11:58 AM UTC
    Posted on February 17, 2018 at 12:58

    I’ve been using a another doc

    So tell us what 'doc' that is!

    Note that the complete documentation for the part is spread across two documents:

    1. The Datasheet: focuses on specifications - mechanical, electrical, etc.
    2. The User Manual

      : focuses on peripheral operation, registers, etc.

    You need both.

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Randy Nelson
    Associate III
    Feb 20, 2018 8:06 PM UTC
    Posted on February 20, 2018 at 20:06

    Andrew,

    I was referencing both the reference manual, RM3016, the programming manual, PM0214,  and the STM32F303x6/x8 doc. I DID miss the RM316 section 11.3.2 dealing with the AF register. Reading this section and using Clives table would have help me resolve my own problem.Thanks for everyone's help.

    Randy