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