cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Transmit_IT not working as expected

paulsmitton9157
Associate II
Posted on November 09, 2015 at 11:00

Hello

I try to debug my application using printf I write some code to overwrite fputc. My problem is with the function HAL_UART_Transmit_IT of the HAL module It doesn't succeed to send long sentences. My code is only sending data when a complete sentence is detected (\r\n) I add some buffer management in case the writing will be faster than the sending but it's not the case here.... If I run the interruption step by step the full sentence is send. query: USER gencf¦query: PASS pae query: SYST qury: FEAT query: PWD query: CWD / query: PWD query: TYPE A query: PASV query: TYPE ýÿÿÿquery: PWD The first line is not complete. Can you help me to understand ? My code:

static char Idx = 0;
static char Transmitting = 0;
static char *pBuffer;
int fputc (int aCh, FILE *stream)
{
char Jdx ;
char aChar ;
char Buffer[TX_BUFFER_SIZE];
char temp = aCh;
char send = 0;
UNUSED(aChar);
UNUSED(temp);
if (stream == &__stdout) {
if (Idx == 0) {
pBuffer = Buffer;
send = 0;
}
*pBuffer++ = aCh;
if (Idx++ >= TX_BUFFER_SIZE) {
send = 1;
}
if (aCh == '\n') {
send = 1;
}
if (send == 1) {
if ( (HAL_UART_GetState (&huart3) & 0x10) || (Transmitting == 1)) {
pBuffer = Buffer;
RingBuffer_Put (DBG_SEND_RINGBUFFER, Idx);
for (Jdx = 0; Jdx < 
Idx
; Jdx++) {
RingBuffer_Put (DBG_SEND_RINGBUFFER, *pBuffer++);
}
} else {
Transmitting
= 
1
;
HAL_UART_Transmit_IT (&huart3, (uint8_t *)Buffer, Idx);
}
Idx
= 
0
;
}
}
return (aCh);
}
void HAL_UART_TxCpltCallback (UART_HandleTypeDef *huart)
{
uint8_t 
Jdx
= 
0
;
uint8_t aChar ;
static uint8_t 
Idx
= 
0
;
static char *pBuffer;
char Buffer[TX_BUFFER_SIZE];
if (RingBuffer_Used (DBG_SEND_RINGBUFFER) > 0) {
pBuffer = Buffer;
RingBuffer_Get (DBG_SEND_RINGBUFFER, &Idx);
for (Jdx = 0; Jdx < Idx; Jdx++) {
RingBuffer_Get (DBG_SEND_RINGBUFFER, &aChar);
*pBuffer++ = aChar;
}
HAL_UART_Transmit_IT (&huart3, (uint8_t *) Buffer, Idx);
}
Transmitting = 0;
}

2 REPLIES 2
Posted on November 09, 2015 at 14:07

The buffer in the interrupt/callback is local, and will collapse when it exits. Consider using a static buffer if you expect it to live longer than the routine.

The static variables inside/outside the routines are not the same or visible to each other.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
paulsmitton9157
Associate II
Posted on November 09, 2015 at 15:23

Hi clive1,

Thanks a lot for your help : it was so easy.

I am ashamed

not to have

seen this

faulty code.

I take this opportunity

to

ask another question

on this

same

code :

I had to call HAL_UART_Transmit_IT once to initiate the communication, and another time in case there is something in the ringbuffer. (that means two static buffers)..

Is there a way to rise a dummy HAL_UART_TxCpltCallback so I could always write in my ringbuffer ans start the communication rising this HAL_UART_TxCpltCallback event ....looking in the RM0090 it looks like the TC bit could only be reset..

I could then have only one place calling HAL_UART_Transmit_IT...