This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtcomm_ipc_test.c
160 lines (125 loc) · 5.91 KB
/
rtcomm_ipc_test.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
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "rtcomm.h"
#define IPC_API_INPUTS_QUANTITY 14u
#define IPC_API_OUTPUTS_QUANTITY 7u
#define IPC_API_ANALOG_INPUTS_QUANTITY 8u
#define IPC_API_PWM_QUANTITY 2u
#define IPC_API_INPUTS_SAMPLING_FREQUENCY_Hz 100u
#define IPC_API_ANALOG_SAMPLING_FREQUENCY_Hz 20000u
#define IPC_API_MASTER_READ_FRAME_RATE_Hz 10u
#define IPC_API_IO_SAMPLES_PER_FRAME \
(IPC_API_INPUTS_SAMPLING_FREQUENCY_Hz / IPC_API_MASTER_READ_FRAME_RATE_Hz)
#define IPC_API_ANALOG_SAMPLES_PER_FRAME \
(IPC_API_ANALOG_SAMPLING_FREQUENCY_Hz / IPC_API_MASTER_READ_FRAME_RATE_Hz)
#define IPC_IO_ATTR_ __attribute__((packed))
struct IPC_IO_ATTR_ ipc_rtc_time
{
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
};
struct IPC_IO_ATTR_ ipc_timestamp
{
struct ipc_rtc_time rtc_time;
uint16_t ms_time;
};
struct IPC_IO_ATTR_ ipc_io_status
{
uint8_t inputs_status[IPC_API_INPUTS_QUANTITY];
uint8_t outputs_status[IPC_API_OUTPUTS_QUANTITY];
};
struct IPC_IO_ATTR_ ipc_acquisition_frame
{
uint32_t frame_counter;
struct ipc_timestamp timestamp;
uint8_t timestamp_validity;
uint16_t analog_data[IPC_API_ANALOG_SAMPLES_PER_FRAME][IPC_API_ANALOG_INPUTS_QUANTITY];
uint8_t analog_data_validity;
struct ipc_io_status io_status[IPC_API_IO_SAMPLES_PER_FRAME];
uint8_t io_status_validity;
};
int main(int argc, char * argv[])
{
int fd;
int buffer_size = sizeof(struct ipc_acquisition_frame);
int count;
char version[20];
void * buffer;
fprintf(stdout, "RTCOMM drv test 1, ver:" __DATE__ " : " __TIME__ "\n");
fprintf(stdout, "Using buffer size %d bytes\n", buffer_size);
buffer = malloc(buffer_size);
if (!buffer) {
fprintf(stderr, "Couldn't allocate buffer of size %d\n",
buffer_size);
return (1);
}
sleep(1);
fd = open("/dev/" RTCOMM_NAME, O_RDONLY);
if (!fd) {
fprintf(stderr, "Failed to open driver: %d\n", errno);
}
if (ioctl(fd, RTCOMM_GET_VERSION, version) == -1) {
fprintf(stderr, "RTCOMM_GET_VERSION failed: %d\n", errno);
return (1);
} else {
fprintf(stderr, "RTCOMM_GET_VERSION get: %s\n", version);
}
if (ioctl(fd, RTCOMM_SET_SIZE, &buffer_size) == -1) {
fprintf(stderr, "RTCOMM_SET_SIZE failed: %d\n", errno);
return (1);
} else {
fprintf(stderr, "RTCOMM_SET_SIZE set: %d\n", buffer_size);
}
if (ioctl(fd, RTCOMM_START)) {
fprintf(stderr, "RTCOMM_START failed: %d\n", errno);
return (1);
} else {
fprintf(stderr, "RTCOMM_START success.\n");
}
count = 0;
for (;;) {
int ret;
uint32_t idx;
uint32_t to_idx;
uint32_t idxl;
struct ipc_acquisition_frame * frame = buffer;
ret = read(fd, buffer, buffer_size);
if (ret != buffer_size) {
fprintf(stderr, "Failed to read %d bytes, error: %d\n",
buffer_size, ret);
return (-1);
}
fprintf(stderr, "Got %d frame: %04u-%02u-%02u - %02u:%02u:%02u.%04u\n",
frame->frame_counter,
frame->timestamp.rtc_time.year,
frame->timestamp.rtc_time.month,
frame->timestamp.rtc_time.day,
frame->timestamp.rtc_time.hour,
frame->timestamp.rtc_time.minute,
frame->timestamp.ms_time);
for (idx = 0; idx < IPC_API_ANALOG_SAMPLES_PER_FRAME; idx++) {
char local_buffer[200];
uint32_t str_idx;
sprintf(local_buffer, "%u, %d, ", frame->frame_counter, idx);
str_idx = strlen(local_buffer);
for (idxl = 0; idxl < IPC_API_ANALOG_INPUTS_QUANTITY; idxl++) {
sprintf(&local_buffer[str_idx], "%d, ",
(int16_t)frame->analog_data[idx][idxl]);
str_idx = strlen(local_buffer);
}
puts(local_buffer);
}
}
return (0);
}