I am in the process of understanding the CAN library created for DUE. I am having trouble in figuring out this exact declaration :
I have seen the typ def Can in the beginning of " due_can.h " but it is only as a reference as to how the struct is defined besides that there is no other definition available of this type. Can someone please clear this up for me.
Hello Owais,Can* m_pCan
is a parameter of a Can function. More specifically, a defined address.
For the two Can controllers inside Arduino Due's SAM3X8E, the current CAN library (originally ported from Atmel) has defined two CAN addresses: CAN0 (0x400B4000U) and CAN1 (0x400B8000U). See sam3x8e.h file at
Let's dissect a bit the Can* m_pCan
: is a structure that contains the Can hardware registers, and it is part of the component_can.h file
: is the Pointer to a CAN peripheral instance.
When we declare Can* m_pCan, m_pCan is a variable that stores the address of a variable of the structure Can.
If for example, we want to enable the CAN controller 0. Then in the main, the function call looks like:
And that's it! but... to get a better understanding (as you requested) of the parameter, let's take a look at the original function (before the CANRaw class was created).
The call of the old function looks like:
And the old function itself:
void can_enable(Can* m_pCan)
m_pCan->CAN_MR |= CAN_MR_CANEN;
is the CAN controller enable register. Bit 0 (0x1u << 0)CAN_MR
is the CAN mode hardware register inside the Can structure.
Thus, when we type can_enable(CAN0);
we are declaring the call of the can enable function that points to the CAN controller 0 that SET the bit 0 of the enable register in the can mode hardware register inside the can structure.
Notice that all of this is transparent for the user given that all the Can functions are handle for the CANRaw class.
I hope this helps. Regards!