forked from projectara/gbsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.c
197 lines (183 loc) · 5.72 KB
/
gpio.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
/*
* Greybus Simulator
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
#include <fcntl.h>
#include <pthread.h>
#include <libsoc_gpio.h>
#include <linux/fs.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "gbsim.h"
static int gpio_dir[6];
static gpio *gpios[6];
int gpio_handler(uint16_t cport_id, uint16_t hd_cport_id, void *rbuf,
size_t rsize, void *tbuf, size_t tsize)
{
struct gb_operation_msg_hdr *oph;
struct op_msg *op_req = rbuf;
struct op_msg *op_rsp;
size_t payload_size;
uint16_t message_size;
ssize_t nbytes;
op_rsp = (struct op_msg *)tbuf;
oph = (struct gb_operation_msg_hdr *)&op_req->header;
switch (oph->type) {
case GB_GPIO_TYPE_PROTOCOL_VERSION:
payload_size = sizeof(struct gb_protocol_version_response);
op_rsp->pv_rsp.major = GREYBUS_VERSION_MAJOR;
op_rsp->pv_rsp.minor = GREYBUS_VERSION_MINOR;
break;
case GB_GPIO_TYPE_LINE_COUNT:
payload_size = sizeof(struct gb_gpio_line_count_response);
op_rsp->gpio_lc_rsp.count = 5; /* Something arbitrary, but useful */
break;
case GB_GPIO_TYPE_ACTIVATE:
payload_size = 0;
gbsim_debug("GPIO %d activate request\n ",
op_req->gpio_act_req.which);
break;
case GB_GPIO_TYPE_DEACTIVATE:
payload_size = 0;
gbsim_debug("GPIO %d deactivate request\n ",
op_req->gpio_deact_req.which);
break;
case GB_GPIO_TYPE_GET_DIRECTION:
payload_size = sizeof(struct gb_gpio_get_direction_response);
if (bbb_backend)
op_rsp->gpio_get_dir_rsp.direction = libsoc_gpio_get_direction(gpios[op_req->gpio_dir_output_req.which]);
else
op_rsp->gpio_get_dir_rsp.direction = gpio_dir[op_req->gpio_get_dir_req.which];
gbsim_debug("GPIO %d get direction (%d) response\n ",
op_req->gpio_get_dir_req.which, op_rsp->gpio_get_dir_rsp.direction);
break;
case GB_GPIO_TYPE_DIRECTION_IN:
payload_size = 0;
gbsim_debug("GPIO %d direction input request\n ",
op_req->gpio_dir_input_req.which);
if (bbb_backend)
libsoc_gpio_set_direction(gpios[op_req->gpio_dir_output_req.which], INPUT);
else
gpio_dir[op_req->gpio_dir_output_req.which] = 0;
break;
case GB_GPIO_TYPE_DIRECTION_OUT:
payload_size = 0;
gbsim_debug("GPIO %d direction output request\n ",
op_req->gpio_dir_output_req.which);
if (bbb_backend)
libsoc_gpio_set_direction(gpios[op_req->gpio_dir_output_req.which], OUTPUT);
else
gpio_dir[op_req->gpio_dir_output_req.which] = 1;
break;
case GB_GPIO_TYPE_GET_VALUE:
payload_size = sizeof(struct gb_gpio_get_value_response);
if (bbb_backend)
op_rsp->gpio_get_val_rsp.value = libsoc_gpio_get_level(gpios[op_req->gpio_dir_output_req.which]);
else
op_rsp->gpio_get_val_rsp.value = 1;
gbsim_debug("GPIO %d get value (%d) response\n ",
op_req->gpio_get_val_req.which, op_rsp->gpio_get_val_rsp.value);
break;
case GB_GPIO_TYPE_SET_VALUE:
payload_size = 0;
gbsim_debug("GPIO %d set value (%d) request\n ",
op_req->gpio_set_val_req.which, op_req->gpio_set_val_req.value);
if (bbb_backend)
libsoc_gpio_set_level(gpios[op_req->gpio_set_val_req.which], op_req->gpio_set_val_req.value);
break;
case GB_GPIO_TYPE_SET_DEBOUNCE:
payload_size = 0;
gbsim_debug("GPIO %d set debounce (%d us) request\n ",
op_req->gpio_set_db_req.which, op_req->gpio_set_db_req.usec);
break;
case GB_GPIO_TYPE_IRQ_TYPE:
payload_size = 0;
gbsim_debug("GPIO protocol IRQ type %d request\n ",
op_req->gpio_irq_type_req.type);
break;
case GB_GPIO_TYPE_IRQ_MASK:
payload_size = 0;
break;
case GB_GPIO_TYPE_IRQ_UNMASK:
payload_size = 0;
break;
default:
return -EINVAL;
}
message_size = sizeof(struct gb_operation_msg_hdr) + payload_size;
nbytes = send_response(op_rsp, hd_cport_id, message_size, oph,
PROTOCOL_STATUS_SUCCESS);
if (nbytes)
return nbytes;
#define TEST_HACK
#ifdef TEST_HACK
/* Test GPIO interrupts by sending one when they become unmasked */
if (oph->type == GB_GPIO_TYPE_IRQ_UNMASK) {
payload_size = sizeof(struct gb_gpio_irq_event_request);
op_req->gpio_irq_event_req.which = 1; /* XXX HACK */
message_size = sizeof(struct gb_operation_msg_hdr) + payload_size;
return send_request(op_req, hd_cport_id, message_size, 0,
GB_GPIO_TYPE_IRQ_EVENT);
}
#endif
return 0;
}
char *gpio_get_operation(uint8_t type)
{
switch (type) {
case GB_GPIO_TYPE_INVALID:
return "GB_GPIO_TYPE_INVALID";
case GB_GPIO_TYPE_PROTOCOL_VERSION:
return "GB_GPIO_TYPE_PROTOCOL_VERSION";
case GB_GPIO_TYPE_LINE_COUNT:
return "GB_GPIO_TYPE_LINE_COUNT";
case GB_GPIO_TYPE_ACTIVATE:
return "GB_GPIO_TYPE_ACTIVATE";
case GB_GPIO_TYPE_DEACTIVATE:
return "GB_GPIO_TYPE_DEACTIVATE";
case GB_GPIO_TYPE_GET_DIRECTION:
return "GB_GPIO_TYPE_GET_DIRECTION";
case GB_GPIO_TYPE_DIRECTION_IN:
return "GB_GPIO_TYPE_DIRECTION_IN";
case GB_GPIO_TYPE_DIRECTION_OUT:
return "GB_GPIO_TYPE_DIRECTION_OUT";
case GB_GPIO_TYPE_GET_VALUE:
return "GB_GPIO_TYPE_GET_VALUE";
case GB_GPIO_TYPE_SET_VALUE:
return "GB_GPIO_TYPE_SET_VALUE";
case GB_GPIO_TYPE_SET_DEBOUNCE:
return "GB_GPIO_TYPE_SET_DEBOUNCE";
case GB_GPIO_TYPE_IRQ_TYPE:
return "GB_GPIO_TYPE_IRQ_TYPE";
case GB_GPIO_TYPE_IRQ_MASK:
return "GB_GPIO_TYPE_IRQ_MASK";
case GB_GPIO_TYPE_IRQ_UNMASK:
return "GB_GPIO_TYPE_IRQ_UNMASK";
case GB_GPIO_TYPE_IRQ_EVENT:
return "GB_GPIO_TYPE_IRQ_EVENT";
default:
return "(Unknown operation)";
}
}
void gpio_init(void)
{
int i;
if (bbb_backend) {
/*
* Grab the four onboard LEDs (gpio1:24-27) and then
* P9-12 and P8-26 (gpio1:28-29) to support input. The
* pins on the header can be used in loopback mode for
* testing.
*/
for (i=0; i<6; i++)
gpios[i] = libsoc_gpio_request(56+i, LS_GREEDY);
}
}