Page 1 of 1

Change CAN driver to use callbacks

Posted: Mon Jan 01, 2018 4:28 am
by faisal
There was a thread a long time ago to modify the CAN driver to use callbacks: viewtopic.php?f=3&t=282 . Any plan to modify HAL CAN?

I think the current system of broadcasting events should be replaced with ISR callbacks. This way, the driver could be used to 'push' the CAN frames into a queue in the ISR context so that they are never lost. As it is right now, the CAN RX processing thread would have to have enough priority to never miss a CAN frame being received - and may actually miss frames when a burst of them come thru (if it's priority isn't high enough).

Re: Change CAN driver to use callbacks

Posted: Mon Jan 01, 2018 8:49 am
by Giovanni
Hi,

Disable events in chconf.h and OSAL will use callbacks instead.

Giovanni

Re: Change CAN driver to use callbacks

Posted: Mon Jan 01, 2018 4:59 pm
by faisal
Giovanni wrote:Hi,

Disable events in chconf.h and OSAL will use callbacks instead.

Giovanni


But then we can't use events elsewhere in the project ... Changing HAL CAN to be more like other drivers that use call backs, apart from better design and being more flexible, would also make the HAL interfaces more consistent.

Re: Change CAN driver to use callbacks

Posted: Tue Jan 02, 2018 12:03 am
by steved
I suggest adding callbacks as an option, in the same way as I've done for the Serial Driver.
Basically, if a callback field (in the configuration structure) is non-null, it's used; otherwise an event is signalled. Certainly works well for serial.

I'll be playing with CAN in the coming weeks, and certainly got some mods to the driver; might look at this one at the same time.

Re: Change CAN driver to use callbacks

Posted: Tue Jan 02, 2018 12:22 pm
by Giovanni
Hi,

I added experimental support for callbacks, it compiles but I haven't tested it.

Giovanni

Re: Change CAN driver to use callbacks

Posted: Tue Jan 09, 2018 11:52 am
by Giovanni
Any feedback about the changes?

Giovanni

Re: Change CAN driver to use callbacks

Posted: Tue Jan 09, 2018 2:15 pm
by steved
Giovanni wrote:Any feedback about the changes?

Giovanni

Not yet from me - having fun (!) with QSPI ATM

Re: Change CAN driver to use callbacks

Posted: Tue Jan 09, 2018 4:58 pm
by faisal
Giovanni wrote:Any feedback about the changes?

Giovanni


Yes, thank you :) ! Just a small feedback: CAN_ENFORCE_USE_CALLBACKS should be checked by the preprocessor if it defined as TRUE or FALSE. From what I've seen this is more consistent with other drivers in HAL. In the header file, you could have a default definition if it is not defined.

I have not tested the changes yet, will probably do so in the coming week.

Re: Change CAN driver to use callbacks

Posted: Sun Mar 18, 2018 6:37 pm
by faisal
Btw, I've tested the callback API on hardware and it works.

Re: Change CAN driver to use callbacks

Posted: Thu Apr 12, 2018 12:57 pm
by yosoufe
Is there any code example to use these callbacks??

Is the following correct??

Code: Select all

void cana_rx_ISR(CANDriver *canp, uint32_t flags);
void my_can_init()
{
   CAND1.rxfull_cb = cana_rx_ISR;
   canStart(&CAND1, &cancfg);
}
void cana_rx_ISR(CANDriver *canp, uint32_t flags)
{
   (void)canp;
   (void)flahs;
   // do sth usefull
}


and adding

Code: Select all

#define CAN_ENFORCE_USE_CALLBACKS         TRUE
to mcuconf.h