cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with USART on NUCLEO-F401RE

Stastny.Petr
Associate III
Posted on August 17, 2016 at 17:48

Hello,

I wrote my first project on Nucleo-F401RE with USART but not working. I do not see any problem there, can you help me please to find some mistake? Thank you very much

#include ''main.h''
static
void
COMInit(
void
);
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
int
main(
void
)
{
COMInit();
while
(1)
{
printf(
''\n\rUSART TEST\n\r''
);
}
}
void
COMInit(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
/* USART2 Config */
USART_InitStructure.USART_BaudRate = 9600;
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 configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART2, ENABLE);
}
PUTCHAR_PROTOTYPE
{
USART_SendData(USART2, (uint8_t) ch);
while
(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {}
return
ch;
}
#ifdef USE_FULL_ASSERT
void
assert_failed(uint8_t* file, uint32_t line)
{ 
while
(1)
{
}
}
#endif

1 REPLY 1
Posted on August 17, 2016 at 18:59

The clock is on APB1, not APB2

RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // !! APB1 APB1 I would recommend front testing on TXE, not back testing on TC, which is the SLOWEST

PUTCHAR_PROTOTYPE
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Is it empty?
USART_SendData(USART2, (uint8_t) ch); // Ok, write it now
return ch;
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..