targets/Xenomai/plc_Xenomai_main.c
author greg
Mon, 30 Mar 2009 17:04:30 +0200
changeset 329 22e65b8e20f4
parent 321 5a4e6278a18b
child 336 ae3488c79283
permissions -rw-r--r--
add autostart plc feature for beremiz_service
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     1
/**
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     2
 * Linux specific code
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     3
 **/ 
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     4
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     5
#include <stdio.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     6
#include <string.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     7
#include <time.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     8
#include <signal.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     9
#include <stdlib.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    10
#include <sys/mman.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    11
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    12
#include <native/task.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    13
#include <native/timer.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    14
#include <native/mutex.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    15
#include <native/sem.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    16
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    17
unsigned int PLC_state = 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    18
#define PLC_STATE_TASK_CREATED                  1
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    19
#define PLC_STATE_PYTHON_MUTEX_CREATED          2
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    20
#define PLC_STATE_PYTHON_WAIT_SEM_CREATED       4
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    21
#define PLC_STATE_DEBUG_MUTEX_CREATED           8
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    22
#define PLC_STATE_DEBUG_WAIT_SEM_CREATED       16
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    23
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    24
/* provided by POUS.C */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    25
extern int common_ticktime__;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    26
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    27
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    28
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    29
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    30
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    31
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    32
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    33
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    34
    RTIME current_time = rt_timer_read();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    35
    CURRENT_TIME->tv_sec = current_time / 1000000000;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    36
    CURRENT_TIME->tv_nsec = current_time % 1000000000;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    37
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    38
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    39
RT_TASK PLC_task;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    40
RT_TASK WaitDebug_task;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    41
RT_TASK WaitPythonCommand_task;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    42
RT_TASK UnLockPython_task;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    43
RT_TASK LockPython_task;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    44
int PLC_shutdown = 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    45
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    46
void PLC_SetTimer(long long next, long long period)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    47
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    48
  RTIME current_time = rt_timer_read();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    49
  rt_task_set_periodic(&PLC_task, current_time + next, rt_timer_ns2ticks(period));
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    50
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    51
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    52
void PLC_task_proc(void *arg)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    53
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    54
    PLC_SetTimer(Ttick, Ttick);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    55
  
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    56
    while (1) {
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    57
        PLC_GetTime(&__CURRENT_TIME);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    58
        __run();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    59
        if (PLC_shutdown) break;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    60
        rt_task_wait_period(NULL);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    61
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    62
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    63
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    64
static int __debug_tick;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    65
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    66
RT_SEM python_wait_sem;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    67
RT_MUTEX python_mutex;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    68
RT_SEM debug_wait_sem;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    69
RT_MUTEX debug_mutex;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    70
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    71
void PLC_cleanup_all(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    72
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    73
    if (PLC_state & PLC_STATE_TASK_CREATED) {
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    74
        rt_task_delete(&PLC_task);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    75
        PLC_state &= ~PLC_STATE_TASK_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    76
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    77
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    78
    if (PLC_state & PLC_STATE_PYTHON_WAIT_SEM_CREATED) {
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    79
        rt_sem_delete(&python_wait_sem);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    80
        PLC_state &= ~ PLC_STATE_PYTHON_WAIT_SEM_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    81
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    82
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    83
    if (PLC_state & PLC_STATE_PYTHON_MUTEX_CREATED) {
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    84
        rt_mutex_delete(&python_mutex);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    85
        PLC_state &= ~ PLC_STATE_PYTHON_MUTEX_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    86
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    87
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    88
    if (PLC_state & PLC_STATE_DEBUG_WAIT_SEM_CREATED) {
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    89
        rt_sem_delete(&debug_wait_sem);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    90
        PLC_state &= ~ PLC_STATE_DEBUG_WAIT_SEM_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    91
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    92
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    93
    if (PLC_state & PLC_STATE_DEBUG_MUTEX_CREATED) {
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    94
        rt_mutex_delete(&debug_mutex);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    95
        PLC_state &= ~ PLC_STATE_DEBUG_MUTEX_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    96
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    97
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    98
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    99
int stopPLC()
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   100
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   101
    PLC_shutdown = 1;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   102
    /* Stop the PLC */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   103
    PLC_SetTimer(0, 0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   104
    PLC_cleanup_all();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   105
    __cleanup();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   106
    __debug_tick = -1;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   107
    rt_sem_v(&debug_wait_sem);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   108
    rt_sem_v(&python_wait_sem);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   109
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   110
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   111
//
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   112
void catch_signal(int sig)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   113
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   114
    stopPLC();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   115
//  signal(SIGTERM, catch_signal);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   116
    signal(SIGINT, catch_signal);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   117
    printf("Got Signal %d\n",sig);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   118
    exit(0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   119
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   120
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   121
#define max_val(a,b) ((a>b)?a:b)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   122
int startPLC(int argc,char **argv)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   123
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   124
    int ret = 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   125
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   126
    signal(SIGINT, catch_signal);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   127
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   128
    /* ne-memory-swapping for this program */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   129
    mlockall(MCL_CURRENT | MCL_FUTURE);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   130
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   131
    /* Translate PLC's microseconds to Ttick nanoseconds */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   132
    Ttick = 1000000 * max_val(common_ticktime__,1);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   133
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   134
    /* create python_wait_sem */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   135
    ret = rt_sem_create(&python_wait_sem, "python_wait_sem", 0, S_FIFO);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   136
    if (ret) goto error;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   137
    PLC_state |= PLC_STATE_PYTHON_WAIT_SEM_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   138
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   139
    /* create python_mutex */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   140
    ret = rt_mutex_create(&python_mutex, "python_mutex");
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   141
    if (ret) goto error;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   142
    PLC_state |= PLC_STATE_PYTHON_MUTEX_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   143
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   144
    /* create debug_wait_sem */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   145
    ret = rt_sem_create(&debug_wait_sem, "debug_wait_sem", 0, S_FIFO);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   146
    if (ret) goto error;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   147
    PLC_state |= PLC_STATE_DEBUG_WAIT_SEM_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   148
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   149
    /* create debug_mutex */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   150
    ret = rt_mutex_create(&debug_mutex, "debug_mutex");
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   151
    if (ret) goto error;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   152
    PLC_state |= PLC_STATE_DEBUG_MUTEX_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   153
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   154
    /* create can_driver_task */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   155
    ret = rt_task_create(&PLC_task, "PLC_task", 0, 50, 0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   156
    if (ret) goto error;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   157
    PLC_state |= PLC_STATE_TASK_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   158
    
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   159
    ret = __init(argc,argv);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   160
    if (ret) goto error;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   161
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   162
    /* start can_driver_task */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   163
    ret = rt_task_start(&PLC_task, &PLC_task_proc, NULL);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   164
    if (ret) goto error;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   165
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   166
    return 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   167
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   168
error:
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   169
    PLC_cleanup_all();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   170
    return 1;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   171
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   172
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   173
int TryEnterDebugSection(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   174
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   175
    return rt_mutex_acquire(&debug_mutex, TM_NONBLOCK) == 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   176
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   177
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   178
void LeaveDebugSection(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   179
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   180
    rt_mutex_release(&debug_mutex);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   181
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   182
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   183
extern int __tick;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   184
/* from plc_debugger.c */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   185
int WaitDebugData()
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   186
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   187
    rt_task_shadow(&WaitDebug_task, "WaitDebug_task", 0, 0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   188
    /* Wait signal from PLC thread */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   189
    rt_sem_p(&debug_wait_sem, TM_INFINITE);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   190
    return __debug_tick;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   191
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   192
 
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   193
/* Called by PLC thread when debug_publish finished
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   194
 * This is supposed to unlock debugger thread in WaitDebugData*/
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   195
void InitiateDebugTransfer()
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   196
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   197
    /* remember tick */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   198
    __debug_tick = __tick;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   199
    /* signal debugger thread it can read data */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   200
    rt_sem_v(&debug_wait_sem);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   201
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   202
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   203
void suspendDebug(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   204
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   205
    __DEBUG = 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   206
    /* Prevent PLC to enter debug code */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   207
    rt_mutex_acquire(&debug_mutex, TM_INFINITE);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   208
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   209
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   210
void resumeDebug(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   211
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   212
    __DEBUG = 1;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   213
    /* Let PLC enter debug code */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   214
    rt_mutex_release(&debug_mutex);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   215
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   216
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   217
/* from plc_python.c */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   218
int WaitPythonCommands(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   219
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   220
    rt_task_shadow(&WaitPythonCommand_task, "WaitPythonCommand_task", 0, 0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   221
    /* Wait signal from PLC thread */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   222
    rt_sem_p(&python_wait_sem, TM_INFINITE);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   223
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   224
 
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   225
/* Called by PLC thread on each new python command*/
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   226
void UnBlockPythonCommands(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   227
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   228
    /* signal debugger thread it can read data */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   229
    rt_sem_v(&python_wait_sem);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   230
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   231
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   232
int TryLockPython(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   233
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   234
    return rt_mutex_acquire(&python_mutex, TM_NONBLOCK) == 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   235
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   236
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   237
void UnLockPython(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   238
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   239
    rt_task_shadow(&UnLockPython_task, "UnLockPython_task", 0, 0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   240
    rt_mutex_release(&python_mutex);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   241
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   242
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   243
void LockPython(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   244
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   245
    rt_task_shadow(&LockPython_task, "LockPython_task", 0, 0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   246
    rt_mutex_acquire(&python_mutex, TM_INFINITE);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   247
}