MMU2 as standard serial device (#20771)
[Marlin.git] / Marlin / src / HAL / STM32F1 / HAL.h
blobecfc1729539774f84ec260fa06f55bb5658829cf
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 /**
26 * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
29 #define CPU_32_BIT
31 #include "../../core/macros.h"
32 #include "../shared/Marduino.h"
33 #include "../shared/math_32bit.h"
34 #include "../shared/HAL_SPI.h"
36 #include "fastio.h"
37 #include "watchdog.h"
40 #include <stdint.h>
41 #include <util/atomic.h>
43 #include "../../inc/MarlinConfigPre.h"
45 #if HAS_SD_HOST_DRIVE
46 #include "msc_sd.h"
47 #endif
49 #include "MarlinSerial.h"
51 // ------------------------
52 // Defines
53 // ------------------------
55 #ifndef STM32_FLASH_SIZE
56 #if EITHER(MCU_STM32F103RE, MCU_STM32F103VE)
57 #define STM32_FLASH_SIZE 512
58 #else
59 #define STM32_FLASH_SIZE 256
60 #endif
61 #endif
63 #ifdef SERIAL_USB
64 #if !HAS_SD_HOST_DRIVE
65 #define UsbSerial Serial
66 #else
67 #define UsbSerial MarlinCompositeSerial
68 #endif
69 #endif
71 #define _MSERIAL(X) MSerial##X
72 #define MSERIAL(X) _MSERIAL(X)
74 #if EITHER(STM32_HIGH_DENSITY, STM32_XL_DENSITY)
75 #define NUM_UARTS 5
76 #else
77 #define NUM_UARTS 3
78 #endif
80 #if SERIAL_PORT == -1
81 #define MYSERIAL0 UsbSerial
82 #elif WITHIN(SERIAL_PORT, 1, NUM_UARTS)
83 #define MYSERIAL0 MSERIAL(SERIAL_PORT)
84 #elif NUM_UARTS == 5
85 #error "SERIAL_PORT must be -1 or from 1 to 5. Please update your configuration."
86 #else
87 #error "SERIAL_PORT must be -1 or from 1 to 3. Please update your configuration."
88 #endif
90 #ifdef SERIAL_PORT_2
91 #if SERIAL_PORT_2 == -1
92 #define MYSERIAL1 UsbSerial
93 #elif WITHIN(SERIAL_PORT_2, 1, NUM_UARTS)
94 #define MYSERIAL1 MSERIAL(SERIAL_PORT_2)
95 #elif NUM_UARTS == 5
96 #error "SERIAL_PORT_2 must be -1 or from 1 to 5. Please update your configuration."
97 #else
98 #error "SERIAL_PORT_2 must be -1 or from 1 to 3. Please update your configuration."
99 #endif
100 #endif
102 #ifdef MMU2_SERIAL_PORT
103 #if MMU2_SERIAL_PORT == -1
104 #define MMU2_SERIAL UsbSerial
105 #elif WITHIN(MMU2_SERIAL_PORT, 1, NUM_UARTS)
106 #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
107 #elif NUM_UARTS == 5
108 #error "MMU2_SERIAL_PORT must be -1 or from 1 to 5. Please update your configuration."
109 #else
110 #error "MMU2_SERIAL_PORT must be -1 or from 1 to 3. Please update your configuration."
111 #endif
112 #endif
114 #ifdef LCD_SERIAL_PORT
115 #if LCD_SERIAL_PORT == -1
116 #define LCD_SERIAL UsbSerial
117 #elif WITHIN(LCD_SERIAL_PORT, 1, NUM_UARTS)
118 #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
119 #elif NUM_UARTS == 5
120 #error "LCD_SERIAL_PORT must be -1 or from 1 to 5. Please update your configuration."
121 #else
122 #error "LCD_SERIAL_PORT must be -1 or from 1 to 3. Please update your configuration."
123 #endif
124 #if HAS_DGUS_LCD
125 #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
126 #endif
127 #endif
129 // Set interrupt grouping for this MCU
130 void HAL_init();
131 #define HAL_IDLETASK 1
132 void HAL_idletask();
135 * TODO: review this to return 1 for pins that are not analog input
137 #ifndef analogInputToDigitalPin
138 #define analogInputToDigitalPin(p) (p)
139 #endif
141 #ifndef digitalPinHasPWM
142 #define digitalPinHasPWM(P) !!PIN_MAP[P].timer_device
143 #define NO_COMPILE_TIME_PWM
144 #endif
146 #define CRITICAL_SECTION_START() uint32_t primask = __get_primask(); (void)__iCliRetVal()
147 #define CRITICAL_SECTION_END() if (!primask) (void)__iSeiRetVal()
148 #define ISRS_ENABLED() (!__get_primask())
149 #define ENABLE_ISRS() ((void)__iSeiRetVal())
150 #define DISABLE_ISRS() ((void)__iCliRetVal())
152 // On AVR this is in math.h?
153 #define square(x) ((x)*(x))
155 #ifndef strncpy_P
156 #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
157 #endif
159 // Fix bug in pgm_read_ptr
160 #undef pgm_read_ptr
161 #define pgm_read_ptr(addr) (*(addr))
163 #define RST_POWER_ON 1
164 #define RST_EXTERNAL 2
165 #define RST_BROWN_OUT 4
166 #define RST_WATCHDOG 8
167 #define RST_JTAG 16
168 #define RST_SOFTWARE 32
169 #define RST_BACKUP 64
171 // ------------------------
172 // Types
173 // ------------------------
175 typedef int8_t pin_t;
177 // ------------------------
178 // Public Variables
179 // ------------------------
181 // Result of last ADC conversion
182 extern uint16_t HAL_adc_result;
184 // ------------------------
185 // Public functions
186 // ------------------------
188 // Disable interrupts
189 #define cli() noInterrupts()
191 // Enable interrupts
192 #define sei() interrupts()
194 // Memory related
195 #define __bss_end __bss_end__
197 // Clear reset reason
198 void HAL_clear_reset_source();
200 // Reset reason
201 uint8_t HAL_get_reset_source();
203 inline void HAL_reboot() {} // reboot the board or restart the bootloader
205 void _delay_ms(const int delay);
207 #pragma GCC diagnostic push
208 #pragma GCC diagnostic ignored "-Wunused-function"
211 extern "C" {
212 int freeMemory();
216 extern "C" char* _sbrk(int incr);
218 static inline int freeMemory() {
219 volatile char top;
220 return &top - _sbrk(0);
223 #pragma GCC diagnostic pop
226 // ADC
229 #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
231 void HAL_adc_init();
233 #define HAL_ADC_VREF 3.3
234 #define HAL_ADC_RESOLUTION 10
235 #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
236 #define HAL_READ_ADC() HAL_adc_result
237 #define HAL_ADC_READY() true
239 void HAL_adc_start_conversion(const uint8_t adc_pin);
240 uint16_t HAL_adc_get_result();
242 uint16_t analogRead(pin_t pin); // need HAL_ANALOG_SELECT() first
243 void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
245 #define GET_PIN_MAP_PIN(index) index
246 #define GET_PIN_MAP_INDEX(pin) pin
247 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
249 #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
250 #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
252 #define PLATFORM_M997_SUPPORT
253 void flashFirmware(const int16_t);
255 #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
258 * set_pwm_frequency
259 * Set the frequency of the timer corresponding to the provided pin
260 * All Timer PWM pins run at the same frequency
262 void set_pwm_frequency(const pin_t pin, int f_desired);
265 * set_pwm_duty
266 * Set the PWM duty cycle of the provided pin to the provided value
267 * Optionally allows inverting the duty cycle [default = false]
268 * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
270 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);