16FXlib
Low level access (can.h)

Detailed Description

#include "can/can.h"
Overview
This library enables low level access to the CAN controller engine.

Modules

 Internal configurations (can.cfg.h)
 

Functions

uint8_t can_init (uint8_t prescaler, uint8_t propSeg, uint8_t phSeg1, uint8_t phSeg2, uint8_t jumpWidth, uint8_t interrupts)
 
uint8_t can_config_buffer (uint8_t buffer, uint32_t id, uint8_t options, uint8_t maskOptions, uint32_t idMask)
 
uint8_t can_set_buffer_autoreply (uint8_t buffer, uint8_t enable)
 
uint8_t can_set_buffer_data (uint8_t buffer, uint8_t *data, uint8_t len)
 
uint8_t can_send_buffer (uint8_t buffer)
 
uint8_t can_buffer_newData (uint8_t buffer)
 
int8_t can_buffer_getData (uint8_t buffer, uint8_t *data, uint32_t *id)
 

CAN-ID conversions

Defines macros conversion between numeric CAN-IDs and their representations in the CAN engine.

#define ID_TO_STDFRAME(id)   ((id & 0x000007FFL) << 18)
 
#define ID_TO_EXTFRAME(id)   (id & 0x1FFFFFFFL)
 
#define STDFRAME_TO_ID(reg)   (((reg & 0x1FFFFFFFL) >> 18) & 0x000007FFL)
 
#define EXTFRAME_TO_ID(reg)   (reg & 0x1FFFFFFFL)
 

Baudrate selections

Defines for standard baudrates usable with can_init. Values are: prescaler, TQ in propagation segment, TQ phase buffer segment 1, TQ phase buffer segment 2, TQ resynchronization jump width

#define CAN_BAUD_50K   14, 8, 6, 5, 1
 
#define CAN_BAUD_125K   7, 7, 4, 4, 1
 
#define CAN_BAUD_1000K   1, 7, 3, 3, 1
 

Buffer options

Options that can be passed to can_config_buffer as "options". Can be or'ed together.

#define CAN_BUF_OPT_ENABLED   (_BV(0))
 
#define CAN_BUF_OPT_TRANSMIT   (_BV(1))
 
#define CAN_BUF_OPT_EXTENDED   (_BV(2))
 
#define CAN_BUF_OPT_RX_INTERRUPT   (_BV(4))
 
#define CAN_BUF_OPT_TX_INTERRUPT   (_BV(5))
 

Buffer mask options

Options that can be passed to can_config_buffer as "maskOptions". Can be or'ed together.

#define CAN_BUF_MASK_USE   (_BV(0))
 
#define CAN_BUF_MASK_EXTENDED   (_BV(1))
 
#define CAN_BUF_MASK_DIRECTION   (_BV(2))
 

Function Documentation

uint8_t can_init ( uint8_t  prescaler,
uint8_t  propSeg,
uint8_t  phSeg1,
uint8_t  phSeg2,
uint8_t  jumpWidth,
uint8_t  interrupts 
)

(Re)Initializes I/Os and registers of CAN controller. Defined constants CAN_BAUD_* can be used for bit timing (constants.h).

Parameters
prescalerPrescaler ()
propSegNumber of TQ for Propagation Segment (1 - 8)
phSeg1Number of TQ for the Phase Segment 1 (2 - 8)
phSeg2Number of TQ for Phase Segment 2 (2 - 8)
jumpWidthResynchronizationJumpWidth (in TQ) (1 - 4)
interruptsIf set CAN interrupts will be enabled
Returns
1 if everything went fine, 0 if there was an error (most probably wrong input values)

Definition at line 37 of file can.c.

uint8_t can_config_buffer ( uint8_t  buffer,
uint32_t  id,
uint8_t  options,
uint8_t  maskOptions,
uint32_t  idMask 
)

Configures a CAN message buffer

Parameters
bufferBuffer to configure (1 - CAN_BUFFERCOUNT)
idCAN-ID to set
optionsSet buffer options. See defines CAN_BUF_OPT_*
maskOptionsSet mask options. See defines CAN_BUF_MASK_*
idMaskCAN-ID mask. Only used when CAN_BUF_MASK_USE is given in maskOptions
Returns
1 if everything went fine, 0 if there was an error (most probably wrong input values)

Definition at line 80 of file can.c.

uint8_t can_set_buffer_autoreply ( uint8_t  buffer,
uint8_t  enable 
)

Enable or disable automatic replies to appropriate remote-transmit-requests.

Parameters
bufferBuffer to configure (1 - CAN_BUFFERCOUNT)
enableEnable (1) or disable (0) automatic replies for this buffer
Returns
1 if everything went fine, 0 if there was an error (most probably wrong input values)

Definition at line 136 of file can.c.

uint8_t can_set_buffer_data ( uint8_t  buffer,
uint8_t data,
uint8_t  len 
)

Set data of buffer

Parameters
bufferBuffer to fill data in (1 - CAN_BUFFERCOUNT)
dataPointer to data to put in buffer
lenNumber of bytes to put (0 - 8)
Returns
1 if everything went fine, 0 if there was an error (most probably wrong input values)

Definition at line 158 of file can.c.

uint8_t can_send_buffer ( uint8_t  buffer)

Set data of buffer

Parameters
bufferBuffer to send (1 - CAN_BUFFERCOUNT)
Returns
1 if everything went fine, 0 if there was an error (most probably wrong input values)

Definition at line 184 of file can.c.

uint8_t can_buffer_newData ( uint8_t  buffer)

Set data of buffer

Parameters
bufferBuffer to check for new data (1 - CAN_BUFFERCOUNT) or 0 to check all buffers
Returns
Buffer number of buffer with new data (1 - CAN_BUFFERCOUNT), 0 if there was an error (most probably wrong input values)

Definition at line 203 of file can.c.

int8_t can_buffer_getData ( uint8_t  buffer,
uint8_t data,
uint32_t id 
)

Get data of buffer

Parameters
bufferBuffer to read data from (1 - CAN_BUFFERCOUNT)
dataPointer to target memory for data, should be at least 8 bytes long as long as you do not exactly know how many bytes there will be in the buffer
idPointer to target memory for read CAN ID
Returns
Number of returned data bytes (0 - 8), -1 if there was an error (most probably wrong input values)

Definition at line 219 of file can.c.

Macro Definition Documentation

#define ID_TO_STDFRAME (   id)    ((id & 0x000007FFL) << 18)

Converts the given ID to the format needed by the arbitration registers for standard frames

Definition at line 124 of file can.h.

#define ID_TO_EXTFRAME (   id)    (id & 0x1FFFFFFFL)

Converts the given ID to the format needed by the arbitration registers for extended frames

Definition at line 129 of file can.h.

#define STDFRAME_TO_ID (   reg)    (((reg & 0x1FFFFFFFL) >> 18) & 0x000007FFL)

Converts the value from the format of standard frames in the arbitration registers to the respective ID

Definition at line 134 of file can.h.

#define EXTFRAME_TO_ID (   reg)    (reg & 0x1FFFFFFFL)

Converts the value from the format of extended frames in the arbitration registers to the respective ID

Definition at line 139 of file can.h.

#define CAN_BAUD_50K   14, 8, 6, 5, 1

50 kBaud/s, sampling point 75%

Definition at line 155 of file can.h.

#define CAN_BAUD_125K   7, 7, 4, 4, 1

125 kBaud/s, sampling point 75%

Definition at line 160 of file can.h.

#define CAN_BAUD_1000K   1, 7, 3, 3, 1

1 MBaud/s, sampling point 78.6%

Definition at line 165 of file can.h.

#define CAN_BUF_OPT_ENABLED   (_BV(0))

Enables the buffer for reception/transmission of frames (Flag for options of can_config_buffer)

Definition at line 181 of file can.h.

#define CAN_BUF_OPT_TRANSMIT   (_BV(1))

Sets the buffer to be a transmit buffer (can respond automaticly to RTR frames if enabled afterwards) (Flag for options of can_config_buffer)

Definition at line 188 of file can.h.

#define CAN_BUF_OPT_EXTENDED   (_BV(2))

Sets the buffer to be used for extended frames (Flag for options of can_config_buffer)

Definition at line 194 of file can.h.

#define CAN_BUF_OPT_RX_INTERRUPT   (_BV(4))

Enables the RX interrupt for the buffer (Flag for options of can_config_buffer)

Definition at line 200 of file can.h.

#define CAN_BUF_OPT_TX_INTERRUPT   (_BV(5))

Enables the TX interrupt for the buffer (Flag for options of can_config_buffer)

Definition at line 206 of file can.h.

#define CAN_BUF_MASK_USE   (_BV(0))

Enables the masks for the buffer acceptance filtering (otherwise always the whole header is used for arbitration) (Flag for maskOptions of can_config_buffer)

Definition at line 223 of file can.h.

#define CAN_BUF_MASK_EXTENDED   (_BV(1))

Selects the extended bit to be checked for acceptance filtering. Only used if CAN_BUF_MASK_USE is also given (Flag for maskOptions of can_config_buffer)

Definition at line 230 of file can.h.

#define CAN_BUF_MASK_DIRECTION   (_BV(2))

Selects the direction bit to be checked for acceptance filtering. Only used if CAN_BUF_MASK_USE is also given (Flag for maskOptions of can_config_buffer)

Definition at line 237 of file can.h.