drivers/win32/win32.c
changeset 698 a0b50d6ce746
parent 697 24a2aec61731
child 723 c33da109a4c8
equal deleted inserted replaced
693:e21822be8189 698:a0b50d6ce746
     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;
   138 {
   150 {
   139 	if (port && (m_canSend != NULL))
   151 	if (port && (m_canSend != NULL))
   140 	{
   152 	{
   141 		return m_canSend(((CANPort*)port)->fd, m);
   153 		return m_canSend(((CANPort*)port)->fd, m);
   142 	}
   154 	}
   143 	return 1;
   155 	return 1; /* NOT OK */	
   144 }
   156 }
   145 
   157 
   146 /***************************************************************************/
   158 /***************************************************************************/
   147 void canReceiveLoop(CAN_PORT port)
   159 DWORD canReceiveLoop(CAN_PORT port)
   148 {
   160 {
   149 	Message m;
   161 	Message m;
   150 	while(((CANPort*)port)->used)
   162 	while(((CANPort*)port)->used)
   151 	{
   163 	{
   152 		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break;
   164 		if(m_canReceive(((CANPort*)port)->fd, &m) != 0) continue;
   153 		EnterMutex();
   165 		EnterMutex();
   154 		canDispatch(((CANPort*)port)->d, &m);
   166 		canDispatch(((CANPort*)port)->d, &m);
   155 		LeaveMutex();
   167 		LeaveMutex();
   156 	}
   168 	}
       
   169 	return 0;
   157 }
   170 }
   158 
   171 
   159 /***************************************************************************/
   172 /***************************************************************************/
   160 CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
   173 CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
   161 {
   174 {
   162 	int i;
   175 	int i;
   163     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 	}
   164 
   184 
   165 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
   185 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
   166 	{
   186 	{
   167 		if(!canports[i].used)
   187 		if(!canports[i].used)
   168 		break;
   188 		break;
   194 }
   214 }
   195 
   215 
   196 /***************************************************************************/
   216 /***************************************************************************/
   197 int canClose(CO_Data * d)
   217 int canClose(CO_Data * d)
   198 {
   218 {
   199 	UNS8 res = 1;
   219 	UNS8 res;
   200 	CANPort* tmp;
   220 	CANPort* tmp;
   201 
   221 
   202 	if((CANPort*)d->canHandle)
       
   203 	{
       
   204 	  ((CANPort*)d->canHandle)->used = 0;
       
   205 	}
       
   206 
       
   207 	tmp = (CANPort*)d->canHandle;
   222 	tmp = (CANPort*)d->canHandle;
   208 
   223 
   209 	if(tmp)
   224 	if(tmp)
   210 	{
   225 	{
   211 	  // 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
   212 	  WaitReceiveTaskEnd(&tmp->receiveTask);
   232 	  WaitReceiveTaskEnd(&tmp->receiveTask);
   213 
   233 
   214 	  // close CAN port
   234 		// release used flag as a last step.
   215 	  res = m_canClose(tmp->fd);
   235 	  tmp->used = 0;
   216 	}
   236 	}
   217 
   237   else res = 255;
   218 	d->canHandle = NULL;
   238 
   219 
   239 return res;
   220 	return res;
       
   221 }
   240 }
   222 
   241 
   223 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
   242 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
   224 {
   243 {
   225    if(port){
   244    if(port){