targets/plc_main_tail.c
author Laurent Bessard
Sun, 17 Mar 2013 19:03:25 +0100
changeset 989 b24c11c93766
parent 985 cd8dadcef426
child 991 afc4963d8f0c
permissions -rw-r--r--
Fixed bug editors empty after being opened
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: 969
diff changeset
     1
/**
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
     2
 * Tail of code common to all C targets
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     3
 **/
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: 969
diff changeset
     4
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
     5
/** 
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
     6
 * LOGGING
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     7
 **/
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
     8
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
     9
#define LOG_LEVELS 4
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    10
#define LOG_CRITICAL 0
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    11
#define LOG_WARNING 1
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    12
#define LOG_INFO 2
923
6ef6e0b3a908 Fixed crash (segfault) when logging debug messages
Edouard Tisserant
parents: 921
diff changeset
    13
#define LOG_DEBUG 3
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    14
944
52a17be9c4d1 Introduce Beremiz Native POU library. Now LOGGER POU is part of Beremiz' native POU library
Edouard Tisserant
parents: 923
diff changeset
    15
#ifndef LOG_BUFFER_SIZE
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    16
#define LOG_BUFFER_SIZE (1<<14) /*16Ko*/
944
52a17be9c4d1 Introduce Beremiz Native POU library. Now LOGGER POU is part of Beremiz' native POU library
Edouard Tisserant
parents: 923
diff changeset
    17
#endif
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: 969
diff changeset
    18
#ifndef LOG_BUFFER_ATTRS
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
    19
#define LOG_BUFFER_ATTRS
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
    20
#endif
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
    21
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    22
#define LOG_BUFFER_MASK (LOG_BUFFER_SIZE-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: 969
diff changeset
    23
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
    24
static char LogBuff[LOG_LEVELS][LOG_BUFFER_SIZE] LOG_BUFFER_ATTRS;
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    25
void inline copy_to_log(uint8_t level, uint32_t buffpos, void* buf, uint32_t size){
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    26
    if(buffpos + size < LOG_BUFFER_SIZE){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    27
        memcpy(&LogBuff[level][buffpos], buf, size);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    28
    }else{
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    29
        uint32_t remaining = LOG_BUFFER_SIZE - buffpos - 1; 
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    30
        memcpy(&LogBuff[level][buffpos], buf, remaining);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    31
        memcpy(LogBuff[level], buf + remaining, size - remaining);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    32
    }
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    33
}
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    34
void inline copy_from_log(uint8_t level, uint32_t buffpos, void* buf, uint32_t size){
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    35
    if(buffpos + size < LOG_BUFFER_SIZE){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    36
        memcpy(buf, &LogBuff[level][buffpos], size);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    37
    }else{
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
    38
        uint32_t remaining = LOG_BUFFER_SIZE - buffpos; 
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    39
        memcpy(buf, &LogBuff[level][buffpos], remaining);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    40
        memcpy(buf + remaining, LogBuff[level], size - remaining);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    41
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    42
}
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    43
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    44
/* Log buffer structure
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    45
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    46
 |<-Tail1.msgsize->|<-sizeof(mTail)->|<--Tail2.msgsize-->|<-sizeof(mTail)->|...
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    47
 |  Message1 Body  |      Tail1      |   Message2 Body   |      Tail2      |
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    48
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    49
*/
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    50
typedef struct {
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    51
    uint32_t msgidx;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    52
    uint32_t msgsize;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    53
    unsigned long tick;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    54
    IEC_TIME time;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    55
} mTail;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    56
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    57
/* Log cursor : 64b
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    58
   |63 ... 32|31 ... 0|
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    59
   | Message | Buffer |
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    60
   | counter | Index  | */
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    61
static uint64_t LogCursor[LOG_LEVELS] = {0x0,0x0,0x0,0x0};
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    62
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    63
/* Store one log message of give size */
969
1950fe687dde Fix warning with LogMessage function
Laurent Bessard
parents: 954
diff changeset
    64
int LogMessage(uint8_t level, uint8_t* buf, uint32_t size){
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    65
    if(size < LOG_BUFFER_SIZE - sizeof(mTail)){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    66
        uint32_t buffpos;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    67
        uint64_t new_cursor, old_cursor;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    68
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    69
        mTail tail;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    70
        tail.msgsize = size;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    71
        tail.tick = __tick;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    72
        PLC_GetTime(&tail.time);
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    73
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    74
        /* We cannot increment both msg index and string pointer 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    75
           in a single atomic operation but we can detect having been interrupted.
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    76
           So we can try with atomic compare and swap in a loop until operation
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    77
           succeeds non interrupted */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    78
        do{
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    79
            old_cursor = LogCursor[level];
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    80
            buffpos = (uint32_t)old_cursor;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    81
            tail.msgidx = (old_cursor >> 32); 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    82
            new_cursor = ((uint64_t)(tail.msgidx + 1)<<32) 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    83
                         | (uint64_t)((buffpos + size + sizeof(mTail)) & LOG_BUFFER_MASK);
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
    84
        }while(AtomicCompareExchange64(
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
    85
            (long long*)&LogCursor[level],
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
    86
            (long long)old_cursor,
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
    87
            (long long)new_cursor)!=old_cursor);
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    88
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    89
        copy_to_log(level, buffpos, buf, size);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    90
        copy_to_log(level, (buffpos + size) & LOG_BUFFER_MASK, &tail, sizeof(mTail));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    91
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    92
        return 1; /* Success */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    93
    }else{
969
1950fe687dde Fix warning with LogMessage function
Laurent Bessard
parents: 954
diff changeset
    94
    	uint8_t mstr[] = "Logging error : message too big";
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    95
        LogMessage(LOG_CRITICAL, mstr, sizeof(mstr));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    96
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    97
    return 0;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    98
}
906
de452d65865c Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents: 649
diff changeset
    99
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   100
uint32_t GetLogCount(uint8_t level){
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   101
    return (uint64_t)LogCursor[level] >> 32;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   102
}
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   103
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   104
/* Return message size and content */
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   105
uint32_t GetLogMessage(uint8_t level, uint32_t msgidx, char* buf, uint32_t max_size, uint32_t* tick, uint32_t* tv_sec, uint32_t* tv_nsec){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   106
    uint64_t cursor = LogCursor[level];
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   107
    if(cursor){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   108
        /* seach cursor */
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   109
        uint32_t stailpos = (uint32_t)cursor; 
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   110
        uint32_t smsgidx;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   111
        mTail tail;
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   112
        tail.msgidx = cursor >> 32;
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   113
        tail.msgsize = 0;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   114
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   115
        /* Message search loop */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   116
        do {
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   117
            smsgidx = tail.msgidx;
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   118
            stailpos = (stailpos - sizeof(mTail) - tail.msgsize ) & LOG_BUFFER_MASK;
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   119
            copy_from_log(level, stailpos, &tail, sizeof(mTail));
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   120
        }while((tail.msgidx == smsgidx - 1) && (tail.msgidx > msgidx));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   121
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   122
        if(tail.msgidx == msgidx){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   123
            uint32_t sbuffpos = (stailpos - tail.msgsize ) & LOG_BUFFER_MASK; 
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   124
            uint32_t totalsize = tail.msgsize;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   125
            *tick = tail.tick; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   126
            *tv_sec = tail.time.tv_sec; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   127
            *tv_nsec = tail.time.tv_nsec; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   128
            copy_from_log(level, sbuffpos, buf, 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   129
                          totalsize > max_size ? max_size : totalsize);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   130
            return totalsize;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   131
        }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   132
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   133
    return 0;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   134
}
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: 969
diff changeset
   135
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   136
#define CALIBRATED -2
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   137
#define NOT_CALIBRATED -1
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   138
static int calibration_count = NOT_CALIBRATED;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   139
static IEC_TIME cal_begin;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   140
static long long Tsync = 0;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   141
static long long FreqCorr = 0;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   142
static int Nticks = 0;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   143
static unsigned long last_tick = 0;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   144
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   145
/*
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   146
 * Called on each external periodic sync event
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   147
 * make PLC tick synchronous with external sync
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   148
 * ratio defines when PLC tick occurs between two external sync
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   149
 * @param sync_align_ratio 
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   150
 *          0->100 : align ratio
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   151
 *          < 0 : no align, calibrate period
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   152
 **/
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   153
void align_tick(int sync_align_ratio)
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   154
{
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   155
	/*
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   156
	printf("align_tick(%d)\n", calibrate);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   157
	*/
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   158
	if(sync_align_ratio < 0){ /* Calibration */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   159
		if(calibration_count == CALIBRATED)
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   160
			/* Re-calibration*/
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   161
			calibration_count = NOT_CALIBRATED;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   162
		if(calibration_count == NOT_CALIBRATED)
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   163
			/* Calibration start, get time*/
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   164
			PLC_GetTime(&cal_begin);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   165
		calibration_count++;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   166
	}else{ /* do alignment (if possible) */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   167
		if(calibration_count >= 0){
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   168
			/* End of calibration */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   169
			/* Get final time */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   170
			IEC_TIME cal_end;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   171
			PLC_GetTime(&cal_end);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   172
			/*adjust calibration_count*/
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   173
			calibration_count++;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   174
			/* compute mean of Tsync, over calibration period */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   175
			Tsync = ((long long)(cal_end.tv_sec - cal_begin.tv_sec) * (long long)1000000000 +
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   176
					(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   177
			if( (Nticks = (Tsync / Ttick)) > 0){
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   178
				FreqCorr = (Tsync % Ttick); /* to be divided by Nticks */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   179
			}else{
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   180
				FreqCorr = Tsync - (Ttick % Tsync);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   181
			}
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   182
			/*
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   183
			printf("Tsync = %ld\n", Tsync);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   184
			printf("calibration_count = %d\n", calibration_count);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   185
			printf("Nticks = %d\n", Nticks);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   186
			*/
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   187
			calibration_count = CALIBRATED;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   188
		}
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   189
		if(calibration_count == CALIBRATED){
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   190
			/* Get Elapsed time since last PLC tick (__CURRENT_TIME) */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   191
			IEC_TIME now;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   192
			long long elapsed;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   193
			long long Tcorr;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   194
			long long PhaseCorr;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   195
			long long PeriodicTcorr;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   196
			PLC_GetTime(&now);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   197
			elapsed = (now.tv_sec - __CURRENT_TIME.tv_sec) * 1000000000 + now.tv_nsec - __CURRENT_TIME.tv_nsec;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   198
			if(Nticks > 0){
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   199
				PhaseCorr = elapsed - (Ttick + FreqCorr/Nticks)*sync_align_ratio/100; /* to be divided by Nticks */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   200
				Tcorr = Ttick + (PhaseCorr + FreqCorr) / Nticks;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   201
				if(Nticks < 2){
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   202
					/* When Sync source period is near Tick time */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   203
					/* PhaseCorr may not be applied to Periodic time given to timer */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   204
					PeriodicTcorr = Ttick + FreqCorr / Nticks;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   205
				}else{
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   206
					PeriodicTcorr = Tcorr;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   207
				}
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   208
			}else if(__tick > last_tick){
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   209
				last_tick = __tick;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   210
				PhaseCorr = elapsed - (Tsync*sync_align_ratio/100);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   211
				PeriodicTcorr = Tcorr = Ttick + PhaseCorr + FreqCorr;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   212
			}else{
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   213
				/*PLC did not run meanwhile. Nothing to do*/
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   214
				return;
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   215
			}
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   216
			/* DO ALIGNEMENT */
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   217
			PLC_SetTimer(Tcorr - elapsed, PeriodicTcorr);
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   218
		}
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   219
	}
cd8dadcef426 Re-organized C code templates for plc_main. Moved logging out of plc_debug. Factorized redundant _common_ticktime external declaration
Edouard Tisserant
parents: 969
diff changeset
   220
}