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