ID: 3459307 - Async problem in drivers/win32/win32.c
authorJaFojtik
Fri, 03 Feb 2012 22:34:44 +0100
changeset 697 24a2aec61731
parent 696 381af6bb6155
child 698 a0b50d6ce746
child 713 ca2c2dad3a22
ID: 3459307 - Async problem in drivers/win32/win32.c
drivers/timers_win32/timers_win32.c
drivers/win32/win32.c
--- a/drivers/timers_win32/timers_win32.c	Fri Feb 03 22:13:25 2012 +0100
+++ b/drivers/timers_win32/timers_win32.c	Fri Feb 03 22:34:44 2012 +0100
@@ -45,7 +45,7 @@
 HANDLE timer_thread = NULL;
 HANDLE timer = NULL;
 
-int stop_timer=0;
+volatile int stop_timer=0;
 
 static TimerCallback_t init_callback;
 
--- a/drivers/win32/win32.c	Fri Feb 03 22:13:25 2012 +0100
+++ b/drivers/win32/win32.c	Fri Feb 03 22:34:44 2012 +0100
@@ -3,6 +3,7 @@
 
 Copyright (C): Edouard TISSERANT and Francis DUPIN
 Copyright (C) Win32 Port Leonid Tochinski
+Modified by: Jaroslav Fojtik
 
 See COPYING file for copyrights details.
 
@@ -155,23 +156,31 @@
 }
 
 /***************************************************************************/
-void canReceiveLoop(CAN_PORT port)
+DWORD canReceiveLoop(CAN_PORT port)
 {
 	Message m;
 	while(((CANPort*)port)->used)
 	{
-		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break;
+		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) continue;
 		EnterMutex();
 		canDispatch(((CANPort*)port)->d, &m);
 		LeaveMutex();
 	}
+	return 0;
 }
 
 /***************************************************************************/
 CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
 {
 	int i;
-    CAN_HANDLE fd0;
+	CAN_HANDLE fd0;
+
+
+	  /* Fix of multiple opening one data set, added by J.Fojtik. */
+	if(d->canHandle)
+	{
+	  canClose(d);
+	}
 
 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
 	{
@@ -207,28 +216,27 @@
 /***************************************************************************/
 int canClose(CO_Data * d)
 {
-	UNS8 res = 1;
+	UNS8 res;
 	CANPort* tmp;
 
-	if((CANPort*)d->canHandle)
-	{
-	  ((CANPort*)d->canHandle)->used = 0;
-	}
-
 	tmp = (CANPort*)d->canHandle;
 
 	if(tmp)
 	{
-	  // kill receiver task before port is closed and handle set to NULL
+	  d->canHandle = NULL;
+
+		// close CAN port
+	  res = m_canClose(tmp->fd);
+
+		// kill receiver task
 	  WaitReceiveTaskEnd(&tmp->receiveTask);
 
-	  // close CAN port
-	  res = m_canClose(tmp->fd);
-	}
-
-	d->canHandle = NULL;
-
-	return res;
+		// release used flag as a last step.
+	  tmp->used = 0;
+	}
+  else res = 255;
+
+return res;
 }
 
 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)