Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

using printf for two usart in stm32

Status
Not open for further replies.

tariq70

New Member
hi
I'm using stm32 by keil5 compiler and This is my printf function

C:
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
    {
    HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,100);
        
        return ch;
        
    }

in this code my printf only sends data in uart1. is there any function to send data by printf and choosing uart.
for example my printf become like this :

printf(&huart2,"HELLO WORLD"%d,variable)?

Thankyou.
 
If you read the C compiler documentation ( I am unsure here as I use Microchip) you provide the putch() and getch() to source the input / output to suit.. You could simple swap the default before sprint() is called...

In your example.. &huart1 is a handle so make anther to handle port 2
Do not use the handle in the sprint() statement printf and sprint are looking for a character array and not a destination output

C:
PUTCHAR_PROTOTYPE 
      { 
      if(SERIAL == 1)  HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,100);   
      if(SERIAL == 2)  HAL_UART_Transmit(&huart2,(uint8_t*)&ch,1,100);          
      return ch;         
      }


Even this doesn't look right... I would rewrite their __io_putchar() function..
 
I tend not to use printf, as it loads a huge library, but I use a very similar system to select either serial port - or sometimes one of three.

C:
void Serial_Write(char port, unsigned char value)
{
    if (port==1)
    {
        EUSART1_Write(value);
    }   
    else if (port==2)
    {
        EUSART2_Write(value);
    }     
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top