dnx RTOS 2.2.0 "Eagle"
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
CAN Driver

Detailed Description

Description

Driver handles CAN controller peripheral existing in microcontroller.

Supported architectures

Details

Meaning of major and minor numbers

Only CAN1 is supported.

Numeration restrictions

Both major and minor number should be set to 0.

Driver initialization

To initialize driver the following code can be used:

driver_init("CAN", 0, 0, "/dev/can");

Driver release

To release driver the following code can be used:

driver_release("CAN", 0, 0);

Driver configuration

To configure CAN controller one should do following steps:

  1. CAN controller configuration (baud rate, transmission parameters),
  2. Acceptance filter setup,
  3. Enter CAN controller to normal mode.

Example code

#include <stdio.h>
#include <sys/ioctl.h>
//...
FILE *f = fopen("/dev/can", "r+");
if (f) {
// basic configuration
cfg.auto_retransmission = false;
cfg.auto_wake_up = false;
cfg.loopback = false;
cfg.silent = false;
cfg.time_triggered_comm = false;
cfg.SJW = 1;
cfg.TS1 = 3;
cfg.TS2 = 5;
cfg.prescaler = 4;
if (ioctl(fileno(f), IOCTL_CAN__CONFIGURE, &cfg) != 0) {
perror("CAN configuration");
...
}
// filter 0 setup
CAN_filter_t filter;
filter.number = 0;
filter.ID = 0xFF10;
filter.mask = 0;
filter.extended_ID = false;
if (ioctl(fileno(f), IOCTL_CAN__SET_FILTER, &filter) != 0) {
perror("CAN filter error");
...
}
// filter 1 setup
filter.number = 1;
filter.ID = 0xFF20;
filter.mask = 0xFFF0;
filter.extended_ID = false;
if (ioctl(fileno(f), IOCTL_CAN__SET_FILTER, &filter) != 0) {
perror("CAN filter error");
...
}
// enabling normal mode
if (ioctl(fileno(f), IOCTL_CAN__SET_MODE, &mode) != 0) {
perror("CAN normal mode error");
...
}
// configuration done
...
fclose(f);
} else {
perror("CAN");
}
//...

Data write

There are two possible methods for data write operation: by using fwrite() and by using ioctl() functions. By using fwrite() function is possibility to send an array of CAN messages.

Example code using fwrite() function

#define <stdio.h>
#define <sys/ioctl.h>
// ...
FILE *f = fopen("/dev/can", "r+");
if (f) {
CAN_msg_t msg;
msg.ID = 0x1000;
msg.extended_ID = true;
msg.data[0] = 0xFF;
msg.data[1] = 0x02;
msg.data_length = 2;
msg.remote_transmission = false;
msg.timeout_ms = 2000;
if (fwrite(&msg, sizeof(msg), 1, f) == 1) {
// success write
// ...
} else {
perror("Write error");
}
fclose(f);
}
// ...

Example code using ioctl() function

#define <stdio.h>
#define <sys/ioctl.h>
// ...
FILE *f = fopen("/dev/can", "r+");
if (f) {
CAN_msg_t msg;
msg.ID = 0x1000;
msg.extended_ID = true;
msg.data[0] = 0xFF;
msg.data[1] = 0x02;
msg.data_length = 2;
msg.remote_transmission = false;
msg.timeout_ms = 2000;
if (ioctl(fileno(f), IOCTL_CAN__SEND_MSG, &msg) == 0) {
// success write
// ...
} else {
perror("Write error");
}
fclose(f);
}
// ...

Data read

There are two possible methods for data read operation: by using fread() and by using ioctl() functions. By using fread() function is possibility to read an array of CAN messages.

Example code using fread() function

#define <stdio.h>
#define <sys/ioctl.h>
// ...
FILE *f = fopen("/dev/can", "r+");
if (f) {
CAN_msg_t msg;
msg.timeout_ms = 2000;
if (fread(&msg, sizeof(msg), 1, f) == 1) {
// success read
// ...
} else {
perror("Read error");
}
fclose(f);
}
// ...

Example code using ioctl() function

#define <stdio.h>
#define <sys/ioctl.h>
// ...
FILE *f = fopen("/dev/can", "r+");
if (f) {
CAN_msg_t msg;
msg.timeout_ms = 2000;
if (ioctl(fileno(f), IOCTL_CAN__RECV_MSG, &msg) == 0) {
// success read
// ...
} else {
perror("Read error");
}
fclose(f);
}
// ...

Baud rate calculation

To calculate CAN baud rate following equation should be applied:

baud = (fPCLK [Hz]) / (prescaler * (1 + TS1 + TS2)) [bps]

Data Structures

struct  CAN_filter_t
 
struct  CAN_config_t
 
struct  CAN_msg_t
 

Macros

#define IOCTL_CAN__CONFIGURE   _IOW(CAN, 0x00, const CAN_config_t*)
 CAN controller configuration. More...
 
#define IOCTL_CAN__SET_FILTER   _IOW(CAN, 0x01, const CAN_filter_t*)
 Acceptance filter setup. More...
 
#define IOCTL_CAN__DISABLE_FILTER   _IOW(CAN, 0x02, const u32_t*)
 Disable selected filter. More...
 
#define IOCTL_CAN__GET_NUMBER_OF_FILTERS   _IOR(CAN, 0x03, u32_t*)
 Get total number of filter slots. More...
 
#define IOCTL_CAN__SET_MODE   _IOW(CAN, 0x04, const CAN_mode_t*)
 CAN controller mode selection. More...
 
#define IOCTL_CAN__SET_SEND_TIMEOUT   _IOW(CAN, 0x05, const u32_t*)
 Set send timeout in milliseconds. Used in write() family function. More...
 
#define IOCTL_CAN__SET_RECV_TIMEOUT   _IOW(CAN, 0x06, const u32_t*)
 Set receive timeout in milliseconds. Used in read() family function. More...
 
#define IOCTL_CAN__SEND_MSG   _IOW(CAN, 0x07, const CAN_msg_t*)
 Send CAN message. More...
 
#define IOCTL_CAN__RECV_MSG   _IOR(CAN, 0x08, CAN_msg_t*)
 Receive CAN message. More...
 

Enumerations

enum  CAN_mode_t
 

Data Structure Documentation

struct CAN_filter_t

Type represent CAN acceptance filter.

Data Fields
bool extended_ID

Extended identifier.

u32_t ID

Identifier.

u32_t mask

Identifier mask.

u32_t number

Filter number.

struct CAN_config_t

Type represent configuration container.

Data Fields
bool auto_bus_off_management

Automatic bus off management.

bool auto_retransmission

Auto retransmission.

bool auto_wake_up

Automatic wake up.

bool loopback

Loopback mode.

u32_t prescaler

Peripheral prescaler <1,n>

bool silent

Silent mode.

u32_t SJW

Resynchronization jump width <1,n>

bool time_triggered_comm

Time triggered communication.

u32_t TS1

Time segment 1 <1,n>

u32_t TS2

Time segment 2 <1,n>

struct CAN_msg_t

Type represent CAN message with timeout control.

Data Fields
u8_t data[8]

Data buffer (8 bytes).

u32_t data_length

Data length.

bool extended_ID

Extended identifier (true).

u32_t ID

Message identifier.

bool remote_transmission

Remote transmission (true).

u32_t timeout_ms

Timeout in milliseconds.

Macro Definition Documentation

#define IOCTL_CAN__CONFIGURE   _IOW(CAN, 0x00, const CAN_config_t*)
Parameters
[WR]const CAN_config_t * configuration container
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__DISABLE_FILTER   _IOW(CAN, 0x02, const u32_t*)
Parameters
[WR]const u32_t * filter number
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__GET_NUMBER_OF_FILTERS   _IOR(CAN, 0x03, u32_t*)
Parameters
[WR]const u32_t * number of filters
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__RECV_MSG   _IOR(CAN, 0x08, CAN_msg_t*)
Parameters
[RD]CAN_msg_t * CAN message destination
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__SEND_MSG   _IOW(CAN, 0x07, const CAN_msg_t*)
Parameters
[WR]const CAN_msg_t * CAN message source
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__SET_FILTER   _IOW(CAN, 0x01, const CAN_filter_t*)
Parameters
[WR]const CAN_filter_t * filter container
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__SET_MODE   _IOW(CAN, 0x04, const CAN_mode_t*)
Parameters
[WR]const CAN_mode_t * can mode
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__SET_RECV_TIMEOUT   _IOW(CAN, 0x06, const u32_t*)
Parameters
[WR]const u32_t * timeout in milliseconds
Returns
On success 0 is returned, otherwise -1.
#define IOCTL_CAN__SET_SEND_TIMEOUT   _IOW(CAN, 0x05, const u32_t*)
Parameters
[WR]const u32_t * timeout in milliseconds
Returns
On success 0 is returned, otherwise -1.

Enumeration Type Documentation

enum CAN_mode_t

Type represent CAN mode.

Enumerator
CAN_MODE__INIT 

CAN controller in initialization mode.

CAN_MODE__NORMAL 

CAN controller in normal mode.

CAN_MODE__SLEEP 

CAN controller in sleep mode.