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 <stdlib.h> |
|
26 #include <errno.h> |
|
27 #include <stddef.h> /* for NULL */ |
|
28 #include <sys/ioctl.h> |
|
29 #include <fcntl.h> |
|
30 #include <signal.h> |
|
31 #include <sys/time.h> |
|
32 #include <unistd.h> |
|
33 |
|
34 /* driver pcan pci for Peak board */ |
|
35 //#include "libpcan.h" |
|
36 //#include "pcan.h" |
|
37 |
|
38 #include "libpcan.h" // for CAN_HANDLE |
|
39 |
|
40 #include <applicfg.h> |
|
41 #include "timer.h" |
|
42 #include "can_driver.h" |
|
43 #include "timers_driver.h" |
|
44 |
|
45 #define MAX_NB_CAN_PORTS 16 |
|
46 |
|
47 typedef struct { |
|
48 char used; |
|
49 HANDLE fd; |
|
50 TASK_HANDLE receiveTask; |
|
51 CO_Data* d; |
|
52 } CANPort; |
|
53 |
|
54 CANPort canports[MAX_NB_CAN_PORTS] = {{0,},}; |
|
55 |
|
56 // Define for rtr CAN message |
|
57 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR |
|
58 |
|
59 /*********functions which permit to communicate with the board****************/ |
|
60 UNS8 canReceive(CAN_HANDLE fd0, Message *m) |
|
61 { |
|
62 UNS8 data; |
|
63 TPCANMsg peakMsg; |
|
64 if ((errno = CAN_Read(((CANPort*)fd0)->fd, & peakMsg))) { // Blocks until no new message or error. |
|
65 if(errno != -EIDRM && errno != -EPERM) // error is not "Can Port closed while reading" |
|
66 { |
|
67 perror("!!! Peak board : error of reading. (from f_can_receive function) \n"); |
|
68 } |
|
69 return 1; |
|
70 } |
|
71 m->cob_id.w = peakMsg.ID; |
|
72 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_*/ |
|
73 m->rtr = 0; |
|
74 else |
|
75 m->rtr = 1; |
|
76 m->len = peakMsg.LEN; /* count of data bytes (0..8) */ |
|
77 for(data = 0 ; data < peakMsg.LEN ; data++) |
|
78 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ |
|
79 |
|
80 return 0; |
|
81 } |
|
82 |
|
83 void canReceiveLoop(CAN_HANDLE fd0) |
|
84 { |
|
85 CO_Data* d = ((CANPort*)fd0)->d; |
|
86 Message m; |
|
87 while (1) { |
|
88 if(!canReceive(fd0, &m)) |
|
89 { |
|
90 EnterMutex(); |
|
91 canDispatch(d, &m); |
|
92 LeaveMutex(); |
|
93 }else{ |
|
94 // printf("canReceive returned error\n"); |
|
95 break; |
|
96 } |
|
97 } |
|
98 } |
|
99 |
|
100 /***************************************************************************/ |
|
101 UNS8 canSend(CAN_HANDLE fd0, Message *m) |
|
102 { |
|
103 UNS8 data; |
|
104 TPCANMsg peakMsg; |
|
105 peakMsg.ID=m -> cob_id.w; /* 11/29 bit code */ |
|
106 if(m->rtr == 0) |
|
107 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST; /* bits of MSGTYPE_*/ |
|
108 else { |
|
109 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR; /* bits of MSGTYPE_*/ |
|
110 } |
|
111 peakMsg.LEN = m->len; |
|
112 /* count of data bytes (0..8) */ |
|
113 for(data = 0 ; data < m->len; data ++) |
|
114 peakMsg.DATA[data] = m->data[data]; /* data bytes, up to 8 */ |
|
115 |
|
116 if((errno = CAN_Write(((CANPort*)fd0)->fd, & peakMsg))) { |
|
117 perror("!!! Peak board : error of writing. (from canSend function) \n"); |
|
118 return 1; |
|
119 } |
|
120 return 0; |
|
121 |
|
122 } |
|
123 |
|
124 /***************************************************************************/ |
|
125 CAN_HANDLE canOpen(s_BOARD *board) |
|
126 { |
|
127 HANDLE fd0 = NULL; |
|
128 char busname[64]; |
|
129 char* pEnd; |
|
130 int i; |
|
131 |
|
132 for(i=0; i < MAX_NB_CAN_PORTS; i++) |
|
133 { |
|
134 if(!canports[i].used) |
|
135 break; |
|
136 } |
|
137 |
|
138 if(strtol(board->busname, &pEnd,0) >= 0) |
|
139 { |
|
140 sprintf(busname,"/dev/pcan%s",board->busname); |
|
141 fd0 = LINUX_CAN_Open(busname, O_RDWR); |
|
142 } |
|
143 |
|
144 if (i==MAX_NB_CAN_PORTS || fd0 == NULL) |
|
145 { |
|
146 fprintf (stderr, "Open failed.\n"); |
|
147 return (CAN_HANDLE)NULL; |
|
148 } |
|
149 |
|
150 CAN_Init(fd0, board->baudrate, CAN_INIT_TYPE_ST); |
|
151 |
|
152 canports[i].used = 1; |
|
153 canports[i].fd = fd0; |
|
154 |
|
155 canports[i].d = board->d; |
|
156 CreateReceiveTask((CANPort*) &canports[i], &canports[i].receiveTask); |
|
157 |
|
158 return (CANPort*) &canports[i]; |
|
159 } |
|
160 |
|
161 /***************************************************************************/ |
|
162 int canClose(CAN_HANDLE fd0) |
|
163 { |
|
164 CAN_Close(((CANPort*)fd0)->fd); |
|
165 WaitReceiveTaskEnd(&((CANPort*)fd0)->receiveTask); |
|
166 ((CANPort*)fd0)->used = 0; |
|
167 return 0; |
|
168 } |
|