2019-04-30 12:34 AM
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
2019-04-30 02:43 AM
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;"
2019-04-30 02:53 AM
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 ?
2019-04-30 06:46 AM
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
2019-04-30 08:03 AM
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.
2019-04-30 08:19 AM
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;
}
2019-04-30 08:23 AM
but i wouldn't use the IT version of transmit without checking first if transmission completed. So my proposal is to use the above code with HAL_UART_Transmit(..
2019-04-30 03:59 PM
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
2019-04-30 05:23 PM
2019-05-01 04:44 AM
How can I use it / relate it with printf();
for example if I want to printf to uart1 ??
Thanks