Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00025
00026 #ifndef _icl_hardware_can_UseMCACan_h_
00027 #define _icl_hardware_can_UseMCACan_h_
00028
00029 #ifdef _SYSTEM_LINUX_
00030 # include <errno.h>
00031 # include <fcntl.h>
00032 # include <unistd.h>
00033 # include <sys/ioctl.h>
00034 # include <string.h>
00035 # include <stdlib.h>
00036 #endif
00037
00038 #include "icl_hardware_can/tCanMessage.h"
00039 typedef icl_hardware::can::tCanMessage tCanMessage;
00040
00041 #ifdef _SYSTEM_LXRT_
00042 # include "CAN-lxrt.h"
00043 #else
00044 # include "icl_hardware_can/UseCanNoLxrt.h"
00045 #endif
00046
00047 #ifdef _IC_BUILDER_CAN_MCA_HAS_IOCTL_H_
00048 # include "CAN-ioctl.h"
00049 #else
00050 # define IOCTL_CAN_IOCRESET 0x01
00051 # define IOCTL_CAN_SETBAUDRATE 0x02
00052 # define IOCTL_CAN_IOCSETRMASK 0x03
00053 # define IOCTL_CAN_SETSENDFIFOSIZE 0x04
00054 # define IOCTL_CAN_SETRECEIVEFIFOSIZE 0x05
00055 #endif
00056
00057 namespace icl_hardware {
00058 namespace can {
00059
00060 typedef int tCanDescriptor;
00061
00062 inline bool CanDescriptorValid(tCanDescriptor can_device)
00063 {
00064 return can_device >= 0;
00065 }
00066
00067 inline tCanDescriptor InvalidCanDescriptor()
00068 {
00069 return -1;
00070 }
00071
00072 inline const char* CanDriverName()
00073 {
00074 return "MCA-CAN";
00075 }
00076
00077 #ifdef _SYSTEM_LXRT_
00078
00079 inline bool CanDriverLxrtSupport()
00080 {
00081 return true;
00082 }
00083 #endif
00084
00085 #ifdef _SYSTEM_LINUX_
00086
00088
00107 inline tCanDescriptor CanDeviceOpen(const char *device_name, int flags,
00108 unsigned char acceptance_code, unsigned char acceptance_mask, unsigned int baud_rate,
00109 unsigned send_fifo_size, unsigned receive_fifo_size)
00110 {
00111 int can_device = open(device_name, flags);
00112 if (can_device >= 0)
00113 {
00114 long value = receive_fifo_size;
00115 if (ioctl(can_device, IOCTL_CAN_SETRECEIVEFIFOSIZE, value) < 0)
00116 {
00117 close(can_device);
00118 return -1;
00119 }
00120 value = send_fifo_size;
00121 if (ioctl(can_device, IOCTL_CAN_SETSENDFIFOSIZE, value) < 0)
00122 {
00123 close(can_device);
00124 return -1;
00125 }
00126 value = acceptance_mask;
00127 value = ((value << 8)) | (acceptance_code);
00128 if (ioctl(can_device, IOCTL_CAN_IOCSETRMASK, value) < 0)
00129 {
00130 close(can_device);
00131 return -1;
00132 }
00133 value = baud_rate;
00134 if (ioctl(can_device, IOCTL_CAN_SETBAUDRATE, value) < 0)
00135 {
00136 close(can_device);
00137 return -1;
00138 }
00139 }
00140 return can_device;
00141 }
00142
00144
00149 inline int CanDeviceClose(tCanDescriptor can_device)
00150 {
00151 return close(can_device);
00152 }
00153
00154 inline int CanDeviceSend(tCanDescriptor can_device, const tCanMessage &msg)
00155 {
00156 return write(can_device, &msg, sizeof(tCanMessage));
00157 }
00158
00159 inline int CanDeviceReceive(tCanDescriptor can_device, tCanMessage &msg)
00160 {
00161 return read(can_device, &msg, sizeof(tCanMessage));
00162 }
00163
00165
00170 inline int CanDeviceReset(tCanDescriptor can_device)
00171 {
00172 long value = 0;
00173 return ioctl(can_device, IOCTL_CAN_IOCRESET, value);
00174 }
00175 #endif
00176
00177 }
00178 }
00179
00180 #endif