cancel
Showing results for 
Search instead for 
Did you mean: 

About printf() out on UART5 of STM32F3DISCOVERY Board

Kye-Hyun Park
Associate II

I'm testing printf() out on USART1 and UART5 of STM32F3DISCOVERY Board.

USART1 tx (PC4) <-> RS232C converter

UART5 Tx (PC12) <-> RS232C converter

printf() out on USART1 is working normally. but, printf() out on UART5 is not working.

Could you please check my code?

Best regards,

[Code for USART1 Tx Configuration ]

// Retargets the C library printf function to the UART
#ifdef __GNUC__
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif // __GNUC__
 
PUTCHAR_PROTOTYPE
{
  // Place your implementation of fputc here
  // e.g. write a character to the USART
  USART_SendData(USART1, (uint8_t) ch);
 
  // Loop until transmit data register is empty
  while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}
 
  return ch;
}
 
void ConfigureUSART1(void)
{
    // Enable GPIO clock
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // Configure USART1 Tx as alternate function push-pull
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    
    // Connect PC4 to USART1 Tx
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_7);
 
    // Configure USART
    USART_InitTypeDef USART_InitStructure;
 
    USART_InitStructure.USART_BaudRate = 19200;
    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_Tx;
    
    // USART configuration
    USART_Init(USART1, &USART_InitStructure);
        
    // Enable USART
    USART_Cmd(USART1, ENABLE);
}

[Code for UART5 Tx Configuration]

// Retargets the C library printf function to the UART
#ifdef __GNUC__
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif // __GNUC__
 
PUTCHAR_PROTOTYPE
{
  // Place your implementation of fputc here
  // e.g. write a character to the USART
  USART_SendData(UART5, (uint8_t) ch);
 
  // Loop until transmit data register is empty
  while (USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET){}
 
  return ch;
}
 
void Configure(void)
{
    // Enable GPIO clock
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // Configure USART5 Tx as alternate function push-pull
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    
    // Connect PC12 to USART5 Tx
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_7);
 
    // Configure UART
    USART_InitTypeDef USART_InitStructure;
 
    USART_InitStructure.USART_BaudRate = 19200;
    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_Tx;
    
    // UART configuration
    USART_Init(UART5, &USART_InitStructure);
        
    // Enable UART
    USART_Cmd(UART5, ENABLE);
}

1 ACCEPTED SOLUTION

Accepted Solutions

GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_5); // PC12 UART5_TX AF5

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

2 REPLIES 2

GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_5); // PC12 UART5_TX AF5

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

I did a mistake for configuration of AF. I just confirmed that printf out on UART5 is working. ​

Thank you for your reply, Clive Two.Zero