Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 #include <fcntl.h> /* File control definitions */
26 #include <errno.h> /* Error number definitions */
27 #include <sys/ioctl.h>
28 
29 #include "mcu_periph/gpio.h"
30 
31 #define GPIO_MAGIC 'p'
32 #define GPIO_DIRECTION _IOW(GPIO_MAGIC, 0, struct gpio_direction)
33 #define GPIO_READ _IOWR(GPIO_MAGIC, 1, struct gpio_data)
34 #define GPIO_WRITE _IOW(GPIO_MAGIC, 2, struct gpio_data)
35 int gpiofp = 0;
36 
37 struct gpio_data {
38  int pin;
39  int value;
40 };
41 
42 enum gpio_mode {
43  GPIO_INPUT = 0,
46 };
47 
49  int pin;
50  enum gpio_mode mode;
51 };
52 
53 
54 void gpio_set(uint32_t port, uint16_t pin)
55 {
56  if (port != 0x32524) { return; } /* protect ardrone board from unauthorized use */
57  struct gpio_data data;
58  // Open the device if not open
59  if (gpiofp == 0) {
60  gpiofp = open("/dev/gpio", O_RDWR);
61  }
62 
63  /* Read the GPIO value */
64  data.pin = pin;
65  data.value = 1;
66  ioctl(gpiofp, GPIO_WRITE, &data);
67 }
68 
69 
71 {
72  if (port != 0x32524) { return; } /* protect ardrone board from unauthorized use */
73  struct gpio_data data;
74  // Open the device if not open
75  if (gpiofp == 0) {
76  gpiofp = open("/dev/gpio", O_RDWR);
77  }
78 
79  /* Read the GPIO value */
80  data.pin = pin;
81  data.value = 0;
82  ioctl(gpiofp, GPIO_WRITE, &data);
83 }
84 
85 
87 {
88  if (port != 0x32524) { return; } /* protect ardrone board from unauthorized use */
89  struct gpio_direction dir;
90  /* Open the device if not yet opened*/
91  if (gpiofp == 0) {
92  gpiofp = open("/dev/gpio", O_RDWR);
93  }
94 
95  /* Read the GPIO value */
96  dir.pin = pin;
97  dir.mode = GPIO_INPUT;
98  ioctl(gpiofp, GPIO_DIRECTION, &dir);
99 }
100 
101 
103 {
104  /*
105  if (port != 0x32524) return; // protect ardrone board from unauthorized use
106  struct gpio_direction dir;
107  // Open the device if not open
108  if (gpiofp == 0)
109  gpiofp = open("/dev/gpio",O_RDWR);
110 
111  // Read the GPIO value
112  dir.pin = pin;
113  dir.mode = GPIO_OUTPUT_LOW;
114  ioctl(gpiofp, GPIO_DIRECTION, &dir);
115  */
116 }
117 
118 
119 
121 {
122  if (port != 0x32524) { return 0; } /* protect ARDroneX board from unauthorized use */
123  struct gpio_data data;
124  /* Open the device if not open */
125  if (gpiofp == 0) {
126  gpiofp = open("/dev/gpio", O_RDWR);
127  }
128 
129  /* Read the GPIO value */
130  data.pin = pin;
131  ioctl(gpiofp, GPIO_READ, &data);
132  return data.value;
133 }
Some architecture independent helper functions for GPIOs.
#define GPIO_READ
Definition: gpio_ardrone.c:33
void gpio_setup_input(uint32_t port, uint16_t pin)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_ardrone.c:86
#define GPIO_WRITE
Definition: gpio_ardrone.c:34
void gpio_set(uint32_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_ardrone.c:54
gpio_mode
Definition: gpio_ardrone.c:42
@ GPIO_OUTPUT_LOW
Pin configured for output with low level.
Definition: gpio_ardrone.c:44
@ GPIO_OUTPUT_HIGH
Pin configured for output with high level.
Definition: gpio_ardrone.c:45
@ GPIO_INPUT
Pin configured for input.
Definition: gpio_ardrone.c:43
enum gpio_mode mode
Definition: gpio_ardrone.c:50
#define GPIO_DIRECTION
Definition: gpio_ardrone.c:32
int gpiofp
Definition: gpio_ardrone.c:35
void gpio_clear(uint32_t port, uint16_t pin)
Clear a gpio output to low level.
Definition: gpio_ardrone.c:70
void gpio_setup_output(uint32_t port, uint16_t pin)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_ardrone.c:102
uint16_t gpio_get(uint32_t port, uint16_t pin)
Read a gpio value.
Definition: gpio_ardrone.c:120
static const float dir[]
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78