runtime/plc_Linux_main.c
author etisserant
Wed, 20 Aug 2008 00:11:40 +0200
changeset 203 cb9901076a21
parent 178 2390b409eb93
permissions -rw-r--r--
Added concepts :
- connector class (for PLC control, debug and flashing abstraction).
- builder class (C toolchain abstraction)

Added features :
- Pyro based connector.
- ctypes loader, load PLC shared object and run it
- ctypes based debugger embryo (not tested)
- gcc builder

Broken:
- Win32 runtime
- Most tests
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>
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
     6
#include <pthread.h> 
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
     7
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
     8
long AtomicCompareExchange(long* atomicvar,long exchange, long compared)
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
     9
{
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    10
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    11
}
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    12
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    13
//long AtomicExchange(long* atomicvar,long exchange)
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    14
//{
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    15
//    return __sync_lock_test_and_set(atomicvar, exchange);
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    16
//}
49
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
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
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
    clock_gettime(CLOCK_REALTIME, CURRENT_TIME);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    21
}
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    22
53
805abb954de2 removed timer_notify conflict with CanFestival
etisserant
parents: 49
diff changeset
    23
void PLC_timer_notify(sigval_t val)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    24
{
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    25
    PLC_GetTime(&__CURRENT_TIME);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    26
    __run();
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    27
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    28
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    29
timer_t PLC_timer;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    30
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    31
void PLC_SetTimer(long long next, long long period)
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    32
{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    33
    struct itimerspec timerValues;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    34
	/*
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    35
	printf("SetTimer(%lld,%lld)\n",next, period);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    36
	*/
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    37
    memset (&timerValues, 0, sizeof (struct itimerspec));
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    38
	{
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    39
#ifdef __lldiv_t_defined
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    40
		lldiv_t nxt_div = lldiv(next, 1000000000);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    41
		lldiv_t period_div = lldiv(period, 1000000000);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    42
	    timerValues.it_value.tv_sec = nxt_div.quot;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    43
	    timerValues.it_value.tv_nsec = nxt_div.rem;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    44
	    timerValues.it_interval.tv_sec = period_div.quot;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    45
	    timerValues.it_interval.tv_nsec = period_div.rem;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    46
#else
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    47
	    timerValues.it_value.tv_sec = next / 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    48
	    timerValues.it_value.tv_nsec = next % 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    49
	    timerValues.it_interval.tv_sec = period / 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    50
	    timerValues.it_interval.tv_nsec = period % 1000000000;
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    51
#endif
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    52
	}	
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    53
    timer_settime (PLC_timer, 0, &timerValues, NULL);
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    54
}
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    55
//
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    56
void catch_signal(int sig)
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    57
{
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    58
//  signal(SIGTERM, catch_signal);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    59
  signal(SIGINT, catch_signal);
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    60
  printf("Got Signal %d\n",sig);
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    61
  exit(0);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    62
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    63
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    64
int startPLC(int argc,char **argv)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    65
{
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    66
    struct sigevent sigev;
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    67
    /* Translate PLC's microseconds to Ttick nanoseconds */
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    68
    Ttick = 1000000 * maxval(common_ticktime__,1);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    69
    
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    70
    memset (&sigev, 0, sizeof (struct sigevent));
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    71
    sigev.sigev_value.sival_int = 0;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    72
    sigev.sigev_notify = SIGEV_THREAD;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    73
    sigev.sigev_notify_attributes = NULL;
53
805abb954de2 removed timer_notify conflict with CanFestival
etisserant
parents: 49
diff changeset
    74
    sigev.sigev_notify_function = PLC_timer_notify;
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    75
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    76
    timer_create (CLOCK_REALTIME, &sigev, &PLC_timer);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    77
    if(  __init(argc,argv) == 0 ){
178
2390b409eb93 Added PLC tick alignement on external synchronization source feature.
etisserant
parents: 100
diff changeset
    78
        PLC_SetTimer(Ttick,Ttick);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    79
        
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    80
        /* install signal handler for manual break */
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    81
//        signal(SIGTERM, catch_signal);
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    82
        signal(SIGINT, catch_signal);
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    83
    }else{
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    84
        return 1;
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 53
diff changeset
    85
    }
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    86
    return 0;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    87
}
203
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    88
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    89
int stopPLC()
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    90
{
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    91
    /* Stop the PLC */
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    92
    PLC_SetTimer(0,0);
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    93
    timer_delete (PLC_timer);
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    94
    __cleanup();
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    95
}
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    96
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    97
pthread_mutex_t DebugLock = PTHREAD_MUTEX_INITIALIZER;
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    98
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
    99
/* from plc_debugger.c */
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   100
void WaitDebugData()
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   101
{
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   102
    /* Wait signal from PLC thread */
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   103
    pthread_mutex_lock(&DebugLock);
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   104
}
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   105
 
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   106
/* Called by PLC thread when debug_publish finished
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   107
 * This is supposed to unlock debugger thread in WaitDebugData*/
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   108
void InitiateDebugTransfer()
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   109
{
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   110
    /* signal debugger thread to continue*/
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   111
    pthread_mutex_unlock(&DebugLock);
cb9901076a21 Added concepts :
etisserant
parents: 178
diff changeset
   112
}