* Code common to all C targets
* Prototypes of functions provided by generated C softPLC
void config_run__(unsigned long tick);
void config_init__(void);
* Prototypes of functions provided by generated target C code
void __cleanup_debug(void);
/*void __retrieve_debug(void);*/
void __publish_debug(void);
* Variables used by generated C softPLC and plugins
unsigned long __tick = 0;
* Variable generated by C softPLC and plugins
extern unsigned long greatest_tick_count__;
/* Help to quit cleanly when init fail at a certain level */
static int init_level = 0;
* Prototypes of functions exported by plugins
* Retrieve input variables, run PLC and publish output variables
if (greatest_tick_count__)
__tick %%= greatest_tick_count__;
* Initialize variables according to PLC's default values,
* and then init plugins with that values
int __init(int argc,char **argv)
* Calls plugin cleanup proc.
void PLC_GetTime(IEC_TIME *CURRENT_TIME);
void PLC_SetTimer(unsigned long long next, unsigned long long period);
#define NOT_CALIBRATED -1
static int calibration_count = NOT_CALIBRATED;
static IEC_TIME cal_begin;
static long long Tsync = 0;
static long long FreqCorr = 0;
static unsigned long last_tick = 0;
static long long Ttick = 0;
* Call this on each external sync,
* @param sync_align_ratio 0->100 : align ratio, < 0 : no align, calibrate period
void align_tick(int sync_align_ratio)
printf("align_tick(%%d)\n", calibrate);
if(sync_align_ratio < 0){ /* Calibration */
if(calibration_count == CALIBRATED)
calibration_count = NOT_CALIBRATED;
if(calibration_count == NOT_CALIBRATED)
/* Calibration start, get time*/
}else{ /* do alignment (if possible) */
if(calibration_count >= 0){
/*adjust calibration_count*/
/* compute mean of Tsync, over calibration period */
Tsync = ((long long)(cal_end.tv_sec - cal_begin.tv_sec) * (long long)1000000000 +
(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
if( (Nticks = (Tsync / Ttick)) > 0){
FreqCorr = (Tsync mod Ttick); /* to be divided by Nticks */
FreqCorr = Tsync - (Ttick mod Tsync);
printf("Tsync = %%ld\n", Tsync);
printf("calibration_count = %%d\n", calibration_count);
printf("Nticks = %%d\n", Nticks);
calibration_count = CALIBRATED;
if(calibration_count == CALIBRATED){
/* Get Elapsed time since last PLC tick (__CURRENT_TIME) */
elapsed = (now.tv_sec - __CURRENT_TIME.tv_sec) * 1000000000 + now.tv_nsec - __CURRENT_TIME.tv_nsec;
PhaseCorr = elapsed - (Ttick + FreqCorr/Nticks)*sync_align_ratio/100; /* to be divided by Nticks */
Tcorr = Ttick + (PhaseCorr + FreqCorr) / Nticks;
/* When Sync source period is near Tick time */
/* PhaseCorr may not be applied to Periodic time given to timer */
PeriodicTcorr = Ttick + FreqCorr / Nticks;
}else if(__tick > last_tick){
PhaseCorr = elapsed - (Tsync*sync_align_ratio/100);
PeriodicTcorr = Tcorr = Ttick + PhaseCorr + FreqCorr;
/*PLC did not run meanwhile. Nothing to do*/
PLC_SetTimer(Tcorr - elapsed, PeriodicTcorr);
* Prototypes for function provided by arch-specific code (main)
* is concatained hereafter