Loading...
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /* gpio.h - public GPIO driver APIs */ /* * Copyright (c) 2015 Intel Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __GPIO_H__ #define __GPIO_H__ /** * @brief GPIO Interface * @defgroup gpio_interface GPIO Interface * @ingroup io_interfaces * @{ */ #ifdef __cplusplus extern "C" { #endif #define GPIO_ACCESS_BY_PIN 0 #define GPIO_ACCESS_BY_PORT 1 #include <stdint.h> #include <stddef.h> #include <device.h> /* TODO Define flag/config bits */ #define GPIO_DIR_IN (0 << 0) #define GPIO_DIR_OUT (1 << 0) #define GPIO_DIR_MASK 0x1 #define GPIO_INT (1 << 1) #define GPIO_INT_ACTIVE_LOW (0 << 2) #define GPIO_INT_ACTIVE_HIGH (1 << 2) #define GPIO_INT_CLOCK_SYNC (1 << 3) #define GPIO_INT_DEBOUNCE (1 << 4) #define GPIO_INT_LEVEL (0 << 5) #define GPIO_INT_EDGE (1 << 5) #define GPIO_INT_DOUBLE_EDGE (1 << 6) /* Polarity of the GPIO (1 bit) */ #define GPIO_POL_POS 7 #define GPIO_POL_NORMAL (0 << GPIO_POL_POS) #define GPIO_POL_INV (1 << GPIO_POL_POS) #define GPIO_POL_MASK (1 << GPIO_POL_POS) /* Pull-up/pull-down for GPIO (2 bits) */ #define GPIO_PUD_POS 8 #define GPIO_PUD_NORMAL (0 << GPIO_PUD_POS) #define GPIO_PUD_PULL_UP (1 << GPIO_PUD_POS) #define GPIO_PUD_PULL_DOWN (2 << GPIO_PUD_POS) #define GPIO_PUD_MASK (3 << GPIO_PUD_POS) /* Pin enable/disable * * Individual pins can be enabled or disabled * if the controller supports this operation. */ #define GPIO_PIN_ENABLE (1 << 10) #define GPIO_PIN_DISABLE (1 << 11) /* application callback function signature*/ typedef void (*gpio_callback_t)(struct device *port, uint32_t pin); /* driver API definition */ typedef int (*gpio_config_t)(struct device *port, int access_op, uint32_t pin, int flags); typedef int (*gpio_write_t)(struct device *port, int access_op, uint32_t pin, uint32_t value); typedef int (*gpio_read_t)(struct device *port, int access_op, uint32_t pin, uint32_t *value); typedef int (*gpio_set_callback_t)(struct device *port, gpio_callback_t callback); typedef int (*gpio_enable_callback_t)(struct device *port, int access_op, uint32_t pin); typedef int (*gpio_disable_callback_t)(struct device *port, int access_op, uint32_t pin); typedef int (*gpio_suspend_port_t)(struct device *port); typedef int (*gpio_resume_port_t)(struct device *port); struct gpio_driver_api { gpio_config_t config; gpio_write_t write; gpio_read_t read; gpio_set_callback_t set_callback; gpio_enable_callback_t enable_callback; gpio_disable_callback_t disable_callback; gpio_suspend_port_t suspend; gpio_resume_port_t resume; }; /** * @brief Configure a single pin * @param port Pointer to device structure for driver instance. * @param pin Pin number operate on. * @param flags Flags for pin configuration. IN/OUT, interrupt ... */ static inline int gpio_pin_configure(struct device *port, uint8_t pin, int flags) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->config(port, GPIO_ACCESS_BY_PIN, pin, flags); } /** * @brief Write data value of a single pin. * @param port Pointer to device structure for driver instance. * @param pin Pin number operate on. * @param value Value to set the pin to. */ static inline int gpio_pin_write(struct device *port, uint32_t pin, uint32_t value) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->write(port, GPIO_ACCESS_BY_PIN, pin, value); } /** * @brief Read data value of a single pin. * @param port Pointer to device structure for driver instance. * @param pin Pin number operate on. * @param value Integer pointer to receive the output of the read. */ static inline int gpio_pin_read(struct device *port, uint32_t pin, uint32_t *value) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->read(port, GPIO_ACCESS_BY_PIN, pin, value); } /** * @brief Set the application callback.. * @param port Pointer to device structure for driver instance. * @param callback Application callback function. */ static inline int gpio_set_callback(struct device *port, gpio_callback_t callback) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->set_callback(port, callback); } /** * @brief Enable pin callback. * @param port Pointer to device structure for driver instance. * @param pin Pin number operate on. */ static inline int gpio_pin_enable_callback(struct device *port, uint32_t pin) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->enable_callback(port, GPIO_ACCESS_BY_PIN, pin); } /** * @brief Disable pin callback. * @param port Pointer to device structure for driver instance. * @param pin Pin number operate on. */ static inline int gpio_pin_disable_callback(struct device *port, uint32_t pin) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->disable_callback(port, GPIO_ACCESS_BY_PIN, pin); } /** * @brief Configure all pins in the port. * @param port Pointer to device structure for driver instance. * @param flags Flags for port configuration. IN/OUT, interrupt ... */ static inline int gpio_port_configure(struct device *port, int flags) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->config(port, GPIO_ACCESS_BY_PORT, 0, flags); } /** * @brief Write data value to the port. * @param port Pointer to device structure for driver instance. * @param value Value to set the pin to. */ static inline int gpio_port_write(struct device *port, uint32_t value) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->write(port, GPIO_ACCESS_BY_PORT, 0, value); } /** * @brief Read data value of the port. * @param port Pointer to device structure for driver instance. * @param value Integer pointer to receive the output of the read. */ static inline int gpio_port_read(struct device *port, uint32_t *value) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->read(port, GPIO_ACCESS_BY_PORT, 0, value); } /** * @brief Enable port callback. * @param port Pointer to device structure for driver instance. */ static inline int gpio_port_enable_callback(struct device *port) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->enable_callback(port, GPIO_ACCESS_BY_PORT, 0); } /** * @brief Disable port callback. * @param port Pointer to device structure for driver instance. */ static inline int gpio_port_disable_callback(struct device *port) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->disable_callback(port, GPIO_ACCESS_BY_PORT, 0); } /** * @brief Save the state of the device and go to low power state * @param port Pointer to device structure for driver instance. */ static inline int gpio_suspend(struct device *port) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->suspend(port); } /** * @brief Restore state stored during suspend and resume operation. * @param port Pointer to device structure for driver instance. */ static inline int gpio_resume(struct device *port) { struct gpio_driver_api *api; api = (struct gpio_driver_api *) port->driver_api; return api->resume(port); } #ifdef __cplusplus } #endif /** * @} */ #endif /* __GPIO_H__ */ |