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/>.
22 #include "../shared/Marduino.h"
23 #include "../shared/HAL_SPI.h"
29 #include <HardwareSerial.h>
31 #define HardwareSerial_h // Hack to prevent HardwareSerial.h header inclusion
32 #include "MarlinSerial.h"
36 #include <util/delay.h>
37 #include <avr/eeprom.h>
38 #include <avr/pgmspace.h>
39 #include <avr/interrupt.h>
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)
50 // ------------------------
52 // ------------------------
54 // AVR PROGMEM extension for sprintf_P
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
64 #define ISRS_ENABLED() TEST(SREG, SREG_I)
65 #define ENABLE_ISRS() sei()
66 #define DISABLE_ISRS() cli()
68 // ------------------------
70 // ------------------------
74 #define SHARED_SERVOS HAS_SERVOS
75 #define HAL_SERVO_LIB Servo
77 // ------------------------
79 // ------------------------
81 //extern uint8_t MCUSR;
85 #define MYSERIAL0 TERN(BLUETOOTH, bluetoothSerial, Serial)
87 #if !WITHIN(SERIAL_PORT, -1, 3)
88 #error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
90 #define MYSERIAL0 customizedSerial1
93 #if !WITHIN(SERIAL_PORT_2, -1, 3)
94 #error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
96 #define MYSERIAL1 customizedSerial2
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."
104 #define MMU2_SERIAL mmuSerial
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."
111 #define LCD_SERIAL lcdSerial
113 #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.get_tx_buffer_free()
117 // ------------------------
119 // ------------------------
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"
137 extern "C" int freeMemory();
139 #if GCC_VERSION <= 50000
140 #pragma GCC diagnostic pop
145 #define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
147 #define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
150 inline void HAL_adc_init() {
151 ADCSRA
= _BV(ADEN
) | _BV(ADSC
) | _BV(ADIF
) | 0x07;
158 #define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
160 #define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
162 #define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
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)
181 #define strtof strtod
183 #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
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
);
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);