drivers/win32/win32.c
changeset 710 e7a45c77d6ec
parent 697 24a2aec61731
child 723 c33da109a4c8
equal deleted inserted replaced
709:4a8b50dcc4c0 710:e7a45c77d6ec
     1 /*
     1 /*
     2 This file is part of CanFestival, a library implementing CanOpen Stack.
     2 This file is part of CanFestival, a library implementing CanOpen Stack.
     3 
     3 
     4 Copyright (C): Edouard TISSERANT and Francis DUPIN
     4 Copyright (C): Edouard TISSERANT and Francis DUPIN
     5 Copyright (C) Win32 Port Leonid Tochinski
     5 Copyright (C) Win32 Port Leonid Tochinski
       
     6 Modified by: Jaroslav Fojtik
     6 
     7 
     7 See COPYING file for copyrights details.
     8 See COPYING file for copyrights details.
     8 
     9 
     9 This library is free software; you can redistribute it and/or
    10 This library is free software; you can redistribute it and/or
    10 modify it under the terms of the GNU Lesser General Public
    11 modify it under the terms of the GNU Lesser General Public
   116 	m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver"));
   117 	m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver"));
   117 	m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver"));
   118 	m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver"));
   118 	m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver"));
   119 	m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver"));
   119 	m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver"));
   120 	m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver"));
   120 	m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver"));
   121 	m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver"));
       
   122 
       
   123 	if(m_canReceive==NULL || m_canSend==NULL || m_canOpen==NULL || m_canClose==NULL || m_canChangeBaudRate==NULL)
       
   124 	{
       
   125 	  m_canReceive = NULL;
       
   126 	  m_canSend = NULL;
       
   127 	  m_canOpen = NULL;
       
   128 	  m_canClose = NULL;
       
   129 	  m_canChangeBaudRate = NULL;
       
   130 	  FreeLibrary(handle);
       
   131 	  handle = NULL;
       
   132 	}
   121 #else
   133 #else
   122   //compiled in...
   134   //compiled in...
   123   handle = 1; //TODO: remove this hack
   135   handle = 1; //TODO: remove this hack
   124 
   136 
   125   m_canReceive = canReceive_driver;
   137   m_canReceive = canReceive_driver;
   134 }
   146 }
   135 
   147 
   136 /***************************************************************************/
   148 /***************************************************************************/
   137 UNS8 canSend(CAN_PORT port, Message *m)
   149 UNS8 canSend(CAN_PORT port, Message *m)
   138 {
   150 {
   139 	UNS8 res = 1; //NOT OK
       
   140 	if (port && (m_canSend != NULL))
   151 	if (port && (m_canSend != NULL))
   141 	{
   152 	{
   142 		res = m_canSend(((CANPort*)port)->fd, m);
   153 		return m_canSend(((CANPort*)port)->fd, m);
   143 	}
   154 	}
   144 	return res;
   155 	return 1; /* NOT OK */	
   145 }
   156 }
   146 
   157 
   147 /***************************************************************************/
   158 /***************************************************************************/
   148 void canReceiveLoop(CAN_PORT port)
   159 DWORD canReceiveLoop(CAN_PORT port)
   149 {
   160 {
   150 	Message m;
   161 	Message m;
   151 	while(((CANPort*)port)->used)
   162 	while(((CANPort*)port)->used)
   152 	{
   163 	{
   153 		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break;
   164 		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) continue;
   154 		EnterMutex();
   165 		EnterMutex();
   155 		canDispatch(((CANPort*)port)->d, &m);
   166 		canDispatch(((CANPort*)port)->d, &m);
   156 		LeaveMutex();
   167 		LeaveMutex();
   157 	}
   168 	}
       
   169 	return 0;
   158 }
   170 }
   159 
   171 
   160 /***************************************************************************/
   172 /***************************************************************************/
   161 CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
   173 CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
   162 {
   174 {
   163 	int i;
   175 	int i;
   164     CAN_HANDLE fd0;
   176 	CAN_HANDLE fd0;
       
   177 
       
   178 
       
   179 	  /* Fix of multiple opening one data set, added by J.Fojtik. */
       
   180 	if(d->canHandle)
       
   181 	{
       
   182 	  canClose(d);
       
   183 	}
   165 
   184 
   166 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
   185 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
   167 	{
   186 	{
   168 		if(!canports[i].used)
   187 		if(!canports[i].used)
   169 		break;
   188 		break;
   195 }
   214 }
   196 
   215 
   197 /***************************************************************************/
   216 /***************************************************************************/
   198 int canClose(CO_Data * d)
   217 int canClose(CO_Data * d)
   199 {
   218 {
   200 	UNS8 res = 1;
   219 	UNS8 res;
   201 	CANPort* tmp;
   220 	CANPort* tmp;
   202 
   221 
   203 	if((CANPort*)d->canHandle)
       
   204 	{
       
   205 	  ((CANPort*)d->canHandle)->used = 0;
       
   206 	}
       
   207 
       
   208 	tmp = (CANPort*)d->canHandle;
   222 	tmp = (CANPort*)d->canHandle;
   209 
   223 
   210 	if(tmp)
   224 	if(tmp)
   211 	{
   225 	{
   212 	  // kill receiver task before port is closed and handle set to NULL
   226 	  d->canHandle = NULL;
       
   227 
       
   228 		// close CAN port
       
   229 	  res = m_canClose(tmp->fd);
       
   230 
       
   231 		// kill receiver task
   213 	  WaitReceiveTaskEnd(&tmp->receiveTask);
   232 	  WaitReceiveTaskEnd(&tmp->receiveTask);
   214 
   233 
   215 	  // close CAN port
   234 		// release used flag as a last step.
   216 	  res = m_canClose(tmp->fd);
   235 	  tmp->used = 0;
   217 	}
   236 	}
   218 
   237   else res = 255;
   219 	d->canHandle = NULL;
   238 
   220 
   239 return res;
   221 	return res;
       
   222 }
   240 }
   223 
   241 
   224 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
   242 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
   225 {
   243 {
   226    if(port){
   244    if(port){