An878 Pic18c Ecan C Routines
An878 Pic18c Ecan C Routines
An878 Pic18c Ecan C Routines
Acceptance Mask
BUFFERS 16-4 to 1 muxs
RXM0
Acceptance Filters
TXB0 TXB1 TXB2 (RXF0 – RXF05)
VCC
A MODE 0
c
MESSAGE
MESSAGE
MESSAGE
MTXBUFF
MTXBUFF
MTXBUFF
MSGREQ
MSGREQ
MSGREQ
c
TXERR
TXERR
TXERR
Acceptance Mask
MLOA
MLOA
MLOA
ABTF
ABTF
ABTF
e
p Acceptance Filters RXF15
RXM1
t (RXF06 – RXF15)
MODE 1, 2
Message MODE 0
2 RX Identifier
Queue
Buffers M
Control A
Transmit Byte Sequencer Data Field B
Transmit Option
MESSAGE
BUFFERS
Transmit Err-Pas
Error Bus-Off
Counter
Transmit<7:0> Receive<8:0>
Shift<14:0>
{Transmit<5:0>, Receive<8:0>}
Comparator
Protocol
Finite
State
CRC<14:0> Machine
Bit
Transmit Clock
Timing
Logic Logic Generator
Configuration
TX RX
Registers
void main(void)
{
unsigned long id;
BYTE data[4];
BYTE dataLen;
ECAN_RX_MSG_FLAGS flags;
do
{
// Wait for a message to get received.
while( !ECANReceiveMessage(&id, data, &dataLen, &flags) );
} while(1);
}
void RunTimeInitialization(void);
void main(void)
{
unsigned long id;
BYTE data[4];
BYTE dataLen;
ECAN_RX_MSG_FLAGS flags;
RunTimeInitialization();
void RunTimeInitialization(void)
{
// Must be in Config mode to change many of settings.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
// Select Mode 1
ECANSetFunctionalMode(ECAN_MODE_1);
// Make B0 as receiver.
ECANSetBnTxRxMode(B0, ECAN_BUFFER_RX);
ECANSetRXF0Value(0, ECAN_MSG_STD);
ECANSetRXF1Value(0, ECAN_MSG_XTD);
ECANSetRXM0Value(0, ECAN_MSG_STD);
ECANSetRXM1Value(0, ECAN_MSG_XTD);
ECANAbortAll();
...
...
while(1)
{
// Send a message.
if ( !ECANReceiveMessage(&myID,
&myData,
&dataLen,
&rxFlags) )
{
// There was no message waiting. Try again...
...
}
// A message is received.
Value Meaning
ECAN_MODE_0 Mode 0
ECAN_MODE_1 Mode 1
ECAN_MODE_2 Mode 2
Pre-condition
None
Side Effects
None
Remarks
None
Example: Usage of ECANGetFunctionalMode
// Set configuration mode.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
...
...
...
else
// in Mode 2
...
...
Pre-condition
None
Side Effects
None
Remarks
None
Example: Usage of ECANGetOperationMode
// Set configuration mode.
ECANSetOperationModeNoWait(ECAN_OP_MODE_CONFIG);
rxErrorCount = ECANGetRxErrorCount();
...
txErrorCount = ECANGetTxErrorCount();
...
ECANAbortAll();
while( !ECANIsAllAborted() )
{
// Do something while abort is in progress…
}
...
if ( ECANIsBusOff() )
// ECAN is Bus off
...
if ( ECANIsRxPassive() )
// ECAN is in receive passive state
...
if ( ECANIsTxPassive() )
// ECAN is in transmit passive state
...
RXFmBuffer
[in] Name of buffer that is to be linked to RXFm filter. The only permitted values are:
Value Meaning
RXB0 Link to RXB0 buffer
RXB1 Link to RXB1 buffer
B0 Link to B0 buffer
B1 Link to B1 buffer
B2 Link to B2 buffer
B3 Link to B3 buffer
B4 Link to B4 buffer
B5 Link to B5 buffer
Return Values
None
Pre-condition
(ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME)
or
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0)
Side Effects
None
Return Values
None
Pre-condition
(ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME)
or
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0)
Side Effects
None
Remarks
These macros perform compile time operations to reduce generated code. If possible, always supply a constant
value of permitted type. A variable argument will result in larger code.
Example: Usage of ECANLinkRXFnThrumToMask
// Link RXF0 to RXM0, RXF1 to RXM0, RXF2 to RXM1 and RXF3 to RXM1
ECANLinkRXF0Thru3ToMask(ECAN_RXM0,
ECAN_RXM0,
ECAN_RXM1,
ECAN_RXM1);
...
...
while(1)
{
// Perform regular logic
...
...
}
Parameters
id
[out] 32-bit Identifier value which may correspond to right justified 11-bit Standard Identifier or 29-bit Extended
Identifier. The exact number of bits to use depends on msgFlags parameter.
data
[out] Pointer to zero or more data bytes to send.
dataLen
[out] Number of bytes to send.
msgFlags
[out] Specifies an enumerated value of the type ECAN_RX_MSG_FLAGS. This represents the logical OR of one or
more flags. The possible values of all variables are listed in the table below:
Value Meaning
ECAN_RX_OVERFLOW Specifies receive buffer overflow
ECAN_RX_INVALID_MSG Specifies invalid message
ECAN_RX_XTD_FRAME Specifies Extended Identifier message
ECAN_RX_STD_FRAME Specifies Standard Identifier message
ECAN_RX_DBL_BUFFERED Specifies that this message was double-buffered
If a flag bit is set, the corresponding meaning is TRUE; if cleared, the corresponding meaning is FALSE.
Return Values
TRUE: If new message was copied to the given buffer.
FALSE: If no new message was found.
Pre-condition
The id, data, dataLen and msgflags pointer must point to the desired and valid memory locations.
Side Effects
None
Remarks
This function will return FALSE if there are no new messages to read. Caller may check the return value to
determine new message availability. Additional information, such as which filter caused this reception, may be
determined by calling the ECANGetFilterHitInfo() macro.
...
while(1)
{
// Send a message.
if ( !ECANReceiveMessage(&myID,
&myData,
&dataLen,
&rxFlags) )
{
// There was no message waiting. Try again...
...
}
// A message is received.
if ( rxFlags & ECAN_RX_XTD_FRAME )
// This is Extended message.
else
// This is Standard message.
Return Values
TRUE: If the given message was successfully placed in one of the empty transmit buffers.
FALSE: If all transmit buffers were full.
Pre-condition
None
Side Effects
None
Remarks
None
...
while(1)
{
// Send a message.
if ( !ECANSendMessage(myID,
myData,
dataLen,
CAN_TX_PRIORITY_0) )
{
// All buffers are full. Try again...
...
}
...
Return Values
None
Pre-condition
(ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME)
or
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0)
Side Effects
None
Remarks
The selected buffer must also be set up as a transmit buffer using the ECAN_Bn_TXRX_MODE_VAL compile time
option or by using the ECANSetBnTxRxMode macro at run-time.
Example: Usage of ECANSetBnAutoRTRMode
// Set B4 and B5 for automatic RTR handling
ECANSetB4AutoRTRMode(ECAN_AUTORTR_MODE_ENABLE);
ECANSetB5AutoRTRMode(ECAN_AUTORTR_MODE_ENABLE);
...
Return Values
None
Pre-condition
(ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME)
or
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0)
Side Effects
None
Remarks
Programmable buffer value must be a constant of permitted values only. A variable parameter would cause a
compile time error. For example, ECANSetBnRxMode(myBuffer, ECAN_RECEIVE_ALL) would not compile.
Example: Usage of ECANSetBnRxMode
// Set B0 to receive all valid messages.
ECANSetBnRxMode(B0, ECAN_RECEIVE_ALL);
mode
[in] Mode to set up. The only permitted values are:
Value Meaning
ECAN_BUFFER_RX Buffer will be configured as receiver
ECAN_BUFFER_TX Buffer will be configured as transmitter
Return Values
None
Pre-condition
(ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME)
or
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0)
Side Effects
None
Remarks
Parameter buffer must be a constant of permitted values only. A variable parameter would cause a compile time
error. For example, ECANSetBnTxRxMode(myBuffer, ECAN_BUFFER_TX) would not compile.
Example: Usage of ECANSetBnTxRxMode
// Set B0-B3 as receiver.
ECANSetBnTxRxMode(B0, ECAN_BUFFER_RX);
ECANSetBnTxRxMode(B1, ECAN_BUFFER_RX);
ECANSetBnTxRxMode(B2, ECAN_BUFFER_RX);
ECANSetBnTxRxMode(B3, ECAN_BUFFER_RX);
...
Return Values
None
Pre-condition
ECAN module must be in Configuration mode.
Side Effects
None
Remarks
None
Example 1: Usage of ECANSetBusSampleMode
// Set configuration mode.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
...
Return Values
None
Pre-condition
None
Side Effects
None
Remarks
This macro automatically enables the CANTX2 pin as CANTX pin. Use ECANDisableCANTX2() to configure
CANTX2 pin as a digital I/O.
Example: Usage of ECANSetCANTX2Mode
// Set CAN clock as CANTX2 source.
ECANSetCANTX2Mode(ECAN_TX2_SOURCE_CLOCK);
...
Return Values
None
Pre-condition
None
Side Effects
None
Remarks
None
Example: Usage of ECANSetCaptureMode
// Enable timestamp mode
ECANSetCaptureMode(ECAN_CAPTURE_MODE_ENABLE);
Return Values
None
Pre-condition
ECAN module must be in Configuration mode.
Side Effects
None
Remarks
None
Example: Usage of ECANSetFilterMode
// Set configuration mode.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
Return Values
None
Pre-condition
The ECAN module must be in Configuration mode and ECAN_LIB_MODE_VAL = ECAN_LIB_MODE_RUN_TIME.
Side Effects
None
Remarks
None
Example: Usage of ECANSetFunctionalMode
// Set configuration mode.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
...
Return Values
None
Pre-condition
None
Side Effects
None
Remarks
This is a blocking function. It waits for a given mode to be accepted by the ECAN module and then returns the
control. If a non-blocking call is required, see the ECANSetOperationModeNoWait macro.
Example: Usage of ECANSetOperationMode
// Set configuration mode.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
// Module is in Configuration mode.
...
Return Values
None
Pre-condition
None
Side Effects
None
Remarks
This is a non-blocking macro. It requests the given mode of operation and immediately returns the control. Caller
must ensure the desired mode of operation is set before performing any mode specific operation. If a blocking call
is required, see the ECANSetOperationMode function.
Example: Usage of ECANSetOperationModeNoWait
// Set configuration mode.
ECANSetOperationModeNoWait(ECAN_OP_MODE_CONFIG);
Return Values
None
Pre-condition
The ECAN module must be in Configuration mode.
Side Effects
None
Remarks
None
Example: Usage of ECANSetPHSEG2Mode
// Set configuration mode.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
...
Return Values
None
Pre-condition
(ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME)
or
(ECAN_FUNC_MODE_VAL == ECAN_MODE_0)
Side Effects
None
Remarks
None
Example: Usage of ECANSetRXB0DblBuffer
// Enable hardware double buffering for RXB0
ECANSetRXB0DblBuffer(ECAN_DBL_BUFFER_MODE_ENABLE);
...
mode
[in] Mode to set up. The only permitted values are:
Value Meaning
ECAN_RECEIVE_ALL_VALID Receive all valid messages
ECAN_RECEIVE_STANDARD Receive only Standard messages
ECAN_RECEIVE_EXTENDED Receive only Extended messages
ECAN_RECEIVE_ALL Receive all including invalid messages
Return Values
None
Pre-condition
None
Side Effects
None
Remarks
Dedicated buffer value must be a constant of permitted values only. A variable parameter would cause a compile
time error. For example, ECANSetRxBnRxMode(myBuffer, ECAN_RECEIVE_ALL) would not compile.
Example: Usage of ECANSetRxBnRxMode
// Set RXB0 to receive all valid messages.
ECANSetRxBnRxMode(RXB0, ECAN_RECEIVE_ALL);
Return Values
None
Pre-condition
The ECAN module must be in Configuration mode. To use ECANSetRXF6Value through ECANSetRXF15Value,
(ECAN_LIB_MODE_VAL == ECAN_LIB_MODE_RUN_TIME)
or
(ECAN_FUNC_MODE_VAL != ECAN_MODE_0) must be TRUE.
Side Effects
None
Remarks
In Mode 0, only ECANSetRXF0Value through ECANSetRXF5Value are available. In Mode 1 and Mode 2,
ECANRXF0Value through ECANRXF15Value are available.
Example: Usage of ECANSetRXFnValue
// Set RXF0 as Standard and RXF5 as Extended filter.
ECANSetRXF0Value(0x123, ECAN_MSG_STD);
ECANSetRXF5Value(0xabcd, ECAN_MSG_XTD);
...
Return Values
None
Pre-condition
The ECAN module must be in Configuration mode.
Side Effects
None
Remarks
None
Example: Usage of ECANSetRXMnValue
// Set RXM0 as Standard and RXM1 as Extended mask values.
ECANSetRXM0Value(0x123, ECAN_MSG_STD);
ECANSetRXM1Value(0xabcd, ECAN_MSG_XTD);
...
Return Values
None
Pre-condition
None
Side Effects
None
Remarks
None
Example: Usage of ECANSetTxDriveMode
// Set CANTX to Vdd in recessive state.
ECANSetTxDriveMode(ECAN_TXDRIVE_MODE_VDD);
...
Return Values
None
Pre-condition
ECAN module must be in Configuration mode.
Side Effects
None
Remarks
None
Example: Usage of ECANSetWakeupMode
// Set configuration mode.
ECANSetOperationMode(ECAN_OP_MODE_CONFIG);
...
• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip's Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
• Microchip is willing to work with the customer who is concerned about the integrity of their code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.