Blame view

RIOT/drivers/include/periph/gpio.h 7.18 KB
a752c7ab   elopes   add first test an...
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  /*
   * Copyright (C) 2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup    drivers_periph_gpio GPIO
   * @ingroup     drivers_periph
   * @brief       Low-level GPIO peripheral driver
   *
   * This is a basic GPIO (General-purpose input/output) interface to allow
   * platform independent access to a MCU's input/output pins. This interface is
   * intentionally designed to be as simple as possible, to allow for easy
   * implementation and maximum portability.
   *
   * The interface provides capabilities to initialize a pin as output-,
   * input- and interrupt pin. With the API you can basically set/clear/toggle the
   * digital signal at the hardware pin when in output mode. Configured as input you can
   * read a digital value that is being applied to the pin externally. When initializing
   * an external interrupt pin, you can register a callback function that is executed
   * in interrupt context once the interrupt condition applies to the pin. Usually you
   * can react to rising or falling signal flanks (or both).
   *
   * In addition the API provides to set standard input/output circuit modes such as
   * e.g. internal push-pull configurations.
   *
   * All modern micro controllers organize their GPIOs in some form of ports,
   * often named 'PA', 'PB', 'PC'..., or 'P0', 'P1', 'P2'..., or similar. Each of
   * these ports is then assigned a number of pins, often 8, 16, or 32. A hardware
   * pin can thus be described by its port/pin tuple. To access a pin, the
   * @p GPIO_PIN(port, pin) macro should be used. For example: If your platform has
   * a pin PB22, it will be port=1 and pin=22. The @p GPIO_PIN macro should be
   * overridden by a MCU, to allow for efficient encoding of the the port/pin tuple.
   * For example, on many platforms it is possible to `OR` the pin number with the
   * corresponding ports base register address. This allows for efficient decoding
   * of pin number and base address without the need of any address lookup.
   *
   * In case the driver does not define it, the below macro definition is used to
   * simply map the port/pin tuple to the pin value. In that case, predefined GPIO
   * definitions in `RIOT/boards/ * /include/periph_conf.h` will define the selected
   * GPIO pin.
   *
   * @{
   * @file
   * @brief       Low-level GPIO peripheral driver interface definitions
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   */
  
  #ifndef PERIPH_GPIO_H
  #define PERIPH_GPIO_H
  
  #include <limits.h>
  
  #include "periph_cpu.h"
  #include "periph_conf.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #ifndef HAVE_GPIO_T
  /**
   * @brief   GPIO type identifier
   */
  typedef unsigned int gpio_t;
  #endif
  
  #ifndef GPIO_PIN
  /**
   * @brief   Convert (port, pin) tuple to @c gpio_t value
   */
  /* Default GPIO macro maps port-pin tuples to the pin value */
  #define GPIO_PIN(x,y)       ((gpio_t)((x & 0) | y))
  #endif
  
  #ifndef GPIO_UNDEF
  /**
   * @brief   GPIO pin not defined
   */
  #define GPIO_UNDEF          ((gpio_t)(UINT_MAX))
  #endif
  
  /**
   * @brief   Available pin modes
   *
   * Generally, a pin can be configured to be input or output. In output mode, a
   * pin can further be put into push-pull or open drain configuration. Though
   * this is supported by most platforms, this is not always the case, so driver
   * implementations may return an error code if a mode is not supported.
   */
  #ifndef HAVE_GPIO_MODE_T
  typedef enum {
      GPIO_IN ,               /**< configure as input without pull resistor */
      GPIO_IN_PD,             /**< configure as input with pull-down resistor */
      GPIO_IN_PU,             /**< configure as input with pull-up resistor */
      GPIO_OUT,               /**< configure as output in push-pull mode */
      GPIO_OD,                /**< configure as output in open-drain mode without
                               *   pull resistor */
      GPIO_OD_PU              /**< configure as output in open-drain mode with
                               *   pull resistor enabled */
  } gpio_mode_t;
  #endif
  
  /**
   * @brief   Definition of possible active flanks for external interrupt mode
   */
  #ifndef HAVE_GPIO_FLANK_T
  typedef enum {
      GPIO_FALLING = 0,       /**< emit interrupt on falling flank */
      GPIO_RISING = 1,        /**< emit interrupt on rising flank */
      GPIO_BOTH = 2           /**< emit interrupt on both flanks */
  } gpio_flank_t;
  #endif
  
  /**
   * @brief   Signature of event callback functions triggered from interrupts
   *
   * @param[in] arg       optional context for the callback
   */
  typedef void (*gpio_cb_t)(void *arg);
  
  /**
   * @brief   Default interrupt context for GPIO pins
   */
  #ifndef HAVE_GPIO_ISR_CTX_T
  typedef struct {
      gpio_cb_t cb;           /**< interrupt callback */
      void *arg;              /**< optional argument */
  } gpio_isr_ctx_t;
  #endif
  
  /**
   * @brief   Initialize the given pin as general purpose input or output
   *
   * When configured as output, the pin state after initialization is undefined.
   * The output pin's state **should** be untouched during the initialization.
   * This behavior can however **not be guaranteed** by every platform.
   *
   * @param[in] pin       pin to initialize
   * @param[in] mode      mode of the pin, see @c gpio_mode_t
   *
   * @return              0 on success
   * @return              -1 on error
   */
  int gpio_init(gpio_t pin, gpio_mode_t mode);
  
  /**
   * @brief   Initialize a GPIO pin for external interrupt usage
   *
   * The registered callback function will be called in interrupt context every
   * time the defined flank(s) are detected.
   *
   * The interrupt is activated automatically after the initialization.
   *
   * @param[in] pin       pin to initialize
   * @param[in] mode      mode of the pin, see @c gpio_mode_t
   * @param[in] flank     define the active flank(s)
   * @param[in] cb        callback that is called from interrupt context
   * @param[in] arg       optional argument passed to the callback
   *
   * @return              0 on success
   * @return              -1 on error
   */
  int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
                    gpio_cb_t cb, void *arg);
  
  /**
   * @brief   Enable pin interrupt if configured as interrupt source
   *
   * @param[in] pin       the pin to enable the interrupt for
   */
  void gpio_irq_enable(gpio_t pin);
  
  /**
   * @brief   Disable the pin interrupt if configured as interrupt source
   *
   * @param[in] pin       the pin to disable the interrupt for
   */
  void gpio_irq_disable(gpio_t pin);
  
  /**
   * @brief   Get the current value of the given pin
   *
   * @param[in] pin       the pin to read
   *
   * @return              0 when pin is LOW
   * @return              >0 for HIGH
   */
  int gpio_read(gpio_t pin);
  
  /**
   * @brief   Set the given pin to HIGH
   *
   * @param[in] pin       the pin to set
   */
  void gpio_set(gpio_t pin);
  
  /**
   * @brief   Set the given pin to LOW
   *
   * @param[in] pin       the pin to clear
   */
  void gpio_clear(gpio_t pin);
  
  /**
   * @brief   Toggle the value of the given pin
   *
   * @param[in] pin       the pin to toggle
   */
  void gpio_toggle(gpio_t pin);
  
  /**
   * @brief   Set the given pin to the given value
   *
   * @param[in] pin       the pin to set
   * @param[in] value     value to set the pin to, 0 for LOW, HIGH otherwise
   */
  void gpio_write(gpio_t pin, int value);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* PERIPH_GPIO_H */
  /** @} */