Back to ESAcademy Home Page


MicroCANopen
Hardware Interface

 

Functions of the hardware driver

The primary duty of the MicroCANopen hardware driver is to handle the CAN interface and implement a one millisecond timer tick. CAN messages passed on to or received from MicroCANopen are queued. This is a method that can easily be implemented on all existing CAN controllers, no matter how sophisticated their CAN interfaces is.

Here is an overview of the functions that need to be implemented by the hardware driver (for exact definitions, see file mcohw.h):

CAN Interrupt
The CAN interrupt should check all the possible error flags and set the global variable gMCOHW_status accordingly. Fatal errors must result in a call to MCOUSER_FatalError with an error code in the range of 0x8000 to 0x87FF. If a transmit queue is implemented, the transmit interrupt should be used to trigger transmission of the next message in the transmit queue. On "Basic CAN" controllers the receive interrupt copies the incoming message into a receive queue. CAN controllers with "Full CAN" style capabilities or internal receive queue might not need to maintain a software queue. In case a hardware queue or buffers are used, the interrupt should still check for a potential overrun and set bit RXOR in gMCOHW_status in case of an overrun.

Timer interrupt
A 1ms timer interrupt needs to implement a local 1ms WORD timer tick. The timer tick is only accessible via the functions MCOHW_GetTime and MCOHW_IsTimeExpired to avoid data inconsistency. In case only a multiple of 1ms is available on a system, the timer tick would need to be incremented in each interrupt in a way that the timer tick is still accurate (for example increment by 4 all 4ms).

BYTE MCOHW_GetStatus ( void );
This function returns the global status variable. The status variable can be changed anytime by this module, for example from within an interrupt service routine or by any of the other functions in this module.

BYTE MCOHW_Init ( WORD BaudRate );
This function implements the initialization of the CAN interface. Allowed values for the baudrate are: 1000, 800, 500, 250, 125, 50, 25 or 10.

BYTE MCOHW_SetCANFilter ( WORD CANID );
This function implements the initialization of a CAN ID hardware filter as supported by many CAN controllers. Per default, the controller is set to not receive any ID. With each call to this function, the filters are set to let messages with this ID pass.

BYTE MCOHW_PushMessage ( CAN_MSG *pTransmitBuf );
This function implements a CAN transmit queue. With each function call the passed CAN message is added to the queue. NOTE: The MicroCANopen stack will not try to add messages to the queue "back-to-back". With each call to MCO_ProcessStack, a maximum of one message is added to the queue. For many applications a queue with length "1" will be sufficient. Only applications with a high busload or very slow bus speed might need a queue of length "3" or more.

BYTE MCOHW_PullMessage ( CAN_MSG *pTransmitBuf );
This function implements a CAN receive queue. With each function call a message is pulled from the queue.  NOTE: Implementation of this function greatly varies with CAN controller used. In an SJA1000 style controller, the hardware queue inside the controller can be used as the queue. Controllers with just one receive buffer need a bigger software queue. "Full CAN" style controllers might just implement multiple message objects, one each for each ID received (using function MCOHW_SetCANFilter).

WORD MCOHW_GetTime ( void );
This function reads a 1 millisecond timer tick. The timer tick must be a WORD and must be incremented once per millisecond. NOTES: Data consistency must be insured by this implementation. (On 8-bit systems, disable the timer interrupt incrementing the timer tick while executing this function). Systems that cannot provide a 1ms tick may consider incrementing the timer tick only once every "x" ms, if the increment is by "x".

BYTE MCOHW_IsTimeExpired ( WORD timestamp );
This function compares a WORD timestamp to the internal timer tick and returns 1 if the timestamp expired/passed. The maximum timer runtime measurable is 0x8000 (about 32 seconds). For the usage in MicroCANopen that is sufficient.

We currently have  drivers implemented for the NXP LPC2000 and the Atmel 89C51CC01. They are part of the examples.

Return to MicroCANopen Home

© Embedded Systems Academy, Inc. All Rights Reserved.

San Jose, CA
Tel. (877) 812-6393
Fax. (877) 812-6382

www.esacademy.com
info@esacademy.com