00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <errno.h>
00026 #include <fcntl.h>
00027
00028
00029
00030
00031
00032 #include "libpcan.h"
00033
00034 #include "can_driver.h"
00035
00036
00037 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR
00038
00039
00040 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
00041 {
00042 UNS8 data;
00043 TPCANMsg peakMsg;
00044 if ((errno = CAN_Read(fd0, & peakMsg))) {
00045 if(errno != -EIDRM && errno != -EPERM)
00046 {
00047 perror("canReceive_driver (Peak_Linux) : error of reading.\n");
00048 }
00049 return 1;
00050 }
00051 m->cob_id.w = peakMsg.ID;
00052 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)
00053 m->rtr = 0;
00054 else
00055 m->rtr = 1;
00056 m->len = peakMsg.LEN;
00057 for(data = 0 ; data < peakMsg.LEN ; data++)
00058 m->data[data] = peakMsg.DATA[data];
00059
00060 return 0;
00061 }
00062
00063
00064 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
00065 {
00066 UNS8 data;
00067 TPCANMsg peakMsg;
00068 peakMsg.ID=m -> cob_id.w;
00069 if(m->rtr == 0)
00070 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST;
00071 else {
00072 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR;
00073 }
00074 peakMsg.LEN = m->len;
00075
00076 for(data = 0 ; data < m->len; data ++)
00077 peakMsg.DATA[data] = m->data[data];
00078
00079 if((errno = CAN_Write(fd0, & peakMsg))) {
00080 perror("canSend_driver (Peak_Linux) : error of writing.\n");
00081 return 1;
00082 }
00083 return 0;
00084
00085 }
00086
00087
00088
00089 int TranslateBaudeRate(char* optarg){
00090 if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
00091 if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
00092 if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
00093 if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
00094 if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
00095 if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
00096 if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
00097 if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
00098 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
00099 if(!strcmp( optarg, "none")) return 0;
00100 return 0x0000;
00101 }
00102
00103
00104 CAN_HANDLE canOpen_driver(s_BOARD *board)
00105 {
00106 HANDLE fd0 = NULL;
00107 char busname[64];
00108 char* pEnd;
00109 int i;
00110 int baudrate;
00111
00112 if(strtol(board->busname, &pEnd,0) >= 0)
00113 {
00114 sprintf(busname,"/dev/pcan%s",board->busname);
00115 fd0 = LINUX_CAN_Open(busname, O_RDWR);
00116 }
00117
00118 if(fd0 && (baudrate = TranslateBaudeRate(board->baudrate)))
00119 {
00120 CAN_Init(fd0, baudrate, CAN_INIT_TYPE_ST);
00121 }else{
00122 fprintf(stderr, "canOpen_driver (Peak_Linux) : error opening %s\n", busname);
00123 }
00124
00125 return (CAN_HANDLE)fd0;
00126 }
00127
00128
00129 int canClose_driver(CAN_HANDLE fd0)
00130 {
00131 CAN_Close(fd0);
00132 return 0;
00133 }