SOEM/osal/win32/osal.c

160 lines
4.5 KiB
C

/*
* Simple Open EtherCAT Master Library
*
* File : osal.c
* Version : 1.3.1
* Date : 11-03-2015
* Copyright (C) 2005-2015 Speciaal Machinefabriek Ketels v.o.f.
* Copyright (C) 2005-2015 Arthur Ketels
* Copyright (C) 2008-2009 TU/e Technische Universiteit Eindhoven
* Copyright (C) 2012-2015 rt-labs AB , Sweden
*
* SOEM is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* SOEM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* As a special exception, if other files instantiate templates or use macros
* or inline functions from this file, or you compile this file and link it
* with other works to produce a work based on this file, this file does not
* by itself cause the resulting work to be covered by the GNU General Public
* License. However the source code for this file must still be made available
* in accordance with section (3) of the GNU General Public License.
*
* This exception does not invalidate any other reasons why a work based on
* this file might be covered by the GNU General Public License.
*
* The EtherCAT Technology, the trade name and logo “EtherCAT” are the intellectual
* property of, and protected by Beckhoff Automation GmbH. You can use SOEM for
* the sole purpose of creating, using and/or selling or otherwise distributing
* an EtherCAT network master provided that an EtherCAT Master License is obtained
* from Beckhoff Automation GmbH.
*
* In case you did not receive a copy of the EtherCAT Master License along with
* SOEM write to Beckhoff Automation GmbH, Eiserstraße 5, D-33415 Verl, Germany
* (www.beckhoff.com).
*/
#include <winsock2.h>
#include <osal.h>
#include "osal_win32.h"
static int64_t sysfrequency;
static double qpc2usec;
#define USECS_PER_SEC 1000000
int osal_gettimeofday (struct timeval *tv, struct timezone *tz)
{
int64_t wintime, usecs;
if(!sysfrequency)
{
timeBeginPeriod(1);
QueryPerformanceFrequency((LARGE_INTEGER *)&sysfrequency);
qpc2usec = 1000000.0 / sysfrequency;
}
QueryPerformanceCounter((LARGE_INTEGER *)&wintime);
usecs = (int64_t)((double)wintime * qpc2usec);
tv->tv_sec = (long)(usecs / 1000000);
tv->tv_usec = (long)(usecs - (tv->tv_sec * 1000000));
return 1;
}
ec_timet osal_current_time (void)
{
struct timeval current_time;
ec_timet return_value;
osal_gettimeofday (&current_time, 0);
return_value.sec = current_time.tv_sec;
return_value.usec = current_time.tv_usec;
return return_value;
}
void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff)
{
diff->sec = end->sec - start->sec;
diff->usec = end->usec - start->usec;
if (diff->usec < 0) {
--diff->sec;
diff->usec += 1000000;
}
}
void osal_timer_start (osal_timert *self, uint32 timeout_usec)
{
struct timeval start_time;
struct timeval timeout;
struct timeval stop_time;
osal_gettimeofday (&start_time, 0);
timeout.tv_sec = timeout_usec / USECS_PER_SEC;
timeout.tv_usec = timeout_usec % USECS_PER_SEC;
timeradd (&start_time, &timeout, &stop_time);
self->stop_time.sec = stop_time.tv_sec;
self->stop_time.usec = stop_time.tv_usec;
}
boolean osal_timer_is_expired (osal_timert *self)
{
struct timeval current_time;
struct timeval stop_time;
int is_not_yet_expired;
osal_gettimeofday (&current_time, 0);
stop_time.tv_sec = self->stop_time.sec;
stop_time.tv_usec = self->stop_time.usec;
is_not_yet_expired = timercmp (&current_time, &stop_time, <);
return is_not_yet_expired == FALSE;
}
int osal_usleep(uint32 usec)
{
osal_timert qtime;
osal_timer_start(&qtime, usec);
if(usec >= 1000)
{
SleepEx(usec / 1000, FALSE);
}
while(!osal_timer_is_expired(&qtime));
return 1;
}
void *osal_malloc(size_t size)
{
return malloc(size);
}
void osal_free(void *ptr)
{
free(ptr);
}
int osal_thread_create(void **thandle, int stacksize, void *func, void *param)
{
*thandle = CreateThread(NULL, stacksize, func, param, 0, NULL);
if(!thandle)
{
return 0;
}
return 1;
}
int osal_thread_create_rt(void **thandle, int stacksize, void *func, void *param)
{
int ret;
ret = osal_thread_create(thandle, stacksize, func, param);
if (ret)
{
ret = SetThreadPriority(*thandle, THREAD_PRIORITY_TIME_CRITICAL);
}
return ret;
}