-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.c
389 lines (326 loc) · 10.5 KB
/
tester.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
/**
* Copyright by Andrea Cervesato <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include "gpio.h"
#include "strutils.h"
#define PROGNAME "rpi3tester"
#define VERSION "1.0"
/********************************
* BLINK COMMAND implementation
********************************/
int m_stop_blinking = 1;
pthread_t m_blink_thread = 0;
void* blink_led(void* args)
{
struct timespec tim;
long time_ns = 0;
int port = 0;
int *params;
int up_down_counter = 0;
if (args == NULL) {
printf("ERROR: Arguments passed to pthread are empty!\n");
goto ret_statement;
}
params = (int*)args;
port = params[0];
time_ns = params[1];
tim.tv_sec = 0;
tim.tv_nsec = time_ns;
// set port direction to gitial output
rpi_gpio_set_output(port);
// this check has been removed from the while,
// to speed up port writing and do hardware speed tests
if (time_ns > 0 && time_ns < 1e10) {
while (m_stop_blinking) {
rpi_gpio_fast_up(port);
if (nanosleep(&tim, NULL) < 0) {
printf("Nano sleep system call failed\n");
break;
}
rpi_gpio_fast_down(port);
if (nanosleep(&tim, NULL) < 0) {
printf("Nano sleep system call failed\n");
break;
}
}
} else {
while (!m_stop_blinking) {
rpi_gpio_fast_up(port);
rpi_gpio_fast_down(port);
}
}
ret_statement:
//printf("Thread completed\n");
return NULL;
}
/************************************
* Frequency reading implementation
************************************/
pthread_t m_freq_read_thread = 0;
pthread_t m_clock_read_thread = 0;
int m_stop_freq_read = 1;
unsigned long m_freq_counter = 0;
unsigned long m_current_frequency = 0;
void* clock_read(void* args)
{
clock_t start_time = 0;
float time_s = 0;
start_time = clock();
while (m_stop_freq_read) {
time_s = (float)((clock() - start_time)/CLOCKS_PER_SEC);
if (time_s >= 1) {
m_current_frequency = m_freq_counter;
m_freq_counter = 0;
start_time = clock();
}
}
}
void* frequency_read(void* args)
{
int port = 0;
int last_status = 0;
int curr_status = 0;
if (args == NULL) { // nothing to do
printf("ERROR: Argument passed to frequency read thread is empty!\n");
goto ret_statement;
}
port = ((int*)args)[0];
m_freq_counter = 0;
// select the port direction to input
rpi_gpio_set_input(port);
// start reading the square wave
last_status = rpi_gpio_fast_read(port);
while (m_stop_freq_read) {
curr_status = rpi_gpio_fast_read(port);
if (last_status != curr_status) {
m_freq_counter++;
}
last_status = curr_status;
}
ret_statement:
return NULL;
}
/****************
* The commands
****************/
struct command {
char* label; // the command label
char* usage; // the usage label
void (*cmd)(int,char**); // the command function
};
void read_cmd(int num_of_token, char** token_array) {
int port = 0;
int value = 0;
if (num_of_token >= 2 && strcmp(token_array[0], "read") == 0) {
port = atoi(token_array[1]);
value = rpi_gpio_read_status(port);
printf("Pin%d: %d\n", port, value);
}
}
void write_cmd(int num_of_token, char** token_array) {
int port = 0;
int value = 0;
if (num_of_token >= 3 && strcmp(token_array[0], "write") == 0) {
port = atoi(token_array[1]);
value = atoi(token_array[2]);
rpi_gpio_write_status(port, value);
printf("Pin%d: written %d\n", port, value);
}
}
void input_cmd(int num_of_token, char** token_array) {
int port = 0;
if (num_of_token >= 2 && strcmp(token_array[0], "input") == 0) {
port = atoi(token_array[1]);
rpi_gpio_set_input(port);
printf("Pin%d set as input\n", port);
}
}
void output_cmd(int num_of_token, char** token_array) {
int port = 0;
if (num_of_token >= 2 && strcmp(token_array[0], "output") == 0) {
port = atoi(token_array[1]);
rpi_gpio_set_output(port);
printf("Pin%d set as output\n", port);
}
}
void blink_cmd(int num_of_token, char** token_array) {
int port = 0;
int time_ns = 0;
int* thread_args = NULL;
if (num_of_token >= 2 && strcmp(token_array[0], "blink") == 0) {
if (num_of_token >= 4 && strcmp(token_array[1], "start") == 0) {
if (m_blink_thread != 0) {
printf ("Blinking already running\n");
goto cleanup;
}
port = atoi(token_array[2]);
time_ns = atoi(token_array[3]);
m_stop_blinking = 1;
// create the thread and launch it
thread_args = (int*)calloc(2, sizeof(int));
thread_args[0] = port;
thread_args[1] = time_ns;
if (pthread_create(&m_blink_thread, NULL, blink_led, (void*)thread_args)) {
printf("Error creating blink led thread!\n");
goto cleanup;
} else {
printf("Pin%d: blinking started with %dns\n", port, time_ns);
}
} else if (strcmp(token_array[1], "stop") == 0) {
if (m_blink_thread == 0) {
printf("You need to call \"blink start\" before stop\n");
goto cleanup;
}
m_stop_blinking = 0;
if (pthread_join(m_blink_thread, NULL)) {
printf("Error joining the blink thread!\n");
goto cleanup;
} else {
printf("Blinking stopped\n");
}
m_blink_thread = 0;
} else {
printf("Use \"blink [start|stop] [time_ns]\"\n");
}
}
cleanup:
free(thread_args);
return;
}
void freq_cmd(int num_of_token, char** token_array) {
int port = 0;
int* thread_args = NULL;
if (num_of_token >= 2 && strcmp(token_array[0], "freq") == 0) {
if (num_of_token >= 3 && strcmp(token_array[1], "start") == 0) {
if (m_freq_read_thread != 0 || m_clock_read_thread != 0) {
printf("Frequency read already running\n");
goto cleanup;
}
port = atoi(token_array[2]);
m_stop_freq_read = 1;
thread_args = (int*)malloc(sizeof(int));
thread_args[0] = port;
if (pthread_create(&m_freq_read_thread, NULL, frequency_read, thread_args)) {
printf("Error creating frequency read thread!\n");
goto cleanup;
}
if (pthread_create(&m_clock_read_thread, NULL, clock_read, NULL)) {
printf("Error creating clock read thread!\n");
goto cleanup;
} else {
printf("Pin%d: frequency read started\n", port);
}
} else if (strcmp(token_array[1], "stop") == 0) {
if (m_freq_read_thread == 0 && m_clock_read_thread == 0) {
printf("You need to call \"freq start\" before stop\n");
goto cleanup;
}
m_stop_freq_read = 0;
if (pthread_join(m_freq_read_thread, NULL)) {
printf("Error joining the frequency read thread\n");
goto cleanup;
}
if (pthread_join(m_clock_read_thread, NULL)) {
printf("Error joining the clock read thread\n");
goto cleanup;
} else {
printf("Frequency read stopped\n");
}
m_freq_read_thread = 0;
m_clock_read_thread = 0;
} else if (strcmp(token_array[1], "print") == 0) {
if (m_freq_read_thread == 0 && m_clock_read_thread == 0) {
printf("You need to call \"freq start\" before print\n");
goto cleanup;
}
printf("Frequency is %luHz\n", m_current_frequency);
} else {
printf("Use \"freq [start|print|stop]\"\n");
}
}
cleanup:
free(thread_args);
return;
}
static const int m_num_of_commands = 9;
static const struct command m_commands_list[] = {
{"input", "input [gpio]", input_cmd},
{"output", "output [gpio]", output_cmd},
{"read", "read [gpio]", read_cmd},
{"write", "write [gpio]", write_cmd},
{"blink", "blink start [gpio] [time_ns]", blink_cmd},
{"blink", "blink stop [gpio]", blink_cmd},
{"freq", "freq start [pin]", freq_cmd},
{"freq", "freq stop", freq_cmd},
{"freq", "freq print", freq_cmd}
};
void execute_cmd(char* cmd)
{
char** token_array = NULL;
int num_of_token = 0;
int i = 0;
token_array = strsplit(cmd, ' ', &num_of_token);
if (num_of_token <= 0) { // empty command
goto cleanup;
}
for (i = 0; i < m_num_of_commands; i++) {
if (strcmp(m_commands_list[i].label, token_array[0]) == 0) {
m_commands_list[i].cmd(num_of_token, token_array);
break;
}
}
cleanup:
strfree(&token_array, num_of_token);
return;
}
int main(int argc, char* argv[])
{
int input = 0;
char line[1024];
char* token = NULL;
int return_error = 0;
int i = 0;
// this command is used to check if program runs
if (argc > 1 && strcmp(argv[1], "-v") == 0) {
printf(PROGNAME" v"VERSION"\n");
return 0;
}
if ((return_error = rpi_gpio_setup()) != GPIO_NO_ERROR) {
return 1;
}
printf("\nUse one of the following commands:\n");
for (i = 0; i < m_num_of_commands; i++) {
printf("- %s\n", m_commands_list[i].usage);
}
printf("- exit\n\n");
while(1) {
printf("cmd> ");
// read the line
fflush(NULL);
if (!fgets(line, 1024, stdin)) {
return 0;
}
// remove the new line characters
token = strtok(line, "\n");
if (token) {
if (strcmp(token, "exit") == 0) {
break; // exit from console mode
}
execute_cmd(token);
}
}
if ((return_error = rpi_gpio_cleanup()) != GPIO_NO_ERROR) {
return 1;
}
return 0;
}