forked from mprymek/mcu-plc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal.cpp
268 lines (231 loc) · 5.8 KB
/
hal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifdef HW_LINUX
#endif
#include "iec_types.h"
#include "hal.h"
//Booleans
extern IEC_BOOL *bool_input[BUFFER_SIZE][8];
extern IEC_BOOL *bool_output[BUFFER_SIZE][8];
//Bytes
extern IEC_BYTE *byte_input[BUFFER_SIZE];
extern IEC_BYTE *byte_output[BUFFER_SIZE];
//Analog I/O
extern IEC_UINT *int_input[BUFFER_SIZE];
extern IEC_UINT *int_output[BUFFER_SIZE];
//Memory
extern IEC_UINT *int_memory[BUFFER_SIZE];
extern IEC_DINT *dint_memory[BUFFER_SIZE];
extern IEC_LINT *lint_memory[BUFFER_SIZE];
//Special Functions
extern IEC_LINT *special_functions[BUFFER_SIZE];
extern IEC_TIME __CURRENT_TIME;
extern unsigned long long common_ticktime__;
#define MILLION 1000000
#define BILLION 1000000000
void update_time()
{
__CURRENT_TIME.tv_nsec += common_ticktime__;
if (__CURRENT_TIME.tv_nsec >= BILLION)
{
__CURRENT_TIME.tv_nsec -= BILLION;
__CURRENT_TIME.tv_sec++;
}
}
/*
* Macros black magic to debug print only variables which are really used in the PLC program.
*/
#define POOL_BOOL_I bool_input
#define POOL_BOOL_Q bool_output
#define POOL_UINT_I int_input
#define INDEX_BOOL(a, b) [a][b]
#define INDEX_UINT(a, b) [a]
#define print_in_Q(...)
inline void print_in_I(const char *name, IEC_BOOL *val)
{
logf("%s = %u\n", name, *val);
}
void print_inputs()
{
#define __LOCATED_VAR(type, name, inout, type_sym, a, b) \
print_in_##inout(#inout #type_sym #a "." #b, POOL_##type##_##inout INDEX_##type(a, b));
#include "LOCATED_VARIABLES.h"
#undef __LOCATED_VAR
}
#define print_out_I(...)
inline void print_out_Q(const char *name, IEC_BOOL *val)
{
logf("%s = %u\n", name, *val);
}
void print_outputs()
{
#define __LOCATED_VAR(type, name, inout, type_sym, a, b) \
print_out_##inout(#inout #type_sym #a "." #b, POOL_##type##_##inout INDEX_##type(a, b));
#include "LOCATED_VARIABLES.h"
#undef __LOCATED_VAR
}
/*
* Linux
*/
#ifdef HW_LINUX
#include <time.h>
static void ts_add(struct timespec *ts, int delay)
{
ts->tv_nsec += delay;
if (ts->tv_nsec >= BILLION)
{
ts->tv_nsec -= BILLION;
ts->tv_sec++;
}
}
void hw_init()
{
logf("initializing hw\n");
}
void hw_close()
{
logf("closing hw\n");
}
void update_buffers_in()
{
DEBUGF("updating input buffers\n");
print_inputs();
}
void update_buffers_out()
{
DEBUGF("updating output buffers\n");
print_outputs();
}
static struct timespec timer;
void timer_init()
{
clock_gettime(CLOCK_MONOTONIC, &timer);
}
void timer_sleep_until(unsigned long long delay_ns)
{
ts_add(&timer, delay_ns);
// NOTE: If the PLC is too slow (real time is > timer), clock_nanosleep
// will sleep for no time but `timer` will be
// behind wall clock. This error could get corrected in the
// subsequent ticks, or can be accumulated.
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &timer, NULL);
#ifdef TIMER_DEBUG
static float time_old = 0;
struct timespec timer2;
clock_gettime(CLOCK_MONOTONIC, &timer2);
float time = timer2.tv_sec + timer2.tv_nsec / (float)BILLION;
DEBUGF("wall time = %.6fs\n", time);
if (time_old)
{
float time_error = (time - time_old) * MILLION - common_ticktime__ / 1000;
DEBUGF("time_error = %.0fus\n", time_error);
}
time_old = time;
#endif
}
#endif
/*
* ESP32
*/
#ifdef HW_ESP32
#include <Arduino.h>
// see https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
// for PIN choice hints
// digital inputs
const uint8_t DIS_NUM = 4;
const uint8_t DIS_PINS[DIS_NUM] = {16, 17, 5, 18};
// digital outputs
const uint8_t DOS_NUM = 5;
const uint8_t DOS_PINS[DOS_NUM] = {2, 13, 12, 14, 27};
// analog inputs
const uint8_t AIS_NUM = 4;
const uint8_t AIS_PINS[AIS_NUM] = {33, 32, 35, 34}; // 26, 25 does not work?!
void hw_init()
{
Serial.begin(115200);
logf("initializing hw\n");
for (uint8_t i = 0; i < AIS_NUM; i++)
{
pinMode(AIS_PINS[i], INPUT);
}
for (uint8_t i = 0; i < DIS_NUM; i++)
{
pinMode(DIS_PINS[i], INPUT_PULLUP);
}
for (uint8_t i = 0; i < DOS_NUM; i++)
{
pinMode(DOS_PINS[i], OUTPUT);
}
}
void hw_close()
{
logf("closing hw\n");
}
void update_buffers_in()
{
DEBUGF("updating input buffers\n");
for (uint8_t i = 0; i < DIS_NUM; i++)
{
uint8_t a = i / 8;
uint8_t b = i % 8;
IEC_BOOL *val = bool_input[a][b];
if (val)
{
*val = !digitalRead(DIS_PINS[i]);
DEBUGF("IX%u.%u = %u\n", a, b, *val);
}
}
for (uint8_t i = 0; i < AIS_NUM; i++)
{
uint8_t a = i / 8;
uint8_t b = i % 8;
IEC_UINT *val = int_input[i];
if (val)
{
*val = analogRead(AIS_PINS[i]);
DEBUGF("IW%u.%u = %u\n", a, b, *val);
}
}
}
void update_buffers_out()
{
DEBUGF("updating output buffers\n");
for (uint8_t i = 0; i < DOS_NUM; i++)
{
uint8_t a = i / 8;
uint8_t b = i % 8;
IEC_BOOL *val = bool_output[a][b];
if (val)
{
DEBUGF("QX%u.%u = %u\n", a, b, *val);
digitalWrite(DOS_PINS[i], *val);
}
}
}
static unsigned long timer;
void timer_init()
{
timer = micros();
}
void timer_sleep_until(unsigned long long delay_ns)
{
unsigned long now = micros();
#ifdef TIMER_DEBUG
float time = now / (float)MILLION;
DEBUGF("wall time = %.6fs\n", time);
#endif
// NOTE: We must use subtraction of timers only because micros() will
// overflow often and subtraction after overflow gives correct results.
// e.g. 0 - 0xFFFF = 1
unsigned long elapsed_ns = now - timer;
long delay_us = (delay_ns - elapsed_ns) / 1000;
if (delay_us < 0)
{
logf("WARNING: timer miss by %ldus", -delay_us);
}
else
{
DEBUGF("timer margin = %ldus\n", delay_us);
delayMicroseconds(delay_us);
}
timer = now;
}
#endif