etisserant@38: /* etisserant@38: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@38: etisserant@38: Copyright (C): Edouard TISSERANT and Francis DUPIN etisserant@38: etisserant@38: See COPYING file for copyrights details. etisserant@38: etisserant@38: This library is free software; you can redistribute it and/or etisserant@38: modify it under the terms of the GNU Lesser General Public etisserant@38: License as published by the Free Software Foundation; either etisserant@38: version 2.1 of the License, or (at your option) any later version. etisserant@38: etisserant@38: This library is distributed in the hope that it will be useful, etisserant@38: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@38: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@38: Lesser General Public License for more details. etisserant@38: etisserant@38: You should have received a copy of the GNU Lesser General Public etisserant@38: License along with this library; if not, write to the Free Software etisserant@38: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@38: */ etisserant@38: etisserant@38: #include etisserant@38: #include etisserant@38: #include etisserant@38: #include etisserant@38: #include /* for NULL */ etisserant@38: #include etisserant@38: #include etisserant@38: #include etisserant@38: #include etisserant@38: #include etisserant@38: etisserant@38: /* driver pcan pci for Peak board */ etisserant@38: //#include "libpcan.h" etisserant@38: //#include "pcan.h" etisserant@38: etisserant@38: #include etisserant@38: #include "timer.h" etisserant@38: #include "can_driver.h" etisserant@38: #include "timers_driver.h" etisserant@38: etisserant@38: #define MAX_NB_CAN_PORTS 1 etisserant@38: etisserant@38: typedef struct { etisserant@38: char used; etisserant@38: TASK_HANDLE receiveTask; etisserant@38: CO_Data* d; etisserant@38: } CANPort; etisserant@38: etisserant@38: CANPort canports[MAX_NB_CAN_PORTS] = {{0,},}; etisserant@38: etisserant@38: // Define for rtr CAN message etisserant@38: #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR etisserant@38: etisserant@38: /*********functions which permit to communicate with the board****************/ etisserant@38: UNS8 canReceive(CAN_HANDLE fd0, Message *m) etisserant@38: { etisserant@38: UNS8 data; etisserant@38: TPCANMsg peakMsg; etisserant@38: if ((errno = CAN_Read(& peakMsg))) { // Blocks until no new message or error. etisserant@38: perror("!!! Peak board : error of reading. (from f_can_receive function) \n"); etisserant@38: return 1; etisserant@38: } etisserant@38: m->cob_id.w = peakMsg.ID; etisserant@38: if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_*/ etisserant@38: m->rtr = 0; etisserant@38: else etisserant@38: m->rtr = 1; etisserant@38: m->len = peakMsg.LEN; /* count of data bytes (0..8) */ etisserant@38: for(data = 0 ; data < peakMsg.LEN ; data++) etisserant@38: m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ etisserant@38: etisserant@38: return 0; etisserant@38: } etisserant@38: etisserant@38: void canReceiveLoop(CAN_HANDLE fd0) etisserant@38: { etisserant@38: CO_Data* d = ((CANPort*)fd0)->d; etisserant@38: Message m; etisserant@38: while (1) { etisserant@38: if(!canReceive(fd0, &m)) etisserant@38: { etisserant@38: EnterMutex(); etisserant@38: canDispatch(d, &m); etisserant@38: LeaveMutex(); etisserant@38: }else{ etisserant@38: // printf("canReceive returned error\n"); etisserant@38: break; etisserant@38: } etisserant@38: } etisserant@38: } etisserant@38: etisserant@38: /***************************************************************************/ etisserant@38: UNS8 canSend(CAN_HANDLE fd0, Message *m) etisserant@38: { etisserant@38: UNS8 data; etisserant@38: TPCANMsg peakMsg; etisserant@38: peakMsg.ID=m -> cob_id.w; /* 11/29 bit code */ etisserant@38: if(m->rtr == 0) etisserant@38: peakMsg.MSGTYPE = CAN_INIT_TYPE_ST; /* bits of MSGTYPE_*/ etisserant@38: else { etisserant@38: peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR; /* bits of MSGTYPE_*/ etisserant@38: } etisserant@38: peakMsg.LEN = m->len; etisserant@38: /* count of data bytes (0..8) */ etisserant@38: for(data = 0 ; data < m->len; data ++) etisserant@38: peakMsg.DATA[data] = m->data[data]; /* data bytes, up to 8 */ etisserant@38: etisserant@38: if((errno = CAN_Write(& peakMsg))) { etisserant@38: perror("!!! Peak board : error of writing. (from canSend function) \n"); etisserant@38: return 1; etisserant@38: } etisserant@38: return 0; etisserant@38: etisserant@38: } etisserant@38: etisserant@38: /***************************************************************************/ etisserant@38: CAN_HANDLE canOpen(s_BOARD *board) etisserant@38: { etisserant@38: HANDLE fd0 = NULL; etisserant@38: char busname[64]; etisserant@38: char* pEnd; etisserant@38: int i; etisserant@38: etisserant@38: for(i=0; i < MAX_NB_CAN_PORTS; i++) etisserant@38: { etisserant@38: if(!canports[i].used) etisserant@38: break; etisserant@38: } etisserant@38: if(canports[i].used) etisserant@38: { etisserant@38: perror("can_peak_win32.c: no more can port available with this pcan library\n"); etisserant@38: perror("can_peak_win32.c: please link another executable with another pcan lib\n"); etisserant@38: return NULL; etisserant@38: } etisserant@38: // if(strtol(board->busname, &pEnd,0) >= 0) etisserant@38: // { etisserant@38: // sprintf(busname,"/dev/pcan%s",board->busname); etisserant@38: // fd0 = LINUX_CAN_Open(busname, O_RDWR); etisserant@38: // } etisserant@38: etisserant@38: if (i==MAX_NB_CAN_PORTS || fd0 == NULL) etisserant@38: { etisserant@38: fprintf (stderr, "Open failed.\n"); etisserant@38: return (CAN_HANDLE)NULL; etisserant@38: } etisserant@38: etisserant@38: CAN_Init(board->baudrate, CAN_INIT_TYPE_ST); etisserant@38: etisserant@38: canports[i].used = 1; etisserant@38: etisserant@38: canports[i].d = board->d; etisserant@38: CreateReceiveTask((CANPort*) &canports[i], &canports[i].receiveTask); etisserant@38: etisserant@38: return (CANPort*) &canports[i]; etisserant@38: } etisserant@38: etisserant@38: /***************************************************************************/ etisserant@38: int canClose(CAN_HANDLE fd0) etisserant@38: { etisserant@38: CAN_Close(); etisserant@38: ((CANPort*)fd0)->used = 0; etisserant@38: WaitReceiveTaskEnd(&((CANPort*)fd0)->receiveTask); etisserant@38: return 0; etisserant@38: }