targets/Linux/plc_Linux_main.c
author etisserant
Wed, 03 Sep 2008 17:28:17 +0200
changeset 235 a66e150f2888
parent 227 48c13b84505c
child 236 a32817e81f5e
permissions -rw-r--r--
Improved debug data feedback.
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     1
#include <stdio.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     2
#include <string.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     3
#include <time.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     4
#include <signal.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     5
#include <stdlib.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     6
#include <pthread.h> 
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     7
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     8
long AtomicCompareExchange(long* atomicvar,long exchange, long compared)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     9
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    10
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    11
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    12
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    13
//long AtomicExchange(long* atomicvar,long exchange)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    14
//{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    15
//    return __sync_lock_test_and_set(atomicvar, exchange);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    16
//}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    17
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    18
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    19
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    20
    clock_gettime(CLOCK_REALTIME, CURRENT_TIME);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    21
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    22
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    23
void PLC_timer_notify(sigval_t val)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    24
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    25
    PLC_GetTime(&__CURRENT_TIME);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    26
    __run();
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    27
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    28
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    29
timer_t PLC_timer;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    30
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    31
void PLC_SetTimer(long long next, long long period)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    32
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    33
    struct itimerspec timerValues;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    34
	/*
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    35
	printf("SetTimer(%lld,%lld)\n",next, period);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    36
	*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    37
    memset (&timerValues, 0, sizeof (struct itimerspec));
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    38
	{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    39
#ifdef __lldiv_t_defined
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    40
		lldiv_t nxt_div = lldiv(next, 1000000000);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    41
		lldiv_t period_div = lldiv(period, 1000000000);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    42
	    timerValues.it_value.tv_sec = nxt_div.quot;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    43
	    timerValues.it_value.tv_nsec = nxt_div.rem;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    44
	    timerValues.it_interval.tv_sec = period_div.quot;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    45
	    timerValues.it_interval.tv_nsec = period_div.rem;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    46
#else
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    47
	    timerValues.it_value.tv_sec = next / 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    48
	    timerValues.it_value.tv_nsec = next % 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    49
	    timerValues.it_interval.tv_sec = period / 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    50
	    timerValues.it_interval.tv_nsec = period % 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    51
#endif
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    52
	}	
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    53
    timer_settime (PLC_timer, 0, &timerValues, NULL);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    54
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    55
//
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    56
void catch_signal(int sig)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    57
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    58
//  signal(SIGTERM, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    59
  signal(SIGINT, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    60
  printf("Got Signal %d\n",sig);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    61
  exit(0);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    62
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    63
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    64
int startPLC(int argc,char **argv)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    65
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    66
    struct sigevent sigev;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    67
    /* Translate PLC's microseconds to Ttick nanoseconds */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    68
    Ttick = 1000000 * maxval(common_ticktime__,1);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    69
    
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    70
    memset (&sigev, 0, sizeof (struct sigevent));
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    71
    sigev.sigev_value.sival_int = 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    72
    sigev.sigev_notify = SIGEV_THREAD;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    73
    sigev.sigev_notify_attributes = NULL;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    74
    sigev.sigev_notify_function = PLC_timer_notify;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    75
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    76
    timer_create (CLOCK_REALTIME, &sigev, &PLC_timer);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    77
    if(  __init(argc,argv) == 0 ){
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    78
        PLC_SetTimer(Ttick,Ttick);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    79
        
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    80
        /* install signal handler for manual break */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    81
//        signal(SIGTERM, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    82
        signal(SIGINT, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    83
    }else{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    84
        return 1;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    85
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    86
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    87
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    88
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    89
static int __debug_tick;
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    90
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    91
static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    92
static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    93
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    94
void AbortDebug()
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    95
{
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    96
    /* Eventually unlock debugger thread*/
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    97
    __debug_tick = -1;
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    98
    pthread_mutex_lock(&wait_mutex);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
    99
    pthread_cond_broadcast(&wait_cond);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   100
    pthread_mutex_unlock(&wait_mutex);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   101
}
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   102
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   103
int stopPLC()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   104
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   105
    /* Stop the PLC */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   106
    PLC_SetTimer(0,0);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   107
    timer_delete (PLC_timer);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   108
    __cleanup();
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   109
    AbortDebug();
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   110
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   111
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   112
extern int __tick;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   113
/* from plc_debugger.c */
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   114
int WaitDebugData()
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   115
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   116
    /* Wait signal from PLC thread */
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   117
    pthread_mutex_lock(&wait_mutex);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   118
    pthread_cond_wait(&wait_cond, &wait_mutex);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   119
    pthread_mutex_unlock(&wait_mutex);
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   120
    return __debug_tick;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   121
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   122
 
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   123
/* Called by PLC thread when debug_publish finished
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   124
 * This is supposed to unlock debugger thread in WaitDebugData*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   125
void InitiateDebugTransfer()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   126
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   127
    /* signal debugger thread to continue*/
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   128
    __debug_tick = __tick;
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   129
    pthread_mutex_lock(&wait_mutex);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   130
    pthread_cond_broadcast(&wait_cond);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   131
    pthread_mutex_unlock(&wait_mutex);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   132
}