YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
gpio_rp2040.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// GPIO driver for RP2040. Supports open-source and
15// open-drain modes as well as interrupts.
16//
17#ifndef _GPIO_RP2040_H_
18#define _GPIO_RP2040_H_
19
20#include "gpio_interface.h"
21#include <cassert>
22
23namespace GPIO {
24// additional gpio modes
25const gpio_mode_t DRIVE_2mA = 0x0000;
26const gpio_mode_t DRIVE_4mA = 0x0400;
27const gpio_mode_t DRIVE_8mA = 0x0800;
28const gpio_mode_t DRIVE_12mA = 0x0c00;
29const gpio_mode_t INPUT_INVERT = 0x1000;
30}
31
32extern "C" {
33void IO_IRQ_BANK0_Handler(void);
34}
35
37public:
38 // CTOR / DTOR
39 explicit gpio_rp2040(gpio_pin_t gpio = 0xffff);
40 ~gpio_rp2040() override = default;
41
42 // No copy, assignment is value passing
43 gpio_rp2040 (const gpio_rp2040&) = delete;
44 gpio_rp2040& operator= (const gpio_rp2040 & lhs) {
45 this->gpioWrite(lhs.operator bool());
46 return *this;
47 };
48
49 // Get and Set the GPIO number during runtime
50 void setGpio(gpio_pin_t gpio) override;
51 gpio_pin_t getGpio() const override;
52
53 // Generic GPIO methods
54 void gpioMode (gpio_mode_t mode) override;
55 bool gpioRead () const override;
56 void gpioWrite (bool value) override;
57 void gpioToggle() override;
58
59 // Interrupt handling
60 void gpioAttachIrq (gpio_mode_t mode,
61 function<void()> handler) override;
62 void gpioDetachIrq () override;
63 void gpioEnableIrq () override;
64 void gpioDisableIrq() override;
65
66 // RP2040 specific methods
67 void setSEL (uint8_t sel);
68 void setMode(gpio_mode_t mode);
69
70 using gpio_interface::operator =;
71 using gpio_interface::operator bool;
72
73 // IRQ handlers are our best friends
74 friend void IO_IRQ_BANK0_Handler(void);
75
76private:
77 gpio_pin_t _gpio;
78 bool _open_source {false};
79 bool _open_drain {false};
80 uint32_t _mask;
81
82 static function<void()> _intHandler[30];
83 static uint8_t _irqConfig [30];
84};
85
86#endif // _GPIO_RP2040_H_