APM:Libraries
GPIO.cpp
Go to the documentation of this file.
1 #include <AP_HAL/AP_HAL.h>
2 
3 #if CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
4 
5 #include "GPIO.h"
6 
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 
12 /* VRBRAIN headers */
13 #include <drivers/drv_led.h>
14 #include <drivers/drv_tone_alarm.h>
15 #include <drivers/drv_gpio.h>
16 
17 #include <arch/board/board.h>
18 #include <board_config.h>
19 
20 #define LOW 0
21 #define HIGH 1
22 
23 extern const AP_HAL::HAL& hal;
24 
25 using namespace VRBRAIN;
26 
28 {}
29 
31 {
32 
33  _led_fd = open(LED0_DEVICE_PATH, O_RDWR);
34  if (_led_fd == -1) {
35  AP_HAL::panic("Unable to open " LED0_DEVICE_PATH);
36  }
37  if (ioctl(_led_fd, LED_OFF, LED_BLUE) != 0) {
38  hal.console->printf("GPIO: Unable to setup GPIO LED BLUE\n");
39  }
40  if (ioctl(_led_fd, LED_OFF, LED_RED) != 0) {
41  hal.console->printf("GPIO: Unable to setup GPIO LED RED\n");
42  }
43 
44  if (ioctl(_led_fd, LED_OFF, LED_GREEN) != 0) {
45  hal.console->printf("GPIO: Unable to setup GPIO LED GREEN\n");
46  }
47 
48 
49 
50  _tone_alarm_fd = open(TONEALARM0_DEVICE_PATH, O_WRONLY);
51  if (_tone_alarm_fd == -1) {
52  AP_HAL::panic("Unable to open " TONEALARM0_DEVICE_PATH);
53  }
54 
55  _gpio_fmu_fd = open(PX4FMU_DEVICE_PATH, 0);
56  if (_gpio_fmu_fd == -1) {
57  AP_HAL::panic("Unable to open GPIO");
58  }
59 #ifdef GPIO_SERVO_1
60  if (ioctl(_gpio_fmu_fd, GPIO_CLEAR, GPIO_SERVO_1) != 0) {
61  hal.console->printf("GPIO: Unable to setup GPIO_1\n");
62  }
63 #endif
64 #ifdef GPIO_SERVO_2
65  if (ioctl(_gpio_fmu_fd, GPIO_CLEAR, GPIO_SERVO_2) != 0) {
66  hal.console->printf("GPIO: Unable to setup GPIO_2\n");
67  }
68 #endif
69 #ifdef GPIO_SERVO_3
70  if (ioctl(_gpio_fmu_fd, GPIO_CLEAR, GPIO_SERVO_3) != 0) {
71  hal.console->printf("GPIO: Unable to setup GPIO_3\n");
72  }
73 #endif
74 #ifdef GPIO_SERVO_4
75  if (ioctl(_gpio_fmu_fd, GPIO_CLEAR, GPIO_SERVO_4) != 0) {
76  hal.console->printf("GPIO: Unable to setup GPIO_4\n");
77  }
78 #endif
79 }
80 
81 void VRBRAINGPIO::pinMode(uint8_t pin, uint8_t output)
82 {
83  switch (pin) {
84  }
85 }
86 
88 {
89  return -1;
90 }
91 
92 
93 uint8_t VRBRAINGPIO::read(uint8_t pin) {
94  switch (pin) {
95 
96 #ifdef GPIO_SERVO_3
97  case EXTERNAL_RELAY1_PIN: {
98  uint32_t relays = 0;
99  ioctl(_gpio_fmu_fd, GPIO_GET, (unsigned long)&relays);
100  return (relays & GPIO_SERVO_3)?HIGH:LOW;
101  }
102 #endif
103 
104 #ifdef GPIO_SERVO_4
105  case EXTERNAL_RELAY2_PIN: {
106  uint32_t relays = 0;
107  ioctl(_gpio_fmu_fd, GPIO_GET, (unsigned long)&relays);
108  return (relays & GPIO_SERVO_4)?HIGH:LOW;
109  }
110 #endif
111 
112  }
113  return LOW;
114 }
115 
116 void VRBRAINGPIO::write(uint8_t pin, uint8_t value)
117 {
118  switch (pin) {
119 
120  case HAL_GPIO_A_LED_PIN: // Arming LED
121  if (value == LOW) {
122  ioctl(_led_fd, LED_OFF, LED_GREEN);
123  } else {
124  ioctl(_led_fd, LED_ON, LED_GREEN);
125  }
126  break;
127 
128  case HAL_GPIO_B_LED_PIN: // not used yet
129  if (value == LOW) {
130  ioctl(_led_fd, LED_OFF, LED_BLUE);
131  } else {
132  ioctl(_led_fd, LED_ON, LED_BLUE);
133  }
134  break;
135 
136  case HAL_GPIO_C_LED_PIN: // GPS LED
137  if (value == LOW) {
138  ioctl(_led_fd, LED_OFF, LED_RED);
139  } else {
140  ioctl(_led_fd, LED_ON, LED_RED);
141  }
142  break;
143 
144 #ifdef GPIO_SERVO_1
145  case EXTERNAL_LED_GPS:
146  ioctl(_gpio_fmu_fd, value==LOW?GPIO_CLEAR:GPIO_SET, GPIO_SERVO_1);
147  break;
148 #endif
149 
150 #ifdef GPIO_SERVO_2
151  case EXTERNAL_LED_ARMED:
152  ioctl(_gpio_fmu_fd, value==LOW?GPIO_CLEAR:GPIO_SET, GPIO_SERVO_2);
153  break;
154 #endif
155 
156 #ifdef GPIO_SERVO_3
157  case EXTERNAL_RELAY1_PIN:
158  ioctl(_gpio_fmu_fd, value==LOW?GPIO_CLEAR:GPIO_SET, GPIO_SERVO_3);
159  break;
160 #endif
161 
162 #ifdef GPIO_SERVO_4
163  case EXTERNAL_RELAY2_PIN:
164  ioctl(_gpio_fmu_fd, value==LOW?GPIO_CLEAR:GPIO_SET, GPIO_SERVO_4);
165  break;
166 #endif
167 
168  }
169 }
170 
172 {
173  write(pin, !read(pin));
174 }
175 
176 /* Alternative interface: */
178  return new VRBRAINDigitalSource(0);
179 }
180 
181 /* Interrupt interface: */
182 bool VRBRAINGPIO::attach_interrupt(uint8_t interrupt_num, AP_HAL::Proc p, uint8_t mode)
183 {
184  return true;
185 }
186 
187 /*
188  return true when USB connected
189  */
191 {
192  /*
193  we use a combination of voltage on the USB connector and the
194  open of the /dev/ttyACM0 character device. This copes with
195  systems where the VBUS may go high even with no USB connected
196  (such as AUAV-X2)
197  */
198  return stm32_gpioread(GPIO_OTGFS_VBUS) && _usb_connected;
199 }
200 
201 
203  _v(v)
204 {}
205 
206 void VRBRAINDigitalSource::mode(uint8_t output)
207 {}
208 
210  return _v;
211 }
212 
214  _v = value;
215 }
216 
218  _v = !_v;
219 }
220 
221 #endif // CONFIG_HAL_BOARD
const AP_HAL::HAL & hal
Definition: AC_PID_test.cpp:14
#define LED_OFF
Definition: Config.h:44
int8_t analogPinToDigitalPin(uint8_t pin) override
Definition: GPIO.cpp:87
#define EXTERNAL_RELAY2_PIN
Definition: GPIO.h:15
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:885
AP_HAL::UARTDriver * console
Definition: HAL.h:110
void write(uint8_t pin, uint8_t value) override
Definition: GPIO.cpp:116
#define LED_BLUE
Definition: GPIO_BBB.h:20
#define HAL_GPIO_A_LED_PIN
Definition: chibios.h:9
int _tone_alarm_fd
Definition: GPIO.h:45
void pinMode(uint8_t pin, uint8_t output) override
Definition: GPIO.cpp:81
#define HAL_GPIO_C_LED_PIN
Definition: chibios.h:15
void(* Proc)(void)
void init() override
Definition: GPIO.cpp:30
void toggle(uint8_t pin) override
Definition: GPIO.cpp:171
AP_HAL::DigitalSource * channel(uint16_t n) override
Definition: GPIO.cpp:177
virtual void printf(const char *,...) FMT_PRINTF(2
Definition: BetterStream.cpp:5
#define GPIO_GET(g)
Definition: GPIO_RPI.cpp:32
void write(uint8_t value)
Definition: GPIO.cpp:213
VRBRAINDigitalSource(uint8_t v)
Definition: GPIO.cpp:202
bool _usb_connected
Definition: GPIO.h:48
float v
Definition: Printf.cpp:15
void mode(uint8_t output)
Definition: GPIO.cpp:206
#define EXTERNAL_RELAY1_PIN
Definition: GPIO.h:14
#define LOW
Definition: GPIO.cpp:20
float value
#define HAL_GPIO_B_LED_PIN
Definition: chibios.h:12
#define EXTERNAL_LED_ARMED
Definition: vrbrain.h:47
bool usb_connected(void) override
Definition: GPIO.cpp:190
#define LED_ON
Definition: Config.h:43
void panic(const char *errormsg,...) FMT_PRINTF(1
Definition: system.cpp:140
static int8_t pin
Definition: AnalogIn.cpp:15
uint8_t read(uint8_t pin) override
Definition: GPIO.cpp:93
#define EXTERNAL_LED_GPS
Definition: vrbrain.h:46
bool attach_interrupt(uint8_t interrupt_num, AP_HAL::Proc p, uint8_t mode) override
Definition: GPIO.cpp:182
#define HIGH
Definition: GPIO.cpp:21