2017-06-18 01:36:10 +02:00
|
|
|
/* **************************************************************************
|
|
|
|
|
|
|
|
Marlin 3D Printer Firmware
|
|
|
|
Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description: HAL for AVR
|
|
|
|
*
|
2017-09-24 06:25:28 +02:00
|
|
|
* For __AVR__
|
2017-06-18 01:36:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-10-15 02:05:10 +02:00
|
|
|
#ifndef _HAL_AVR_H_
|
|
|
|
#define _HAL_AVR_H_
|
2017-06-18 01:36:10 +02:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Includes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2017-11-19 20:57:07 +01:00
|
|
|
#include <Arduino.h>
|
2017-06-18 01:36:10 +02:00
|
|
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
#include <avr/eeprom.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
2018-04-13 03:25:08 +02:00
|
|
|
#include "../HAL_SPI.h"
|
2017-06-18 01:36:10 +02:00
|
|
|
#include "fastio_AVR.h"
|
|
|
|
#include "watchdog_AVR.h"
|
|
|
|
#include "math_AVR.h"
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
#ifdef USBCON
|
|
|
|
#include "HardwareSerial.h"
|
|
|
|
#else
|
|
|
|
#include "MarlinSerial.h"
|
|
|
|
#endif
|
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Defines
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//#define analogInputToDigitalPin(IO) IO
|
|
|
|
|
|
|
|
#ifndef CRITICAL_SECTION_START
|
|
|
|
#define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli();
|
|
|
|
#define CRITICAL_SECTION_END SREG = _sreg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// On AVR this is in math.h?
|
|
|
|
//#define square(x) ((x)*(x))
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2017-11-06 02:31:07 +01:00
|
|
|
typedef uint16_t hal_timer_t;
|
2017-06-18 01:36:10 +02:00
|
|
|
#define HAL_TIMER_TYPE_MAX 0xFFFF
|
|
|
|
|
2017-10-26 20:37:26 +02:00
|
|
|
typedef int8_t pin_t;
|
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
#define HAL_SERVO_LIB Servo
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//extern uint8_t MCUSR;
|
|
|
|
|
2017-11-05 15:49:38 +01:00
|
|
|
#define NUM_SERIAL 1
|
|
|
|
|
|
|
|
#ifdef USBCON
|
|
|
|
#if ENABLED(BLUETOOTH)
|
|
|
|
#define MYSERIAL0 bluetoothSerial
|
|
|
|
#else
|
|
|
|
#define MYSERIAL0 Serial
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define MYSERIAL0 customizedSerial
|
|
|
|
#endif
|
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//void cli(void);
|
|
|
|
|
2017-09-27 11:57:33 +02:00
|
|
|
//void _delay_ms(const int delay);
|
2017-06-18 01:36:10 +02:00
|
|
|
|
|
|
|
inline void HAL_clear_reset_source(void) { MCUSR = 0; }
|
|
|
|
inline uint8_t HAL_get_reset_source(void) { return MCUSR; }
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
int freeMemory(void);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eeprom
|
|
|
|
//void eeprom_write_byte(unsigned char *pos, unsigned char value);
|
|
|
|
//unsigned char eeprom_read_byte(unsigned char *pos);
|
|
|
|
|
|
|
|
// timers
|
2017-10-07 20:34:25 +02:00
|
|
|
#define HAL_TIMER_RATE ((F_CPU) / 8) // i.e., 2MHz or 2.5MHz
|
2017-12-10 02:57:25 +01:00
|
|
|
|
2018-05-13 23:48:02 +02:00
|
|
|
#define STEP_TIMER_NUM 1
|
|
|
|
#define TEMP_TIMER_NUM 0
|
|
|
|
#define PULSE_TIMER_NUM TEMP_TIMER_NUM
|
2017-12-10 02:57:25 +01:00
|
|
|
|
2017-10-07 20:34:25 +02:00
|
|
|
#define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE
|
2018-05-13 23:48:02 +02:00
|
|
|
#define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double
|
2017-12-10 03:00:10 +01:00
|
|
|
#define STEPPER_TIMER_PRESCALE 8
|
2018-03-08 00:53:25 +01:00
|
|
|
#define STEP_TIMER_MIN_INTERVAL 8 // minimum time in µs between stepper interrupts
|
|
|
|
|
2018-05-13 23:48:02 +02:00
|
|
|
#define TEMP_TIMER_FREQUENCY ((F_CPU) / 64.0 / 256.0)
|
|
|
|
|
2017-12-10 02:57:25 +01:00
|
|
|
#define TIMER_OCR_1 OCR1A
|
|
|
|
#define TIMER_COUNTER_1 TCNT1
|
|
|
|
|
|
|
|
#define TIMER_OCR_0 OCR0A
|
2017-12-10 02:59:12 +01:00
|
|
|
#define TIMER_COUNTER_0 TCNT0
|
2017-12-10 02:57:25 +01:00
|
|
|
|
2017-12-10 02:59:12 +01:00
|
|
|
#define PULSE_TIMER_PRESCALE 8
|
|
|
|
|
2017-06-18 01:36:10 +02:00
|
|
|
#define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
|
|
|
|
#define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
|
2018-04-24 05:05:07 +02:00
|
|
|
#define STEPPER_ISR_ENABLED() TEST(TIMSK1, OCIE1A)
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2018-04-24 05:05:07 +02:00
|
|
|
#define ENABLE_TEMPERATURE_INTERRUPT() SBI(TIMSK0, OCIE0B)
|
|
|
|
#define DISABLE_TEMPERATURE_INTERRUPT() CBI(TIMSK0, OCIE0B)
|
|
|
|
#define TEMPERATURE_ISR_ENABLED() TEST(TIMSK0, OCIE0B)
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2017-12-10 02:57:25 +01:00
|
|
|
#define HAL_timer_start(timer_num, frequency)
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2017-12-10 02:57:25 +01:00
|
|
|
#define _CAT(a, ...) a ## __VA_ARGS__
|
2018-02-11 03:42:00 +01:00
|
|
|
#define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
|
2018-03-08 05:18:13 +01:00
|
|
|
#define HAL_timer_restrain(timer, interval_ticks) NOLESS(_CAT(TIMER_OCR_, timer), _CAT(TIMER_COUNTER_, timer) + interval_ticks)
|
2018-03-08 00:53:25 +01:00
|
|
|
|
2018-02-11 03:42:00 +01:00
|
|
|
#define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
|
|
|
|
#define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2018-04-24 05:05:07 +02:00
|
|
|
/**
|
|
|
|
* On AVR there is no hardware prioritization and preemption of
|
|
|
|
* interrupts, so this emulates it. The UART has first priority
|
|
|
|
* (otherwise, characters will be lost due to UART overflow).
|
|
|
|
* Then: Stepper, Endstops, Temperature, and -finally- all others.
|
|
|
|
*/
|
|
|
|
#define HAL_timer_isr_prologue_0 do{ DISABLE_TEMPERATURE_INTERRUPT(); sei(); }while(0)
|
|
|
|
#define HAL_timer_isr_epilogue_0 do{ cli(); ENABLE_TEMPERATURE_INTERRUPT(); }while(0)
|
|
|
|
|
|
|
|
#define HAL_timer_isr_prologue_1 \
|
|
|
|
const bool temp_isr_was_enabled = TEMPERATURE_ISR_ENABLED(); \
|
|
|
|
do{ \
|
|
|
|
DISABLE_TEMPERATURE_INTERRUPT(); \
|
|
|
|
DISABLE_STEPPER_DRIVER_INTERRUPT(); \
|
|
|
|
sei(); \
|
|
|
|
}while(0)
|
|
|
|
|
|
|
|
#define HAL_timer_isr_epilogue_1 do{ cli(); ENABLE_STEPPER_DRIVER_INTERRUPT(); if (temp_isr_was_enabled) ENABLE_TEMPERATURE_INTERRUPT(); }while(0)
|
|
|
|
|
|
|
|
#define HAL_timer_isr_prologue(TIMER_NUM) _CAT(HAL_timer_isr_prologue_, TIMER_NUM)
|
|
|
|
#define HAL_timer_isr_epilogue(TIMER_NUM) _CAT(HAL_timer_isr_epilogue_, TIMER_NUM)
|
2017-06-18 01:36:10 +02:00
|
|
|
|
|
|
|
#define HAL_STEP_TIMER_ISR ISR(TIMER1_COMPA_vect)
|
|
|
|
#define HAL_TEMP_TIMER_ISR ISR(TIMER0_COMPB_vect)
|
|
|
|
|
|
|
|
// ADC
|
|
|
|
#ifdef DIDR2
|
2018-03-14 04:33:08 +01:00
|
|
|
#define HAL_ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin & 0x07); }while(0)
|
2017-06-18 01:36:10 +02:00
|
|
|
#else
|
|
|
|
#define HAL_ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline void HAL_adc_init(void) {
|
|
|
|
ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
|
|
|
|
DIDR0 = 0;
|
|
|
|
#ifdef DIDR2
|
|
|
|
DIDR2 = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
|
|
|
|
#ifdef MUX5
|
|
|
|
#define HAL_START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
|
|
|
|
#else
|
|
|
|
#define HAL_START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HAL_READ_ADC ADC
|
|
|
|
|
2017-10-26 20:37:26 +02: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
|
|
|
|
|
2017-10-15 02:05:10 +02:00
|
|
|
#endif // _HAL_AVR_H_
|