Skip to main content
antonius
Associate III
Apr 30, 2019 7:34 AM UTC
Question

PUTCHAR_PROTOTYPE question ?

Dear Members,

Is it possible that I use two UART with  PUTCHAR_PROTOTYPE ?

I have used UART3 with  PUTCHAR_PROTOTYPE for talking with PC,

I want to use it as well for talking with modem (UART1)

Thanks

This topic has been closed for replies.

16 replies

Vangelis Fortounas
Associate II
Apr 30, 2019 9:43 AM UTC

Hi.

In run time is possible to do this.

define a global variable "target" visible also from PUTCHAR_PROTOTYPE's module.

Inside PUTCHAR_PROTOTYPE's definition, decide where to target the character by evaluating the "target" variable.

Just before call any printf function set the "target" variable to the proper target like "target=enum_UART3;"

antonius
antoniusAuthor
Associate III
Apr 30, 2019 9:53 AM UTC
int target 
 
#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)
 
#else
 
 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
 
#endif /* __GNUC__ */
 
 
/**
 
 * @brief Retargets the C library printf function to the USART.
 
 * @param None
 
 * @retval None
 
 */
 
PUTCHAR_PROTOTYPE 
 
{
 
 /* Place your implementation of fputc here */
 
 /* e.g. write a character to the USART */
 
 // HAL_UART_Transmit_IT(&huart3, (uint8_t *)&ch, 1);
 
 HAL_UART_Transmit_IT(target, (uint8_t *)&ch, 1);
 
 return ch;
 
}

Like that ?

Vangelis Fortounas
Associate II
Apr 30, 2019 3:19 PM UTC

No i didn't meant that.

PUTCHAR_PROTOTYPE 
{
 
switch (target)
{
	case 0:
 HAL_UART_Transmit_IT(&huart1, (uint8_t *)&ch, 1);
		break;
	case 1:
 HAL_UART_Transmit_IT(&huart3, (uint8_t *)&ch, 1);
		break;
	default:
 //do nothing
		break;
}
 return ch; 
}

antonius
antoniusAuthor
Associate III
Apr 30, 2019 1:46 PM UTC

Experiment :

void usart_transmit( BYTE data )
 
{
	HAL_UART_Transmit_IT(&huart1, (uint8_t *)&data, 1);
}
 
 
 
void usart_pstr(unsigned char *s) {
//void usart_pstr(BYTE *s) {
	// loop through entire string
 
	while (*s) {
		usart_transmit(*s);
		s++;
	}
}
 
 
void test_modem (void)
{ 
	printf("TEST SIM900\r\n");
	usart_pstr("AT\r\n");
 
	
}
 
...
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart)
{ char RxModem[8];
	if(huart->Instance==USART2)
		{
			GPS_CallBack();
		}	
	if(huart->Instance==USART1)
		{
			HAL_UART_Receive_IT(&huart1, (uint8_t *)&RxModem, 5);
			printf(RxModem);
 
		}
		 
}

correct me ? thanks

Tesla DeLorean
Guru
Apr 30, 2019 3:03 PM UTC

Expecting 5 characters is probably inadvisable in situations where the responses can be variable in size and sporadic. My general recommendation is to process USART data to a buffer in the IRQHandler where each byte gets an interrupt, and then processing the buffer in a separate task/thread, where it can take more than a byte time.

Also the HAL Callback here needs to complete in less than a byte time as it is executing in interrupt context and blocking here will cause data loss.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
antonius
antoniusAuthor
Associate III
Apr 30, 2019 10:59 PM UTC

Yes that's what I've been thingking, I loss a lot of data on the callback,

so I need to make another function to parse those bytes,

Is my transmitting way correct ? usart_pstr() ?

Any function examples or links ?

Thanks