My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Gpio.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "Clock.h"
  24. #include "../../../inc/MarlinConfigPre.h"
  25. #include <stdint.h>
  26. typedef int16_t pin_type;
  27. struct GpioEvent {
  28. enum Type {
  29. NOP,
  30. FALL,
  31. RISE,
  32. SET_VALUE,
  33. SETM,
  34. SETD
  35. };
  36. uint64_t timestamp;
  37. pin_type pin_id;
  38. GpioEvent::Type event;
  39. GpioEvent(uint64_t timestamp, pin_type pin_id, GpioEvent::Type event){
  40. this->timestamp = timestamp;
  41. this->pin_id = pin_id;
  42. this->event = event;
  43. }
  44. };
  45. class IOLogger {
  46. public:
  47. virtual ~IOLogger(){};
  48. virtual void log(GpioEvent ev) = 0;
  49. };
  50. class Peripheral {
  51. public:
  52. virtual ~Peripheral(){};
  53. virtual void interrupt(GpioEvent ev) = 0;
  54. virtual void update() = 0;
  55. };
  56. struct pin_data {
  57. uint8_t dir;
  58. uint8_t mode;
  59. uint16_t value;
  60. Peripheral* cb;
  61. };
  62. class Gpio {
  63. public:
  64. static const pin_type pin_count = 255;
  65. static pin_data pin_map[pin_count+1];
  66. static bool valid_pin(pin_type pin) {
  67. return pin >= 0 && pin <= pin_count;
  68. }
  69. static void set(pin_type pin) {
  70. set(pin, 1);
  71. }
  72. static void set(pin_type pin, uint16_t value) {
  73. if (!valid_pin(pin)) return;
  74. 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;
  75. pin_map[pin].value = value;
  76. GpioEvent evt(Clock::nanos(), pin, evt_type);
  77. if (pin_map[pin].cb != nullptr) {
  78. pin_map[pin].cb->interrupt(evt);
  79. }
  80. if (Gpio::logger != nullptr) Gpio::logger->log(evt);
  81. }
  82. static uint16_t get(pin_type pin) {
  83. if (!valid_pin(pin)) return 0;
  84. return pin_map[pin].value;
  85. }
  86. static void clear(pin_type pin) {
  87. set(pin, 0);
  88. }
  89. static void setMode(pin_type pin, uint8_t value) {
  90. if (!valid_pin(pin)) return;
  91. pin_map[pin].mode = value;
  92. GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETM);
  93. if (pin_map[pin].cb != nullptr) pin_map[pin].cb->interrupt(evt);
  94. if (Gpio::logger != nullptr) Gpio::logger->log(evt);
  95. }
  96. static uint8_t getMode(pin_type pin) {
  97. if (!valid_pin(pin)) return 0;
  98. return pin_map[pin].mode;
  99. }
  100. static void setDir(pin_type pin, uint8_t value) {
  101. if (!valid_pin(pin)) return;
  102. pin_map[pin].dir = value;
  103. GpioEvent evt(Clock::nanos(), pin, GpioEvent::Type::SETD);
  104. if (pin_map[pin].cb != nullptr) pin_map[pin].cb->interrupt(evt);
  105. if (Gpio::logger != nullptr) Gpio::logger->log(evt);
  106. }
  107. static uint8_t getDir(pin_type pin) {
  108. if (!valid_pin(pin)) return 0;
  109. return pin_map[pin].dir;
  110. }
  111. static void attachPeripheral(pin_type pin, Peripheral* per) {
  112. if (!valid_pin(pin)) return;
  113. pin_map[pin].cb = per;
  114. }
  115. static void attachLogger(IOLogger* logger) {
  116. Gpio::logger = logger;
  117. }
  118. private:
  119. static IOLogger* logger;
  120. };