cancel
Showing results for 
Search instead for 
Did you mean: 

Need help using USUART2 on STM32F303

Randy Nelson
Associate III
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

1 ACCEPTED SOLUTION

Accepted Solutions
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 Up vote any posts that you find helpful, it shows what's working..

View solution in original post

13 REPLIES 13
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 Up vote any posts that you find helpful, it shows what's working..
Randy Nelson
Associate III
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,

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

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

Posted on February 16, 2018 at 22:59

{

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

/* Enable GPIO clock */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

/* Enable USART clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Connect PA2 to USART2_Tx */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7); // USART2 MUX AF7

/* Connect PA3to USART2_Rx */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);

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

USART_Cmd(USART2, ENABLE); // Enable

while(1)

{

  if (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != RESET)

    USART_SendData(USART2, 0x55);

}

}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
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

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 Up vote any posts that you find helpful, it shows what's working..
Posted on February 16, 2018 at 23:27

It now works. 

I have often wondered how the 'AF' devices connect to the pins. Can you point to a document that clearly explains this? The RM0316 on the STM32F303 MCU does not cover it much.

Thanks Clive,

Randy

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 Up vote any posts that you find helpful, it shows what's working..