targets/Xenomai/plc_Xenomai_main.c
author Surkov Sergey <surkovsv93@gmail.com>
Wed, 31 Jan 2018 16:31:01 +0300
changeset 1968 64827679f3f1
parent 1717 a86deec92e08
child 1903 084256be3658
permissions -rw-r--r--
add fields autocomplete when adding task or instance in resources

values will be copied from previous row, first row fills with the default values
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     1
/**
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 745
diff changeset
     2
 * Xenomai Linux specific code
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
     3
 **/
321
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>
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
     6
#include <unistd.h>
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     7
#include <string.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     8
#include <time.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
     9
#include <signal.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    10
#include <stdlib.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    11
#include <sys/mman.h>
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
    12
#include <sys/fcntl.h>
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    13
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    14
#include <native/task.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    15
#include <native/timer.h>
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    16
#include <native/sem.h>
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
    17
#include <native/pipe.h>
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    18
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    19
unsigned int PLC_state = 0;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    20
#define PLC_STATE_TASK_CREATED                 1
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    21
#define PLC_STATE_DEBUG_FILE_OPENED            2 
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    22
#define PLC_STATE_DEBUG_PIPE_CREATED           4 
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    23
#define PLC_STATE_PYTHON_FILE_OPENED           8 
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    24
#define PLC_STATE_PYTHON_PIPE_CREATED          16   
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    25
#define PLC_STATE_WAITDEBUG_FILE_OPENED        32   
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    26
#define PLC_STATE_WAITDEBUG_PIPE_CREATED       64
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    27
#define PLC_STATE_WAITPYTHON_FILE_OPENED       128
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    28
#define PLC_STATE_WAITPYTHON_PIPE_CREATED      256
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    29
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    30
#define WAITDEBUG_PIPE_DEVICE        "/dev/rtp0"
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    31
#define WAITDEBUG_PIPE_MINOR         0
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    32
#define DEBUG_PIPE_DEVICE            "/dev/rtp1"
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    33
#define DEBUG_PIPE_MINOR             1
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    34
#define WAITPYTHON_PIPE_DEVICE       "/dev/rtp2"
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    35
#define WAITPYTHON_PIPE_MINOR        2
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    36
#define PYTHON_PIPE_DEVICE           "/dev/rtp3"
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    37
#define PYTHON_PIPE_MINOR            3
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    38
#define PIPE_SIZE                    1 
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    39
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    40
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    41
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    42
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    43
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    44
}
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 745
diff changeset
    45
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 745
diff changeset
    46
{
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 745
diff changeset
    47
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 745
diff changeset
    48
}
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    49
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    50
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    51
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    52
    RTIME current_time = rt_timer_read();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    53
    CURRENT_TIME->tv_sec = current_time / 1000000000;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    54
    CURRENT_TIME->tv_nsec = current_time % 1000000000;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    55
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    56
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    57
RT_TASK PLC_task;
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
    58
RT_PIPE WaitDebug_pipe;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    59
RT_PIPE WaitPython_pipe;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    60
RT_PIPE Debug_pipe;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    61
RT_PIPE Python_pipe;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    62
int WaitDebug_pipe_fd;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    63
int WaitPython_pipe_fd;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    64
int Debug_pipe_fd;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    65
int Python_pipe_fd;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    66
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    67
int PLC_shutdown = 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    68
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    69
void PLC_SetTimer(unsigned long long next, unsigned long long period)
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    70
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    71
  RTIME current_time = rt_timer_read();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    72
  rt_task_set_periodic(&PLC_task, current_time + next, rt_timer_ns2ticks(period));
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    73
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    74
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    75
void PLC_task_proc(void *arg)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    76
{
1428
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 985
diff changeset
    77
    PLC_SetTimer(common_ticktime__, common_ticktime__);
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
    78
695
f378fe028f6c Fixed stopping crash in some cases
Edouard Tisserant
parents: 617
diff changeset
    79
    while (!PLC_shutdown) {
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    80
        PLC_GetTime(&__CURRENT_TIME);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    81
        __run();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    82
        if (PLC_shutdown) break;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    83
        rt_task_wait_period(NULL);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    84
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    85
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    86
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 345
diff changeset
    87
static unsigned long __debug_tick;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    88
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    89
void PLC_cleanup_all(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    90
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    91
    if (PLC_state & PLC_STATE_TASK_CREATED) {
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    92
        rt_task_delete(&PLC_task);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    93
        PLC_state &= ~PLC_STATE_TASK_CREATED;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    94
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
    95
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    96
    if (PLC_state & PLC_STATE_WAITDEBUG_PIPE_CREATED) {
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    97
        rt_pipe_delete(&WaitDebug_pipe);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    98
        PLC_state &= ~PLC_STATE_WAITDEBUG_PIPE_CREATED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
    99
    }
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   100
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   101
    if (PLC_state & PLC_STATE_WAITDEBUG_FILE_OPENED) {
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   102
        close(WaitDebug_pipe_fd);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   103
        PLC_state &= ~PLC_STATE_WAITDEBUG_FILE_OPENED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   104
    }
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   105
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   106
    if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) {
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   107
        rt_pipe_delete(&WaitPython_pipe);
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   108
        PLC_state &= ~PLC_STATE_WAITPYTHON_PIPE_CREATED;
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   109
    }
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   110
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   111
    if (PLC_state & PLC_STATE_WAITPYTHON_FILE_OPENED) {
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   112
        close(WaitPython_pipe_fd);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   113
        PLC_state &= ~PLC_STATE_WAITPYTHON_FILE_OPENED;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   114
    }
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
   115
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   116
    if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) {
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   117
        rt_pipe_delete(&Debug_pipe);
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   118
        PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED;
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   119
    }
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   120
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   121
    if (PLC_state & PLC_STATE_DEBUG_FILE_OPENED) {
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   122
        close(Debug_pipe_fd);
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   123
        PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   124
    }
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   125
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   126
    if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) {
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   127
        rt_pipe_delete(&Python_pipe);
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   128
        PLC_state &= ~PLC_STATE_PYTHON_PIPE_CREATED;
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   129
    }
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   130
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   131
    if (PLC_state & PLC_STATE_PYTHON_FILE_OPENED) {
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   132
        close(Python_pipe_fd);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   133
        PLC_state &= ~PLC_STATE_PYTHON_FILE_OPENED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   134
    }
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   135
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   136
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   137
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   138
int stopPLC()
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   139
{
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   140
    /* Stop the PLC */
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   141
    PLC_shutdown = 1;
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   142
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   143
    /* Wait until PLC task stops */
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   144
    rt_task_join(&PLC_task);
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   145
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   146
    PLC_cleanup_all();
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   147
    __cleanup();
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   148
    __debug_tick = -1;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   149
    return 0;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   150
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   151
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   152
//
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   153
void catch_signal(int sig)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   154
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   155
    stopPLC();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   156
//  signal(SIGTERM, catch_signal);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   157
    signal(SIGINT, catch_signal);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   158
    printf("Got Signal %d\n",sig);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   159
    exit(0);
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   160
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   161
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   162
#define max_val(a,b) ((a>b)?a:b)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   163
int startPLC(int argc,char **argv)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   164
{
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   165
    signal(SIGINT, catch_signal);
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
   166
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   167
    /* no memory swapping for that process */
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   168
    mlockall(MCL_CURRENT | MCL_FUTURE);
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
   169
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   170
    PLC_shutdown = 0;
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   171
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   172
    /*** RT Pipes creation and opening ***/
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   173
    /* create Debug_pipe */
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   174
    if(rt_pipe_create(&Debug_pipe, "Debug_pipe", DEBUG_PIPE_MINOR, PIPE_SIZE)) 
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   175
        goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   176
    PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   177
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   178
    /* open Debug_pipe*/
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   179
    if((Debug_pipe_fd = open(DEBUG_PIPE_DEVICE, O_RDWR)) == -1) goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   180
    PLC_state |= PLC_STATE_DEBUG_FILE_OPENED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   181
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   182
    /* create Python_pipe */
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   183
    if(rt_pipe_create(&Python_pipe, "Python_pipe", PYTHON_PIPE_MINOR, PIPE_SIZE)) 
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   184
        goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   185
    PLC_state |= PLC_STATE_PYTHON_PIPE_CREATED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   186
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   187
    /* open Python_pipe*/
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   188
    if((Python_pipe_fd = open(PYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   189
    PLC_state |= PLC_STATE_PYTHON_FILE_OPENED;
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
   190
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   191
    /* create WaitDebug_pipe */
616
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   192
    if(rt_pipe_create(&WaitDebug_pipe, "WaitDebug_pipe", WAITDEBUG_PIPE_MINOR, PIPE_SIZE))
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   193
        goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   194
    PLC_state |= PLC_STATE_WAITDEBUG_PIPE_CREATED;
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   195
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   196
    /* open WaitDebug_pipe*/
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   197
    if((WaitDebug_pipe_fd = open(WAITDEBUG_PIPE_DEVICE, O_RDWR)) == -1) goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   198
    PLC_state |= PLC_STATE_WAITDEBUG_FILE_OPENED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   199
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   200
    /* create WaitPython_pipe */
616
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   201
    if(rt_pipe_create(&WaitPython_pipe, "WaitPython_pipe", WAITPYTHON_PIPE_MINOR, PIPE_SIZE))
342
80e5876bc53b Replacing Debug rt_sem by rt_pipe
greg
parents: 336
diff changeset
   202
        goto error;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   203
    PLC_state |= PLC_STATE_WAITPYTHON_PIPE_CREATED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   204
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   205
    /* open WaitPython_pipe*/
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   206
    if((WaitPython_pipe_fd = open(WAITPYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   207
    PLC_state |= PLC_STATE_WAITPYTHON_FILE_OPENED;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   208
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   209
    /*** create PLC task ***/
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   210
    if(rt_task_create(&PLC_task, "PLC_task", 0, 50, T_JOINABLE)) goto error;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   211
    PLC_state |= PLC_STATE_TASK_CREATED;
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
   212
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   213
    if(__init(argc,argv)) goto error;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   214
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   215
    /* start PLC task */
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   216
    if(rt_task_start(&PLC_task, &PLC_task_proc, NULL)) goto error;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   217
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   218
    return 0;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   219
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   220
error:
616
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   221
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   222
    PLC_cleanup_all();
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   223
    return 1;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   224
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   225
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   226
#define DEBUG_FREE 0
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   227
#define DEBUG_BUSY 1
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   228
static long debug_state = DEBUG_FREE;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   229
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   230
int TryEnterDebugSection(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   231
{
616
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   232
    if(AtomicCompareExchange(
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   233
        &debug_state,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   234
        DEBUG_FREE,
616
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   235
        DEBUG_BUSY) == DEBUG_FREE){
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   236
        if(__DEBUG){
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   237
            return 1;
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   238
        }
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   239
        AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE);
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   240
    }
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   241
    return 0;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   242
}
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   243
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   244
#define DEBUG_UNLOCK 1
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   245
void LeaveDebugSection(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   246
{
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   247
    if(AtomicCompareExchange( &debug_state, 
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   248
        DEBUG_BUSY, DEBUG_FREE) == DEBUG_BUSY){
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   249
        char msg = DEBUG_UNLOCK;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   250
        /* signal to NRT for wakeup */
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   251
        rt_pipe_write(&Debug_pipe, &msg, sizeof(msg), P_NORMAL);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   252
    }
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   253
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   254
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 345
diff changeset
   255
extern unsigned long __tick;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   256
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   257
#define DEBUG_PENDING_DATA 1
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   258
int WaitDebugData(unsigned long *tick)
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   259
{
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   260
    char cmd;
345
a3520d75a722 get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents: 342
diff changeset
   261
    int res;
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   262
    if (PLC_shutdown) return -1;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   263
    /* Wait signal from PLC thread */
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   264
    res = read(WaitDebug_pipe_fd, &cmd, sizeof(cmd));
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 616
diff changeset
   265
    if (res == sizeof(cmd) && cmd == DEBUG_PENDING_DATA){
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 616
diff changeset
   266
        *tick = __debug_tick;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   267
        return 0;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 616
diff changeset
   268
    }
345
a3520d75a722 get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents: 342
diff changeset
   269
    return -1;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   270
}
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
   271
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   272
/* Called by PLC thread when debug_publish finished
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   273
 * This is supposed to unlock debugger thread in WaitDebugData*/
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   274
void InitiateDebugTransfer()
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   275
{
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   276
    char msg = DEBUG_PENDING_DATA;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   277
    /* remember tick */
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   278
    __debug_tick = __tick;
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   279
    /* signal debugger thread it can read data */
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   280
    rt_pipe_write(&WaitDebug_pipe, &msg, sizeof(msg), P_NORMAL);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   281
}
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   282
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   283
int suspendDebug(int disable)
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   284
{
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   285
    char cmd = DEBUG_UNLOCK;
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   286
    if (PLC_shutdown) return -1;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   287
    while(AtomicCompareExchange(
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   288
            &debug_state,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   289
            DEBUG_FREE,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   290
            DEBUG_BUSY) != DEBUG_FREE &&
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   291
            cmd == DEBUG_UNLOCK){
616
b9271faec96e Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents: 615
diff changeset
   292
       if(read(Debug_pipe_fd, &cmd, sizeof(cmd)) != sizeof(cmd)){
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   293
           return -1;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   294
       }
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   295
    }
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   296
    __DEBUG = !disable;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 616
diff changeset
   297
    if (disable)
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 616
diff changeset
   298
        AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE);
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   299
    return 0;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   300
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   301
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   302
void resumeDebug(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   303
{
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   304
    AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   305
}
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   306
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   307
#define PYTHON_PENDING_COMMAND 1
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   308
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   309
#define PYTHON_FREE 0
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   310
#define PYTHON_BUSY 1
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   311
static long python_state = PYTHON_FREE;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   312
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   313
int WaitPythonCommands(void)
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   314
{ 
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   315
    char cmd;
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   316
    if (PLC_shutdown) return -1;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   317
    /* Wait signal from PLC thread */
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   318
    if(read(WaitPython_pipe_fd, &cmd, sizeof(cmd))==sizeof(cmd) && cmd==PYTHON_PENDING_COMMAND){
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   319
        return 0;
345
a3520d75a722 get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents: 342
diff changeset
   320
    }
a3520d75a722 get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents: 342
diff changeset
   321
    return -1;
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   322
}
336
ae3488c79283 Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents: 321
diff changeset
   323
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   324
/* Called by PLC thread on each new python command*/
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   325
void UnBlockPythonCommands(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   326
{
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   327
    char msg = PYTHON_PENDING_COMMAND;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   328
    rt_pipe_write(&WaitPython_pipe, &msg, sizeof(msg), P_NORMAL);
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   329
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   330
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   331
int TryLockPython(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   332
{
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   333
    return AtomicCompareExchange(
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   334
        &python_state,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   335
        PYTHON_FREE,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   336
        PYTHON_BUSY) == PYTHON_FREE;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   337
}
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   338
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   339
#define UNLOCK_PYTHON 1
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   340
void LockPython(void)
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   341
{
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   342
    char cmd = UNLOCK_PYTHON;
745
96dd6650854d Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents: 695
diff changeset
   343
    if (PLC_shutdown) return;
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   344
    while(AtomicCompareExchange(
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   345
            &python_state,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   346
            PYTHON_FREE,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   347
            PYTHON_BUSY) != PYTHON_FREE &&
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   348
            cmd == UNLOCK_PYTHON){
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   349
       read(Python_pipe_fd, &cmd, sizeof(cmd));
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   350
    }
321
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   351
}
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   352
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   353
void UnLockPython(void)
5a4e6278a18b Adding support for Xenomai targets.
lbessard
parents:
diff changeset
   354
{
615
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   355
    if(AtomicCompareExchange(
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   356
            &python_state,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   357
            PYTHON_BUSY,
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   358
            PYTHON_FREE) == PYTHON_BUSY){
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   359
        if(rt_task_self()){/*is that the real time task ?*/
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   360
           char cmd = UNLOCK_PYTHON;
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   361
           rt_pipe_write(&Python_pipe, &cmd, sizeof(cmd), P_NORMAL);
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   362
        }/* otherwise, no signaling from non real time */
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   363
    }    /* as plc does not wait for lock. */
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   364
}
72bc3e53a1fa Xenomai Fix : build
Edouard Tisserant
parents: 446
diff changeset
   365
1717
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   366
int CheckRetainBuffer(void)
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   367
{
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   368
	return 1;
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   369
}
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   370
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   371
void ValidateRetainBuffer(void)
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   372
{
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   373
}
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   374
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   375
void InValidateRetainBuffer(void)
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   376
{
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   377
}
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   378
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   379
void Retain(unsigned int offset, unsigned int count, void *p)
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   380
{
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   381
}
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   382
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   383
void Remind(unsigned int offset, unsigned int count, void *p)
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   384
{
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   385
}
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   386
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   387
void CleanupRetain(void)
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   388
{
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   389
}
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   390
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   391
void InitRetain(void)
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   392
{
a86deec92e08 fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents: 1456
diff changeset
   393
}