MMU2 as standard serial device (#20771)
[Marlin.git] / Marlin / src / HAL / LPC1768 / HAL.h
blob44a4e88624c2d1ec2bec2202b37a31d4297c9571
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
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.
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.
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/>.
22 #pragma once
24 /**
25 * HAL_LPC1768/HAL.h
26 * Hardware Abstraction Layer for NXP LPC1768
29 #define CPU_32_BIT
31 void HAL_init();
33 #include <stdint.h>
34 #include <stdarg.h>
35 #include <algorithm>
37 extern "C" volatile uint32_t _millis;
39 #include "../shared/Marduino.h"
40 #include "../shared/math_32bit.h"
41 #include "../shared/HAL_SPI.h"
42 #include "fastio.h"
43 #include "watchdog.h"
44 #include "MarlinSerial.h"
46 #include <adc.h>
47 #include <pinmapping.h>
48 #include <CDCSerial.h>
51 // Default graphical display delays
53 #ifndef ST7920_DELAY_1
54 #define ST7920_DELAY_1 DELAY_NS(600)
55 #endif
56 #ifndef ST7920_DELAY_2
57 #define ST7920_DELAY_2 DELAY_NS(750)
58 #endif
59 #ifndef ST7920_DELAY_3
60 #define ST7920_DELAY_3 DELAY_NS(750)
61 #endif
63 #define _MSERIAL(X) MSerial##X
64 #define MSERIAL(X) _MSERIAL(X)
65 #define MSerial0 MSerial
67 #if SERIAL_PORT == -1
68 #define MYSERIAL0 UsbSerial
69 #elif WITHIN(SERIAL_PORT, 0, 3)
70 #define MYSERIAL0 MSERIAL(SERIAL_PORT)
71 #else
72 #error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
73 #endif
75 #ifdef SERIAL_PORT_2
76 #if SERIAL_PORT_2 == -1
77 #define MYSERIAL1 UsbSerial
78 #elif WITHIN(SERIAL_PORT_2, 0, 3)
79 #define MYSERIAL1 MSERIAL(SERIAL_PORT_2)
80 #else
81 #error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
82 #endif
83 #endif
85 #ifdef MMU2_SERIAL_PORT
86 #if MMU2_SERIAL_PORT == -1
87 #define MMU2_SERIAL UsbSerial
88 #elif WITHIN(MMU2_SERIAL_PORT, 0, 3)
89 #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
90 #else
91 #error "MMU2_SERIAL_PORT must be from -1 to 3. Please update your configuration."
92 #endif
93 #endif
95 #ifdef LCD_SERIAL_PORT
96 #if LCD_SERIAL_PORT == -1
97 #define LCD_SERIAL UsbSerial
98 #elif WITHIN(LCD_SERIAL_PORT, 0, 3)
99 #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
100 #else
101 #error "LCD_SERIAL_PORT must be from -1 to 3. Please update your configuration."
102 #endif
103 #endif
106 // Interrupts
108 #define CRITICAL_SECTION_START() uint32_t primask = __get_PRIMASK(); __disable_irq()
109 #define CRITICAL_SECTION_END() if (!primask) __enable_irq()
110 #define ISRS_ENABLED() (!__get_PRIMASK())
111 #define ENABLE_ISRS() __enable_irq()
112 #define DISABLE_ISRS() __disable_irq()
115 // Utility functions
117 #if GCC_VERSION <= 50000
118 #pragma GCC diagnostic push
119 #pragma GCC diagnostic ignored "-Wunused-function"
120 #endif
122 int freeMemory();
124 #if GCC_VERSION <= 50000
125 #pragma GCC diagnostic pop
126 #endif
129 // ADC API
132 #define ADC_MEDIAN_FILTER_SIZE (23) // Higher values increase step delay (phase shift),
133 // (ADC_MEDIAN_FILTER_SIZE + 1) / 2 sample step delay (12 samples @ 500Hz: 24ms phase shift)
134 // Memory usage per ADC channel (bytes): (6 * ADC_MEDIAN_FILTER_SIZE) + 16
135 // 8 * ((6 * 23) + 16 ) = 1232 Bytes for 8 channels
137 #define ADC_LOWPASS_K_VALUE (2) // Higher values increase rise time
138 // Rise time sample delays for 100% signal convergence on full range step
139 // (1 : 13, 2 : 32, 3 : 67, 4 : 139, 5 : 281, 6 : 565, 7 : 1135, 8 : 2273)
140 // K = 6, 565 samples, 500Hz sample rate, 1.13s convergence on full range step
141 // Memory usage per ADC channel (bytes): 4 (32 Bytes for 8 channels)
143 #define HAL_ADC_VREF 3.3 // ADC voltage reference
145 #define HAL_ADC_RESOLUTION 12 // 15 bit maximum, raw temperature is stored as int16_t
146 #define HAL_ADC_FILTERED // Disable oversampling done in Marlin as ADC values already filtered in HAL
148 using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
149 extern uint32_t HAL_adc_reading;
150 [[gnu::always_inline]] inline void HAL_start_adc(const pin_t pin) {
151 HAL_adc_reading = FilteredADC::read(pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits
153 [[gnu::always_inline]] inline uint16_t HAL_read_adc() {
154 return HAL_adc_reading;
157 #define HAL_adc_init()
158 #define HAL_ANALOG_SELECT(pin) FilteredADC::enable_channel(pin)
159 #define HAL_START_ADC(pin) HAL_start_adc(pin)
160 #define HAL_READ_ADC() HAL_read_adc()
161 #define HAL_ADC_READY() (true)
163 // Test whether the pin is valid
164 constexpr bool VALID_PIN(const pin_t pin) {
165 return LPC176x::pin_is_valid(pin);
168 // Get the analog index for a digital pin
169 constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t pin) {
170 return (LPC176x::pin_is_valid(pin) && LPC176x::pin_has_adc(pin)) ? pin : -1;
173 // Return the index of a pin number
174 constexpr int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
175 return LPC176x::pin_index(pin);
178 // Get the pin number at the given index
179 constexpr pin_t GET_PIN_MAP_PIN(const int16_t index) {
180 return LPC176x::pin_index(index);
183 // Parse a G-code word into a pin index
184 int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
185 // P0.6 thru P0.9 are for the onboard SD card
186 #define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09
188 #define HAL_IDLETASK 1
189 void HAL_idletask();
191 #define PLATFORM_M997_SUPPORT
192 void flashFirmware(const int16_t);
194 #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
197 * set_pwm_frequency
198 * Set the frequency of the timer corresponding to the provided pin
199 * All Hardware PWM pins run at the same frequency and all
200 * Software PWM pins run at the same frequency
202 void set_pwm_frequency(const pin_t pin, int f_desired);
205 * set_pwm_duty
206 * Set the PWM duty cycle of the provided pin to the provided value
207 * Optionally allows inverting the duty cycle [default = false]
208 * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
210 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
212 // Reset source
213 void HAL_clear_reset_source(void);
214 uint8_t HAL_get_reset_source(void);
216 inline void HAL_reboot() {} // reboot the board or restart the bootloader
218 // Add strcmp_P if missing
219 #ifndef strcmp_P
220 #define strcmp_P(a, b) strcmp((a), (b))
221 #endif
223 #ifndef strcat_P
224 #define strcat_P(a, b) strcat((a), (b))
225 #endif
227 #ifndef strcpy_P
228 #define strcpy_P(a, b) strcpy((a), (b))
229 #endif