MMU2 as standard serial device (#20771)
[Marlin.git] / Marlin / src / HAL / AVR / HAL.h
blob8b95acb0acbc7b92cee464347bdad604351236aa
1 /**
2 * Marlin 3D Printer Firmware
3 * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4 * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 #pragma once
22 #include "../shared/Marduino.h"
23 #include "../shared/HAL_SPI.h"
24 #include "fastio.h"
25 #include "watchdog.h"
26 #include "math.h"
28 #ifdef USBCON
29 #include <HardwareSerial.h>
30 #else
31 #define HardwareSerial_h // Hack to prevent HardwareSerial.h header inclusion
32 #include "MarlinSerial.h"
33 #endif
35 #include <stdint.h>
36 #include <util/delay.h>
37 #include <avr/eeprom.h>
38 #include <avr/pgmspace.h>
39 #include <avr/interrupt.h>
40 #include <avr/io.h>
42 #ifndef pgm_read_ptr
43 // Compatibility for avr-libc 1.8.0-4.1 included with Ubuntu for
44 // Windows Subsystem for Linux on Windows 10 as of 10/18/2019
45 #define pgm_read_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))
46 #define pgm_read_ptr_near(address_short) (void*)__LPM_word((uint16_t)(address_short))
47 #define pgm_read_ptr(address_short) pgm_read_ptr_near(address_short)
48 #endif
50 // ------------------------
51 // Defines
52 // ------------------------
54 // AVR PROGMEM extension for sprintf_P
55 #define S_FMT "%S"
57 // AVR PROGMEM extension for string define
58 #define PGMSTR(NAM,STR) const char NAM[] PROGMEM = STR
60 #ifndef CRITICAL_SECTION_START
61 #define CRITICAL_SECTION_START() unsigned char _sreg = SREG; cli()
62 #define CRITICAL_SECTION_END() SREG = _sreg
63 #endif
64 #define ISRS_ENABLED() TEST(SREG, SREG_I)
65 #define ENABLE_ISRS() sei()
66 #define DISABLE_ISRS() cli()
68 // ------------------------
69 // Types
70 // ------------------------
72 typedef int8_t pin_t;
74 #define SHARED_SERVOS HAS_SERVOS
75 #define HAL_SERVO_LIB Servo
77 // ------------------------
78 // Public Variables
79 // ------------------------
81 //extern uint8_t MCUSR;
83 // Serial ports
84 #ifdef USBCON
85 #define MYSERIAL0 TERN(BLUETOOTH, bluetoothSerial, Serial)
86 #else
87 #if !WITHIN(SERIAL_PORT, -1, 3)
88 #error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
89 #endif
90 #define MYSERIAL0 customizedSerial1
92 #ifdef SERIAL_PORT_2
93 #if !WITHIN(SERIAL_PORT_2, -1, 3)
94 #error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
95 #endif
96 #define MYSERIAL1 customizedSerial2
97 #endif
98 #endif
100 #ifdef MMU2_SERIAL_PORT
101 #if !WITHIN(MMU2_SERIAL_PORT, -1, 3)
102 #error "MMU2_SERIAL_PORT must be from -1 to 3. Please update your configuration."
103 #endif
104 #define MMU2_SERIAL mmuSerial
105 #endif
107 #ifdef LCD_SERIAL_PORT
108 #if !WITHIN(LCD_SERIAL_PORT, -1, 3)
109 #error "LCD_SERIAL_PORT must be from -1 to 3. Please update your configuration."
110 #endif
111 #define LCD_SERIAL lcdSerial
112 #if HAS_DGUS_LCD
113 #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.get_tx_buffer_free()
114 #endif
115 #endif
117 // ------------------------
118 // Public functions
119 // ------------------------
121 void HAL_init();
123 //void cli();
125 //void _delay_ms(const int delay);
127 inline void HAL_clear_reset_source() { MCUSR = 0; }
128 inline uint8_t HAL_get_reset_source() { return MCUSR; }
130 inline void HAL_reboot() {} // reboot the board or restart the bootloader
132 #if GCC_VERSION <= 50000
133 #pragma GCC diagnostic push
134 #pragma GCC diagnostic ignored "-Wunused-function"
135 #endif
137 extern "C" int freeMemory();
139 #if GCC_VERSION <= 50000
140 #pragma GCC diagnostic pop
141 #endif
143 // ADC
144 #ifdef DIDR2
145 #define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
146 #else
147 #define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
148 #endif
150 inline void HAL_adc_init() {
151 ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
152 DIDR0 = 0;
153 #ifdef DIDR2
154 DIDR2 = 0;
155 #endif
158 #define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
159 #ifdef MUX5
160 #define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
161 #else
162 #define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
163 #endif
165 #define HAL_ADC_VREF 5.0
166 #define HAL_ADC_RESOLUTION 10
167 #define HAL_READ_ADC() ADC
168 #define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
170 #define GET_PIN_MAP_PIN(index) index
171 #define GET_PIN_MAP_INDEX(pin) pin
172 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
174 #define HAL_SENSITIVE_PINS 0, 1
176 #ifdef __AVR_AT90USB1286__
177 #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
178 #endif
180 // AVR compatibility
181 #define strtof strtod
183 #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
186 * set_pwm_frequency
187 * Sets the frequency of the timer corresponding to the provided pin
188 * as close as possible to the provided desired frequency. Internally
189 * calculates the required waveform generation mode, prescaler and
190 * resolution values required and sets the timer registers accordingly.
191 * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
192 * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
194 void set_pwm_frequency(const pin_t pin, int f_desired);
197 * set_pwm_duty
198 * Sets the PWM duty cycle of the provided pin to the provided value
199 * Optionally allows inverting the duty cycle [default = false]
200 * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
202 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);