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/>.
26 * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
31 #include "../../core/macros.h"
32 #include "../shared/Marduino.h"
33 #include "../shared/math_32bit.h"
34 #include "../shared/HAL_SPI.h"
41 #include <util/atomic.h>
43 #include "../../inc/MarlinConfigPre.h"
49 #include "MarlinSerial.h"
51 // ------------------------
53 // ------------------------
55 #ifndef STM32_FLASH_SIZE
56 #if EITHER(MCU_STM32F103RE, MCU_STM32F103VE)
57 #define STM32_FLASH_SIZE 512
59 #define STM32_FLASH_SIZE 256
64 #if !HAS_SD_HOST_DRIVE
65 #define UsbSerial Serial
67 #define UsbSerial MarlinCompositeSerial
71 #define _MSERIAL(X) MSerial##X
72 #define MSERIAL(X) _MSERIAL(X)
74 #if EITHER(STM32_HIGH_DENSITY, STM32_XL_DENSITY)
81 #define MYSERIAL0 UsbSerial
82 #elif WITHIN(SERIAL_PORT, 1, NUM_UARTS)
83 #define MYSERIAL0 MSERIAL(SERIAL_PORT)
85 #error "SERIAL_PORT must be -1 or from 1 to 5. Please update your configuration."
87 #error "SERIAL_PORT must be -1 or from 1 to 3. Please update your configuration."
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)
96 #error "SERIAL_PORT_2 must be -1 or from 1 to 5. Please update your configuration."
98 #error "SERIAL_PORT_2 must be -1 or from 1 to 3. Please update your configuration."
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)
108 #error "MMU2_SERIAL_PORT must be -1 or from 1 to 5. Please update your configuration."
110 #error "MMU2_SERIAL_PORT must be -1 or from 1 to 3. Please update your configuration."
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)
120 #error "LCD_SERIAL_PORT must be -1 or from 1 to 5. Please update your configuration."
122 #error "LCD_SERIAL_PORT must be -1 or from 1 to 3. Please update your configuration."
125 #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
129 // Set interrupt grouping for this MCU
131 #define HAL_IDLETASK 1
135 * TODO: review this to return 1 for pins that are not analog input
137 #ifndef analogInputToDigitalPin
138 #define analogInputToDigitalPin(p) (p)
141 #ifndef digitalPinHasPWM
142 #define digitalPinHasPWM(P) !!PIN_MAP[P].timer_device
143 #define NO_COMPILE_TIME_PWM
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))
156 #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
159 // Fix bug in 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
168 #define RST_SOFTWARE 32
169 #define RST_BACKUP 64
171 // ------------------------
173 // ------------------------
175 typedef int8_t pin_t
;
177 // ------------------------
179 // ------------------------
181 // Result of last ADC conversion
182 extern uint16_t HAL_adc_result
;
184 // ------------------------
186 // ------------------------
188 // Disable interrupts
189 #define cli() noInterrupts()
192 #define sei() interrupts()
195 #define __bss_end __bss_end__
197 // Clear reset reason
198 void HAL_clear_reset_source();
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"
216 extern "C" char* _sbrk(int incr
);
218 static inline int freeMemory() {
220 return &top
- _sbrk(0);
223 #pragma GCC diagnostic pop
229 #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
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
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
);
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);