targets/plc_main_tail.c
author Edouard Tisserant
Tue, 30 Oct 2018 14:08:28 +0100
changeset 2328 7eb6cb70bf5b
parent 1800 1711339585ce
child 3947 9b5cb90297e4
permissions -rw-r--r--
PSK : Stunnel and/or OpenSSL (undocumented) handles PSK better without special chars, apparently
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
 **/
1800
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
     8
#ifndef TARGET_LOGGING_DISABLE
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
     9
944
52a17be9c4d1 Introduce Beremiz Native POU library. Now LOGGER POU is part of Beremiz' native POU library
Edouard Tisserant
parents: 923
diff changeset
    10
#ifndef LOG_BUFFER_SIZE
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    11
#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
    12
#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
    13
#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
    14
#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
    15
#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
    16
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    17
#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
    18
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
static char LogBuff[LOG_LEVELS][LOG_BUFFER_SIZE] LOG_BUFFER_ATTRS;
1479
8f41aa88aa46 fix many compilation warnings about static variables used by non-static functions
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1428
diff changeset
    20
static 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
    21
    if(buffpos + size < LOG_BUFFER_SIZE){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    22
        memcpy(&LogBuff[level][buffpos], buf, size);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    23
    }else{
996
17d9c81b5ed8 Fixed typo in copy_to_log
Edouard Tisserant
parents: 995
diff changeset
    24
        uint32_t remaining = LOG_BUFFER_SIZE - buffpos; 
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    25
        memcpy(&LogBuff[level][buffpos], buf, remaining);
991
afc4963d8f0c Fixed cast in target code to remove void* arithmetic warning
Edouard Tisserant
parents: 985
diff changeset
    26
        memcpy(LogBuff[level], (char*)buf + remaining, size - remaining);
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    27
    }
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    28
}
1479
8f41aa88aa46 fix many compilation warnings about static variables used by non-static functions
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1428
diff changeset
    29
static 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
    30
    if(buffpos + size < LOG_BUFFER_SIZE){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    31
        memcpy(buf, &LogBuff[level][buffpos], size);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    32
    }else{
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
    33
        uint32_t remaining = LOG_BUFFER_SIZE - buffpos; 
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    34
        memcpy(buf, &LogBuff[level][buffpos], remaining);
991
afc4963d8f0c Fixed cast in target code to remove void* arithmetic warning
Edouard Tisserant
parents: 985
diff changeset
    35
        memcpy((char*)buf + remaining, LogBuff[level], size - remaining);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    36
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    37
}
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    38
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    39
/* Log buffer structure
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    40
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    41
 |<-Tail1.msgsize->|<-sizeof(mTail)->|<--Tail2.msgsize-->|<-sizeof(mTail)->|...
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    42
 |  Message1 Body  |      Tail1      |   Message2 Body   |      Tail2      |
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
*/
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    45
typedef struct {
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    46
    uint32_t msgidx;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    47
    uint32_t msgsize;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    48
    unsigned long tick;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    49
    IEC_TIME time;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    50
} mTail;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    51
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    52
/* Log cursor : 64b
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    53
   |63 ... 32|31 ... 0|
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    54
   | Message | Buffer |
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    55
   | counter | Index  | */
995
5dcb361a55ef Extended LogCursor attributes
Edouard Tisserant
parents: 991
diff changeset
    56
static uint64_t LogCursor[LOG_LEVELS] LOG_BUFFER_ATTRS = {0x0,0x0,0x0,0x0};
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    57
1093
b5f78cff4459 Added support for resetting log messages
Laurent Bessard
parents: 1012
diff changeset
    58
void ResetLogCount(void) {
b5f78cff4459 Added support for resetting log messages
Laurent Bessard
parents: 1012
diff changeset
    59
	uint8_t level;
b5f78cff4459 Added support for resetting log messages
Laurent Bessard
parents: 1012
diff changeset
    60
	for(level=0;level<LOG_LEVELS;level++){
b5f78cff4459 Added support for resetting log messages
Laurent Bessard
parents: 1012
diff changeset
    61
		LogCursor[level] = 0;
b5f78cff4459 Added support for resetting log messages
Laurent Bessard
parents: 1012
diff changeset
    62
	}
b5f78cff4459 Added support for resetting log messages
Laurent Bessard
parents: 1012
diff changeset
    63
}
b5f78cff4459 Added support for resetting log messages
Laurent Bessard
parents: 1012
diff changeset
    64
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    65
/* Store one log message of give size */
1002
15c05ba95df4 LogMessage string pointer now char* instead of uint8_t*
Edouard Tisserant
parents: 1001
diff changeset
    66
int LogMessage(uint8_t level, char* buf, uint32_t size){
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    67
    if(size < LOG_BUFFER_SIZE - sizeof(mTail)){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    68
        uint32_t buffpos;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    69
        uint64_t new_cursor, old_cursor;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    70
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    71
        mTail tail;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    72
        tail.msgsize = size;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    73
        tail.tick = __tick;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    74
        PLC_GetTime(&tail.time);
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
    75
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    76
        /* We cannot increment both msg index and string pointer 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    77
           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
    78
           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
    79
           succeeds non interrupted */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    80
        do{
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    81
            old_cursor = LogCursor[level];
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    82
            buffpos = (uint32_t)old_cursor;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    83
            tail.msgidx = (old_cursor >> 32); 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    84
            new_cursor = ((uint64_t)(tail.msgidx + 1)<<32) 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    85
                         | (uint64_t)((buffpos + size + sizeof(mTail)) & LOG_BUFFER_MASK);
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
    86
        }while(AtomicCompareExchange64(
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
    87
            (long long*)&LogCursor[level],
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
    88
            (long long)old_cursor,
991
afc4963d8f0c Fixed cast in target code to remove void* arithmetic warning
Edouard Tisserant
parents: 985
diff changeset
    89
            (long long)new_cursor)!=(long long)old_cursor);
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    90
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    91
        copy_to_log(level, buffpos, buf, size);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    92
        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
    93
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    94
        return 1; /* Success */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    95
    }else{
1012
deb0fcab0c64 Fixed warning with LogMessage
Edouard Tisserant
parents: 1002
diff changeset
    96
    	char mstr[] = "Logging error : message too big";
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
    97
        LogMessage(LOG_CRITICAL, mstr, sizeof(mstr));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    98
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
    99
    return 0;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   100
}
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
   101
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   102
uint32_t GetLogCount(uint8_t level){
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   103
    return (uint64_t)LogCursor[level] >> 32;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   104
}
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   105
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   106
/* Return message size and content */
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   107
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
   108
    uint64_t cursor = LogCursor[level];
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   109
    if(cursor){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   110
        /* 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
   111
        uint32_t stailpos = (uint32_t)cursor; 
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   112
        uint32_t smsgidx;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   113
        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
   114
        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
   115
        tail.msgsize = 0;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   116
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   117
        /* Message search loop */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   118
        do {
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   119
            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
   120
            stailpos = (stailpos - sizeof(mTail) - tail.msgsize ) & LOG_BUFFER_MASK;
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   121
            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
   122
        }while((tail.msgidx == smsgidx - 1) && (tail.msgidx > msgidx));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   123
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   124
        if(tail.msgidx == msgidx){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   125
            uint32_t sbuffpos = (stailpos - tail.msgsize ) & LOG_BUFFER_MASK; 
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   126
            uint32_t totalsize = tail.msgsize;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   127
            *tick = tail.tick; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   128
            *tv_sec = tail.time.tv_sec; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   129
            *tv_nsec = tail.time.tv_nsec; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   130
            copy_from_log(level, sbuffpos, buf, 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   131
                          totalsize > max_size ? max_size : totalsize);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   132
            return totalsize;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   133
        }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   134
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   135
    return 0;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   136
}
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
   137
1800
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   138
#endif
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   139
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   140
#ifndef TARGET_EXT_SYNC_DISABLE
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   141
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
   142
#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
   143
#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
   144
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
   145
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
   146
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
   147
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
   148
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
   149
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
   150
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
/*
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
 * 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
   153
 * 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
   154
 * 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
   155
 * @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
   156
 *          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
   157
 *          < 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
   158
 **/
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
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
   160
{
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
	/*
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
	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
   163
	*/
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
	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
   165
		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
   166
			/* 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
   167
			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
   168
		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
   169
			/* 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
   170
			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
   171
		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
   172
	}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
   173
		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
   174
			/* 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
   175
			/* 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
   176
			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
   177
			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
   178
			/*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
   179
			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
   180
			/* 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
   181
			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
   182
					(cal_end.tv_nsec - cal_begin.tv_nsec)) / calibration_count;
1428
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 1093
diff changeset
   183
			if( (Nticks = (Tsync / common_ticktime__)) > 0){
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 1093
diff changeset
   184
				FreqCorr = (Tsync % common_ticktime__); /* to be divided by Nticks */
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
   185
			}else{
1428
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 1093
diff changeset
   186
				FreqCorr = Tsync - (common_ticktime__ % Tsync);
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
   187
			}
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
			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
   190
			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
   191
			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
   192
			*/
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
			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
   194
		}
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
		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
   196
			/* 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
   197
			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
   198
			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
   199
			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
   200
			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
   201
			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
   202
			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
   203
			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
   204
			if(Nticks > 0){
1428
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 1093
diff changeset
   205
				PhaseCorr = elapsed - (common_ticktime__ + FreqCorr/Nticks)*sync_align_ratio/100; /* to be divided by Nticks */
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 1093
diff changeset
   206
				Tcorr = common_ticktime__ + (PhaseCorr + FreqCorr) / Nticks;
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
   207
				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
   208
					/* 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
   209
					/* PhaseCorr may not be applied to Periodic time given to timer */
1428
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 1093
diff changeset
   210
					PeriodicTcorr = common_ticktime__ + FreqCorr / Nticks;
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
   211
				}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
   212
					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
   213
				}
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
			}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
   215
				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
   216
				PhaseCorr = elapsed - (Tsync*sync_align_ratio/100);
1428
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 1093
diff changeset
   217
				PeriodicTcorr = Tcorr = common_ticktime__ + PhaseCorr + FreqCorr;
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
   218
			}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
   219
				/*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
   220
				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
   221
			}
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
   222
			/* 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
   223
			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
   224
		}
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
   225
	}
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
   226
}
1800
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   227
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   228
#endif