Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
gpio_ardrone.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Hugo Perquin - http://blog.perquin.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA.
18  */
19 
25 #ifdef ARDRONE2_RAW
26 
27 #include <fcntl.h> /* File control definitions */
28 #include <errno.h> /* Error number definitions */
29 #include <sys/ioctl.h>
30 
31 #include "mcu_periph/gpio.h"
32 
33 #define GPIO_MAGIC 'p'
34 #define GPIO_DIRECTION _IOW(GPIO_MAGIC, 0, struct gpio_direction)
35 #define GPIO_READ _IOWR(GPIO_MAGIC, 1, struct gpio_data)
36 #define GPIO_WRITE _IOW(GPIO_MAGIC, 2, struct gpio_data)
37 int gpiofp = 0;
38 
39 struct gpio_data {
40  int pin;
41  int value;
42 };
43 
44 enum gpio_mode {
45  GPIO_INPUT = 0,
46  GPIO_OUTPUT_LOW,
47  GPIO_OUTPUT_HIGH,
48 };
49 
50 struct gpio_direction {
51  int pin;
52  enum gpio_mode mode;
53 };
54 
55 
56 void gpio_set(uint32_t port, uint16_t pin)
57 {
58  if (port != 0x32524) return; // protect ardrone board from unauthorized use
59  struct gpio_data data;
60  // Open the device if not open
61  if (gpiofp == 0)
62  gpiofp = open("/dev/gpio",O_RDWR);
63 
64  // Read the GPIO value
65  data.pin = pin;
66  data.value = 1;
67  ioctl(gpiofp, GPIO_WRITE, &data);
68 }
69 
70 
71 void gpio_clear(uint32_t port, uint16_t pin)
72 {
73  if (port != 0x32524) return; // protect ardrone board from unauthorized use
74  struct gpio_data data;
75  // Open the device if not open
76  if (gpiofp == 0)
77  gpiofp = open("/dev/gpio",O_RDWR);
78 
79  // Read the GPIO value
80  data.pin = pin;
81  data.value = 0;
82  ioctl(gpiofp, GPIO_WRITE, &data);
83 }
84 
85 
86 void gpio_setup_input(uint32_t port, uint16_t pin)
87 {
88  if (port != 0x32524) return; // protect ardrone board from unauthorized use
89  struct gpio_direction dir;
90  // Open the device if not open
91  if (gpiofp == 0)
92  gpiofp = open("/dev/gpio",O_RDWR);
93 
94  // Read the GPIO value
95  dir.pin = pin;
96  dir.mode = GPIO_INPUT;
97  ioctl(gpiofp, GPIO_DIRECTION, &dir);
98 }
99 
100 
101 void gpio_setup_output(uint32_t port, uint16_t pin)
102 {
103  /*
104  if (port != 0x32524) return; // protect ardrone board from unauthorized use
105  struct gpio_direction dir;
106  // Open the device if not open
107  if (gpiofp == 0)
108  gpiofp = open("/dev/gpio",O_RDWR);
109 
110  // Read the GPIO value
111  dir.pin = pin;
112  dir.mode = GPIO_OUTPUT_LOW;
113  ioctl(gpiofp, GPIO_DIRECTION, &dir);
114  */
115 }
116 
117 
118 
120 {
121  if (port != 0x32524) return 0; // protect ardrone board from unauthorized use
122  struct gpio_data data;
123  // Open the device if not open
124  if (gpiofp == 0)
125  gpiofp = open("/dev/gpio",O_RDWR);
126 
127  // Read the GPIO value
128  data.pin = pin;
129  ioctl(gpiofp, GPIO_READ, &data);
130  return data.value;
131 }
132 
133 #endif
unsigned short uint16_t
Definition: types.h:16
uint16_t value
Definition: adc_arch.c:585
Some architecture independent helper functions for GPIOs.
void gpio_set(uint32_t port, uint16_t pin)
Set a gpio output to high level.
unsigned long uint32_t
Definition: types.h:18
void gpio_clear(uint32_t port, uint16_t pin)
Clear a gpio output to low level.
static void gpio_setup_output(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.h:76
static void gpio_setup_input(uint32_t port, uint32_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.h:88
uint16_t gpio_get(uint32_t gpioport, uint16_t gpios)
Read a gpio value.