1 /** |
|
2 * Yagarto specific code |
|
3 **/ |
|
4 |
|
5 #include <string.h> |
|
6 #include <app_glue.h> |
|
7 |
|
8 /* provided by POUS.C */ |
|
9 extern unsigned long long common_ticktime__; |
|
10 extern unsigned long __tick; |
|
11 |
|
12 extern unsigned long idLen; |
|
13 extern unsigned char *idBuf; |
|
14 |
|
15 static unsigned char RetainedIdBuf[128] __attribute__((section (".nvolatile"))); |
|
16 static unsigned char retain_buffer[RETAIN_BUFFER_SIZE] __attribute__((section (".nvolatile"))); |
|
17 |
|
18 static int debug_locked = 0; |
|
19 static int _DebugDataAvailable = 0; |
|
20 static unsigned long __debug_tick; |
|
21 |
|
22 void LPC_GetTime(IEC_TIME*); |
|
23 void LPC_SetTimer(unsigned long long next, unsigned long long period); |
|
24 |
|
25 long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
|
26 { |
|
27 /* No need for real atomic op on LPC, |
|
28 * no possible preemption between debug and PLC */ |
|
29 long res = *atomicvar; |
|
30 if(res == compared){ |
|
31 *atomicvar = exchange; |
|
32 } |
|
33 return res; |
|
34 } |
|
35 |
|
36 void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
37 { |
|
38 /* Call target GetTime function */ |
|
39 LPC_GetTime(CURRENT_TIME); |
|
40 } |
|
41 |
|
42 void PLC_SetTimer(unsigned long long next, unsigned long long period) |
|
43 { |
|
44 LPC_SetTimer(next, period); |
|
45 } |
|
46 |
|
47 int startPLC(int argc,char **argv) |
|
48 { |
|
49 if(__init(argc,argv) == 0){ |
|
50 /* sign retain buffer */ |
|
51 PLC_SetTimer(0, common_ticktime__); |
|
52 return 0; |
|
53 }else{ |
|
54 return 1; |
|
55 } |
|
56 } |
|
57 |
|
58 int TryEnterDebugSection(void) |
|
59 { |
|
60 if(!debug_locked && __DEBUG){ |
|
61 debug_locked = 1; |
|
62 return 1; |
|
63 } |
|
64 return 0; |
|
65 } |
|
66 |
|
67 void LeaveDebugSection(void) |
|
68 { |
|
69 debug_locked = 0; |
|
70 } |
|
71 |
|
72 int stopPLC(void) |
|
73 { |
|
74 __cleanup(); |
|
75 return 0; |
|
76 } |
|
77 |
|
78 /* from plc_debugger.c */ |
|
79 int WaitDebugData(unsigned long *tick) |
|
80 { |
|
81 /* no blocking call on LPC */ |
|
82 if(_DebugDataAvailable && !debug_locked){ |
|
83 /* returns 0 on success */ |
|
84 *tick = __debug_tick; |
|
85 _DebugDataAvailable = 0; |
|
86 return 0; |
|
87 } |
|
88 return 1; |
|
89 } |
|
90 |
|
91 /* Called by PLC thread when debug_publish finished |
|
92 * This is supposed to unlock debugger thread in WaitDebugData*/ |
|
93 void InitiateDebugTransfer(void) |
|
94 { |
|
95 /* remember tick */ |
|
96 __debug_tick = __tick; |
|
97 _DebugDataAvailable = 1; |
|
98 } |
|
99 |
|
100 void suspendDebug(int disable) |
|
101 { |
|
102 /* Prevent PLC to enter debug code */ |
|
103 __DEBUG = !disable; |
|
104 debug_locked = !disable; |
|
105 } |
|
106 |
|
107 void resumeDebug(void) |
|
108 { |
|
109 /* Let PLC enter debug code */ |
|
110 __DEBUG = 1; |
|
111 debug_locked = 0; |
|
112 } |
|
113 |
|
114 void ValidateRetainBuffer(void) |
|
115 { |
|
116 memcpy(RetainedIdBuf, idBuf, idLen); |
|
117 } |
|
118 |
|
119 void InValidateRetainBuffer(void) |
|
120 { |
|
121 /* invalidate that buffer */ |
|
122 RetainedIdBuf[0] = 0; |
|
123 } |
|
124 |
|
125 int CheckRetainBuffer(void) |
|
126 { |
|
127 /* compare RETAIN ID buffer with MD5 */ |
|
128 /* return true if identical */ |
|
129 int res = memcmp(RetainedIdBuf, idBuf, idLen) == 0; |
|
130 return res; |
|
131 } |
|
132 |
|
133 void Retain(unsigned int offset, unsigned int count, void *p) |
|
134 { |
|
135 if(offset + count < RETAIN_BUFFER_SIZE) |
|
136 /* write in RETAIN buffer at offset*/ |
|
137 memcpy(&retain_buffer[offset], p, count); |
|
138 } |
|
139 |
|
140 void Remind(unsigned int offset, unsigned int count, void *p) |
|
141 { |
|
142 if(offset + count < RETAIN_BUFFER_SIZE) |
|
143 /* read at offset in RETAIN buffer */ |
|
144 memcpy(p, &retain_buffer[offset], count); |
|
145 } |
|