]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - drivers/can_peak_linux/can_peak_linux.c
a4b7d2ef74630a9bb971f5179f465d561fe69a0d
[CanFestival-3.git] / drivers / can_peak_linux / can_peak_linux.c
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("canReceive_driver (Peak_Linux) : error of reading.\n");
48     }
49     return 1;
50   }
51   m->cob_id = 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 #if defined DEBUG_MSG_CONSOLE_ON
61   printf("in : ");
62   print_message(m);
63 #endif
64   
65   return 0;
66 }
67
68 /***************************************************************************/
69 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
70 {
71   UNS8 data;
72   TPCANMsg peakMsg;
73   peakMsg.ID=m -> cob_id;                               /* 11/29 bit code */
74   if(m->rtr == 0)       
75     peakMsg.MSGTYPE = CAN_INIT_TYPE_ST;       /* bits of MSGTYPE_*/
76   else {
77     peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR;       /* bits of MSGTYPE_*/
78   }
79   peakMsg.LEN = m->len;   
80                                 /* count of data bytes (0..8) */
81   for(data = 0 ; data <  m->len; data ++)
82         peakMsg.DATA[data] = m->data[data];             /* data bytes, up to 8 */
83   
84 #if defined DEBUG_MSG_CONSOLE_ON
85   printf("out : ");
86   print_message(m);
87 #endif
88   if((errno = CAN_Write(fd0, & peakMsg))) {
89     perror("canSend_driver (Peak_Linux) : error of writing.\n");
90     return 1;
91   }
92   return 0;
93
94 }
95
96
97 /***************************************************************************/
98 int TranslateBaudeRate(char* optarg){
99         if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
100         if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
101         if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
102         if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
103         if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
104         if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
105         if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
106         if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
107         if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
108         if(!strcmp( optarg, "none")) return 0;
109         return 0x0000;
110 }
111
112 UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
113 {
114         printf("canChangeBaudRate not yet supported by this driver\n");
115         return 0;
116 }
117
118 /***************************************************************************/
119 CAN_HANDLE canOpen_driver(s_BOARD *board)
120 {
121   HANDLE fd0 = NULL;
122   char busname[64];
123   char* pEnd;
124   int i;  
125   int baudrate;
126   
127   if(strtol(board->busname, &pEnd,0) >= 0)
128   {
129     sprintf(busname,"/dev/pcan%s",board->busname);
130     fd0 = LINUX_CAN_Open(busname, O_RDWR);
131   }
132
133   if(fd0 && (baudrate = TranslateBaudeRate(board->baudrate)))
134   {
135         CAN_Init(fd0, baudrate, CAN_INIT_TYPE_ST);
136   }else{
137         fprintf(stderr, "canOpen_driver (Peak_Linux) : error opening %s\n", busname);
138   }
139
140    return (CAN_HANDLE)fd0;
141 }
142
143 /***************************************************************************/
144 int canClose_driver(CAN_HANDLE fd0)
145 {
146   CAN_Close(fd0);
147   return 0;
148 }