AtMega CAN Library
Well documented CAN library for AtMega processors utilizing the original Atmel driver
Functions | Variables
can_library.c File Reference
#include "can_library.h"
#include <util/atomic.h>

Functions

uint8_t can_getRxBufferLength ()
 Returns the number of messages in the buffer. More...
 
int canbuf_add (canMessage **m)
 
int canbuf_remove (canMessage *m)
 
int8_t can_hasFreeTxBuffer ()
 Check if there are ant free transmitting MOBs. More...
 
void can_enableTransciever ()
 
void can_disableTransciever ()
 
uint8_t can_init ()
 Initialize the bus and activate hardware. More...
 
void can_close ()
 Disable driver & hardware. More...
 
uint8_t can_sendMessage (canMessage *m)
 Sends a can message. More...
 
int8_t can_getMessage (canMessage *m)
 Gets a can message from the reception buffer. More...
 
 SIGNAL (CAN_TOVF_vect)
 
 SIGNAL (CAN_INT_vect)
 

Variables

volatile unsigned char msg_received = 0
 
volatile uint32_t cantimecounter = 0
 
#define RING_SIZE   32
 
volatile uint8_t ring_head
 
volatile uint8_t ring_tail
 
volatile canMessage ring_data [RING_SIZE]
 

Detailed Description

CAN library for AtMega64C1. (license: GPLv2 or LGPLv2.1)

Version
0.1
Author
Tuomas Huuki tuoma.nosp@m.s.hu.nosp@m.uki@p.nosp@m.roxi.nosp@m.mia.f.nosp@m.i
See also
http://proximia.fi

Function Documentation

◆ can_close()

void can_close ( )

Disable driver & hardware.

Disabled the driver and the transciever if the option has been configured.

◆ can_getMessage()

int8_t can_getMessage ( canMessage m)

Gets a can message from the reception buffer.

Parameters
mPointer to message.
Return values
-1No message in buffer.
nNumber of messages in buffer.

Retrieves a message from the internal circular buffer and returns the amount of messages left in the buffer OR CAN_BUFFEREMPTY if the buffer is empty. You may use either:

if(can_getMessage(&m) != CAN_BUFFEREMPTY){
// Process one message.
}

or

while(can_getMessage(&m) != CAN_BUFFEREMPTY){
// Process until buffer empty.
}

depending on your implementation, buffer length and bus speed.

◆ can_getRxBufferLength()

uint8_t can_getRxBufferLength ( )

Returns the number of messages in the buffer.

Returns
n Number of messages in buffer.

◆ can_hasFreeTxBuffer()

int8_t can_hasFreeTxBuffer ( )

Check if there are ant free transmitting MOBs.

Return values
-1No buffers
nfree buffer.

◆ can_init()

uint8_t can_init ( )

Initialize the bus and activate hardware.

Return values
0Init/autobaud failure.
1Init/autobaud success.

Initializes the driver, speed and also activates the transciever if the option has been configured.

#define CAN_BAUDRATE 250 // Use 0 for autobaud.

◆ can_sendMessage()

uint8_t can_sendMessage ( canMessage m)

Sends a can message.

Parameters
mPointer to a canMessage object that has been configured.
Return values
0Message NOT sent.
1Message sent.

Sends a canmessage with the configured id and data. Usage example:

m.mesid = MTYPESDOSLAVE | nodeid;
m.data[1] = BL_VERSION_MAJOR;
m.data[2] = BL_VERSION_MINOR;
m.data[3] = BL_VERSION_BUILD;
m.data[4] = boot_signature_byte_get(0x00);
m.data[5] = boot_signature_byte_get(0x02);
m.data[6] = boot_signature_byte_get(0x04);
m.data[7] = NODETYPEDEF;
m.length = 8;
// Blocking (use return value for information and resend on next loop for non blocking operation).
while(!can_sendMessage(&m));