runtime/plc_Linux_main.c
author etisserant
Fri, 27 Jun 2008 16:21:22 +0200
changeset 178 2390b409eb93
parent 100 24b504f67c72
child 203 cb9901076a21
permissions -rw-r--r--
Added PLC tick alignement on external synchronization source feature.
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     1
#include <stdio.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     2
#include <string.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     3
#include <time.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     4
#include <signal.h>
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
     5
#include <stdlib.h>
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     6
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
     7
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
     8
{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
     9
    clock_gettime(CLOCK_REALTIME, CURRENT_TIME);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    10
}
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    11
53
805abb954de2 removed timer_notify conflict with CanFestival
etisserant
parents: 49
diff changeset
    12
void PLC_timer_notify(sigval_t val)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    13
{
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    14
    PLC_GetTime(&__CURRENT_TIME);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    15
    __run();
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    16
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    17
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    18
timer_t PLC_timer;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    19
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    20
void PLC_SetTimer(long long next, long long period)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    21
{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    22
    struct itimerspec timerValues;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    23
	/*
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    24
	printf("SetTimer(%lld,%lld)\n",next, period);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    25
	*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    26
    memset (&timerValues, 0, sizeof (struct itimerspec));
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    27
	{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    28
#ifdef __lldiv_t_defined
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    29
		lldiv_t nxt_div = lldiv(next, 1000000000);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    30
		lldiv_t period_div = lldiv(period, 1000000000);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    31
	    timerValues.it_value.tv_sec = nxt_div.quot;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    32
	    timerValues.it_value.tv_nsec = nxt_div.rem;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    33
	    timerValues.it_interval.tv_sec = period_div.quot;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    34
	    timerValues.it_interval.tv_nsec = period_div.rem;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    35
#else
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    36
	    timerValues.it_value.tv_sec = next / 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    37
	    timerValues.it_value.tv_nsec = next % 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    38
	    timerValues.it_interval.tv_sec = period / 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    39
	    timerValues.it_interval.tv_nsec = period % 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    40
#endif
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    41
	}	
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    42
    timer_settime (PLC_timer, 0, &timerValues, NULL);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    43
}
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    44
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    45
void catch_signal(int sig)
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    46
{
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    47
  signal(SIGTERM, catch_signal);
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    48
  signal(SIGINT, catch_signal);
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    49
  printf("Got Signal %d\n",sig);
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    50
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    51
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    52
int main(int argc,char **argv)
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    53
{
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    54
    struct sigevent sigev;
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    55
    /* Translate PLC's microseconds to Ttick nanoseconds */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    56
    Ttick = 1000000 * maxval(common_ticktime__,1);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    57
    
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    58
    memset (&sigev, 0, sizeof (struct sigevent));
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    59
    sigev.sigev_value.sival_int = 0;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    60
    sigev.sigev_notify = SIGEV_THREAD;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    61
    sigev.sigev_notify_attributes = NULL;
53
805abb954de2 removed timer_notify conflict with CanFestival
etisserant
parents: 49
diff changeset
    62
    sigev.sigev_notify_function = PLC_timer_notify;
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    63
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    64
    timer_create (CLOCK_REALTIME, &sigev, &PLC_timer);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    65
    if(  __init(argc,argv) == 0 ){
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    66
        PLC_SetTimer(Ttick,Ttick);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    67
        
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    68
        /* install signal handler for manual break */
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    69
        signal(SIGTERM, catch_signal);
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    70
        signal(SIGINT, catch_signal);
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    71
        /* Wait some signal */
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    72
        pause();
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    73
        /* Stop the PLC */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    74
        PLC_SetTimer(0,0);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    75
    }
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    76
    __cleanup();
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    77
    timer_delete (PLC_timer);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    78
    
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    79
    return 0;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    80
}