drivers/unix/unix.c
author Edouard Tisserant
Mon, 18 Jun 2012 02:27:19 +0200
changeset 742 10e165c6743c
parent 644 11b21e9a92f6
child 777 bbbfd27c1bd1
permissions -rw-r--r--
pdo.c : missalignment problem on some cpu when comparing cobids, various typo fixes
dcf.c : factorized code, and added automatic state change to operational when DCF finished
drivers/none : default time value is now nanoseconds
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     1
/*
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     3
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     5
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     7
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    12
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    17
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    21
*/
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
    22
#ifndef __KERNEL__
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    23
#include <unistd.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    24
#include <stdio.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    25
#include <stdlib.h>
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
    26
#else
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
    27
#include <linux/module.h>
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
    28
#include <linux/delay.h>
644
11b21e9a92f6 FIXED: - added missing endif
Christian Taedcke <hacking@taedcke.com>
parents: 629
diff changeset
    29
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    30
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    31
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    32
#define DLL_CALL(funcname) (* funcname##_driver)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    33
#define FCT_PTR_INIT =NULL
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    34
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    35
#define DLSYM(name)\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    36
	*(void **) (&name##_driver) = dlsym(handle, #name"_driver");\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    37
	if ((error = dlerror()) != NULL)  {\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    38
		fprintf (stderr, "%s\n", error);\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    39
		UnLoadCanDriver(handle);\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    40
		return NULL;\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    41
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    42
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    43
#else /*NOT_USE_DYNAMIC_LOADING*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    44
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    45
/*Function call is direct*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    46
#define DLL_CALL(funcname) funcname##_driver
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    47
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    48
#endif /*NOT_USE_DYNAMIC_LOADING*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    49
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    50
#include "data.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    51
#include "canfestival.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    52
#include "timers_driver.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    53
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    54
#define MAX_NB_CAN_PORTS 16
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    55
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    56
/** CAN port structure */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    57
typedef struct {
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    58
  char used;  /**< flag indicating CAN port usage, will be used to abort Receiver task*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    59
  CAN_HANDLE fd; /**< CAN port file descriptor*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    60
  TASK_HANDLE receiveTask; /**< CAN Receiver task*/
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    61
  CO_Data* d; /**< CAN object data*/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    62
} CANPort;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    63
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    64
#include "can_driver.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    65
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    66
/*Declares the funtion pointers for dll binding or simple protos*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    67
/*UNS8 DLL_CALL(canReceive)(CAN_HANDLE, Message *);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    68
UNS8 DLL_CALL(canSend)(CAN_HANDLE, Message *);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    69
CAN_HANDLE DLL_CALL(canOpen)(s_BOARD *);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    70
int DLL_CALL(canClose)(CAN_HANDLE);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    71
*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    72
CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}};
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    73
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    74
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    75
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    76
/*UnLoads the dll*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    77
UNS8 UnLoadCanDriver(LIB_HANDLE handle)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    78
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    79
	if(handle!=NULL)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    80
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    81
		dlclose(handle);
151
ebf4bd44f282 Updated doc. Cleaned some code.
etisserant
parents: 149
diff changeset
    82
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    83
		handle=NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    84
		return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    85
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    86
	return -1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    87
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    88
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    89
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    90
 * Loads the dll and get funcs ptr
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    91
 *
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    92
 * @param driver_name String containing driver's dynamic library name
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    93
 * @return Library handle
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
    94
 */
480
d0d76932ad74 Added missing const for dll names strings
etisserant
parents: 467
diff changeset
    95
LIB_HANDLE LoadCanDriver(const char* driver_name)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    96
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    97
	LIB_HANDLE handle = NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
    98
	char *error;
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
    99
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   100
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   101
	if(handle==NULL)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   102
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   103
		handle = dlopen(driver_name, RTLD_LAZY);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   104
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   105
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   106
	if (!handle) {
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   107
		fprintf (stderr, "%s\n", dlerror());
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   108
        	return NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   109
	}
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   110
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   111
	/*Get function ptr*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   112
	DLSYM(canReceive)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   113
	DLSYM(canSend)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   114
	DLSYM(canOpen)
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   115
	DLSYM(canChangeBaudRate)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   116
	DLSYM(canClose)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   117
197
182008694a32 Fixed "function LoadCanDriver() should return library handle instead of 0 if succeeded" bug
etisserant
parents: 162
diff changeset
   118
	return handle;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   119
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   120
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   121
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   122
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   123
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   124
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   125
/*Not needed -- canReceiveLoop calls _canReceive directly *//*
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   126
UNS8 canReceive(CAN_PORT port, Message *m)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   127
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   128
	return DLL_CALL(canReceive)(port->fd, Message *m);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   129
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   130
*/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   131
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   132
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   133
 * CAN send routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   134
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   135
 * @param m CAN message
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   136
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   137
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   138
UNS8 canSend(CAN_PORT port, Message *m)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   139
{
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   140
	if(port){
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   141
		UNS8 res;
155
746b49869cbc Removed Mutex liberation on CanSend. Too much race condition to avoid. Cannot be deeply tested.
etisserant
parents: 151
diff changeset
   142
	        //LeaveMutex();
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   143
		res = DLL_CALL(canSend)(((CANPort*)port)->fd, m);
155
746b49869cbc Removed Mutex liberation on CanSend. Too much race condition to avoid. Cannot be deeply tested.
etisserant
parents: 151
diff changeset
   144
		//EnterMutex();
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 197
diff changeset
   145
		return res; // OK
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   146
	}
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 197
diff changeset
   147
	return 1; // NOT OK
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   148
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   149
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   150
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   151
 * CAN Receiver Task
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   152
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   153
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   154
void canReceiveLoop(CAN_PORT port)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   155
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   156
       Message m;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   157
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   158
       while (((CANPort*)port)->used) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   159
               if (DLL_CALL(canReceive)(((CANPort*)port)->fd, &m) != 0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   160
                       break;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   161
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   162
               EnterMutex();
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   163
               canDispatch(((CANPort*)port)->d, &m);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   164
               LeaveMutex();
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   165
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   166
#ifdef __KERNEL__
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   167
#ifdef USE_XENO
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   168
               /* periodic task for Xenomai kernel realtime */
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   169
               rt_task_wait_period(NULL);
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   170
#endif
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   171
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   172
       }
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   173
}
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   174
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   175
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   176
 * CAN open routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   177
 * @param board device name and baudrate
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   178
 * @param d CAN object data
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   179
 * @return valid CAN_PORT pointer or NULL
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   180
 */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   181
CAN_PORT canOpen(s_BOARD *board, CO_Data * d)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   182
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   183
	int i;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   184
	for(i=0; i < MAX_NB_CAN_PORTS; i++)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   185
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   186
		if(!canports[i].used)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   187
		break;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   188
	}
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   189
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   190
#ifndef NOT_USE_DYNAMIC_LOADING
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   191
	if (&DLL_CALL(canOpen)==NULL) {
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   192
        	fprintf(stderr,"CanOpen : Can Driver dll not loaded\n");
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   193
        	return NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   194
	}
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   195
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   196
	CAN_HANDLE fd0 = DLL_CALL(canOpen)(board);
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   197
	if(fd0){
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   198
		canports[i].used = 1;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   199
		canports[i].fd = fd0;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   200
		canports[i].d = d;
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   201
		d->canHandle = (CAN_PORT)&canports[i];
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   202
		CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, &canReceiveLoop);
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   203
		return (CAN_PORT)&canports[i];
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   204
	}else{
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   205
        	MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate);
162
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   206
		return NULL;
8331c670a3de Better error handling for unix and peak_linux.
etisserant
parents: 155
diff changeset
   207
	}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   208
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   209
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   210
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   211
 * CAN close routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   212
 * @param d CAN object data
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   213
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   214
 */
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   215
int canClose(CO_Data * d)
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   216
{
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   217
	UNS8 res;
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   218
149
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   219
	((CANPort*)d->canHandle)->used = 0;
fe50ada8020b Changes in the API:
etisserant
parents: 145
diff changeset
   220
	CANPort* tmp = (CANPort*)d->canHandle;
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   221
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   222
	// kill receiver task before port is closed and handle set to NULL
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   223
	WaitReceiveTaskEnd(&tmp->receiveTask);
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   224
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   225
	// close CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   226
	res = DLL_CALL(canClose)(tmp->fd);
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   227
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   228
	d->canHandle = NULL;
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   229
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   230
	return res;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
diff changeset
   231
}
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   232
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   233
442
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   234
/**
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   235
 * CAN change baudrate routine
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   236
 * @param port CAN port
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   237
 * @param baud baudrate
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   238
 * @return success or error
010c26b9ba89 Doxy-style comments and various cleanup in unix.c
etisserant
parents: 401
diff changeset
   239
 */
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   240
UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   241
{
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   242
   if(port){
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   243
		UNS8 res;
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   244
	        //LeaveMutex();
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   245
		res = DLL_CALL(canChangeBaudRate)(((CANPort*)port)->fd, baud);
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   246
		//EnterMutex();
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   247
		return res; // OK
629
b9274b595650 CosateQ contribution.
edouard
parents: 480
diff changeset
   248
	}
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   249
	return 1; // NOT OK
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 267
diff changeset
   250
}
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   251
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   252
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   253
#ifdef __KERNEL__
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   254
EXPORT_SYMBOL (canOpen);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   255
EXPORT_SYMBOL (canClose);
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   256
EXPORT_SYMBOL (canSend);
467
40efa79d27dd kerneltest updated for the new api
greg
parents: 454
diff changeset
   257
EXPORT_SYMBOL (canChangeBaudRate);
391
7802a7d5584f Accepted Vladimir Chren linux kernelspace port patch.
etisserant
parents: 384
diff changeset
   258
#endif