-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptoboard.c
390 lines (316 loc) · 8.81 KB
/
optoboard.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "optoboard.h"
/* Loads aclinedrv kernel module */
int board_start_acline(unsigned int pin)
{
char module[128];
sprintf(module, "modprobe aclinedrv opto_input=%u", pin);
if (system(module))
return EXIT_FAILURE;
return 0;
}
void board_stop_acline(void)
{
system("rmmod aclinedrv");
return;
}
/* Loads triacNdrv kernel module */
int board_start_triacdrv(unsigned int n, unsigned int pin, char *name)
{
char module[128];
sprintf(module, "modprobe triac%udrv gpio=%u pos=%u neg=%u name=%s", n, pin, 0, 0, name);
if (system(module))
return EXIT_FAILURE;
return 0;
}
void board_stop_triacdrv(unsigned int n)
{
char module[128];
sprintf(module, "rmmod triac%udrv", n);
system(module);
return;
}
/* Function that sets channel parameters
* This function is used to avoid exposing struct triac_status to triacd.c
* triacd.c will parse required parameters and pass them to us
*/
void board_update_channel(unsigned int n, bool fade, unsigned int time, unsigned int pos, unsigned int neg)
{
unsigned int i = n - 1;
if (triac[i].gpio.status == enabled) {
if (fade)
if (!time)
fader_stop(i);
else
fader_start(i, time, pos, neg);
else {
fader_stop(i);
triac[i].phase.pos = pos;
triac[i].phase.neg = neg;
triac[i].phase.refresh = true;
}
}
return;
}
/* Reads HAT and initializes struct triac_status.gpio
* according to HAT Devie-Tree parameters.
*
* Returns the number of configured channels [0-4].
* User should catch 0, since means NO channels available.
*/
unsigned int board_init_channels(void)
{
unsigned int i, channels, version, gpio_pin;
int fd;
char buffer[128];
char filename[128];
/* Read vendor string */
sprintf(filename, "%s/%s", HAT_DIR, HAT_VENDOR_FILE);
fd = open(filename, O_RDONLY);
if (read(fd, buffer, sizeof(buffer)) == -1)
goto read_error;
else
fprintf(FPRINTF_FD, "%s HAT detected!\n", buffer);
close(fd);
/* Read product string */
sprintf(filename, "%s/%s", HAT_DIR, HAT_PRODUCT_FILE);
fd = open(filename, O_RDONLY);
if (read(fd, buffer, sizeof(buffer)) == -1)
goto read_error;
else
fprintf(FPRINTF_FD, "\t%s", buffer);
close(fd);
/* Read HAT version uint32 */
sprintf(filename, "%s/%s", HAT_DIR, HAT_VERSION_FILE);
fd = open(filename, O_RDONLY);
if (read(fd, &version, sizeof(version)) == -1)
goto read_error;
else
fprintf(FPRINTF_FD, "\tv%u.%u\n", (ntohl(version) & 0x0000FF00) >> 8, (ntohl(version) & 0x000000FF));
close(fd);
/* Read input channels uint32 */
sprintf(filename, "%s/%s/%s", HAT_DIR, HAT_INPUTS_DIR, HAT_IO_CHANNELS);
fd = open(filename, O_RDONLY);
if (read(fd, &channels, sizeof(channels)) == -1)
goto read_error;
close(fd);
/* Read input channel N pin */
sprintf(filename, "%s/%s/%u/%s", HAT_DIR, HAT_INPUTS_DIR, ntohl(channels), HAT_GPIO_PIN);
fd = open(filename, O_RDONLY);
if (read(fd, &gpio_pin, sizeof(gpio_pin)) == -1)
goto read_error;
close(fd);
if (board_start_acline(ntohl(gpio_pin)))
fprintf(FPRINTF_FD, "board_init_channels error: no input pin found\n");
else
fprintf(FPRINTF_FD, "board_init_channels: input pin found - %02u\n", ntohl(gpio_pin));
/* Read output channels uint32 */
sprintf(filename, "%s/%s/%s", HAT_DIR, HAT_OUTPUTS_DIR, HAT_IO_CHANNELS);
fd = open(filename, O_RDONLY);
if (read(fd, &triac_status_len, sizeof(triac_status_len)) == -1)
goto read_error;
close(fd);
/* fixup endianess */
triac_status_len = ntohl(triac_status_len);
/* Allocate memory for struct triac_status vector */
triac = calloc(triac_status_len, sizeof(struct triac_status));
if (triac == NULL)
goto ptr_error;
for (i = 0, channels = 0; i < triac_status_len; i++) {
/* Read output channel N pin */
sprintf(filename, "%s/%s/%u/%s", HAT_DIR, HAT_OUTPUTS_DIR, (i + 1), HAT_GPIO_PIN);
fd = open(filename, O_RDONLY);
if (read(fd, &gpio_pin, sizeof(gpio_pin)) == -1) {
triac[i].gpio.status = error;
close(fd);
}
else {
close(fd);
triac[i].gpio.pin = ntohl(gpio_pin);
triac[i].phase.pos = 0;
triac[i].phase.neg = 0;
triac[i].phase.status = off;
/* Read output channel N name */
sprintf(filename, "%s/%s/%u/%s", HAT_DIR, HAT_OUTPUTS_DIR, (i + 1), HAT_GPIO_LABEL);
fd = open(filename, O_RDONLY);
if (read(fd, triac[i].gpio.label, sizeof(triac[i].gpio.label)) == -1)
sprintf(triac[i].gpio.label, "nnn%u", i + 1);
close(fd);
if (board_start_triacdrv(i + 1, triac[i].gpio.pin, triac[i].gpio.label)) {
fprintf(FPRINTF_FD, "board_init_channels: error - cannot start triac%udrv module\n", i + 1);
triac[i].gpio.status = error;
}
else {
triac[i].gpio.status = enabled;
channels++;
}
}
}
fader_init(triac, triac_status_len);
return channels;
read_error:
fprintf(FPRINTF_FD, "board_init_channels read error: %d - %s\n", errno, strerror(errno));
close(fd);
return 0;
ptr_error:
fprintf(FPRINTF_FD, "board_init_channels: memory error\n");
free(triac);
return 0;
}
/* Releases all channels, stops Kernel modules */
void board_free_channels(void)
{
unsigned int i;
fader_release();
for (i = 0; i < triac_status_len; i++) {
if (triac[i].gpio.status == enabled) {
board_stop_triacdrv(i + 1);
triac[i].gpio.status = disabled;
fprintf(FPRINTF_FD, "board_free_channels: channel %u released\n", i + 1);
}
}
board_stop_acline();
free(triac);
return;
}
/* Sends command to /sysfs triac channel */
int statem_send_command(char *name, unsigned int pos, unsigned int neg)
{
int fd;
char params[128];
char filename[128];
sprintf(params, "%u %u", pos, neg);
sprintf(filename, "%s/%s", MODULE_DIR, name);
fd = open(filename, O_WRONLY);
if (write(fd, params, strlen(params)) <= 0) {
fprintf(FPRINTF_FD, "statem_send_command error: %d - %s\n", errno, strerror(errno));
return EXIT_FAILURE;
}
// fprintf(FPRINTF_FD, "statem_send_command: %s %s\n", filename, params);
close(fd);
return 0;
}
void statem_set_off(unsigned int i)
{
triac[i].phase.status = off;
triac[i].phase.pos = 0;
triac[i].phase.neg = 0;
statem_send_command(triac[i].gpio.label, 0, 0);
return;
}
void statem_set_on(unsigned int i)
{
triac[i].phase.status = on;
triac[i].phase.pos = 180;
triac[i].phase.neg = 180;
statem_send_command(triac[i].gpio.label, 180, 180);
return;
}
void statem_set_sym(unsigned int i, unsigned int phase)
{
triac[i].phase.status = sym;
statem_send_command(triac[i].gpio.label, phase, phase);
return;
}
void statem_set_asym(unsigned int i, unsigned int pos, unsigned int neg)
{
triac[i].phase.status = asym;
statem_send_command(triac[i].gpio.label, pos, neg);
return;
}
/* State machine
* Parses phase.status and changes state
* according to phase values
* Possible states:
* on triac fully on (180 deg)
* off triac fully off (0 deg)
* sym symmetic phase control (negative == positive)
* ssym asymmetic phase control (negative != positive)
*/
void statem_loop(void)
{
unsigned int i;
unsigned int local_pos, local_neg;
for (i = 0; i < triac_status_len; i++) {
if (triac[i].gpio.status == enabled) {
local_pos = triac[i].phase.pos;
local_neg = triac[i].phase.neg;
switch (triac[i].phase.status) {
case off:
if (local_pos == 180 && local_neg == 180) {
statem_set_on(i);
break;
}
if (local_pos < 180 && local_pos > 0) {
if (local_neg == local_pos) {
statem_set_sym(i, local_pos);
break;
}
else {
statem_set_asym(i, local_pos, local_neg);
break;
}
}
/* no state change */
break;
case on:
if (local_pos == 0 && local_neg == 0) {
statem_set_off(i);
break;
}
if (local_pos < 180 && local_pos > 0) {
if (local_neg == local_pos) {
statem_set_sym(i, local_pos);
break;
}
else {
statem_set_asym(i, local_pos, local_neg);
break;
}
}
/* no state change */
break;
case sym:
if (local_pos == 0 && local_neg == 0) {
statem_set_off(i);
break;
}
if (local_pos == 180 && local_neg == 180) {
statem_set_on(i);
break;
}
if (local_pos != local_neg) {
statem_set_asym(i, local_pos, local_neg);
break;
}
/* no state change */
if (triac[i].phase.refresh) {
statem_set_sym(i, local_pos);
triac[i].phase.refresh = false;
}
break;
case asym:
if (local_pos == 0 && local_neg == 0) {
statem_set_off(i);
break;
}
if (local_pos == 180 && local_neg == 180) {
statem_set_on(i);
break;
}
if (local_neg == local_pos) {
statem_set_sym(i, local_pos);
break;
}
/* no state change */
if (triac[i].phase.refresh) {
statem_set_asym(i, local_pos, local_neg);
triac[i].phase.refresh = false;
}
break;
default:
break;
} //end switch
} //end if enabled
} //end for
return;
}