cancel
Showing results for 
Search instead for 
Did you mean: 

Help with UART2 output on new STM32nucleo board

toddberk
Associate II
Posted on March 14, 2014 at 04:56

I just got a STM32F103 nucleo board and am having issues getting UART2 enabled in my code to communicate with the PC through the ST-Link virtual port. UART2 is conncected to the ST-Link uart, which gets passed through the USB virtual serial port.

I can get the port to work using code from mbed.org (nucleo_printf_button example) and see the output on Realterm. However on my own application using CooCox I cannot get UART2 to work. Tracing it with the debug functions the program is hanging in the USART_GetFlagStatus() function. Here is my code:

/**
*
* @brief Serial Test Task
*
* @param pvParameters Not used
*
*/
static void prvSerialTestTask( void *pvParameters )
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART2 Tx (PA.2) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.3) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable USART1 and GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 57600;
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);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
for( ;; )
{
/* Simple toggle the LED periodically. This just provides some timing
verification. */
vTaskDelay( mainSERIALTEST_DELAY );
/* Send Serial String*/
const unsigned char menu[] = '' Welcome to CooCox!\r\n'';
/* print welcome information */
UARTSend(menu, sizeof(menu));
}
}
/*******************************************************************************
* Function Name : UARTSend
* Description : Send a string to the UART.
* Input : - pucBuffer: buffers to be printed.
* : - ulCount : buffer's length
* Output : None
* Return : None
*******************************************************************************/
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
//
// Loop while there are more characters to send.
//
while(ulCount--)
{
USART_SendData(USART2, *pucBuffer++);// Last Version USART_SendData(USART1,(uint16_t) *pucBuffer++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{
}
}
}

#solder-bridge #nucleo #usart2
3 REPLIES 3
Posted on March 14, 2014 at 06:46

USART2 is on APB1, you must enable the clock on the right bus.

You also should front test for TXE rather than back test for TC
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
toddberk
Associate II
Posted on March 14, 2014 at 18:48

Thank you! You caught it, I was trying to send an APB1 clock constant to the APB2 function. I've been away from embedded systems a couple years and have gotten rusty. I also switched TC to TXE. Here's the code that works for anyone else's reference trying to send serial thru USB on STM32 nucleo board:

/**
*
* @brief Serial Test Task
*
* @param pvParameters Not used
*
*/
static void prvSerialTestTask( void *pvParameters )
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART2 Tx (PA.2) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.3) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable USART2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* USART2 configuration ------------------------------------------------------*/
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);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
for( ;; )
{
/* Simple toggle the LED periodically. This just provides some timing
verification. */
vTaskDelay( mainSERIALTEST_DELAY );
/* Send Serial String*/
const unsigned char menu[] = ''Welcome to CooCox!

'';
/* print welcome information */
UARTSend(menu, sizeof(menu));
}
}
/*******************************************************************************
* Function Name : UARTSend
* Description : Send a string to the UART.
* Input : - pucBuffer: buffers to be printed.
* : - ulCount : buffer's length
* Output : None
* Return : None
*******************************************************************************/
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
//
// Loop while there are more characters to send.
//
ulCount--;
while(ulCount--)
{
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
{
}
USART_SendData(USART2, *pucBuffer++);
}
}

thomasmueller
Associate
Posted on August 20, 2015 at 22:39

It's a bit off topic but I want to point out that if you want to use the USART2 either on the Morpho or the arduino headers the solder bridge SB62 and SB63 must be closed.

Thanks for sharing your code.

Best regards

Thomas