diff options
Diffstat (limited to 'Marlin/src/HAL/LINUX/include')
-rw-r--r-- | Marlin/src/HAL/LINUX/include/Arduino.h | 95 | ||||
-rw-r--r-- | Marlin/src/HAL/LINUX/include/pinmapping.cpp | 69 | ||||
-rw-r--r-- | Marlin/src/HAL/LINUX/include/pinmapping.h | 59 | ||||
-rw-r--r-- | Marlin/src/HAL/LINUX/include/serial.h | 118 |
4 files changed, 341 insertions, 0 deletions
diff --git a/Marlin/src/HAL/LINUX/include/Arduino.h b/Marlin/src/HAL/LINUX/include/Arduino.h new file mode 100644 index 0000000..d4086e2 --- /dev/null +++ b/Marlin/src/HAL/LINUX/include/Arduino.h @@ -0,0 +1,95 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ +#pragma once + +#include <stddef.h> +#include <stdint.h> +#include <math.h> +#include <cstring> + +#include <pinmapping.h> + +#define HIGH 0x01 +#define LOW 0x00 + +#define INPUT 0x00 +#define OUTPUT 0x01 +#define INPUT_PULLUP 0x02 +#define INPUT_PULLDOWN 0x03 + +#define LSBFIRST 0 +#define MSBFIRST 1 + +#define CHANGE 0x02 +#define FALLING 0x03 +#define RISING 0x04 + +typedef uint8_t byte; +#define PROGMEM +#define PSTR(v) (v) +#define PGM_P const char * + +// Used for libraries, preprocessor, and constants +#define abs(x) ((x)>0?(x):-(x)) + +#ifndef isnan + #define isnan std::isnan +#endif +#ifndef isinf + #define isinf std::isinf +#endif + +#define sq(v) ((v) * (v)) +#define square(v) sq(v) +#define constrain(value, arg_min, arg_max) ((value) < (arg_min) ? (arg_min) :((value) > (arg_max) ? (arg_max) : (value))) + +//Interrupts +void cli(); // Disable +void sei(); // Enable +void attachInterrupt(uint32_t pin, void (*callback)(), uint32_t mode); +void detachInterrupt(uint32_t pin); + +extern "C" { + void GpioEnableInt(uint32_t port, uint32_t pin, uint32_t mode); + void GpioDisableInt(uint32_t port, uint32_t pin); +} + +// Time functions +extern "C" void delay(const int milis); +void _delay_ms(const int delay); +void delayMicroseconds(unsigned long); +uint32_t millis(); + +//IO functions +void pinMode(const pin_t, const uint8_t); +void digitalWrite(pin_t, uint8_t); +bool digitalRead(pin_t); +void analogWrite(pin_t, int); +uint16_t analogRead(pin_t); + +int32_t random(int32_t); +int32_t random(int32_t, int32_t); +void randomSeed(uint32_t); + +char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s); + +int map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max); diff --git a/Marlin/src/HAL/LINUX/include/pinmapping.cpp b/Marlin/src/HAL/LINUX/include/pinmapping.cpp new file mode 100644 index 0000000..870ab3a --- /dev/null +++ b/Marlin/src/HAL/LINUX/include/pinmapping.cpp @@ -0,0 +1,69 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ +#ifdef __PLAT_LINUX__ + +#include <pinmapping.h> + +#include "../../../gcode/parser.h" + +uint8_t analog_offset = NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS; + +// Get the digital pin for an analog index +pin_t analogInputToDigitalPin(const int8_t p) { + return (WITHIN(p, 0, NUM_ANALOG_INPUTS) ? analog_offset + p : P_NC); +} + +// Return the index of a pin number +int16_t GET_PIN_MAP_INDEX(const pin_t pin) { + return pin; +} + +// Test whether the pin is valid +bool VALID_PIN(const pin_t p) { + return WITHIN(p, 0, NUM_DIGITAL_PINS); +} + +// Get the analog index for a digital pin +int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p) { + return (WITHIN(p, analog_offset, NUM_DIGITAL_PINS) ? p - analog_offset : P_NC); +} + +// Test whether the pin is PWM +bool PWM_PIN(const pin_t p) { + return false; +} + +// Test whether the pin is interruptable +bool INTERRUPT_PIN(const pin_t p) { + return false; +} + +// Get the pin number at the given index +pin_t GET_PIN_MAP_PIN(const int16_t ind) { + return ind; +} + +int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) { + return parser.intval(code, dval); +} + +#endif // __PLAT_LINUX__ diff --git a/Marlin/src/HAL/LINUX/include/pinmapping.h b/Marlin/src/HAL/LINUX/include/pinmapping.h new file mode 100644 index 0000000..98f4b81 --- /dev/null +++ b/Marlin/src/HAL/LINUX/include/pinmapping.h @@ -0,0 +1,59 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ +#pragma once + +#include "../../../inc/MarlinConfigPre.h" + +#include <stdint.h> +#include "../hardware/Gpio.h" + +typedef pin_type pin_t; + +#define P_NC -1 +constexpr uint16_t NUM_DIGITAL_PINS = Gpio::pin_count; +constexpr uint8_t NUM_ANALOG_INPUTS = 16; + +#define HAL_SENSITIVE_PINS + +// Get the digital pin for an analog index +pin_t analogInputToDigitalPin(const int8_t p); + +// Return the index of a pin number +int16_t GET_PIN_MAP_INDEX(const pin_t pin); + +// Test whether the pin is valid +bool VALID_PIN(const pin_t p); + +// Get the analog index for a digital pin +int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t p); + +// Test whether the pin is PWM +bool PWM_PIN(const pin_t p); + +// Test whether the pin is interruptable +bool INTERRUPT_PIN(const pin_t p); + +// Get the pin number at the given index +pin_t GET_PIN_MAP_PIN(const int16_t ind); + +// Parse a G-code word into a pin index +int16_t PARSED_PIN_INDEX(const char code, const int16_t dval); diff --git a/Marlin/src/HAL/LINUX/include/serial.h b/Marlin/src/HAL/LINUX/include/serial.h new file mode 100644 index 0000000..2585be2 --- /dev/null +++ b/Marlin/src/HAL/LINUX/include/serial.h @@ -0,0 +1,118 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ +#pragma once + +#include "../../../inc/MarlinConfigPre.h" +#if ENABLED(EMERGENCY_PARSER) + #include "../../../feature/e_parser.h" +#endif +#include "../../../core/serial_hook.h" + +#include <stdarg.h> +#include <stdio.h> + +/** + * Generic RingBuffer + * T type of the buffer array + * S size of the buffer (must be power of 2) + */ +template <typename T, uint32_t S> class RingBuffer { +public: + RingBuffer() { index_read = index_write = 0; } + uint32_t available() volatile { return index_write - index_read; } + uint32_t free() volatile { return buffer_size - available(); } + bool empty() volatile { return index_read == index_write; } + bool full() volatile { return available() == buffer_size; } + void clear() volatile { index_read = index_write = 0; } + + bool peek(T *value) volatile { + if (value == 0 || available() == 0) + return false; + *value = buffer[mask(index_read)]; + return true; + } + + int read() volatile { + if (empty()) return -1; + return buffer[mask(index_read++)]; + } + + bool write(T value) volatile { + if (full()) return false; + buffer[mask(index_write++)] = value; + return true; + } + +private: + uint32_t mask(uint32_t val) volatile { + return buffer_mask & val; + } + + static const uint32_t buffer_size = S; + static const uint32_t buffer_mask = buffer_size - 1; + volatile T buffer[buffer_size]; + volatile uint32_t index_write; + volatile uint32_t index_read; +}; + +struct HalSerial { + HalSerial() { host_connected = true; } + + void begin(int32_t) {} + void end() {} + + int peek() { + uint8_t value; + return receive_buffer.peek(&value) ? value : -1; + } + + int read() { return receive_buffer.read(); } + + size_t write(char c) { + if (!host_connected) return 0; + while (!transmit_buffer.free()); + return transmit_buffer.write(c); + } + + bool connected() { return host_connected; } + + uint16_t available() { + return (uint16_t)receive_buffer.available(); + } + + void flush() { receive_buffer.clear(); } + + uint8_t availableForWrite() { + return transmit_buffer.free() > 255 ? 255 : (uint8_t)transmit_buffer.free(); + } + + void flushTX() { + if (host_connected) + while (transmit_buffer.available()) { /* nada */ } + } + + volatile RingBuffer<uint8_t, 128> receive_buffer; + volatile RingBuffer<uint8_t, 128> transmit_buffer; + volatile bool host_connected; +}; + +typedef Serial0Type<HalSerial> MSerialT; |