targets/LPC/plc_LPC_main.c
author Edouard Tisserant
Mon, 28 Mar 2011 11:42:00 +0200
changeset 587 c6354f7fe26e
parent 580 9dd978e6537c
permissions -rwxr-xr-x
Restore RefreshFileMenu in CFileEditor
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     1
/**
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     2
 * Yagarto specific code
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     3
 **/
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     4
579
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
     5
#include <string.h>
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
     6
#include <app_glue.h>
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     7
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     8
/* provided by POUS.C */
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
     9
extern unsigned long long common_ticktime__;
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    10
extern unsigned long __tick;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    11
579
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
    12
extern unsigned long idLen;
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
    13
extern unsigned char *idBuf;
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
    14
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
    15
static unsigned char RetainedIdBuf[128] __attribute__((section (".nvolatile")));
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
    16
static unsigned char retain_buffer[RETAIN_BUFFER_SIZE] __attribute__((section (".nvolatile")));
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
    17
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    18
static int debug_locked = 0;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    19
static int _DebugDataAvailable = 0;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    20
static unsigned long __debug_tick;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    21
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    22
void LPC_GetTime(IEC_TIME*);
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    23
void LPC_SetTimer(unsigned long long next, unsigned long long period);
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    24
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    25
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    26
{
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    27
	/* No need for real atomic op on LPC,
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    28
	 * no possible preemption between debug and PLC */
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    29
	long res = *atomicvar;
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    30
	if(res == compared){
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    31
		*atomicvar = exchange;
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    32
	}
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    33
	return res;
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    34
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    35
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    36
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    37
{
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    38
	/* Call target GetTime function */
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    39
	LPC_GetTime(CURRENT_TIME);
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    40
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    41
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    42
void PLC_SetTimer(unsigned long long next, unsigned long long period)
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    43
{
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    44
	LPC_SetTimer(next, period);
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    45
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    46
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    47
int startPLC(int argc,char **argv)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    48
{
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    49
	if(__init(argc,argv) == 0){
579
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
    50
        /* sign retain buffer */
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    51
		PLC_SetTimer(0, common_ticktime__);
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    52
		return 0;
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    53
	}else{
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    54
		return 1;
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 502
diff changeset
    55
	}
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    56
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    57
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    58
int TryEnterDebugSection(void)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    59
{
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    60
    if(!debug_locked && __DEBUG){
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    61
        debug_locked = 1;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    62
		return 1;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    63
    }
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    64
    return 0;
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    65
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    66
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    67
void LeaveDebugSection(void)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    68
{
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    69
        debug_locked = 0;
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    70
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    71
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    72
int stopPLC(void)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    73
{
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    74
    __cleanup();
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    75
    return 0;
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    76
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    77
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    78
/* from plc_debugger.c */
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 425
diff changeset
    79
int WaitDebugData(unsigned long *tick)
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    80
{
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    81
    /* no blocking call on LPC */
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    82
    if(_DebugDataAvailable && !debug_locked){
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    83
        /* returns 0 on success */
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    84
        *tick = __debug_tick;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    85
        _DebugDataAvailable = 0;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    86
        return 0;
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    87
    }
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    88
    return 1;
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    89
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    90
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    91
/* Called by PLC thread when debug_publish finished
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    92
 * This is supposed to unlock debugger thread in WaitDebugData*/
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    93
void InitiateDebugTransfer(void)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    94
{
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    95
    /* remember tick */
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
    96
    __debug_tick = __tick;
502
5343ae43f6d0 LPC connector - one step further
edouard
parents: 497
diff changeset
    97
    _DebugDataAvailable = 1;
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    98
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    99
502
5343ae43f6d0 LPC connector - one step further
edouard
parents: 497
diff changeset
   100
void suspendDebug(int disable)
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
   101
{
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
   102
    /* Prevent PLC to enter debug code */
502
5343ae43f6d0 LPC connector - one step further
edouard
parents: 497
diff changeset
   103
    __DEBUG = !disable;
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
   104
    debug_locked = !disable;
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
   105
}
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
   106
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
   107
void resumeDebug(void)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
   108
{
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
   109
    /* Let PLC enter debug code */
502
5343ae43f6d0 LPC connector - one step further
edouard
parents: 497
diff changeset
   110
    __DEBUG = 1;
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 521
diff changeset
   111
    debug_locked = 0;
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
   112
}
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 478
diff changeset
   113
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   114
void ValidateRetainBuffer(void)
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   115
{
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   116
        memcpy(RetainedIdBuf, idBuf, idLen);
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   117
}
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   118
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   119
void InValidateRetainBuffer(void)
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   120
{
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   121
    /* invalidate that buffer */
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   122
    RetainedIdBuf[0] = 0;
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   123
}
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   124
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   125
int CheckRetainBuffer(void)
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   126
{
579
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   127
	/* compare RETAIN ID buffer with MD5 */
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   128
    /* return true if identical */
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   129
    int res = memcmp(RetainedIdBuf, idBuf, idLen) == 0;
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   130
    return res;
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   131
}
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   132
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 478
diff changeset
   133
void Retain(unsigned int offset, unsigned int count, void *p)
497
ed7f72fc785f Typo in LPC
edouard
parents: 483
diff changeset
   134
{
579
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   135
    if(offset + count < RETAIN_BUFFER_SIZE)
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   136
        /* write in RETAIN buffer at offset*/
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   137
        memcpy(&retain_buffer[offset], p, count);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 478
diff changeset
   138
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 478
diff changeset
   139
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 478
diff changeset
   140
void Remind(unsigned int offset, unsigned int count, void *p)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 478
diff changeset
   141
{
579
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   142
    if(offset + count < RETAIN_BUFFER_SIZE)
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   143
        /* read at offset in RETAIN buffer */
554ba6df4ee0 Retain variable for LPC
edouard
parents: 577
diff changeset
   144
        memcpy(p, &retain_buffer[offset], count);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 478
diff changeset
   145
}