cancel
Showing results for 
Search instead for 
Did you mean: 

Using two UART

hospodar
Associate III
Posted on October 21, 2015 at 20:31

Hello,

I using STM periph. library and I would need to use two UARTs on STM32F103. One UART for serial Modbus RTU comunication with sensors and one UART for terminal interface which will show data from sensors and provide a possibility to set up some parameters like Slave ID, function code, etc. 

Now, I have implemented one UART with Rx and Tx FIFO buffers and I'm using printf function.

How is it possible to implement second UART and using printf (or printf-like) function also for that?

Thank you.

Ivan
3 REPLIES 3
Posted on October 21, 2015 at 21:04

Well you could use sprintf(), and for tools like Keil you can use things like fprintf(), and manage the file handle to direct to a specific USART.

GNU/GCC newlib would also allow for a read/write with a file handle.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on October 25, 2015 at 12:56

Where or how is possible to redirect fprintf to UART? 

For redirecting of classic printf function I have been modified fputc and fgetc function in retarget.c module. Is it possible to do this in the same way with fprintf?

How should be handle first argument FILE *stream in fprintf function?

  

Posted on October 25, 2015 at 23:15

//****************************************************************************
/* Implementation of putchar (also used by printf function to output data) */
int SendChar(int ch) /* Write character to Serial Wire */
{
ITM_SendChar(ch); // From core_cm4.c
return(ch);
}
//****************************************************************************
int USART_SendChar(USART_TypeDef* USARTx, int ch) /* Write character to Serial Port */
{
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, ch);
return(ch);
}
//****************************************************************************
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
FILE _usart1;
FILE _usart3;
#define US1 (&_usart1)
#define US3 (&_usart3)
// You could put stuff into the FILE structure, but you need to sanity check it
int fputc(int ch, FILE *f)
{
if (f == US1)
return(USART_SendChar(USART1, ch)); 
else if (f == US3)
return(USART_SendChar(USART3, ch));
else 
return(SendChar(ch)); // Default
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
if (f == US1)
USART_SendChar(USART1, ch); 
else if (f == US3)
USART_SendChar(USART3, ch);
else 
SendChar(ch); // Default
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
//****************************************************************************
fprint(US1, ''Too USART1

'');
fprint(US3, ''Too USART3

'');

Use different handles for different serial ports.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..