2018-02-16 01:30 PM
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 clockGPIO_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
Solved! Go to Solution.
2018-02-16 03:34 PM
Data Sheet, Table 14
2018-02-16 01:33 PM
For baud issues confirm HSE_VALUE define matches the hardware
USART_Cmd(USART2, ENABLE); // Enable UART
2018-02-16 02:01 PM
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,
2018-02-16 02:37 PM
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
2018-02-16 02:41 PM
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
2018-02-16 02:59 PM
{
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); // InitializeUSART_Cmd(USART2, ENABLE); // Enablewhile(1)
{ if (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != RESET) USART_SendData(USART2, 0x55);}}
2018-02-16 03:04 PM
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
2018-02-16 03:16 PM
Also needs mapping through the mux
/* Connect PA2 to USART2_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7); // USART2 MUX AF72018-02-16 03:27 PM
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
2018-02-16 03:34 PM
Data Sheet, Table 14