2018-06-13 19:08:26 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-06-13 19:08:26 -05:00
|
|
|
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-11-04 02:25:55 -06:00
|
|
|
#pragma once
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2019-05-02 00:45:50 -05:00
|
|
|
#include "../shared/Marduino.h"
|
2018-08-14 01:28:52 -07:00
|
|
|
#include "../shared/HAL_SPI.h"
|
2019-09-02 19:49:58 -05:00
|
|
|
#include "fastio.h"
|
|
|
|
#include "watchdog.h"
|
|
|
|
#include "math.h"
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2017-11-05 08:49:38 -06:00
|
|
|
#ifdef USBCON
|
2019-11-12 20:16:54 -08:00
|
|
|
#include <HardwareSerial.h>
|
2017-11-05 08:49:38 -06:00
|
|
|
#else
|
2019-07-08 23:42:29 -05:00
|
|
|
#define HardwareSerial_h // Hack to prevent HardwareSerial.h header inclusion
|
2017-11-05 08:49:38 -06:00
|
|
|
#include "MarlinSerial.h"
|
|
|
|
#endif
|
|
|
|
|
2019-07-08 23:42:29 -05:00
|
|
|
#include <stdint.h>
|
2019-05-02 00:45:50 -05:00
|
|
|
#include <util/delay.h>
|
|
|
|
#include <avr/eeprom.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
2019-10-24 13:57:20 -06:00
|
|
|
#ifndef pgm_read_ptr
|
|
|
|
// Compatibility for avr-libc 1.8.0-4.1 included with Ubuntu for
|
|
|
|
// Windows Subsystem for Linux on Windows 10 as of 10/18/2019
|
2019-10-27 15:29:37 -05:00
|
|
|
#define pgm_read_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))
|
2019-10-24 13:57:20 -06:00
|
|
|
#define pgm_read_ptr_near(address_short) (void*)__LPM_word((uint16_t)(address_short))
|
|
|
|
#define pgm_read_ptr(address_short) pgm_read_ptr_near(address_short)
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
// Defines
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2020-05-05 17:55:35 -05:00
|
|
|
// AVR PROGMEM extension for sprintf_P
|
|
|
|
#define S_FMT "%S"
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2020-05-06 10:04:04 +05:30
|
|
|
// AVR PROGMEM extension for string define
|
|
|
|
#define PGMSTR(NAM,STR) const char NAM[] PROGMEM = STR
|
|
|
|
|
2017-06-18 00:36:10 +01:00
|
|
|
#ifndef CRITICAL_SECTION_START
|
2020-02-11 01:13:02 -06:00
|
|
|
#define CRITICAL_SECTION_START() unsigned char _sreg = SREG; cli()
|
|
|
|
#define CRITICAL_SECTION_END() SREG = _sreg
|
2017-06-18 00:36:10 +01:00
|
|
|
#endif
|
2018-06-01 21:02:22 -03:00
|
|
|
#define ISRS_ENABLED() TEST(SREG, SREG_I)
|
|
|
|
#define ENABLE_ISRS() sei()
|
|
|
|
#define DISABLE_ISRS() cli()
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
// Types
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2017-10-26 13:37:26 -05:00
|
|
|
typedef int8_t pin_t;
|
|
|
|
|
2019-06-26 07:40:29 +02:00
|
|
|
#define SHARED_SERVOS HAS_SERVOS
|
2017-06-18 00:36:10 +01:00
|
|
|
#define HAL_SERVO_LIB Servo
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
// Public Variables
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
|
|
|
|
//extern uint8_t MCUSR;
|
|
|
|
|
2018-10-03 02:47:27 -03:00
|
|
|
// Serial ports
|
2017-11-05 08:49:38 -06:00
|
|
|
#ifdef USBCON
|
|
|
|
#if ENABLED(BLUETOOTH)
|
|
|
|
#define MYSERIAL0 bluetoothSerial
|
|
|
|
#else
|
|
|
|
#define MYSERIAL0 Serial
|
|
|
|
#endif
|
2018-10-03 02:47:27 -03:00
|
|
|
#define NUM_SERIAL 1
|
2017-11-05 08:49:38 -06:00
|
|
|
#else
|
2018-10-03 02:47:27 -03:00
|
|
|
#if !WITHIN(SERIAL_PORT, -1, 3)
|
2020-01-02 17:59:06 -06:00
|
|
|
#error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
|
2018-10-03 02:47:27 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MYSERIAL0 customizedSerial1
|
|
|
|
|
|
|
|
#ifdef SERIAL_PORT_2
|
|
|
|
#if !WITHIN(SERIAL_PORT_2, -1, 3)
|
2020-01-02 17:59:06 -06:00
|
|
|
#error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
|
2018-10-03 02:47:27 -03:00
|
|
|
#elif SERIAL_PORT_2 == SERIAL_PORT
|
2020-01-02 17:59:06 -06:00
|
|
|
#error "SERIAL_PORT_2 must be different than SERIAL_PORT. Please update your configuration."
|
2018-10-03 02:47:27 -03:00
|
|
|
#endif
|
|
|
|
#define MYSERIAL1 customizedSerial2
|
2020-01-02 17:59:06 -06:00
|
|
|
#define NUM_SERIAL 2
|
2018-10-03 02:47:27 -03:00
|
|
|
#else
|
|
|
|
#define NUM_SERIAL 1
|
|
|
|
#endif
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
|
|
|
|
2020-01-04 11:00:44 +08:00
|
|
|
#ifdef DGUS_SERIAL_PORT
|
|
|
|
#if !WITHIN(DGUS_SERIAL_PORT, -1, 3)
|
|
|
|
#error "DGUS_SERIAL_PORT must be from -1 to 3. Please update your configuration."
|
|
|
|
#elif DGUS_SERIAL_PORT == SERIAL_PORT
|
|
|
|
#error "DGUS_SERIAL_PORT must be different than SERIAL_PORT. Please update your configuration."
|
|
|
|
#elif defined(SERIAL_PORT_2) && DGUS_SERIAL_PORT == SERIAL_PORT_2
|
|
|
|
#error "DGUS_SERIAL_PORT must be different than SERIAL_PORT_2. Please update your configuration."
|
|
|
|
#endif
|
|
|
|
#define DGUS_SERIAL internalDgusSerial
|
|
|
|
|
|
|
|
#define DGUS_SERIAL_GET_TX_BUFFER_FREE DGUS_SERIAL.get_tx_buffer_free
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
// Public functions
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_init();
|
2019-06-27 16:29:17 -05:00
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
//void cli();
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2017-09-27 04:57:33 -05:00
|
|
|
//void _delay_ms(const int delay);
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
inline void HAL_clear_reset_source() { MCUSR = 0; }
|
|
|
|
inline uint8_t HAL_get_reset_source() { return MCUSR; }
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2019-08-06 04:46:30 -05:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-function"
|
2017-06-18 00:36:10 +01:00
|
|
|
extern "C" {
|
2019-09-16 20:31:08 -05:00
|
|
|
int freeMemory();
|
2017-06-18 00:36:10 +01:00
|
|
|
}
|
2019-08-06 04:46:30 -05:00
|
|
|
#pragma GCC diagnostic pop
|
2017-06-18 00:36:10 +01:00
|
|
|
|
|
|
|
// ADC
|
|
|
|
#ifdef DIDR2
|
2020-01-17 02:39:22 -06:00
|
|
|
#define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
|
2017-06-18 00:36:10 +01:00
|
|
|
#else
|
2020-01-17 02:39:22 -06:00
|
|
|
#define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
|
2017-06-18 00:36:10 +01:00
|
|
|
#endif
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
inline void HAL_adc_init() {
|
2017-06-18 00:36:10 +01:00
|
|
|
ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
|
|
|
|
DIDR0 = 0;
|
|
|
|
#ifdef DIDR2
|
|
|
|
DIDR2 = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-17 02:39:22 -06:00
|
|
|
#define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
|
2017-06-18 00:36:10 +01:00
|
|
|
#ifdef MUX5
|
2020-01-17 02:39:22 -06:00
|
|
|
#define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
|
2017-06-18 00:36:10 +01:00
|
|
|
#else
|
2020-01-17 02:39:22 -06:00
|
|
|
#define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
|
2017-06-18 00:36:10 +01:00
|
|
|
#endif
|
|
|
|
|
2019-11-07 02:49:17 +03:00
|
|
|
#define HAL_ADC_RESOLUTION 10
|
2018-07-26 09:59:19 +01:00
|
|
|
#define HAL_READ_ADC() ADC
|
|
|
|
#define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
|
2017-06-18 00:36:10 +01:00
|
|
|
|
2017-10-26 13:37:26 -05:00
|
|
|
#define GET_PIN_MAP_PIN(index) index
|
|
|
|
#define GET_PIN_MAP_INDEX(pin) pin
|
|
|
|
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
|
|
|
|
|
|
|
|
#define HAL_SENSITIVE_PINS 0, 1
|
|
|
|
|
2018-09-28 02:02:50 +03:00
|
|
|
#ifdef __AVR_AT90USB1286__
|
|
|
|
#define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
|
|
|
|
#endif
|
|
|
|
|
2018-07-01 17:20:28 -03:00
|
|
|
// AVR compatibility
|
|
|
|
#define strtof strtod
|
2019-03-26 06:03:23 +00:00
|
|
|
|
2020-04-03 01:31:08 +01:00
|
|
|
#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
|
|
|
|
|
2019-03-26 06:03:23 +00:00
|
|
|
/**
|
|
|
|
* set_pwm_frequency
|
|
|
|
* Sets the frequency of the timer corresponding to the provided pin
|
|
|
|
* as close as possible to the provided desired frequency. Internally
|
|
|
|
* calculates the required waveform generation mode, prescaler and
|
|
|
|
* resolution values required and sets the timer registers accordingly.
|
|
|
|
* NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
|
|
|
|
* NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
|
|
|
|
*/
|
|
|
|
void set_pwm_frequency(const pin_t pin, int f_desired);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_pwm_duty
|
|
|
|
* Sets the PWM duty cycle of the provided pin to the provided value
|
|
|
|
* Optionally allows inverting the duty cycle [default = false]
|
|
|
|
* Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
|
|
|
|
*/
|
|
|
|
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
|