Prusa MINI Firmware overview
Gpio.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4  *
5  * Based on Sprinter and grbl.
6  * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 #include "Clock.h"
25 #include "../../../inc/MarlinConfigPre.h"
26 #include <stdint.h>
27 
28 typedef int16_t pin_type;
29 
30 struct GpioEvent {
31  enum Type {
32  NOP,
38  };
39  uint64_t timestamp;
42 
44  this->timestamp = timestamp;
45  this->pin_id = pin_id;
46  this->event = event;
47  }
48 };
49 
50 class IOLogger {
51 public:
52  virtual ~IOLogger(){};
53  virtual void log(GpioEvent ev) = 0;
54 };
55 
56 class Peripheral {
57 public:
58  virtual ~Peripheral(){};
59  virtual void interrupt(GpioEvent ev) = 0;
60  virtual void update() = 0;
61 };
62 
63 struct pin_data {
66  uint16_t value;
68 };
69 
70 class Gpio {
71 public:
72 
73  static const pin_type pin_count = 255;
75 
76  static bool valid_pin(pin_type pin) {
77  return pin >= 0 && pin <= pin_count;
78  }
79 
80  static void set(pin_type pin) {
81  set(pin, 1);
82  }
83 
84  static void set(pin_type pin, uint16_t value) {
85  if (!valid_pin(pin)) return;
86  GpioEvent::Type evt_type = value > 1 ? GpioEvent::SET_VALUE : value > pin_map[pin].value ? GpioEvent::RISE : value < pin_map[pin].value ? GpioEvent::FALL : GpioEvent::NOP;
87  pin_map[pin].value = value;
88  GpioEvent evt(Clock::nanos(), pin, evt_type);
89  if (pin_map[pin].cb != nullptr) {
90  pin_map[pin].cb->interrupt(evt);
91  }
92  if (Gpio::logger != nullptr) Gpio::logger->log(evt);
93  }
94 
95  static uint16_t get(pin_type pin) {
96  if (!valid_pin(pin)) return 0;
97  return pin_map[pin].value;
98  }
99 
100  static void clear(pin_type pin) {
101  set(pin, 0);
102  }
103 
104  static void setMode(pin_type pin, uint8_t value) {
105  if (!valid_pin(pin)) return;
106  pin_map[pin].mode = value;
107  GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETM);
108  if (pin_map[pin].cb != nullptr) pin_map[pin].cb->interrupt(evt);
109  if (Gpio::logger != nullptr) Gpio::logger->log(evt);
110  }
111 
112  static uint8_t getMode(pin_type pin) {
113  if (!valid_pin(pin)) return 0;
114  return pin_map[pin].mode;
115  }
116 
117  static void setDir(pin_type pin, uint8_t value) {
118  if (!valid_pin(pin)) return;
119  pin_map[pin].dir = value;
120  GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETD);
121  if (pin_map[pin].cb != nullptr) pin_map[pin].cb->interrupt(evt);
122  if (Gpio::logger != nullptr) Gpio::logger->log(evt);
123  }
124 
125  static uint8_t getDir(pin_type pin) {
126  if (!valid_pin(pin)) return 0;
127  return pin_map[pin].dir;
128  }
129 
130  static void attachPeripheral(pin_type pin, Peripheral* per) {
131  if (!valid_pin(pin)) return;
132  pin_map[pin].cb = per;
133  }
134 
135  static void attachLogger(IOLogger* logger) {
136  Gpio::logger = logger;
137  }
138 
139 private:
140  static IOLogger* logger;
141 };
Gpio::get
static uint16_t get(pin_type pin)
Definition: Gpio.h:95
Gpio::valid_pin
static bool valid_pin(pin_type pin)
Definition: Gpio.h:76
GpioEvent::pin_id
pin_type pin_id
Definition: Gpio.h:40
Gpio::set
static void set(pin_type pin)
Definition: Gpio.h:80
pin_type
int16_t pin_type
Definition: Gpio.h:28
GpioEvent::RISE
Definition: Gpio.h:34
Peripheral
Definition: Gpio.h:56
pin_data
Definition: Gpio.h:63
GpioEvent::Type
Type
Definition: Gpio.h:31
Gpio::attachLogger
static void attachLogger(IOLogger *logger)
Definition: Gpio.h:135
Peripheral::~Peripheral
virtual ~Peripheral()
Definition: Gpio.h:58
pin_data::cb
Peripheral * cb
Definition: Gpio.h:67
Gpio::clear
static void clear(pin_type pin)
Definition: Gpio.h:100
pin_data::dir
uint8_t dir
Definition: Gpio.h:64
pin_data::mode
uint8_t mode
Definition: Gpio.h:65
GpioEvent::FALL
Definition: Gpio.h:33
Gpio::pin_count
static const pin_type pin_count
Definition: Gpio.h:73
GpioEvent::NOP
Definition: Gpio.h:32
Peripheral::interrupt
virtual void interrupt(GpioEvent ev)=0
Gpio
Definition: Gpio.h:70
IOLogger::log
virtual void log(GpioEvent ev)=0
GpioEvent::GpioEvent
GpioEvent(uint64_t timestamp, pin_type pin_id, GpioEvent::Type event)
Definition: Gpio.h:43
GpioEvent::event
GpioEvent::Type event
Definition: Gpio.h:41
GpioEvent::SET_VALUE
Definition: Gpio.h:35
Gpio::getMode
static uint8_t getMode(pin_type pin)
Definition: Gpio.h:112
Gpio::pin_map
static pin_data pin_map[pin_count+1]
Definition: Gpio.h:74
Gpio::setMode
static void setMode(pin_type pin, uint8_t value)
Definition: Gpio.h:104
Gpio::attachPeripheral
static void attachPeripheral(pin_type pin, Peripheral *per)
Definition: Gpio.h:130
GpioEvent::SETM
Definition: Gpio.h:36
GpioEvent::timestamp
uint64_t timestamp
Definition: Gpio.h:39
Gpio::set
static void set(pin_type pin, uint16_t value)
Definition: Gpio.h:84
GpioEvent::SETD
Definition: Gpio.h:37
Clock::nanos
static uint64_t nanos()
Definition: Clock.h:47
uint8_t
const uint8_t[]
Definition: 404_html.c:3
Gpio::getDir
static uint8_t getDir(pin_type pin)
Definition: Gpio.h:125
Gpio::setDir
static void setDir(pin_type pin, uint8_t value)
Definition: Gpio.h:117
IOLogger
Definition: Gpio.h:50
Clock.h
IOLogger::~IOLogger
virtual ~IOLogger()
Definition: Gpio.h:52
GpioEvent
Definition: Gpio.h:30
Peripheral::update
virtual void update()=0
pin_data::value
uint16_t value
Definition: Gpio.h:66