drivers/can_multi_peeakwin32/can_multi_peak_win32.c
author Edouard Tisserant
Wed, 06 Mar 2013 11:32:27 +0900
changeset 771 4c74be252095
parent 760 71623bbf8093
permissions -rw-r--r--
Now can_tcp_win32 default to 127.0.0.1 and can_tcp_win32_server is verbose
695
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     1
/*
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     2
  This file is part of CanFestival, a library implementing CanOpen Stack. 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     3
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     4
  Copyright (C): Jaroslav Fojtik
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     5
*/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     6
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     7
#if defined(WIN32) && !defined(__CYGWIN__)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     8
#define usleep(micro) Sleep(micro%1000 ? (micro/1000) + 1 : (micro/1000))
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
     9
#else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    10
#include <stdio.h>
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    11
#include <string.h>
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    12
#include <errno.h>
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    13
#include <fcntl.h>
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    14
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    15
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    16
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    17
#include "cancfg.h"
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    18
#include "can_driver.h"
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    19
#include "def.h"
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    20
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    21
UNS8 LIBAPI canSend_driver(CAN_HANDLE fd0, Message const *m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    22
760
71623bbf8093 Fixed compilability with Micrsoft visual C++
fojtik <>
parents: 758
diff changeset
    23
#define Data data  /* temporary fix */
71623bbf8093 Fixed compilability with Micrsoft visual C++
fojtik <>
parents: 758
diff changeset
    24
695
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    25
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    26
#define VERSION_2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    27
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    28
/* dummy implementation for older version. */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    29
#ifndef VERSION_2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    30
void CAN_SetRcvEvent(HANDLE hEventx)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    31
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    32
  SetEvent(hEventx);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    33
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    34
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    35
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    36
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    37
#define SLAVE_COUNT 10
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    38
#define QueueSize 100
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    39
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    40
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    41
#ifndef extra_PCAN_init_params
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    42
    #define extra_PCAN_init_params  /**/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    43
#else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    44
    long int print_getenv(const char* pcanparam)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    45
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    46
        char* param=NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    47
        long int res=0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    48
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    49
        param = getenv(pcanparam);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    50
        if(param != NULL){
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    51
            res = strtol(param,NULL,0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    52
        }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    53
        else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    54
            printf("Environment variable %s not defined !\n", pcanparam);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    55
        printf("Found environment variable %s : %ld\n", pcanparam ,res);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    56
        return res;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    57
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    58
    #define extra_PCAN_init_params\
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    59
        ,print_getenv("PCANHwType")\
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    60
        ,print_getenv("PCANIO_Port")\
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    61
        ,print_getenv("PCANInterupt")
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    62
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    63
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    64
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    65
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    66
typedef struct 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    67
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    68
  s_BOARD *board;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    69
  Message MQueue[QueueSize];
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    70
  unsigned QStart, QEnd;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    71
  HANDLE hEventx;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    72
} QueueRecord;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    73
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    74
int initialisedQ = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    75
QueueRecord Q_DATA[10];
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    76
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    77
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    78
/** Store message into a queue. */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    79
static void PushMsgToQueue(QueueRecord *QR, Message *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    80
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    81
  if(QR==NULL || m==NULL) return;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    82
  if(QR->board==NULL) return;							// No Board assigned yet
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    83
  memcpy(&QR->MQueue[QR->QStart], m, sizeof(Message));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    84
  QR->QStart = (QR->QStart + 1) % QueueSize;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    85
  if(QR->QEnd==QR->QStart) QR->QEnd = (QR->QEnd+1) % QueueSize;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    86
  if(QR->hEventx) SetEvent(QR->hEventx);					// Signalise internal flag
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    87
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    88
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    89
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    90
/** Get message from a queue. */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    91
static int PopMsgFromQueue(QueueRecord *QR, Message *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    92
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    93
  if(QR==NULL || m==NULL) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    94
  if(QR->QEnd == QR->QStart) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    95
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    96
  memcpy(m, &QR->MQueue[QR->QEnd], sizeof(Message));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    97
  QR->QEnd = (QR->QEnd+1) % QueueSize;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    98
  return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    99
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   100
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   101
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   102
/** Create the Event for the first board */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   103
HANDLE hEvent1 = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   104
CRITICAL_SECTION InitLock;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   105
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   106
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   107
// Define for rtr CAN message
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   108
#define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   109
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   110
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   111
/***************************************************************************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   112
static int TranslateBaudeRate(char* optarg)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   113
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   114
  if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   115
  if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   116
  if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   117
  if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   118
  if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   119
  if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   120
  if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   121
  if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   122
  if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   123
  if(!strcmp( optarg, "none")) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   124
  return 0x0000;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   125
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   126
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   127
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   128
static UNS8 canInit(s_BOARD *board)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   129
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   130
int baudrate;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   131
int ret = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   132
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   133
  if(hEvent1==NULL)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   134
  {	//Create the Event for the first board
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   135
    hEvent1 = CreateEvent(NULL, // lpEventAttributes
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   136
                        FALSE,  // bManualReset
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   137
                        FALSE,  // bInitialState
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   138
                        "");    // lpName
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   139
    InitializeCriticalSection(&InitLock);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   140
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   141
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   142
  EnterCriticalSection(&InitLock);  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   143
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   144
  if(baudrate = TranslateBaudeRate(board->baudrate))
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   145
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   146
    ret = CAN_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   147
    if(ret != CAN_ERR_OK) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   148
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   149
      LeaveCriticalSection(&InitLock);      
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   150
      return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   151
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   152
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   153
        
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   154
  CAN_SetRcvEvent(hEvent1);  //Set Event Handler for CANReadExt
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   155
  LeaveCriticalSection(&InitLock);    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   156
  return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   157
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   158
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   159
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   160
/********* functions which permit to communicate with the board ****************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   161
UNS8 LIBAPI canReceive_driver(CAN_HANDLE fd0, Message *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   162
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   163
static int HeavyCounter = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   164
int ret=0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   165
UNS8 data;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   166
TPCANMsg peakMsg;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   167
DWORD Res;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   168
DWORD result;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   169
HANDLE hh[2];
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   170
int i;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   171
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   172
#ifdef CAN_READ_EX
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   173
    TPCANTimestamp peakRcvTime;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   174
#endif      
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   175
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   176
  i = strtol(((s_BOARD *)fd0)->busname,NULL,0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   177
  if(i>=SLAVE_COUNT || i<0) return 1;           // error
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   178
  if(Q_DATA[i].board!=(s_BOARD *)fd0) return 1;      // error
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   179
  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   180
  hh[0]=hEvent1; hh[1]=Q_DATA[i].hEventx;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   181
  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   182
	// loop until valid message or fatal error
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   183
  do
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   184
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   185
CONTINUE:
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   186
     if(PopMsgFromQueue(&Q_DATA[i],m)) return 0;	//message is waiting in the internal queue
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   187
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   188
        // We read the queue looking for messages.
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   189
#ifdef VERSION_2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   190
     result = WaitForMultipleObjects(2,hh,FALSE,15);  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   191
     if(Q_DATA[i].board==NULL) return 1;		//exit hook, exit immediatelly when device is closed
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   192
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   193
     if(result == WAIT_OBJECT_0+1)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   194
	 goto CONTINUE;	//look to a PopMsgFromQueue() (continue will check while(), goto skips it)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   195
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   196
     if(result==WAIT_OBJECT_0 || result==WAIT_TIMEOUT)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   197
     {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   198
#endif     
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   199
#ifdef CAN_READ_EX
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   200
       Res = CAN_ReadEx(&peakMsg, &peakRcvTime);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   201
#else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   202
       Res = CAN_Read(&peakMsg);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   203
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   204
                // Exit receive thread when handle is no more valid
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   205
#ifdef CAN_ERRMASK_ILLHANDLE
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   206
       if(Res & CAN_ERRMASK_ILLHANDLE) return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   207
#else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   208
       if(Res & CAN_ERR_ILLHANDLE) return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   209
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   210
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   211
#ifndef VERSION_2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   212
       if(Res != CAN_ERR_OK) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   213
             result = WaitForSingleObject(hEvent1, 1);   //pooling for pcan release<2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   214
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   215
	if(Res==CAN_ERR_QRCVEMPTY) goto CONTINUE;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   216
#ifdef VERSION_2       
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   217
     }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   218
     else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   219
     {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   220
       //if(result==WAIT_TIMEOUT || result==(WAIT_OBJECT_0+1)) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   221
       //     Res = CAN_ERR_BUSLIGHT;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   222
       //else 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   223
              Res = CAN_ERR_UNKNOWN;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   224
     }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   225
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   226
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   227
    if(Res==CAN_ERR_BUSHEAVY)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   228
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   229
      if(HeavyCounter++>10) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   230
      {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   231
	HeavyCounter = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   232
	Res=CAN_ERR_BUSOFF;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   233
      }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   234
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   235
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   236
    if(Res & CAN_ERR_BUSOFF)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   237
    {      
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   238
      peakMsg.MSGTYPE = MSGTYPE_STATUS;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   239
      peakMsg.DATA[3] = CAN_ERR_BUSOFF;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   240
      Res = CAN_ERR_OK;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   241
    }   
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   242
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   243
        // A message was received : we process the message(s)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   244
    if(Res == CAN_ERR_OK)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   245
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   246
            // if something different that 11bit or rtr... problem      
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   247
	switch(peakMsg.MSGTYPE)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   248
	{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   249
	  case MSGTYPE_STATUS:
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   250
		  switch(peakMsg.DATA[3])
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   251
	          {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   252
		    case CAN_ERR_BUSHEAVY: 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   253
				      break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   254
	            case CAN_ERR_BUSOFF: 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   255
			              printf ("Peak board read BUSOFF: re-init!!!\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   256
				      canInit((s_BOARD*)fd0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   257
				      usleep(2000);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   258
				      break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   259
		  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   260
		  return peakMsg.DATA[3];	/* if something different that 11bit or rtr... problem */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   261
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   262
	  case MSGTYPE_STANDARD:		/* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   263
	  case MSGTYPE_EXTENDED:
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   264
			  m->rtr = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   265
			  break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   266
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   267
	  case MSGTYPE_RTR:			/* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   268
			  m->rtr = 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   269
			  break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   270
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   271
	  default: return CAN_ERR_OVERRUN;	/* If status, return status if 29bit, return overrun. */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   272
		    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   273
	}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   274
   
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   275
      m->cob_id = peakMsg.ID;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   276
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   277
      if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)  /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   278
                m->rtr = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   279
      else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   280
                m->rtr = 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   281
      m->len = peakMsg.LEN; /* count of data bytes (0..8) */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   282
      for(data=0; data<peakMsg.LEN; data++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   283
                m->Data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   284
#if defined DEBUG_MSG_CONSOLE_ON
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   285
      MSG("in : ");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   286
      print_message(m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   287
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   288
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   289
    else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   290
    {       // not benign error => fatal error
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   291
      if(!(Res & CAN_ERR_QRCVEMPTY
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   292
                    || Res & CAN_ERR_BUSLIGHT
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   293
                    || Res & CAN_ERR_BUSHEAVY))
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   294
      {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   295
        printf ("canReceive returned error (%d)\n", Res);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   296
        return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   297
      }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   298
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   299
  } while(Res != CAN_ERR_OK);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   300
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   301
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   302
     // populate message received to other drivers
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   303
  for(i=0; i<SLAVE_COUNT; i++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   304
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   305
    if(Q_DATA[i].board != (s_BOARD *)fd0)	// do not populate to own queue
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   306
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   307
      PushMsgToQueue(&Q_DATA[i],m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   308
    }   
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   309
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   310
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   311
  return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   312
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   313
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   314
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   315
/***************************************************************************/
758
9234361a4678 Added comments
fojtik <>
parents: 695
diff changeset
   316
9234361a4678 Added comments
fojtik <>
parents: 695
diff changeset
   317
/* return 0 - OK; 1 - failure */
695
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   318
UNS8 LIBAPI canSend_driver(CAN_HANDLE fd0, Message const *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   319
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   320
UNS8 data;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   321
TPCANMsg peakMsg;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   322
int i, j;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   323
int loc_errno;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   324
int MaxLoops = 100;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   325
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   326
  i = -1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   327
  for(j=0; j<SLAVE_COUNT; j++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   328
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   329
    if(Q_DATA[j].board != (s_BOARD *)fd0)   // store this message forr all other drivers
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   330
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   331
      PushMsgToQueue(&Q_DATA[j],m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   332
      i = j;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   333
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   334
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   335
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   336
  if(i<0) return 1;                         // no board found
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   337
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   338
  peakMsg.ID = m->cob_id;                   /* 11/29 bit code */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   339
  if(m->rtr == 0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   340
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   341
    if(peakMsg.ID > 0x7FF)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   342
	peakMsg.MSGTYPE = MSGTYPE_EXTENDED; /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   343
    else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   344
	peakMsg.MSGTYPE = MSGTYPE_STANDARD; /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   345
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   346
  else	
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   347
    peakMsg.MSGTYPE = MSGTYPE_RTR;	    /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   348
  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   349
  peakMsg.LEN = m->len;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   350
    /* count of data bytes (0..8) */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   351
  for(data = 0; data < m->len; data++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   352
        peakMsg.DATA[data] = m->Data[data]; /* data bytes, up to 8 */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   353
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   354
  do 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   355
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   356
    errno = loc_errno = CAN_Write(&peakMsg);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   357
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   358
    if(loc_errno)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   359
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   360
      if(loc_errno==CAN_ERR_BUSOFF && (MaxLoops%20)==1)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   361
      {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   362
#if defined DEBUG_MSG_CONSOLE_ON
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   363
        printf ("Peak board write: re-init!!!\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   364
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   365
        canInit((s_BOARD*)fd0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   366
        usleep(1000);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   367
      }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   368
      usleep(80);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   369
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   370
    if(MaxLoops-- == 0) break;			// limit max looping
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   371
  } while(loc_errno != CAN_ERR_OK);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   372
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   373
#if defined DEBUG_MSG_CONSOLE_ON
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   374
    MSG("out : ");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   375
    print_message(m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   376
#endif
758
9234361a4678 Added comments
fojtik <>
parents: 695
diff changeset
   377
    return 0;					// OK
695
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   378
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   379
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   380
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   381
/***************************************************************************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   382
UNS8 LIBAPI canChangeBaudRate_driver(CAN_HANDLE fd, char* baud)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   383
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   384
    printf("canChangeBaudRate not yet supported by this driver\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   385
    return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   386
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   387
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   388
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   389
/***************************************************************************/
758
9234361a4678 Added comments
fojtik <>
parents: 695
diff changeset
   390
CAN_HANDLE LIBAPI canOpen_driver(s_BOARD * board)
695
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   391
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   392
int ret;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   393
int i;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   394
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   395
  if(!initialisedQ)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   396
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   397
    memset(Q_DATA,0,sizeof(Q_DATA));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   398
    initialisedQ = 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   399
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   400
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   401
  i = strtol(board->busname,NULL,0);			// Get slot name
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   402
  //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   403
  if(i<SLAVE_COUNT && i>=0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   404
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   405
    Q_DATA[i].board = board;    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   406
      //printf ("First Board selected\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   407
    if(Q_DATA[i].hEventx==NULL)				// Create local event
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   408
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   409
      Q_DATA[i].hEventx = CreateEvent(NULL, FALSE, FALSE, "");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   410
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   411
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   412
    if(hEvent1!=NULL) return (CAN_HANDLE)board;		// Create global event, if needed
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   413
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   414
    ret = canInit(board);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   415
    if(ret)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   416
      return (CAN_HANDLE)board;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   417
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   418
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   419
  return NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   420
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   421
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   422
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   423
/***************************************************************************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   424
int LIBAPI canClose_driver(CAN_HANDLE fd0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   425
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   426
s_BOARD *x_board = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   427
int ActiveBoards = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   428
int i;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   429
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   430
  if((s_BOARD *)fd0==NULL) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   431
  for(i=0; i<SLAVE_COUNT; i++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   432
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   433
    if(Q_DATA[i].board == (s_BOARD *)fd0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   434
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   435
      x_board = Q_DATA[i].board;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   436
      Q_DATA[i].board = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   437
      CloseHandle(Q_DATA[i].hEventx);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   438
      Q_DATA[i].hEventx = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   439
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   440
    else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   441
      ActiveBoards++;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   442
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   443
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   444
  if(ActiveBoards<=0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   445
  {				// No can device is used.
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   446
    CAN_SetRcvEvent(NULL);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   447
    CAN_Close();    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   448
    if(hEvent1) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   449
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   450
      SetEvent(hEvent1);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   451
      CloseHandle(hEvent1);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   452
      hEvent1 = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   453
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   454
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   455
  else    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   456
    SetEvent(hEvent1);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   457
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   458
  return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   459
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   460
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   461
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   462
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   463
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   464
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   465