drivers/can_multi_peeakwin32/can_multi_peak_win32.c
author Christian Taedcke <hacking@taedcke.com>
Tue, 12 Jun 2012 00:13:39 +0200
changeset 735 d3db2140d3e6
parent 695 0271f08ba819
child 758 9234361a4678
permissions -rw-r--r--
Building 4 can drivers now.
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
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    23
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    24
#define VERSION_2
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
/* dummy implementation for older version. */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    27
#ifndef VERSION_2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    28
void CAN_SetRcvEvent(HANDLE hEventx)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    29
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    30
  SetEvent(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
#endif
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
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    35
#define SLAVE_COUNT 10
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    36
#define QueueSize 100
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    37
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    38
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    39
#ifndef extra_PCAN_init_params
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    40
    #define extra_PCAN_init_params  /**/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    41
#else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    42
    long int print_getenv(const char* pcanparam)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    43
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    44
        char* param=NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    45
        long int res=0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    46
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    47
        param = getenv(pcanparam);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    48
        if(param != NULL){
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    49
            res = strtol(param,NULL,0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    50
        }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    51
        else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    52
            printf("Environment variable %s not defined !\n", pcanparam);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    53
        printf("Found environment variable %s : %ld\n", pcanparam ,res);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    54
        return res;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    55
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    56
    #define extra_PCAN_init_params\
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    57
        ,print_getenv("PCANHwType")\
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    58
        ,print_getenv("PCANIO_Port")\
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    59
        ,print_getenv("PCANInterupt")
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    60
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    61
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    62
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
typedef struct 
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
  s_BOARD *board;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    67
  Message MQueue[QueueSize];
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    68
  unsigned QStart, QEnd;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    69
  HANDLE hEventx;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    70
} QueueRecord;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    71
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    72
int initialisedQ = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    73
QueueRecord Q_DATA[10];
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    74
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    75
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    76
/** Store message into a queue. */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    77
static void PushMsgToQueue(QueueRecord *QR, Message *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    78
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    79
  if(QR==NULL || m==NULL) return;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    80
  if(QR->board==NULL) return;							// No Board assigned yet
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    81
  memcpy(&QR->MQueue[QR->QStart], m, sizeof(Message));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    82
  QR->QStart = (QR->QStart + 1) % QueueSize;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    83
  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
    84
  if(QR->hEventx) SetEvent(QR->hEventx);					// Signalise internal flag
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    85
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    86
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
/** Get message from a queue. */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    89
static int PopMsgFromQueue(QueueRecord *QR, Message *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    90
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    91
  if(QR==NULL || m==NULL) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    92
  if(QR->QEnd == QR->QStart) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    93
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    94
  memcpy(m, &QR->MQueue[QR->QEnd], sizeof(Message));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    95
  QR->QEnd = (QR->QEnd+1) % QueueSize;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    96
  return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    97
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
    98
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
/** Create the Event for the first board */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   101
HANDLE hEvent1 = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   102
CRITICAL_SECTION InitLock;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   103
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   104
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   105
// Define for rtr CAN message
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   106
#define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   107
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   108
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
static int TranslateBaudeRate(char* optarg)
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
  if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   113
  if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   114
  if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   115
  if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   116
  if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   117
  if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   118
  if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   119
  if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   120
  if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   121
  if(!strcmp( optarg, "none")) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   122
  return 0x0000;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   123
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   124
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
static UNS8 canInit(s_BOARD *board)
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
int baudrate;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   129
int ret = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   130
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   131
  if(hEvent1==NULL)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   132
  {	//Create the Event for the first board
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   133
    hEvent1 = CreateEvent(NULL, // lpEventAttributes
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   134
                        FALSE,  // bManualReset
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   135
                        FALSE,  // bInitialState
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   136
                        "");    // lpName
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   137
    InitializeCriticalSection(&InitLock);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   138
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   139
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   140
  EnterCriticalSection(&InitLock);  
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
  if(baudrate = TranslateBaudeRate(board->baudrate))
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
    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
   145
    if(ret != CAN_ERR_OK) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   146
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   147
      LeaveCriticalSection(&InitLock);      
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   148
      return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   149
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   150
  }
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
  CAN_SetRcvEvent(hEvent1);  //Set Event Handler for CANReadExt
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   153
  LeaveCriticalSection(&InitLock);    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   154
  return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   155
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   156
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
/********* functions which permit to communicate with the board ****************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   159
UNS8 LIBAPI canReceive_driver(CAN_HANDLE fd0, Message *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   160
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   161
static int HeavyCounter = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   162
int ret=0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   163
UNS8 data;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   164
TPCANMsg peakMsg;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   165
DWORD Res;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   166
DWORD result;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   167
HANDLE hh[2];
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   168
int i;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   169
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   170
#ifdef CAN_READ_EX
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   171
    TPCANTimestamp peakRcvTime;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   172
#endif      
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   173
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   174
  i = strtol(((s_BOARD *)fd0)->busname,NULL,0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   175
  if(i>=SLAVE_COUNT || i<0) return 1;           // error
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   176
  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
   177
  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   178
  hh[0]=hEvent1; hh[1]=Q_DATA[i].hEventx;
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
	// loop until valid message or fatal error
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   181
  do
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   182
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   183
CONTINUE:
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   184
     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
   185
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   186
        // We read the queue looking for messages.
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   187
#ifdef VERSION_2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   188
     result = WaitForMultipleObjects(2,hh,FALSE,15);  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   189
     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
   190
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   191
     if(result == WAIT_OBJECT_0+1)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   192
	 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
   193
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   194
     if(result==WAIT_OBJECT_0 || result==WAIT_TIMEOUT)
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
#endif     
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   197
#ifdef CAN_READ_EX
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   198
       Res = CAN_ReadEx(&peakMsg, &peakRcvTime);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   199
#else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   200
       Res = CAN_Read(&peakMsg);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   201
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   202
                // Exit receive thread when handle is no more valid
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   203
#ifdef CAN_ERRMASK_ILLHANDLE
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   204
       if(Res & CAN_ERRMASK_ILLHANDLE) return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   205
#else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   206
       if(Res & CAN_ERR_ILLHANDLE) return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   207
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   208
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   209
#ifndef VERSION_2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   210
       if(Res != CAN_ERR_OK) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   211
             result = WaitForSingleObject(hEvent1, 1);   //pooling for pcan release<2
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   212
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   213
	if(Res==CAN_ERR_QRCVEMPTY) goto CONTINUE;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   214
#ifdef VERSION_2       
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   215
     }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   216
     else
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
       //if(result==WAIT_TIMEOUT || result==(WAIT_OBJECT_0+1)) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   219
       //     Res = CAN_ERR_BUSLIGHT;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   220
       //else 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   221
              Res = CAN_ERR_UNKNOWN;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   222
     }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   223
#endif
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
    if(Res==CAN_ERR_BUSHEAVY)
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(HeavyCounter++>10) 
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
	HeavyCounter = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   230
	Res=CAN_ERR_BUSOFF;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   231
      }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   232
    }
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
    if(Res & CAN_ERR_BUSOFF)
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
      peakMsg.MSGTYPE = MSGTYPE_STATUS;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   237
      peakMsg.DATA[3] = CAN_ERR_BUSOFF;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   238
      Res = CAN_ERR_OK;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   239
    }   
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   240
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   241
        // A message was received : we process the message(s)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   242
    if(Res == CAN_ERR_OK)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   243
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   244
            // if something different that 11bit or rtr... problem      
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   245
	switch(peakMsg.MSGTYPE)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   246
	{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   247
	  case MSGTYPE_STATUS:
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   248
		  switch(peakMsg.DATA[3])
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   249
	          {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   250
		    case CAN_ERR_BUSHEAVY: 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   251
				      break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   252
	            case CAN_ERR_BUSOFF: 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   253
			              printf ("Peak board read BUSOFF: re-init!!!\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   254
				      canInit((s_BOARD*)fd0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   255
				      usleep(2000);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   256
				      break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   257
		  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   258
		  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
   259
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   260
	  case MSGTYPE_STANDARD:		/* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   261
	  case MSGTYPE_EXTENDED:
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   262
			  m->rtr = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   263
			  break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   264
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   265
	  case MSGTYPE_RTR:			/* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   266
			  m->rtr = 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   267
			  break;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   268
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   269
	  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
   270
		    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   271
	}
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
      m->cob_id = peakMsg.ID;
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
      if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)  /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   276
                m->rtr = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   277
      else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   278
                m->rtr = 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   279
      m->len = peakMsg.LEN; /* count of data bytes (0..8) */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   280
      for(data=0; data<peakMsg.LEN; data++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   281
                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
   282
#if defined DEBUG_MSG_CONSOLE_ON
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   283
      MSG("in : ");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   284
      print_message(m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   285
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   286
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   287
    else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   288
    {       // not benign error => fatal error
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   289
      if(!(Res & CAN_ERR_QRCVEMPTY
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   290
                    || Res & CAN_ERR_BUSLIGHT
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   291
                    || Res & CAN_ERR_BUSHEAVY))
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   292
      {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   293
        printf ("canReceive returned error (%d)\n", Res);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   294
        return 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   295
      }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   296
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   297
  } while(Res != CAN_ERR_OK);
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
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   300
     // populate message received to other drivers
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   301
  for(i=0; i<SLAVE_COUNT; i++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   302
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   303
    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
   304
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   305
      PushMsgToQueue(&Q_DATA[i],m);
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
  }
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
  return 0;
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
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
UNS8 LIBAPI canSend_driver(CAN_HANDLE fd0, Message const *m)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   315
{
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   316
UNS8 data;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   317
TPCANMsg peakMsg;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   318
int i, j;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   319
int loc_errno;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   320
int MaxLoops = 100;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   321
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   322
  i = -1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   323
  for(j=0; j<SLAVE_COUNT; j++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   324
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   325
    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
   326
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   327
      PushMsgToQueue(&Q_DATA[j],m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   328
      i = j;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   329
    }
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
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   332
  if(i<0) return 1;                         // no board found
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
  peakMsg.ID = m->cob_id;                   /* 11/29 bit code */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   335
  if(m->rtr == 0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   336
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   337
    if(peakMsg.ID > 0x7FF)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   338
	peakMsg.MSGTYPE = MSGTYPE_EXTENDED; /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   339
    else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   340
	peakMsg.MSGTYPE = MSGTYPE_STANDARD; /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   341
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   342
  else	
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   343
    peakMsg.MSGTYPE = MSGTYPE_RTR;	    /* bits of MSGTYPE_ */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   344
  
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   345
  peakMsg.LEN = m->len;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   346
    /* count of data bytes (0..8) */
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   347
  for(data = 0; data < m->len; data++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   348
        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
   349
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   350
  do 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   351
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   352
    errno = loc_errno = CAN_Write(&peakMsg);
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
    if(loc_errno)
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
      if(loc_errno==CAN_ERR_BUSOFF && (MaxLoops%20)==1)
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 defined DEBUG_MSG_CONSOLE_ON
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   359
        printf ("Peak board write: re-init!!!\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   360
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   361
        canInit((s_BOARD*)fd0);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   362
        usleep(1000);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   363
      }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   364
      usleep(80);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   365
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   366
    if(MaxLoops-- == 0) break;			// limit max looping
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   367
  } while(loc_errno != CAN_ERR_OK);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   368
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   369
#if defined DEBUG_MSG_CONSOLE_ON
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   370
    MSG("out : ");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   371
    print_message(m);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   372
#endif
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   373
    return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   374
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   375
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   376
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   377
/***************************************************************************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   378
UNS8 LIBAPI canChangeBaudRate_driver(CAN_HANDLE fd, char* baud)
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
    printf("canChangeBaudRate not yet supported by this driver\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   381
    return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   382
}
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
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   385
/***************************************************************************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   386
LIBPUBLIC CAN_HANDLE LIBAPI canOpen_driver(s_BOARD * board)
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
int ret;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   389
int i;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   390
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   391
  if(!initialisedQ)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   392
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   393
    memset(Q_DATA,0,sizeof(Q_DATA));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   394
    initialisedQ = 1;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   395
  }
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
  i = strtol(board->busname,NULL,0);			// Get slot name
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   398
  //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0));
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   399
  if(i<SLAVE_COUNT && i>=0)
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
    Q_DATA[i].board = board;    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   402
      //printf ("First Board selected\n");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   403
    if(Q_DATA[i].hEventx==NULL)				// Create local event
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].hEventx = CreateEvent(NULL, FALSE, FALSE, "");
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   406
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   407
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   408
    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
   409
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   410
    ret = canInit(board);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   411
    if(ret)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   412
      return (CAN_HANDLE)board;
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
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   415
  return NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   416
}
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
/***************************************************************************/
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   420
int LIBAPI canClose_driver(CAN_HANDLE fd0)
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
s_BOARD *x_board = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   423
int ActiveBoards = 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   424
int i;
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
  if((s_BOARD *)fd0==NULL) return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   427
  for(i=0; i<SLAVE_COUNT; i++)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   428
  {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   429
    if(Q_DATA[i].board == (s_BOARD *)fd0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   430
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   431
      x_board = Q_DATA[i].board;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   432
      Q_DATA[i].board = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   433
      CloseHandle(Q_DATA[i].hEventx);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   434
      Q_DATA[i].hEventx = NULL;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   435
    }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   436
    else
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   437
      ActiveBoards++;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   438
  }
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
  if(ActiveBoards<=0)
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   441
  {				// No can device is used.
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   442
    CAN_SetRcvEvent(NULL);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   443
    CAN_Close();    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   444
    if(hEvent1) 
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   445
    {
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   446
      SetEvent(hEvent1);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   447
      CloseHandle(hEvent1);
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   448
      hEvent1 = NULL;
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
  }
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   451
  else    
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   452
    SetEvent(hEvent1);
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
  return 0;
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   455
}
0271f08ba819 Multiple instances CAN wirtual network for PEAK and Windows.
JaFojtik
parents:
diff changeset
   456
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
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