YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
gpio_interface.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// This file defines a generic and abstract C++
15// interface for all kinds of GPIO devices.
16// A single pin is identified by a uint16_t, but
17// macros are provided for PORT/PIN syntax, if
18// these concepts are supported by the platform.
19//
20// Error handling is intentionally left out: A
21// concrete GPIO class on smaller microcontrollers
22// might not perform any error handling at all
23// or only use assert() in error cases, while an
24// advanced platform like a Linux-based board
25// might throw exceptions...
26
27#ifndef _GPIO_INTERFACE_H_
28#define _GPIO_INTERFACE_H_
29
30#include <stdint.h>
31#include <functional>
32using std::function;
33
34#ifndef HIGH
35#define HIGH true
36#endif
37
38#ifndef LOW
39#define LOW false
40#endif
41
42// For Arduino compatibility
43#undef INPUT
44#undef OUTPUT
45#undef OUTPUT_OPEN_DRAIN
46#undef RISING
47#undef FALLING
48
49// PORT and PIN macros for systems having
50// these concepts. Value is 0...255 each.
51typedef uint16_t gpio_pin_t;
52#define PORT_PIN(port, pin) ((port) << 8 | (pin))
53#define PORT(gpio) ((gpio) >> 8)
54#define PIN(gpio) ((gpio) & 0xff)
55
56typedef uint16_t gpio_mode_t;
57typedef uint16_t gpio_irq_t;
58
59namespace GPIO {
60
61// basic gpio modes
62const gpio_mode_t NONE = 0x0000;
63const gpio_mode_t INPUT = 0x0001;
64const gpio_mode_t OUTPUT = 0x0002;
65const gpio_mode_t OUTPUT_OPEN_DRAIN = 0x0004;
66const gpio_mode_t OUTPUT_OPEN_SOURCE = 0x0008;
67
68// additional gpio modes
69const gpio_mode_t PULLUP = 0x0010;
70const gpio_mode_t PULLDOWN = 0x0020;
71const gpio_mode_t SLOW = 0x0040;
72const gpio_mode_t FAST = 0x0080;
73const gpio_mode_t INIT_HIGH = 0x0100;
74const gpio_mode_t INIT_LOW = 0x0200;
75
76// the gpio irq modes
77const gpio_irq_t RISING = 0x0001;
78const gpio_irq_t FALLING = 0x0002;
79const gpio_irq_t LEVEL_HIGH = 0x0004;
80const gpio_irq_t LEVEL_LOW = 0x0008;
81}
82
84public:
85 // The CTOR of a concrete GPIO class has to set a
86 // specific GPIO pin with a gpio_pin_t argument.
87
88 // The following methods can be used to change/get the
89 // GPIO pin in an existing GPIO object.
90 virtual void setGpio(gpio_pin_t gpio) = 0;
91 virtual gpio_pin_t getGpio() const = 0;
92
93 // Basic GPIO handling
94 virtual void gpioMode (gpio_mode_t mode) = 0;
95 virtual bool gpioRead () const = 0;
96 virtual void gpioWrite (bool value) = 0;
97 virtual void gpioToggle() = 0;
98
99 // Attach a interrupt handler to the GPIO pin 'gpio'.
100 // The irq_mode specifies the signal edges to listen to.
101 // The handler will be called when the event occurs.
102 virtual void gpioAttachIrq (gpio_irq_t irq_mode,
103 function<void()> handler) = 0;
104 // Remove the interrupt handler from the GPIO pin
105 virtual void gpioDetachIrq () = 0;
106 // Enable the interrupt on the GPIO pin
107 virtual void gpioEnableIrq () = 0;
108 // Disable the interrupt on the GPIO pin
109 virtual void gpioDisableIrq() = 0;
110
111 // Boolean operators for easy usage of GPIOs
112 inline void operator = (bool b) {
113 gpioWrite(b);
114 }
115 inline operator bool () const {
116 return gpioRead();
117 }
118
119protected:
120 virtual ~gpio_interface() = default;
121};
122
123#endif // _GPIO_INTERFACE_H_
124