MMU2 as standard serial device (#20771)
[Marlin.git] / Marlin / src / HAL / STM32 / HAL.h
blob65074f0967efb4e2c54134cb3dfa4619ca8ed93d
1 /**
2 * Marlin 3D Printer Firmware
4 * Copyright (c) 2020 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 * Copyright (c) 2017 Victor Perez
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #pragma once
25 #define CPU_32_BIT
27 #include "../../core/macros.h"
28 #include "../shared/Marduino.h"
29 #include "../shared/math_32bit.h"
30 #include "../shared/HAL_SPI.h"
31 #include "fastio.h"
32 #include "Servo.h"
33 #include "watchdog.h"
34 #include "MarlinSerial.h"
36 #include "../../inc/MarlinConfigPre.h"
38 #include <stdint.h>
40 #ifdef USBCON
41 #include <USBSerial.h>
42 #endif
44 // ------------------------
45 // Defines
46 // ------------------------
47 #define _MSERIAL(X) MSerial##X
48 #define MSERIAL(X) _MSERIAL(X)
50 #if SERIAL_PORT == -1
51 #define MYSERIAL0 SerialUSB
52 #elif WITHIN(SERIAL_PORT, 1, 6)
53 #define MYSERIAL0 MSERIAL(SERIAL_PORT)
54 #else
55 #error "SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
56 #endif
58 #ifdef SERIAL_PORT_2
59 #if SERIAL_PORT_2 == -1
60 #define MYSERIAL1 SerialUSB
61 #elif WITHIN(SERIAL_PORT_2, 1, 6)
62 #define MYSERIAL1 MSERIAL(SERIAL_PORT_2)
63 #else
64 #error "SERIAL_PORT_2 must be -1 or from 1 to 6. Please update your configuration."
65 #endif
66 #endif
68 #ifdef MMU2_SERIAL_PORT
69 #if MMU2_SERIAL_PORT == -1
70 #define MMU2_SERIAL SerialUSB
71 #elif WITHIN(MMU2_SERIAL_PORT, 1, 6)
72 #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
73 #else
74 #error "MMU2_SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
75 #endif
76 #endif
78 #ifdef LCD_SERIAL_PORT
79 #if LCD_SERIAL_PORT == -1
80 #define LCD_SERIAL SerialUSB
81 #elif WITHIN(LCD_SERIAL_PORT, 1, 6)
82 #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
83 #else
84 #error "LCD_SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
85 #endif
86 #if HAS_DGUS_LCD
87 #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
88 #endif
89 #endif
91 /**
92 * TODO: review this to return 1 for pins that are not analog input
94 #ifndef analogInputToDigitalPin
95 #define analogInputToDigitalPin(p) (p)
96 #endif
98 #define CRITICAL_SECTION_START() uint32_t primask = __get_PRIMASK(); __disable_irq()
99 #define CRITICAL_SECTION_END() if (!primask) __enable_irq()
100 #define ISRS_ENABLED() (!__get_PRIMASK())
101 #define ENABLE_ISRS() __enable_irq()
102 #define DISABLE_ISRS() __disable_irq()
103 #define cli() __disable_irq()
104 #define sei() __enable_irq()
106 // On AVR this is in math.h?
107 #define square(x) ((x)*(x))
109 #ifndef strncpy_P
110 #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
111 #endif
113 // Fix bug in pgm_read_ptr
114 #undef pgm_read_ptr
115 #define pgm_read_ptr(addr) (*(addr))
117 // ------------------------
118 // Types
119 // ------------------------
121 typedef int16_t pin_t;
123 #define HAL_SERVO_LIB libServo
124 #define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos()
125 #define RESUME_SERVO_OUTPUT() libServo::resume_all_servos()
127 // ------------------------
128 // Public Variables
129 // ------------------------
131 // result of last ADC conversion
132 extern uint16_t HAL_adc_result;
134 // ------------------------
135 // Public functions
136 // ------------------------
138 // Memory related
139 #define __bss_end __bss_end__
141 // Enable hooks into setup for HAL
142 void HAL_init();
144 // Clear reset reason
145 void HAL_clear_reset_source();
147 // Reset reason
148 uint8_t HAL_get_reset_source();
150 inline void HAL_reboot() {} // reboot the board or restart the bootloader
152 void _delay_ms(const int delay);
154 extern "C" char* _sbrk(int incr);
156 #pragma GCC diagnostic push
157 #pragma GCC diagnostic ignored "-Wunused-function"
159 static inline int freeMemory() {
160 volatile char top;
161 return &top - reinterpret_cast<char*>(_sbrk(0));
164 #pragma GCC diagnostic pop
167 // ADC
170 #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
172 #define HAL_ADC_VREF 3.3
173 #define HAL_ADC_RESOLUTION ADC_RESOLUTION // 12
174 #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
175 #define HAL_READ_ADC() HAL_adc_result
176 #define HAL_ADC_READY() true
178 inline void HAL_adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); }
180 void HAL_adc_start_conversion(const uint8_t adc_pin);
182 uint16_t HAL_adc_get_result();
184 #define GET_PIN_MAP_PIN(index) index
185 #define GET_PIN_MAP_INDEX(pin) pin
186 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
188 #ifdef STM32F1xx
189 #define JTAG_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_JTAGDISABLE)
190 #define JTAGSWD_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_DISABLE)
191 #endif
193 #define PLATFORM_M997_SUPPORT
194 void flashFirmware(const int16_t);
196 // Maple Compatibility
197 typedef void (*systickCallback_t)(void);
198 void systick_attach_callback(systickCallback_t cb);
199 void HAL_SYSTICK_Callback();
200 extern volatile uint32_t systick_uptime_millis;
202 #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
205 * set_pwm_frequency
206 * Set the frequency of the timer corresponding to the provided pin
207 * All Timer PWM pins run at the same frequency
209 void set_pwm_frequency(const pin_t pin, int f_desired);
212 * set_pwm_duty
213 * Set the PWM duty cycle of the provided pin to the provided value
214 * Optionally allows inverting the duty cycle [default = false]
215 * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
217 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);