diff -r 9d793667504e -r 394440e3b6e4 drivers/can_peak_win32/can_peak_win32.c --- a/drivers/can_peak_win32/can_peak_win32.c Thu Jul 16 10:44:32 2009 +0200 +++ b/drivers/can_peak_win32/can_peak_win32.c Thu Jul 16 11:53:07 2009 +0200 @@ -1,5 +1,5 @@ /* -This file is part of CanFestival, a library implementing CanOpen Stack. +This file is part of CanFestival, a library implementing CanOpen Stack. Copyright (C): Edouard TISSERANT and Francis DUPIN @@ -42,11 +42,15 @@ #endif static s_BOARD *first_board = NULL; -#ifdef PCAN2_HEADER_ -static s_BOARD *second_board = NULL; -#endif - -//pthread_mutex_t PeakCan_mutex = PTHREAD_MUTEX_INITIALIZER; + +//Create the Event for the first board +HANDLE hEvent1 = NULL; + + +#ifdef PCAN2_HEADER_ + static s_BOARD *second_board = NULL; + HANDLE hEvent2 = NULL; +#endif // Define for rtr CAN message #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR @@ -66,98 +70,119 @@ return 0x0000; } -void -canInit (s_BOARD *board) +void canInit (s_BOARD *board) { int baudrate; - + #ifdef PCAN2_HEADER_ // if not the first handler - if(second_board == (s_BOARD *)board) + if(second_board == (s_BOARD *)board) { if(baudrate = TranslateBaudeRate(board->baudrate)) CAN2_Init (baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params); -#endif - if(first_board == (s_BOARD *)board) + + //Create the Event for the first board + hEvent2 = CreateEvent(NULL, // lpEventAttributes + FALSE, // bManualReset + FALSE, // bInitialState + ""); // lpName + + //Set Event Handle for CANReadExt + CAN2_SetRcvEvent(hEvent2); + } + else +#endif + if(first_board == (s_BOARD *)board) { if(baudrate = TranslateBaudeRate(board->baudrate)) CAN_Init (baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params); -} - -/*********functions which permit to communicate with the board****************/ -UNS8 -canReceive_driver (CAN_HANDLE fd0, Message * m) -{ + + //Create the Event for the first board + hEvent1 = CreateEvent(NULL, // lpEventAttributes + FALSE, // bManualReset + FALSE, // bInitialState + ""); // lpName + //Set Event Handle for CANReadExt + CAN_SetRcvEvent(hEvent1); + } +} + +/********* functions which permit to communicate with the board ****************/ +UNS8 canReceive_driver (CAN_HANDLE fd0, Message * m) +{ + int ret=0; UNS8 data; TPCANMsg peakMsg; + TPCANTimestamp peakRcvTime; DWORD Res; - do{ - // We read the queue looking for messages. - // - //pthread_mutex_lock (&PeakCan_mutex); -#ifdef PCAN2_HEADER_ - // if not the first handler - if(second_board == (s_BOARD *)fd0) - Res = CAN2_Read (&peakMsg); + DWORD result; + +#ifdef PCAN2_HEADER_ + // if not the first handler + if(second_board == (s_BOARD *)fd0) { + //wait for CAN msg... + result = WaitForSingleObject(hEvent2, INFINITE); + if (result == WAIT_OBJECT_0) + Res = CAN2_ReadEx(&peakMsg, &peakRcvTime); + } + else +#endif + + // We read the queue looking for messages. + if(first_board == (s_BOARD *)fd0) { + result = WaitForSingleObject(hEvent1, INFINITE); + if (result == WAIT_OBJECT_0) + Res = CAN_ReadEx(&peakMsg, &peakRcvTime); + } + else + Res = CAN_ERR_BUSOFF; + + // A message was received : we process the message(s) + if (Res == CAN_ERR_OK) + { + // if something different that 11bit or rtr... problem + if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR)) + { + if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF) + { + printf ("!!! Peak board read : re-init\n"); + canInit((s_BOARD*) fd0); + usleep (10000); + } + + // If status, return status if 29bit, return overrun + return peakMsg.MSGTYPE == + MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN; + } + m->cob_id = peakMsg.ID; + + if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_ */ + m->rtr = 0; else -#endif - if(first_board == (s_BOARD *)fd0) - Res = CAN_Read (&peakMsg); - else - Res = CAN_ERR_BUSOFF; - // A message was received - // We process the message(s) - // - if (Res == CAN_ERR_OK) - { - // if something different that 11bit or rtr... problem - if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR)) - { - if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF) - { - printf ("!!! Peak board read : re-init\n"); - canInit((s_BOARD*) fd0); - usleep (10000); - } - - // If status, return status if 29bit, return overrun - //pthread_mutex_unlock (&PeakCan_mutex); - return peakMsg.MSGTYPE == - MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN; - } - m->cob_id = peakMsg.ID; - - if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_ */ - m->rtr = 0; - else - m->rtr = 1; - m->len = peakMsg.LEN; /* count of data bytes (0..8) */ - for (data = 0; data < peakMsg.LEN; data++) - m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ + m->rtr = 1; + m->len = peakMsg.LEN; /* count of data bytes (0..8) */ + for (data = 0; data < peakMsg.LEN; data++) + m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ #if defined DEBUG_MSG_CONSOLE_ON - MSG("in : "); - print_message(m); -#endif - }else{ - //pthread_mutex_unlock (&PeakCan_mutex); - //if (Res != CAN_ERR_OK) - //{ - if (! - (Res & CAN_ERR_QRCVEMPTY || Res & CAN_ERR_BUSLIGHT - || Res & CAN_ERR_BUSHEAVY)) - { - printf ("canReceive returned error (%d)\n", Res); - return 1; - } - usleep (1000); - } - }while(Res != CAN_ERR_OK); - return 0; -} - -/***************************************************************************/ -UNS8 -canSend_driver (CAN_HANDLE fd0, Message * m) + MSG("in : "); + print_message(m); +#endif + } + else + { + if (!(Res & CAN_ERR_QRCVEMPTY + || Res & CAN_ERR_BUSLIGHT + || Res & CAN_ERR_BUSHEAVY)) + { + printf ("canReceive returned error (%d)\n", Res); + return 1; + } + } + return 0; +} + +/***************************************************************************/ +UNS8 canSend_driver (CAN_HANDLE fd0, Message * m) { UNS8 data; TPCANMsg peakMsg; @@ -172,7 +197,7 @@ /* count of data bytes (0..8) */ for (data = 0; data < m->len; data++) peakMsg.DATA[data] = m->data[data]; /* data bytes, up to 8 */ - + do { #ifdef PCAN2_HEADER_ @@ -181,13 +206,13 @@ { errno = CAN2_Write (&peakMsg); } - else + else #endif if(first_board == (s_BOARD *)fd0) { errno = CAN_Write (&peakMsg); } - else + else goto fail; if (errno) { @@ -218,18 +243,17 @@ } /***************************************************************************/ -CAN_HANDLE -canOpen_driver (s_BOARD * board) +CAN_HANDLE canOpen_driver (s_BOARD * board) { char busname[64]; char* pEnd; - + //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0)); if (strtol(board->busname, &pEnd,0) == 0) { first_board = board; printf ("First Board selected\n"); - canInit(board); + canInit(board); return (CAN_HANDLE)board; } #ifdef PCAN2_HEADER_ @@ -237,7 +261,7 @@ { second_board = board; printf ("Second Board selected\n"); - canInit(board); + canInit(board); return (CAN_HANDLE)board; } #endif @@ -245,19 +269,20 @@ } /***************************************************************************/ -int -canClose_driver (CAN_HANDLE fd0) +int canClose_driver (CAN_HANDLE fd0) { #ifdef PCAN2_HEADER_ // if not the first handler if(second_board == (s_BOARD *)fd0) { + ResetEvent(hEvent2); CAN2_Close (); second_board = (s_BOARD *)NULL; - }else + }else #endif if(first_board == (s_BOARD *)fd0) { + ResetEvent(hEvent1); CAN_Close (); first_board = (s_BOARD *)NULL; }