|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * |
| 4 | + * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] |
| 5 | + * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com |
| 6 | + * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com |
| 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 <https://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | +#pragma once |
| 23 | + |
| 24 | +#define CPU_32_BIT |
| 25 | +#define HAL_IDLETASK |
| 26 | +void HAL_idletask(); |
| 27 | + |
| 28 | +#define F_CPU 100000000 |
| 29 | +#define SystemCoreClock F_CPU |
| 30 | +#include <stdint.h> |
| 31 | +#include <stdarg.h> |
| 32 | + |
| 33 | +#undef min |
| 34 | +#undef max |
| 35 | + |
| 36 | +#include <algorithm> |
| 37 | +#include "pinmapping.h" |
| 38 | + |
| 39 | +void _printf (const char *format, ...); |
| 40 | +void _putc(uint8_t c); |
| 41 | +uint8_t _getc(); |
| 42 | + |
| 43 | +//extern "C" volatile uint32_t _millis; |
| 44 | + |
| 45 | +//arduino: Print.h |
| 46 | +#define DEC 10 |
| 47 | +#define HEX 16 |
| 48 | +#define OCT 8 |
| 49 | +#define BIN 2 |
| 50 | +//arduino: binary.h (weird defines) |
| 51 | +#define B01 1 |
| 52 | +#define B10 2 |
| 53 | + |
| 54 | +#include "../shared/Marduino.h" |
| 55 | +#include "../shared/math_32bit.h" |
| 56 | +#include "../shared/HAL_SPI.h" |
| 57 | +#include "fastio.h" |
| 58 | +#include "watchdog.h" |
| 59 | +#include "serial.h" |
| 60 | + |
| 61 | +#define SHARED_SERVOS HAS_SERVOS |
| 62 | + |
| 63 | +extern MSerialT serial_stream_0; |
| 64 | +extern MSerialT serial_stream_1; |
| 65 | +extern MSerialT serial_stream_2; |
| 66 | +extern MSerialT serial_stream_3; |
| 67 | + |
| 68 | +#define _MSERIAL(X) serial_stream_##X |
| 69 | +#define MSERIAL(X) _MSERIAL(X) |
| 70 | + |
| 71 | +#if WITHIN(SERIAL_PORT, 0, 3) |
| 72 | + #define MYSERIAL1 MSERIAL(SERIAL_PORT) |
| 73 | +#else |
| 74 | + #error "SERIAL_PORT must be from 0 to 3. Please update your configuration." |
| 75 | +#endif |
| 76 | + |
| 77 | +#ifdef SERIAL_PORT_2 |
| 78 | + #if WITHIN(SERIAL_PORT_2, 0, 3) |
| 79 | + #define MYSERIAL2 MSERIAL(SERIAL_PORT_2) |
| 80 | + #else |
| 81 | + #error "SERIAL_PORT_2 must be from 0 to 3. Please update your configuration." |
| 82 | + #endif |
| 83 | +#endif |
| 84 | + |
| 85 | +#ifdef MMU2_SERIAL_PORT |
| 86 | + #if WITHIN(MMU2_SERIAL_PORT, 0, 3) |
| 87 | + #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT) |
| 88 | + #else |
| 89 | + #error "MMU2_SERIAL_PORT must be from 0 to 3. Please update your configuration." |
| 90 | + #endif |
| 91 | +#endif |
| 92 | + |
| 93 | +#ifdef LCD_SERIAL_PORT |
| 94 | + #if WITHIN(LCD_SERIAL_PORT, 0, 3) |
| 95 | + #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT) |
| 96 | + #else |
| 97 | + #error "LCD_SERIAL_PORT must be from 0 to 3. Please update your configuration." |
| 98 | + #endif |
| 99 | +#endif |
| 100 | + |
| 101 | + |
| 102 | +#define ST7920_DELAY_1 DELAY_NS(600) |
| 103 | +#define ST7920_DELAY_2 DELAY_NS(750) |
| 104 | +#define ST7920_DELAY_3 DELAY_NS(750) |
| 105 | + |
| 106 | +// |
| 107 | +// Interrupts |
| 108 | +// |
| 109 | +#define CRITICAL_SECTION_START() |
| 110 | +#define CRITICAL_SECTION_END() |
| 111 | +#define ISRS_ENABLED() |
| 112 | +#define ENABLE_ISRS() |
| 113 | +#define DISABLE_ISRS() |
| 114 | + |
| 115 | +inline void HAL_init() {} |
| 116 | + |
| 117 | +// Utility functions |
| 118 | +#pragma GCC diagnostic push |
| 119 | +#pragma GCC diagnostic ignored "-Wunused-function" |
| 120 | +int freeMemory(); |
| 121 | +#pragma GCC diagnostic pop |
| 122 | + |
| 123 | +// ADC |
| 124 | +#define HAL_ADC_VREF 5.0 |
| 125 | +#define HAL_ADC_RESOLUTION 10 |
| 126 | +#define HAL_ANALOG_SELECT(ch) HAL_adc_enable_channel(ch) |
| 127 | +#define HAL_START_ADC(ch) HAL_adc_start_conversion(ch) |
| 128 | +#define HAL_READ_ADC() HAL_adc_get_result() |
| 129 | +#define HAL_ADC_READY() true |
| 130 | + |
| 131 | +void HAL_adc_init(); |
| 132 | +void HAL_adc_enable_channel(const uint8_t ch); |
| 133 | +void HAL_adc_start_conversion(const uint8_t ch); |
| 134 | +uint16_t HAL_adc_get_result(); |
| 135 | + |
| 136 | +// Reset source |
| 137 | +inline void HAL_clear_reset_source(void) {} |
| 138 | +inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; } |
| 139 | + |
| 140 | +/* ---------------- Delay in cycles */ |
| 141 | + |
| 142 | +#define DELAY_CYCLES(x) Kernel::delayCycles(x) |
| 143 | +#define SYSTEM_YIELD() Kernel::yield() |
| 144 | + |
| 145 | +// Maple Compatibility |
| 146 | +typedef void (*systickCallback_t)(void); |
| 147 | +void systick_attach_callback(systickCallback_t cb); |
| 148 | +extern volatile uint32_t systick_uptime_millis; |
| 149 | + |
| 150 | +// Marlin uses strstr in constexpr context, this is not supported, workaround by defining constexpr versions of the required functions. |
| 151 | +#define strstr(a, b) strstr_constexpr((a), (b)) |
| 152 | + |
| 153 | +constexpr inline std::size_t strlen_constexpr(const char* str) { |
| 154 | + // https://github.com/gcc-mirror/gcc/blob/5c7634a0e5f202935aa6c11b6ea953b8bf80a00a/libstdc%2B%2B-v3/include/bits/char_traits.h#L329 |
| 155 | + if (str != nullptr) { |
| 156 | + std::size_t i = 0; |
| 157 | + while (str[i] != '\0') { |
| 158 | + ++i; |
| 159 | + } |
| 160 | + |
| 161 | + return i; |
| 162 | + } |
| 163 | + |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +constexpr inline int strncmp_constexpr(const char* lhs, const char* rhs, std::size_t count) { |
| 168 | + // https://github.com/gcc-mirror/gcc/blob/13b9cbfc32fe3ac4c81c4dd9c42d141c8fb95db4/libstdc%2B%2B-v3/include/bits/char_traits.h#L655 |
| 169 | + if (lhs == nullptr || rhs == nullptr) { |
| 170 | + return rhs != nullptr ? -1 : 1; |
| 171 | + } |
| 172 | + |
| 173 | + for (std::size_t i = 0; i < count; ++i) { |
| 174 | + if (lhs[i] != rhs[i]) { |
| 175 | + return lhs[i] < rhs[i] ? -1 : 1; |
| 176 | + } else if (lhs[i] == '\0') { |
| 177 | + return 0; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
| 184 | +constexpr inline const char* strstr_constexpr(const char* str, const char* target) { |
| 185 | + // https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c |
| 186 | + if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) { |
| 187 | + std::size_t len = strlen_constexpr(target); |
| 188 | + do { |
| 189 | + char sc = {}; |
| 190 | + do { |
| 191 | + if ((sc = *str++) == '\0') { |
| 192 | + return nullptr; |
| 193 | + } |
| 194 | + } while (sc != c); |
| 195 | + } while (strncmp_constexpr(str, target, len) != 0); |
| 196 | + --str; |
| 197 | + } |
| 198 | + |
| 199 | + return str; |
| 200 | +} |
| 201 | + |
| 202 | +constexpr inline char* strstr_constexpr(char* str, const char* target) { |
| 203 | + // https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c |
| 204 | + if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) { |
| 205 | + std::size_t len = strlen_constexpr(target); |
| 206 | + do { |
| 207 | + char sc = {}; |
| 208 | + do { |
| 209 | + if ((sc = *str++) == '\0') { |
| 210 | + return nullptr; |
| 211 | + } |
| 212 | + } while (sc != c); |
| 213 | + } while (strncmp_constexpr(str, target, len) != 0); |
| 214 | + --str; |
| 215 | + } |
| 216 | + return str; |
| 217 | +} |
0 commit comments