Change CAN driver to use callbacks

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Change CAN driver to use callbacks

Postby faisal » Mon Jan 01, 2018 4:28 am

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).

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Change CAN driver to use callbacks

Postby Giovanni » Mon Jan 01, 2018 8:49 am

Hi,

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

Giovanni

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Change CAN driver to use callbacks

Postby faisal » Mon Jan 01, 2018 4:59 pm

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.

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Change CAN driver to use callbacks

Postby steved » Tue Jan 02, 2018 12:03 am

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.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Change CAN driver to use callbacks

Postby Giovanni » Tue Jan 02, 2018 12:22 pm

Hi,

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

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Change CAN driver to use callbacks

Postby Giovanni » Tue Jan 09, 2018 11:52 am

Any feedback about the changes?

Giovanni

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Change CAN driver to use callbacks

Postby steved » Tue Jan 09, 2018 2:15 pm

Giovanni wrote:Any feedback about the changes?

Giovanni

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

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Change CAN driver to use callbacks

Postby faisal » Tue Jan 09, 2018 4:58 pm

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.

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: Change CAN driver to use callbacks

Postby faisal » Sun Mar 18, 2018 6:37 pm

Btw, I've tested the callback API on hardware and it works.

yosoufe
Posts: 9
Joined: Thu Jan 26, 2017 1:34 pm
Has thanked: 1 time

Re: Change CAN driver to use callbacks

Postby yosoufe » Thu Apr 12, 2018 12:57 pm

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


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 4 guests