|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * Copyright (c) 2025 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 <https://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | +#pragma once |
| 23 | + |
| 24 | +#define CPU_32_BIT |
| 25 | + |
| 26 | +#include "../../core/macros.h" |
| 27 | +#include "../shared/Marduino.h" |
| 28 | +#include "../shared/math_32bit.h" |
| 29 | +#include "../shared/HAL_SPI.h" |
| 30 | + |
| 31 | +#include "temp_soc.h" |
| 32 | +#include "fastio.h" |
| 33 | +#include "Servo.h" |
| 34 | + |
| 35 | +#include "../../inc/MarlinConfigPre.h" |
| 36 | + |
| 37 | +#include <stdint.h> |
| 38 | +#include <GPIO.hpp> |
| 39 | +#include <AFIO.hpp> |
| 40 | + |
| 41 | +// Default graphical display delays |
| 42 | +#define CPU_ST7920_DELAY_1 300 |
| 43 | +#define CPU_ST7920_DELAY_2 40 |
| 44 | +#define CPU_ST7920_DELAY_3 340 |
| 45 | + |
| 46 | +// Serial Ports |
| 47 | +#include "MarlinSerial.h" |
| 48 | + |
| 49 | +// Interrupts |
| 50 | +#define CRITICAL_SECTION_START() const bool irqon = !__get_PRIMASK(); __disable_irq() |
| 51 | +#define CRITICAL_SECTION_END() if (irqon) __enable_irq() |
| 52 | + |
| 53 | +#define cli() __disable_irq() |
| 54 | +#define sei() __enable_irq() |
| 55 | + |
| 56 | +// Alias of __bss_end__ |
| 57 | +#define __bss_end __bss_end__ |
| 58 | + |
| 59 | +// Types |
| 60 | +typedef double isr_float_t; // FPU ops are used for single-precision, so use double for ISRs. |
| 61 | +typedef uint8_t pin_t; // Parity with mfl platform |
| 62 | + |
| 63 | +// Servo |
| 64 | +class libServo; |
| 65 | +typedef libServo hal_servo_t; |
| 66 | +#define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos() |
| 67 | +#define RESUME_SERVO_OUTPUT() libServo::resume_all_servos() |
| 68 | + |
| 69 | +// Debugging |
| 70 | +#define JTAG_DISABLE() AFIO_I.set_remap(gpio::Pin_Remap_Select::SWJ_DP_ONLY_REMAP) |
| 71 | +#define JTAGSWD_DISABLE() AFIO_I.set_remap(gpio::Pin_Remap_Select::SWJ_ALL_DISABLED_REMAP) |
| 72 | +#define JTAGSWD_RESET() AFIO_I.set_remap(gpio::Pin_Remap_Select::FULL_SWJ_REMAP) |
| 73 | + |
| 74 | +// ADC |
| 75 | +#ifdef ADC_RESOLUTION |
| 76 | + #define HAL_ADC_RESOLUTION ADC_RESOLUTION |
| 77 | +#else |
| 78 | + #define HAL_ADC_RESOLUTION 12 |
| 79 | +#endif |
| 80 | + |
| 81 | +#define HAL_ADC_VREF_MV 3300 |
| 82 | + |
| 83 | +// Disable Marlin's software oversampling. |
| 84 | +// The MFL framework uses 16x hardware oversampling by default |
| 85 | +#define HAL_ADC_FILTERED |
| 86 | + |
| 87 | +#define GET_PIN_MAP_PIN(index) index |
| 88 | +#define GET_PIN_MAP_INDEX(pin) pin |
| 89 | +#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval) |
| 90 | + |
| 91 | +#ifndef PLATFORM_M997_SUPPORT |
| 92 | + #define PLATFORM_M997_SUPPORT |
| 93 | +#endif |
| 94 | + |
| 95 | +void flashFirmware(const int16_t); |
| 96 | + |
| 97 | +#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment |
| 98 | + |
| 99 | +extern "C" char* _sbrk(int incr); |
| 100 | +extern "C" char* dtostrf(double val, signed char width, unsigned char prec, char* sout); |
| 101 | + |
| 102 | +// MarlinHAL Class |
| 103 | +class MarlinHAL { |
| 104 | +public: |
| 105 | + // Before setup() |
| 106 | + MarlinHAL() {} |
| 107 | + |
| 108 | + // Watchdog |
| 109 | + static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {}); |
| 110 | + static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {}); |
| 111 | + |
| 112 | + static void init(); // called early in setup() |
| 113 | + static void init_board() {} // called less early in setup() |
| 114 | + static void reboot() { NVIC_SystemReset(); } // restart the firmware from 0x0 |
| 115 | + |
| 116 | + // Interrupts |
| 117 | + static bool isr_state() { return !__get_PRIMASK(); } |
| 118 | + static void isr_on() { sei(); } |
| 119 | + static void isr_off() { cli(); } |
| 120 | + static void delay_ms(const int ms) { delay(ms); } |
| 121 | + |
| 122 | + // Tasks called from idle() |
| 123 | + static void idletask() {} |
| 124 | + |
| 125 | + // Reset |
| 126 | + static uint8_t get_reset_source(); |
| 127 | + static void clear_reset_source() { RCU_I.clear_all_reset_flags(); } |
| 128 | + |
| 129 | + // Free SRAM |
| 130 | + static int freeMemory(); |
| 131 | + |
| 132 | + // ADC methods |
| 133 | + static uint16_t adc_result; |
| 134 | + |
| 135 | + // Called by Temperature::init once at startup |
| 136 | + static void adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); } |
| 137 | + |
| 138 | + // Called by Temperature::init for each sensor at startup |
| 139 | + static void adc_enable(const pin_t pin) { pinMode(pin, INPUT); } |
| 140 | + |
| 141 | + // Called from Temperature::isr to start ADC sampling on the given pin |
| 142 | + static void adc_start(const pin_t pin) { adc_result = static_cast<uint16_t>(analogRead(pin)); } |
| 143 | + |
| 144 | + // Check if ADC is ready for reading |
| 145 | + static bool adc_ready() { return true; } |
| 146 | + |
| 147 | + // Current value of the ADC register |
| 148 | + static uint16_t adc_value() { return adc_result; } |
| 149 | + |
| 150 | + // Set the PWM duty cycle for the pin to the given value. |
| 151 | + // Optionally invert the duty cycle [default = false] |
| 152 | + // Optionally change the maximum size of the provided value to enable finer PWM duty control [default = 255] |
| 153 | + static void set_pwm_duty(const pin_t pin, const uint16_t value, const uint16_t scale = 255U, const bool invert = false); |
| 154 | + |
| 155 | + // Set the frequency of the timer for the given pin. |
| 156 | + // All Timer PWM pins run at the same frequency. |
| 157 | + static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired); |
| 158 | +}; |
0 commit comments