targets/plc_common_main.c
changeset 397 6a7ff66a811d
parent 386 2932b0dd437c
child 423 4d7ac355701d
equal deleted inserted replaced
396:d1083f580ca1 397:6a7ff66a811d
     3  **/
     3  **/
     4 
     4 
     5 #include <locale.h>
     5 #include <locale.h>
     6 #include "iec_types.h"
     6 #include "iec_types.h"
     7 /*
     7 /*
     8  * Prototypes of functions provied by generated C softPLC
     8  * Prototypes of functions provided by generated C softPLC
     9  **/
     9  **/
    10 void config_run__(int tick);
    10 void config_run__(unsigned long tick);
    11 void config_init__(void);
    11 void config_init__(void);
    12 
    12 
    13 /*
    13 /*
    14  * Prototypes of functions provied by generated target C code
    14  * Prototypes of functions provided by generated target C code
    15  * */
    15  * */
    16 void __init_debug(void);
    16 void __init_debug(void);
    17 void __cleanup_debug(void);
    17 void __cleanup_debug(void);
    18 /*void __retrieve_debug(void);*/
    18 /*void __retrieve_debug(void);*/
    19 void __publish_debug(void);
    19 void __publish_debug(void);
    20 
    20 
    21 /*
    21 /*
    22  *  Variables used by generated C softPLC and plugins
    22  *  Variables used by generated C softPLC and plugins
    23  **/
    23  **/
    24 IEC_TIME __CURRENT_TIME;
    24 IEC_TIME __CURRENT_TIME;
    25 IEC_BOOL __DEBUG;
    25 IEC_BOOL __DEBUG = 0;
    26 int __tick = -1;
    26 unsigned long __tick = -1;
       
    27 
       
    28 /*
       
    29  *  Variable generated by C softPLC and plugins
       
    30  **/
       
    31 extern unsigned long greatest_tick_count__;
    27 
    32 
    28 /* Help to quit cleanly when init fail at a certain level */
    33 /* Help to quit cleanly when init fail at a certain level */
    29 static int init_level = 0;
    34 static int init_level = 0;
    30 
    35 
    31 /*
    36 /*
    37  * Retrieve input variables, run PLC and publish output variables
    42  * Retrieve input variables, run PLC and publish output variables
    38  **/
    43  **/
    39 void __run()
    44 void __run()
    40 {
    45 {
    41     __tick++;
    46     __tick++;
       
    47     if (greatest_tick_count__)
       
    48         __tick %%= greatest_tick_count__;
    42 
    49 
    43     %(retrieve_calls)s
    50     %(retrieve_calls)s
    44 
    51 
    45     /*__retrieve_debug();*/
    52     /*__retrieve_debug();*/
    46 
    53 
    51     %(publish_calls)s
    58     %(publish_calls)s
    52 
    59 
    53 }
    60 }
    54 
    61 
    55 /*
    62 /*
    56  * Initialize variables according to PLC's defalut values,
    63  * Initialize variables according to PLC's default values,
    57  * and then init plugins with that values
    64  * and then init plugins with that values
    58  **/
    65  **/
    59 int __init(int argc,char **argv)
    66 int __init(int argc,char **argv)
    60 {
    67 {
    61     int res = 0;
    68     int res = 0;
    83 static int calibration_count = NOT_CALIBRATED;
    90 static int calibration_count = NOT_CALIBRATED;
    84 static IEC_TIME cal_begin;
    91 static IEC_TIME cal_begin;
    85 static long long Tsync = 0;
    92 static long long Tsync = 0;
    86 static long long FreqCorr = 0;
    93 static long long FreqCorr = 0;
    87 static int Nticks = 0;
    94 static int Nticks = 0;
    88 static int  last_tick = 0;
    95 static unsigned long last_tick = 0;
    89 static long long Ttick = 0;
    96 static long long Ttick = 0;
    90 #define mod %%
    97 #define mod %%
    91 /*
    98 /*
    92  * Call this on each external sync,
    99  * Call this on each external sync,
    93  * @param sync_align_ratio 0->100 : align ratio, < 0 : no align, calibrate period
   100  * @param sync_align_ratio 0->100 : align ratio, < 0 : no align, calibrate period