Skip to content

Commit 272dcb4

Browse files
committed
gpio: Updated GPIO actuator API to only expose init function (on/off
handled by lower-half actuator API). GPIO now take a device path to allow control over multiple different GPIO. Real GPIO can be actuated in NuttX.
1 parent d62b224 commit 272dcb4

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

pad_server/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ MAINSRC = src/pad_server_main.c
1818

1919
CSRCS += $(wildcard src/*.c)
2020
CSRCS += ../packets/packet.c
21-
CSRCS := $(filter-out src/gpio_actuator.c, $(CSRCS))
21+
CSRCS := $(filter-out src/gpio_dummy_actuator.c, $(CSRCS))
2222

2323
include $(APPDIR)/Application.mk
2424

pad_server/src/gpio_actuator.c

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
// BEWARE, this file is expluded from Linux compilation
2-
// because the of the missing nuttx header file
1+
/* WARNING: This file is expluded from Linux compilation because the of the missing NuttX header file */
32

43
#include "gpio_actuator.h"
4+
#include "actuator.h"
55
#include "stdio.h"
66
#include <errno.h>
77
#include <fcntl.h>
88
#include <nuttx/ioexpander/gpio.h>
99
#include <sys/ioctl.h>
1010
#include <unistd.h>
1111

12-
int gpio_actuator_on(actuator_t *act) {
13-
// TODO: change the device to be actuator specific
14-
int fd = open("/dev/gpio12", O_RDWR);
12+
/*
13+
* Turn on a GPIO actuator.
14+
* @param act The actuator to turn on.
15+
* @return 0 on success, an error code on failure.
16+
*/
17+
static int gpio_actuator_on(actuator_t *act) {
18+
int fd = open((char *)act->priv, O_RDWR);
1519
if (fd < 0) {
16-
fprintf(stderr, "Failed to open gpio with err %d\n", errno);
20+
fprintf(stderr, "Failed to open gpio '%s' with err %d\n", (char *)act->priv, errno);
1721
return -1;
1822
}
1923

@@ -32,11 +36,16 @@ int gpio_actuator_on(actuator_t *act) {
3236
return 0;
3337
}
3438

35-
int gpio_actuator_off(actuator_t *act) {
36-
// TODO: change the device to be actuator specific
37-
int fd = open("/dev/gpio12", O_RDWR);
39+
/*
40+
* Turn off a GPIO actuator.
41+
* @param act The actuator to turn off.
42+
* @return 0 on success, an error code on failure.
43+
*/
44+
static int gpio_actuator_off(actuator_t *act) {
45+
46+
int fd = open((char *)act->priv, O_RDWR);
3847
if (fd < 0) {
39-
fprintf(stderr, "Failed to open gpio with err %d\n", errno);
48+
fprintf(stderr, "Failed to open gpio '%s' with err %d\n", (char *)act->priv, errno);
4049
return -1;
4150
}
4251

@@ -54,3 +63,13 @@ int gpio_actuator_off(actuator_t *act) {
5463
fprintf(stdout, "Actuator #%d turned off\n", act->id);
5564
return 0;
5665
}
66+
67+
/*
68+
* Initialize a GPIO actuator.
69+
* @param act The actuator structure to initialize.
70+
* @param id The actuator ID
71+
* @param dev The path to the GPIO character device
72+
*/
73+
void gpio_actuator_init(actuator_t *act, uint8_t id, const char *dev) {
74+
actuator_init(act, id, gpio_actuator_on, gpio_actuator_off, (char *)dev);
75+
}

pad_server/src/gpio_actuator.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include "actuator.h"
22

3-
int gpio_actuator_on(actuator_t *act);
4-
int gpio_actuator_off(actuator_t *act);
3+
/*
4+
* Initialize a GPIO actuator.
5+
* @param act The actuator structure to initialize.
6+
* @param id The actuator ID
7+
* @param dev The path to the GPIO character device
8+
*/
9+
void gpio_actuator_init(actuator_t *act, uint8_t id, const char *dev);

pad_server/src/gpio_dummy_actuator.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
#include "gpio_actuator.h"
22
#include "stdio.h"
33

4-
int gpio_actuator_on(actuator_t *act) {
4+
/*
5+
* Turn on a GPIO actuator.
6+
* @param act The actuator to turn on.
7+
* @return 0 on success, an error code on failure.
8+
*/
9+
static int gpio_actuator_on(actuator_t *act) {
510
printf("Dummy actuator #%d turned on\n", act->id);
611
return 0;
712
}
8-
int gpio_actuator_off(actuator_t *act) {
13+
14+
/*
15+
* Turn off a GPIO actuator.
16+
* @param act The actuator to turn off.
17+
* @return 0 on success, an error code on failure.
18+
*/
19+
static int gpio_actuator_off(actuator_t *act) {
920
printf("Dummy actuator #%d turned off\n", act->id);
1021
return 0;
1122
}
23+
24+
/*
25+
* Initialize a GPIO actuator.
26+
* @param act The actuator structure to initialize.
27+
* @param id The actuator ID
28+
* @param dev The path to the GPIO character device (unused)
29+
*/
30+
void gpio_actuator_init(actuator_t *act, uint8_t id, const char *dev) {
31+
(void)(dev);
32+
actuator_init(act, id, gpio_actuator_on, gpio_actuator_off, NULL);
33+
}

pad_server/src/state.c

+23-6
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,42 @@
1212
#include "state.h"
1313
#include <sys/ioctl.h>
1414

15-
// defined in include/nuttx/ioexpander/gpio.h
16-
#define GPIOC_WRITE 8961
15+
static const char *ACTUATOR_GPIO[NUM_ACTUATORS] = {
16+
[ID_FIRE_VALVE] = "/dev/gpio27", [ID_XV1] = "/dev/gpio2",
17+
[ID_XV2] = "/dev/gpio3", [ID_XV3] = "/dev/gpio4",
18+
[ID_XV4] = "/dev/gpio5", [ID_XV5] = "/dev/gpio6",
19+
[ID_XV6] = "/dev/gpio7", [ID_XV7] = "/dev/gpio8",
20+
[ID_XV8] = "/dev/gpio9", [ID_XV9] = "/dev/gpio10",
21+
[ID_XV10] = "/dev/gpio11", [ID_XV11] = "/dev/gpio12",
22+
[ID_XV12] = "/dev/gpio12", [ID_QUICK_DISCONNECT] = "/dev/gpio26",
23+
[ID_IGNITER] = "/dev/gpio28", // TODO double check?
24+
};
1725

18-
/* TODO: docs */
26+
/*
27+
* Initialize the shared pad state. This includes initializing the synchronization objects (rwlock), pad arming state
28+
* and actuators.
29+
* @param state The state to initialize.
30+
*/
1931
void padstate_init(padstate_t *state) {
2032
pthread_rwlock_init(&state->rw_lock, NULL);
33+
2134
// TODO: Is this right? Can we assume if the program is running then the pad is armed?
35+
// TODO: make GPIO device path change
36+
2237
state->arm_level = ARMED_PAD;
2338
for (unsigned int i = 0; i < NUM_ACTUATORS; i++) {
24-
actuator_init(&state->actuators[i], i, gpio_actuator_on, gpio_actuator_off, NULL);
39+
gpio_actuator_init(&state->actuators[i], i, ACTUATOR_GPIO[i]);
2540
}
2641

2742
pthread_mutex_init(&state->update_mut, NULL);
2843
pthread_cond_init(&state->update_cond, NULL);
2944
state->update_recorded = false;
3045
}
3146

32-
/* TODO: docs
33-
*
47+
/*
48+
* Gets the current arming level of the pad.
49+
* @param state The pad state
50+
* @return The current arming level
3451
*/
3552
arm_lvl_e padstate_get_level(padstate_t *state) {
3653
/* Something has gone terribly wrong if reading a variable doesn't work, so no errors are returned from here */

0 commit comments

Comments
 (0)