Question
Help with UART2 output on new STM32nucleo board
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