2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
6 See COPYING file for copyrights details.
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.
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.
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
27 #include <stddef.h> /* for NULL */
28 #include <sys/ioctl.h>
34 /* driver pcan pci for Peak board */
35 //#include "libpcan.h"
40 #include "can_driver.h"
41 #include "timers_driver.h"
43 #define MAX_NB_CAN_PORTS 1
47 TASK_HANDLE receiveTask;
51 CANPort canports[MAX_NB_CAN_PORTS] = {{0,},};
53 // Define for rtr CAN message
54 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR
56 /*********functions which permit to communicate with the board****************/
57 UNS8 canReceive(CAN_HANDLE fd0, Message *m)
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");
65 m->cob_id.w = peakMsg.ID;
66 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_*/
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 */
77 void canReceiveLoop(CAN_HANDLE fd0)
79 CO_Data* d = ((CANPort*)fd0)->d;
82 if(!canReceive(fd0, &m))
88 // printf("canReceive returned error\n");
94 /***************************************************************************/
95 UNS8 canSend(CAN_HANDLE fd0, Message *m)
99 peakMsg.ID=m -> cob_id.w; /* 11/29 bit code */
101 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST; /* bits of MSGTYPE_*/
103 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR; /* bits of MSGTYPE_*/
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 */
110 if((errno = CAN_Write(& peakMsg))) {
111 perror("!!! Peak board : error of writing. (from canSend function) \n");
118 /***************************************************************************/
119 CAN_HANDLE canOpen(s_BOARD *board)
126 for(i=0; i < MAX_NB_CAN_PORTS; i++)
128 if(!canports[i].used)
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");
137 // if(strtol(board->busname, &pEnd,0) >= 0)
139 // sprintf(busname,"/dev/pcan%s",board->busname);
140 // fd0 = LINUX_CAN_Open(busname, O_RDWR);
143 if (i==MAX_NB_CAN_PORTS || fd0 == NULL)
145 fprintf (stderr, "Open failed.\n");
146 return (CAN_HANDLE)NULL;
149 CAN_Init(board->baudrate, CAN_INIT_TYPE_ST);
151 canports[i].used = 1;
153 canports[i].d = board->d;
154 CreateReceiveTask((CANPort*) &canports[i], &canports[i].receiveTask);
156 return (CANPort*) &canports[i];
159 /***************************************************************************/
160 int canClose(CAN_HANDLE fd0)
163 ((CANPort*)fd0)->used = 0;
164 WaitReceiveTaskEnd(&((CANPort*)fd0)->receiveTask);