|
1 /* |
|
2 This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
|
4 Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
|
6 See COPYING file for copyrights details. |
|
7 |
|
8 This library is free software; you can redistribute it and/or |
|
9 modify it under the terms of the GNU Lesser General Public |
|
10 License as published by the Free Software Foundation; either |
|
11 version 2.1 of the License, or (at your option) any later version. |
|
12 |
|
13 This library is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 Lesser General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU Lesser General Public |
|
19 License along with this library; if not, write to the Free Software |
|
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 */ |
|
22 |
|
23 #include <stdio.h> |
|
24 #include <string.h> |
|
25 #include <errno.h> |
|
26 #include <fcntl.h> |
|
27 |
|
28 /* driver pcan pci for Peak board */ |
|
29 //#include "libpcan.h" |
|
30 //#include "pcan.h" |
|
31 |
|
32 #include "libpcan.h" // for CAN_HANDLE |
|
33 |
|
34 #include "can_driver.h" |
|
35 |
|
36 // Define for rtr CAN message |
|
37 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR |
|
38 |
|
39 /*********functions which permit to communicate with the board****************/ |
|
40 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
|
41 { |
|
42 UNS8 data; |
|
43 TPCANMsg peakMsg; |
|
44 if ((errno = CAN_Read(fd0, & peakMsg))) { // Blocks until no new message or error. |
|
45 if(errno != -EIDRM && errno != -EPERM) // error is not "Can Port closed while reading" |
|
46 { |
|
47 perror("!!! Peak board : error of reading. (from f_can_receive function) \n"); |
|
48 } |
|
49 return 1; |
|
50 } |
|
51 m->cob_id.w = peakMsg.ID; |
|
52 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_*/ |
|
53 m->rtr = 0; |
|
54 else |
|
55 m->rtr = 1; |
|
56 m->len = peakMsg.LEN; /* count of data bytes (0..8) */ |
|
57 for(data = 0 ; data < peakMsg.LEN ; data++) |
|
58 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ |
|
59 |
|
60 return 0; |
|
61 } |
|
62 |
|
63 /***************************************************************************/ |
|
64 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) |
|
65 { |
|
66 UNS8 data; |
|
67 TPCANMsg peakMsg; |
|
68 peakMsg.ID=m -> cob_id.w; /* 11/29 bit code */ |
|
69 if(m->rtr == 0) |
|
70 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST; /* bits of MSGTYPE_*/ |
|
71 else { |
|
72 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR; /* bits of MSGTYPE_*/ |
|
73 } |
|
74 peakMsg.LEN = m->len; |
|
75 /* count of data bytes (0..8) */ |
|
76 for(data = 0 ; data < m->len; data ++) |
|
77 peakMsg.DATA[data] = m->data[data]; /* data bytes, up to 8 */ |
|
78 |
|
79 if((errno = CAN_Write(fd0, & peakMsg))) { |
|
80 perror("!!! Peak board : error of writing. (from canSend function) \n"); |
|
81 return 1; |
|
82 } |
|
83 return 0; |
|
84 |
|
85 } |
|
86 |
|
87 |
|
88 /***************************************************************************/ |
|
89 int TranslateBaudeRate(char* optarg){ |
|
90 if(!strcmp( optarg, "1M")) return CAN_BAUD_1M; |
|
91 if(!strcmp( optarg, "500K")) return CAN_BAUD_500K; |
|
92 if(!strcmp( optarg, "250K")) return CAN_BAUD_250K; |
|
93 if(!strcmp( optarg, "125K")) return CAN_BAUD_125K; |
|
94 if(!strcmp( optarg, "100K")) return CAN_BAUD_100K; |
|
95 if(!strcmp( optarg, "50K")) return CAN_BAUD_50K; |
|
96 if(!strcmp( optarg, "20K")) return CAN_BAUD_20K; |
|
97 if(!strcmp( optarg, "10K")) return CAN_BAUD_10K; |
|
98 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K; |
|
99 if(!strcmp( optarg, "none")) return 0; |
|
100 return 0x0000; |
|
101 } |
|
102 |
|
103 /***************************************************************************/ |
|
104 CAN_HANDLE canOpen_driver(s_BOARD *board) |
|
105 { |
|
106 HANDLE fd0 = NULL; |
|
107 char busname[64]; |
|
108 char* pEnd; |
|
109 int i; |
|
110 int baudrate; |
|
111 |
|
112 if(strtol(board->busname, &pEnd,0) >= 0) |
|
113 { |
|
114 sprintf(busname,"/dev/pcan%s",board->busname); |
|
115 fd0 = LINUX_CAN_Open(busname, O_RDWR); |
|
116 } |
|
117 |
|
118 if(baudrate = TranslateBaudeRate(board->baudrate)) |
|
119 CAN_Init(fd0, baudrate, CAN_INIT_TYPE_ST); |
|
120 |
|
121 return (CAN_HANDLE)fd0; |
|
122 } |
|
123 |
|
124 /***************************************************************************/ |
|
125 int canClose_driver(CAN_HANDLE fd0) |
|
126 { |
|
127 CAN_Close(fd0); |
|
128 return 0; |
|
129 } |