cancel
Showing results for 
Search instead for 
Did you mean: 

usart hangs when nothing connected

michaelmccartyeng
Associate II
Posted on April 14, 2013 at 00:00

If I dont have anything on the usart port then it hangs when sending data. I have a ''debug'' usart output that I only want to use sometimes. But if I dont have anything connected then it will sit and hang here 

PUTCHAR_PROTOTYPE

{

  /* Place your implementation of fputc here */

  /* e.g. write a character to the USART */

  USART_SendData(Debug_USART, (uint8_t) ch);

  /* Loop until the end of transmission */

  while (USART_GetFlagStatus(Debug_USART, USART_FLAG_TC) == RESET)

  {}

  return ch;

}

which makes sense because its trying to send data but is never able to. 

Guess the best way around this is to have a switch to turn debug on or off, but it would be bad if a user hits that switch and then their unit never starts. 
2 REPLIES 2
Posted on April 14, 2013 at 01:57

Not sure what having nothing attached would change, unless you have hardware flow control enabled or something. Also why wait on TC? Back testing on TC is one of the least efficient ways of waiting for the USART, it would be better to front test on TXE.

Some RS232 level converters have an INVALID signal that can be used to detect cable attachment.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
michaelmccartyeng
Associate II
Posted on April 19, 2013 at 22:31

Thanks Clive. 

I had just copy pasted that out of some example somewhere without even thinking about it. Does that make me a script kiddie ? 

Anyway I changed it and apart from being more efficient it does not hang there anymore. I think the TC flag was holding up something. Now I can disconnect it and it will still work no problem. 

PUTCHAR_PROTOTYPE

{

  /* Loop until the end of transmission */

  while (USART_GetFlagStatus(Debug_USART, USART_FLAG_TXE) == RESET)

  {}

    /* Place your implementation of fputc here */

  /* e.g. write a character to the USART */

  USART_SendData(Debug_USART, (uint8_t) ch);

  return ch;

}