26
|
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 |
#include "canmsg.h"
|
|
35 |
#include "lincan.h"
|
|
36 |
|
|
37 |
struct CANPort;
|
|
38 |
#define CAN_HANDLE struct CANPort *
|
|
39 |
|
|
40 |
#include <applicfg.h>
|
|
41 |
|
|
42 |
#include "timer.h"
|
|
43 |
#include "can_driver.h"
|
|
44 |
#include "timers_driver.h"
|
|
45 |
|
|
46 |
typedef struct CANPort {
|
|
47 |
char used;
|
|
48 |
HANDLE fd;
|
|
49 |
TASK_HANDLE receiveTask;
|
|
50 |
CO_Data* d;
|
|
51 |
} CANPort;
|
|
52 |
|
|
53 |
/*********functions which permit to communicate with the board****************/
|
|
54 |
UNS8 canReceive(CAN_HANDLE fd0, Message *m)
|
|
55 |
{
|
|
56 |
int res;
|
|
57 |
struct canmsg_t canmsg;
|
|
58 |
|
|
59 |
canmsg.flags = 0; /* Ensure standard receive, not required for LinCAN>=0.3.1 */
|
|
60 |
|
|
61 |
do{
|
|
62 |
res = read(fd0->fd,&canmsg,sizeof(canmsg_t));
|
|
63 |
if((res<0)&&(errno == -EAGAIN)) res = 0;
|
|
64 |
}while(res==0);
|
|
65 |
|
|
66 |
if(res != sizeof(canmsg_t)) // No new message
|
|
67 |
return 1;
|
|
68 |
|
|
69 |
if(canmsg.flags&MSG_EXT){
|
|
70 |
/* There is no mark for extended messages in CanFestival */;
|
|
71 |
}
|
|
72 |
|
|
73 |
m->cob_id.w = canmsg.id;
|
|
74 |
m->len = canmsg.length;
|
|
75 |
if(canmsg.flags&MSG_RTR){
|
|
76 |
m->rtr = 1;
|
|
77 |
}else{
|
|
78 |
m->rtr = 0;
|
|
79 |
memcpy(m->data,canmsg.data,8);
|
|
80 |
}
|
|
81 |
|
|
82 |
return 0;
|
|
83 |
}
|
|
84 |
|
|
85 |
void canReceiveLoop(CAN_HANDLE fd0)
|
|
86 |
{
|
|
87 |
CO_Data* d = fd0->d;
|
|
88 |
Message m;
|
|
89 |
while (fd0->used) {
|
|
90 |
if(!canReceive(fd0, &m))
|
|
91 |
{
|
|
92 |
EnterMutex();
|
|
93 |
canDispatch(d, &m);
|
|
94 |
LeaveMutex();
|
|
95 |
}else{
|
|
96 |
printf("canReceive returned error\n");
|
|
97 |
break;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
/***************************************************************************/
|
|
103 |
UNS8 canSend(CAN_HANDLE fd0, Message *m)
|
|
104 |
{
|
|
105 |
int res;
|
|
106 |
struct canmsg_t canmsg;
|
|
107 |
|
|
108 |
|
|
109 |
canmsg.flags = 0;
|
|
110 |
canmsg.id = m->cob_id.w;
|
|
111 |
canmsg.length = m->len;
|
|
112 |
if(m->rtr){
|
|
113 |
canmsg.flags |= MSG_RTR;
|
|
114 |
}else{
|
|
115 |
memcpy(canmsg.data,m->data,8);
|
|
116 |
}
|
|
117 |
|
|
118 |
if(canmsg.id >= 0x800){
|
|
119 |
canmsg.flags |= MSG_EXT;
|
|
120 |
}
|
|
121 |
|
|
122 |
res = write(fd0->fd,&canmsg,sizeof(canmsg_t));
|
|
123 |
if(res!=sizeof(canmsg_t))
|
|
124 |
return 1;
|
|
125 |
|
|
126 |
return 0;
|
|
127 |
}
|
|
128 |
|
|
129 |
/***************************************************************************/
|
|
130 |
static const char lnx_can_dev_prefix[] = "/dev/can";
|
|
131 |
|
|
132 |
CAN_HANDLE canOpen(s_BOARD *board)
|
|
133 |
{
|
|
134 |
int name_len = strlen(board->busname);
|
|
135 |
int prefix_len = strlen(lnx_can_dev_prefix);
|
|
136 |
char dev_name[prefix_len+name_len+1];
|
|
137 |
int o_flags = 0;
|
|
138 |
CAN_HANDLE fd0;
|
|
139 |
|
|
140 |
fd0=malloc(sizeof(*fd0));
|
|
141 |
if(fd0==NULL)
|
|
142 |
return NULL;
|
|
143 |
|
|
144 |
/*o_flags = O_NONBLOCK;*/
|
|
145 |
|
|
146 |
memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
|
|
147 |
memcpy(dev_name+prefix_len,board->busname,name_len);
|
|
148 |
dev_name[prefix_len+name_len] = 0;
|
|
149 |
|
|
150 |
fd0->fd = open(dev_name, O_RDWR|o_flags);
|
|
151 |
if(fd0->fd < 0){
|
|
152 |
fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname);
|
|
153 |
goto error_ret;
|
|
154 |
}
|
|
155 |
|
|
156 |
fd0->d = board->d;
|
|
157 |
fd0->used = 1;
|
|
158 |
CreateReceiveTask(fd0, &fd0->receiveTask);
|
|
159 |
return fd0;
|
|
160 |
|
|
161 |
error_ret:
|
|
162 |
free(fd0);
|
|
163 |
return NULL;
|
|
164 |
}
|
|
165 |
|
|
166 |
/***************************************************************************/
|
|
167 |
int canClose(CAN_HANDLE fd0)
|
|
168 |
{
|
|
169 |
if(!fd0)
|
|
170 |
return 0;
|
|
171 |
fd0->used = 0;
|
|
172 |
WaitReceiveTaskEnd(&fd0->receiveTask);
|
|
173 |
close(fd0->fd);
|
|
174 |
free(fd0);
|
|
175 |
return 0;
|
|
176 |
}
|