targets/plc_main_head.c
author Laurent Bessard
Mon, 14 Oct 2013 12:28:44 +0200
changeset 1365 debc97102b23
parent 1001 3f966bbb3fba
child 1391 f6818bb5e292
permissions -rw-r--r--
Added support for optimizing debug, preventing to filling buffers with only the last value for debug data consumers that only show the current state
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
     1
/**
985
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
     2
 * Head of code common to all C targets
332
4f0e1d66bba5 add utf-8 and save shortcut support in PythonST
greg
parents: 289
diff changeset
     3
 **/
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     4
1001
3f966bbb3fba Added beremiz.h header file for extensions
Edouard Tisserant
parents: 985
diff changeset
     5
#include "beremiz.h"
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     6
/*
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
     7
 * Prototypes of functions provided by generated C softPLC
332
4f0e1d66bba5 add utf-8 and save shortcut support in PythonST
greg
parents: 289
diff changeset
     8
 **/
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
     9
void config_run__(unsigned long tick);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    10
void config_init__(void);
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    11
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    12
/*
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    13
 * Prototypes of functions provided by generated target C code
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    14
 * */
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 209
diff changeset
    15
void __init_debug(void);
a66e150f2888 Improved debug data feedback.
etisserant
parents: 209
diff changeset
    16
void __cleanup_debug(void);
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    17
/*void __retrieve_debug(void);*/
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    18
void __publish_debug(void);
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 209
diff changeset
    19
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    20
/*
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    21
 *  Variables used by generated C softPLC and plugins
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    22
 **/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    23
IEC_TIME __CURRENT_TIME;
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    24
IEC_BOOL __DEBUG = 0;
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 423
diff changeset
    25
unsigned long __tick = 0;
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    26
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    27
/*
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    28
 *  Variable generated by C softPLC and plugins
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    29
 **/
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    30
extern unsigned long greatest_tick_count__;
985
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    31
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    32
/* Effective tick time with 1ms default value */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    33
static long long Ttick = 1000000;
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    34
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    35
/* Help to quit cleanly when init fail at a certain level */
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    36
static int init_level = 0;
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    37
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    38
/*
332
4f0e1d66bba5 add utf-8 and save shortcut support in PythonST
greg
parents: 289
diff changeset
    39
 * Prototypes of functions exported by plugins
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    40
 **/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    41
%(calls_prototypes)s
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    42
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    43
/*
332
4f0e1d66bba5 add utf-8 and save shortcut support in PythonST
greg
parents: 289
diff changeset
    44
 * Retrieve input variables, run PLC and publish output variables
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    45
 **/
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
    46
void __run(void)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    47
{
236
a32817e81f5e Now debug all ticks, not only odd ones :-)
etisserant
parents: 235
diff changeset
    48
    __tick++;
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    49
    if (greatest_tick_count__)
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    50
        __tick %%= greatest_tick_count__;
236
a32817e81f5e Now debug all ticks, not only odd ones :-)
etisserant
parents: 235
diff changeset
    51
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    52
    %(retrieve_calls)s
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    53
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
    54
    /*__retrieve_debug();*/
332
4f0e1d66bba5 add utf-8 and save shortcut support in PythonST
greg
parents: 289
diff changeset
    55
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    56
    config_run__(__tick);
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    57
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
    58
    __publish_debug();
332
4f0e1d66bba5 add utf-8 and save shortcut support in PythonST
greg
parents: 289
diff changeset
    59
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    60
    %(publish_calls)s
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    61
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    62
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    63
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    64
/*
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
    65
 * Initialize variables according to PLC's default values,
332
4f0e1d66bba5 add utf-8 and save shortcut support in PythonST
greg
parents: 289
diff changeset
    66
 * and then init plugins with that values
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    67
 **/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    68
int __init(int argc,char **argv)
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    69
{
386
2932b0dd437c Applying patch from Iztok for old gcc versions
laurent
parents: 366
diff changeset
    70
    int res = 0;
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
    71
    init_level = 0;
985
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    72
    
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    73
    if(common_ticktime__)
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    74
        Ttick = common_ticktime__;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    75
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    76
    config_init__();
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 209
diff changeset
    77
    __init_debug();
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    78
    %(init_calls)s
386
2932b0dd437c Applying patch from Iztok for old gcc versions
laurent
parents: 366
diff changeset
    79
    return res;
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    80
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    81
/*
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    82
 * Calls plugin cleanup proc.
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    83
 **/
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
    84
void __cleanup(void)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    85
{
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    86
    %(cleanup_calls)s
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 209
diff changeset
    87
    __cleanup_debug();
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    88
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    89
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    90
void PLC_GetTime(IEC_TIME *CURRENT_TIME);
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 446
diff changeset
    91
void PLC_SetTimer(unsigned long long next, unsigned long long period);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    92
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    93
985
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 568
diff changeset
    94