1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef GPIO_H_
18#define GPIO_H_
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#include <stdint.h>
25#include <stdbool.h>
26
27enum GpioMode
28{
29    GPIO_MODE_IN = 0,
30    GPIO_MODE_OUT,
31    GPIO_MODE_ALTERNATE,
32    GPIO_MODE_ANALOG,
33};
34
35enum GpioOpenDrainMode
36{
37    GPIO_OUT_PUSH_PULL = 0,
38    GPIO_OUT_OPEN_DRAIN,
39};
40
41enum GpioPullMode
42{
43    GPIO_PULL_NONE = 0,
44    GPIO_PULL_UP,
45    GPIO_PULL_DOWN,
46};
47
48#define GPIO_SPEED_BEST_POWER      -1 //all non-negative values are platform specific, YMMV
49#define GPIO_SPEED_BEST_SPEED      -2
50#define GPIO_SPEED_DEFAULT         -3
51#define GPIO_SPEED_1MHZ_PLUS       -4
52#define GPIO_SPEED_3MHZ_PLUS       -5
53#define GPIO_SPEED_5MHZ_PLUS       -6
54#define GPIO_SPEED_10MHZ_PLUS      -7
55#define GPIO_SPEED_15MHZ_PLUS      -8
56#define GPIO_SPEED_20MHZ_PLUS      -9
57#define GPIO_SPEED_30MHZ_PLUS      -10
58#define GPIO_SPEED_50MHZ_PLUS      -11
59#define GPIO_SPEED_100MHZ_PLUS     -12
60#define GPIO_SPEED_150MHZ_PLUS     -13
61#define GPIO_SPEED_200MHZ_PLUS     -14
62
63
64struct Gpio;
65
66/* Requests a GPIO and allocates the gpio handle/struct/etc */
67struct Gpio* gpioRequest(uint32_t gpioNum);
68void gpioRelease(struct Gpio* __restrict gpio);
69
70/* Configures the direction and pull type of a GPIO */
71void gpioConfigInput(const struct Gpio* __restrict gpio, int32_t gpioSpeed, enum GpioPullMode pull);
72void gpioConfigOutput(const struct Gpio* __restrict gpio, int32_t gpioSpeed, enum GpioPullMode pull, enum GpioOpenDrainMode odrMode, bool value);
73void gpioConfigAlt(const struct Gpio* __restrict gpio, int32_t gpioSpeed, enum GpioPullMode pull, enum GpioOpenDrainMode odrMode, uint32_t altFunc);
74void gpioConfigAnalog(const struct Gpio* __restrict gpio);
75
76/* Sets and gets a value for a specific GPIO pin */
77void gpioSet(const struct Gpio* __restrict gpio, bool value);
78bool gpioGet(const struct Gpio* __restrict gpio);
79
80#ifdef __cplusplus
81}
82#endif
83
84#endif
85