UseITECCan.h
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00025 //----------------------------------------------------------------------
00026 #ifndef _icl_hardware_can_UseITECCan_h_
00027 #define _icl_hardware_can_UseITECCan_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 #ifdef _SYSTEM_LXRT_
00039 # include "CAN-lxrt.h"
00040 #else
00041 # include "icl_hardware_can/UseCanNoLxrt.h"
00042 #endif
00043 
00044 #include "icl_hardware_can/tCanMessage.h"
00045 
00046 namespace icl_hardware {
00047 namespace can {
00048 
00049 typedef int tCanDescriptor;
00050 
00051 inline bool CanDescriptorValid(tCanDescriptor can_device)
00052 {
00053   return can_device >= 0;
00054 }
00055 
00056 inline tCanDescriptor InvalidCanDescriptor()
00057 {
00058   return -1;
00059 }
00060 
00061 inline const char* CanDriverName()
00062 {
00063   return "MCA-CAN";
00064 }
00065 
00066 #ifdef _SYSTEM_LXRT_
00067 
00068 inline bool CanDriverLxrtSupport()
00069 {
00070   return true;
00071 }
00072 #endif
00073 
00074 #define IOCTL_CAN_IOCRESET    0x01
00075 #define IOCTL_CAN_SETBAUDRATE 0x02
00076 #define IOCTL_CAN_IOCSETRMASK 0x03
00077 #define IOCTL_CAN_SETSENDFIFOSIZE 0x04
00078 #define IOCTL_CAN_SETRECEIVEFIFOSIZE 0x05
00079 #define IOCTL_CAN_KNOCKNOCK 0x06
00080 
00081 #ifdef _SYSTEM_LINUX_
00082 
00084 
00103 inline tCanDescriptor CanDeviceOpen(const char *device_name, int flags,
00104                                     unsigned char acceptance_code, unsigned char acceptance_mask, unsigned int baud_rate,
00105                                     unsigned send_fifo_size, unsigned receive_fifo_size)
00106 {
00107   int can_device = open(device_name, flags);
00108   if (can_device >= 0)
00109   {
00110     long value = receive_fifo_size;
00111         if (ioctl(can_device, IOCTL_CAN_KNOCKNOCK, value)<0)
00112         {
00113                 close(can_device);
00114                 return -1;
00115         }
00116     if (ioctl(can_device, IOCTL_CAN_SETRECEIVEFIFOSIZE, value) < 0)
00117     {
00118       close(can_device);
00119       return -1;
00120     }
00121     value = send_fifo_size;
00122     if (ioctl(can_device, IOCTL_CAN_SETSENDFIFOSIZE, value) < 0)
00123     {
00124       close(can_device);
00125       return -1;
00126     }
00127     value = acceptance_mask;
00128     value = ((value << 8)) | (acceptance_code);
00129     if (ioctl(can_device, IOCTL_CAN_IOCSETRMASK, value) < 0)
00130     {
00131       close(can_device);
00132       return -1;
00133     }
00134     value = baud_rate;
00135     if (ioctl(can_device, IOCTL_CAN_SETBAUDRATE, value) < 0)
00136     {
00137       close(can_device);
00138       return -1;
00139     }
00140   }
00141   return can_device;
00142 }
00143 
00145 
00150 inline int CanDeviceClose(tCanDescriptor can_device)
00151 {
00152   return close(can_device);
00153 }
00154 
00155 inline int CanDeviceSend(tCanDescriptor can_device, const tCanMessage &msg)
00156 {
00157   return write(can_device, &msg, sizeof(tCanMessage));
00158 }
00159 
00160 inline int CanDeviceReceive(tCanDescriptor can_device, tCanMessage &msg)
00161 {
00162   return read(can_device, &msg, sizeof(tCanMessage));
00163 }
00164 
00166 
00171 inline int CanDeviceReset(tCanDescriptor can_device)
00172 {
00173   long value = 0;
00174   return ioctl(can_device, IOCTL_CAN_IOCRESET, value);
00175 }
00176 #endif
00177 
00178 }
00179 }
00180 
00181 #endif


fzi_icl_can
Author(s):
autogenerated on Thu Jun 6 2019 20:26:01