cancel
Showing results for 
Search instead for 
Did you mean: 

Can I have two different callbacks? (to use two buffers)

LMI2
Lead

I want use two or more buffers with IO functions. One way I could imagine was if I use two different callbacks. I would call HAL_UART_Receive_IT using buffer A which calls HAL_UART_RxCpltCallback A which calls HAL_UART_Receive_IT using buffer B which calls HAL_UART_RxCpltCallback B which calls A and so on.

5 REPLIES 5
T J
Lead

Just make a global buffers.

be careful not to adjust them in different threads.

"IO conditions" write buffers and set Flags in interrupts (or Polled)

a state machine process would check Flags and then check/process the buffer entries

The trick is:

only read or write a buffer from a thread.

The interrupt will write to the buffer and set a flag,

then the foreground state machine can check the flag and read/process the buffer

easy as.

LMI2
Lead

Yes, flags are probably the easiest way of doing it, unless ST or some one else show an other way. I have worked with callbacks recently so that was my first idea.

AvaTar
Lead

A callback is related to the interrupt. one interrupt, one callback. as suggested, do the differenciation in the callback. Like the Linux top-half/bottom-half approach.

The form used here is clumsy. You could use different handles/objects, or you could extend/overload the structure you pass so it has your own buffers, data pointers and function pointer.

ie you wrap/encapsulate the UART_HandleTypeDef structure inside one of your own, and then pass that

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
LMI2
Lead

Thank you for your ideas and suggestions. I looked at my code and what I'm trying to do with it and I think that the best way now is to change code in both ends of the UART and send data only when the receiver wants it. That makes buffering easier.