UseITECCan.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
25 //----------------------------------------------------------------------
26 #ifndef _icl_hardware_can_UseITECCan_h_
27 #define _icl_hardware_can_UseITECCan_h_
28 
29 #ifdef _SYSTEM_LINUX_
30 # include <errno.h>
31 # include <fcntl.h>
32 # include <unistd.h>
33 # include <sys/ioctl.h>
34 # include <string.h>
35 # include <stdlib.h>
36 #endif
37 
38 #ifdef _SYSTEM_LXRT_
39 # include "CAN-lxrt.h"
40 #else
42 #endif
43 
45 
46 namespace icl_hardware {
47 namespace can {
48 
49 typedef int tCanDescriptor;
50 
51 inline bool CanDescriptorValid(tCanDescriptor can_device)
52 {
53  return can_device >= 0;
54 }
55 
56 inline tCanDescriptor InvalidCanDescriptor()
57 {
58  return -1;
59 }
60 
61 inline const char* CanDriverName()
62 {
63  return "MCA-CAN";
64 }
65 
66 #ifdef _SYSTEM_LXRT_
67 inline bool CanDriverLxrtSupport()
69 {
70  return true;
71 }
72 #endif
73 
74 #define IOCTL_CAN_IOCRESET 0x01
75 #define IOCTL_CAN_SETBAUDRATE 0x02
76 #define IOCTL_CAN_IOCSETRMASK 0x03
77 #define IOCTL_CAN_SETSENDFIFOSIZE 0x04
78 #define IOCTL_CAN_SETRECEIVEFIFOSIZE 0x05
79 #define IOCTL_CAN_KNOCKNOCK 0x06
80 
81 #ifdef _SYSTEM_LINUX_
82 
84 
103 inline tCanDescriptor CanDeviceOpen(const char *device_name, int flags,
104  unsigned char acceptance_code, unsigned char acceptance_mask, unsigned int baud_rate,
105  unsigned send_fifo_size, unsigned receive_fifo_size)
106 {
107  int can_device = open(device_name, flags);
108  if (can_device >= 0)
109  {
110  long value = receive_fifo_size;
111  if (ioctl(can_device, IOCTL_CAN_KNOCKNOCK, value)<0)
112  {
113  close(can_device);
114  return -1;
115  }
116  if (ioctl(can_device, IOCTL_CAN_SETRECEIVEFIFOSIZE, value) < 0)
117  {
118  close(can_device);
119  return -1;
120  }
121  value = send_fifo_size;
122  if (ioctl(can_device, IOCTL_CAN_SETSENDFIFOSIZE, value) < 0)
123  {
124  close(can_device);
125  return -1;
126  }
127  value = acceptance_mask;
128  value = ((value << 8)) | (acceptance_code);
129  if (ioctl(can_device, IOCTL_CAN_IOCSETRMASK, value) < 0)
130  {
131  close(can_device);
132  return -1;
133  }
134  value = baud_rate;
135  if (ioctl(can_device, IOCTL_CAN_SETBAUDRATE, value) < 0)
136  {
137  close(can_device);
138  return -1;
139  }
140  }
141  return can_device;
142 }
143 
145 
150 inline int CanDeviceClose(tCanDescriptor can_device)
151 {
152  return close(can_device);
153 }
154 
155 inline int CanDeviceSend(tCanDescriptor can_device, const tCanMessage &msg)
156 {
157  return write(can_device, &msg, sizeof(tCanMessage));
158 }
159 
160 inline int CanDeviceReceive(tCanDescriptor can_device, tCanMessage &msg)
161 {
162  return read(can_device, &msg, sizeof(tCanMessage));
163 }
164 
166 
171 inline int CanDeviceReset(tCanDescriptor can_device)
172 {
173  long value = 0;
174  return ioctl(can_device, IOCTL_CAN_IOCRESET, value);
175 }
176 #endif
177 
178 }
179 }
180 
181 #endif
int CanDeviceClose(tCanDescriptor)
Definition: UseDummyCan.h:62
Implements a struct representing a can message.
Definition: tCanMessage.h:43
#define IOCTL_CAN_KNOCKNOCK
Definition: UseITECCan.h:79
#define IOCTL_CAN_SETRECEIVEFIFOSIZE
Definition: UseITECCan.h:78
bool CanDescriptorValid(tCanDescriptor can_device)
Definition: UseDummyCan.h:38
int CanDeviceReset(tCanDescriptor)
Definition: UseDummyCan.h:77
bool CanDriverLxrtSupport()
no support for LXRT available
Definition: UseCanNoLxrt.h:35
#define IOCTL_CAN_IOCRESET
Definition: UseITECCan.h:74
const char * CanDriverName()
Definition: UseDummyCan.h:48
#define IOCTL_CAN_SETSENDFIFOSIZE
Definition: UseITECCan.h:77
Contains CAN driver interface functions.
tCanDescriptor InvalidCanDescriptor()
Definition: UseDummyCan.h:43
Contains CAN driver interface functions.
#define IOCTL_CAN_SETBAUDRATE
Definition: UseITECCan.h:75
tCanDescriptor CanDeviceOpen(const char *, int, unsigned char, unsigned char, unsigned int, unsigned, unsigned)
Open a can device and set parameters.
Definition: UseDummyCan.h:53
int CanDeviceSend(tCanDescriptor, const tCanMessage &)
Definition: UseDummyCan.h:67
#define IOCTL_CAN_IOCSETRMASK
Definition: UseITECCan.h:76
int CanDeviceReceive(tCanDescriptor, tCanMessage &)
Definition: UseDummyCan.h:72


fzi_icl_can
Author(s):
autogenerated on Mon Jun 10 2019 13:17:02