2017-06-17 16:19:42 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
*
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-06-17 16:19:42 -05:00
|
|
|
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
|
|
|
* Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.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
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-06-17 16:19:42 -05:00
|
|
|
*
|
|
|
|
*/
|
2018-11-04 02:25:55 -06:00
|
|
|
#pragma once
|
2017-06-17 16:19:42 -05:00
|
|
|
|
|
|
|
/**
|
2018-02-21 18:43:42 -06:00
|
|
|
* HAL_LPC1768/HAL.h
|
|
|
|
* Hardware Abstraction Layer for NXP LPC1768
|
2017-06-17 16:19:42 -05:00
|
|
|
*/
|
|
|
|
|
2018-04-12 20:25:08 -05:00
|
|
|
#define CPU_32_BIT
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_init();
|
2017-06-17 16:19:42 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2019-07-08 23:42:29 -05:00
|
|
|
extern "C" volatile uint32_t _millis;
|
2017-06-17 16:19:42 -05:00
|
|
|
|
2019-05-02 00:45:50 -05:00
|
|
|
#include "../shared/Marduino.h"
|
2018-08-14 03:28:52 -05:00
|
|
|
#include "../shared/math_32bit.h"
|
|
|
|
#include "../shared/HAL_SPI.h"
|
2017-06-17 16:19:42 -05:00
|
|
|
#include "fastio.h"
|
|
|
|
#include "watchdog.h"
|
2018-08-22 19:08:39 -05:00
|
|
|
#include "MarlinSerial.h"
|
2017-06-17 16:19:42 -05:00
|
|
|
|
2019-05-02 00:45:50 -05:00
|
|
|
#include <adc.h>
|
|
|
|
#include <pinmapping.h>
|
|
|
|
#include <CDCSerial.h>
|
|
|
|
|
2018-09-28 15:57:21 -05:00
|
|
|
//
|
|
|
|
// Default graphical display delays
|
|
|
|
//
|
2018-07-26 23:33:13 -05:00
|
|
|
#ifndef ST7920_DELAY_1
|
|
|
|
#define ST7920_DELAY_1 DELAY_NS(600)
|
|
|
|
#endif
|
|
|
|
#ifndef ST7920_DELAY_2
|
|
|
|
#define ST7920_DELAY_2 DELAY_NS(750)
|
|
|
|
#endif
|
|
|
|
#ifndef ST7920_DELAY_3
|
|
|
|
#define ST7920_DELAY_3 DELAY_NS(750)
|
|
|
|
#endif
|
2017-06-17 16:19:42 -05:00
|
|
|
|
2020-09-23 19:45:33 -05:00
|
|
|
#define _MSERIAL(X) MSerial##X
|
|
|
|
#define MSERIAL(X) _MSERIAL(X)
|
|
|
|
#define MSerial0 MSerial
|
|
|
|
|
2017-10-24 17:28:33 -05:00
|
|
|
#if SERIAL_PORT == -1
|
2018-08-22 19:08:39 -05:00
|
|
|
#define MYSERIAL0 UsbSerial
|
2020-09-23 19:45:33 -05:00
|
|
|
#elif WITHIN(SERIAL_PORT, 0, 3)
|
|
|
|
#define MYSERIAL0 MSERIAL(SERIAL_PORT)
|
2020-01-02 17:59:06 -06:00
|
|
|
#else
|
|
|
|
#error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SERIAL_PORT_2
|
2020-09-23 19:45:33 -05:00
|
|
|
#if SERIAL_PORT_2 == -1
|
2018-08-22 19:08:39 -05:00
|
|
|
#define MYSERIAL1 UsbSerial
|
2020-09-23 19:45:33 -05:00
|
|
|
#elif WITHIN(SERIAL_PORT_2, 0, 3)
|
|
|
|
#define MYSERIAL1 MSERIAL(SERIAL_PORT_2)
|
2020-01-02 17:59:06 -06:00
|
|
|
#else
|
|
|
|
#error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
2017-10-24 17:28:33 -05:00
|
|
|
#endif
|
2017-06-17 16:19:42 -05:00
|
|
|
|
2021-01-15 20:38:34 -06:00
|
|
|
#ifdef MMU2_SERIAL_PORT
|
|
|
|
#if MMU2_SERIAL_PORT == -1
|
|
|
|
#define MMU2_SERIAL UsbSerial
|
|
|
|
#elif WITHIN(MMU2_SERIAL_PORT, 0, 3)
|
|
|
|
#define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
|
|
|
|
#else
|
|
|
|
#error "MMU2_SERIAL_PORT must be from -1 to 3. Please update your configuration."
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-09-24 20:28:48 -05:00
|
|
|
#ifdef LCD_SERIAL_PORT
|
|
|
|
#if LCD_SERIAL_PORT == -1
|
|
|
|
#define LCD_SERIAL UsbSerial
|
|
|
|
#elif WITHIN(LCD_SERIAL_PORT, 0, 3)
|
|
|
|
#define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
|
2020-01-03 21:00:44 -06:00
|
|
|
#else
|
2020-09-24 20:28:48 -05:00
|
|
|
#error "LCD_SERIAL_PORT must be from -1 to 3. Please update your configuration."
|
2020-01-03 21:00:44 -06:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-09-28 15:57:21 -05:00
|
|
|
//
|
|
|
|
// Interrupts
|
|
|
|
//
|
2020-02-11 01:13:02 -06:00
|
|
|
#define CRITICAL_SECTION_START() uint32_t primask = __get_PRIMASK(); __disable_irq()
|
|
|
|
#define CRITICAL_SECTION_END() if (!primask) __enable_irq()
|
2018-06-01 19:02:22 -05:00
|
|
|
#define ISRS_ENABLED() (!__get_PRIMASK())
|
|
|
|
#define ENABLE_ISRS() __enable_irq()
|
|
|
|
#define DISABLE_ISRS() __disable_irq()
|
2017-06-17 16:19:42 -05:00
|
|
|
|
2018-09-28 15:57:21 -05:00
|
|
|
//
|
|
|
|
// Utility functions
|
|
|
|
//
|
2020-11-25 21:40:56 -06:00
|
|
|
#if GCC_VERSION <= 50000
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-function"
|
|
|
|
#endif
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
int freeMemory();
|
2020-11-25 21:40:56 -06:00
|
|
|
|
|
|
|
#if GCC_VERSION <= 50000
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2017-06-17 16:19:42 -05:00
|
|
|
|
2018-09-28 15:57:21 -05:00
|
|
|
//
|
2018-08-22 19:08:39 -05:00
|
|
|
// ADC API
|
2018-09-28 15:57:21 -05:00
|
|
|
//
|
2018-08-22 19:08:39 -05:00
|
|
|
|
|
|
|
#define ADC_MEDIAN_FILTER_SIZE (23) // Higher values increase step delay (phase shift),
|
|
|
|
// (ADC_MEDIAN_FILTER_SIZE + 1) / 2 sample step delay (12 samples @ 500Hz: 24ms phase shift)
|
|
|
|
// Memory usage per ADC channel (bytes): (6 * ADC_MEDIAN_FILTER_SIZE) + 16
|
|
|
|
// 8 * ((6 * 23) + 16 ) = 1232 Bytes for 8 channels
|
|
|
|
|
2019-07-24 18:40:51 -05:00
|
|
|
#define ADC_LOWPASS_K_VALUE (2) // Higher values increase rise time
|
2018-08-22 19:08:39 -05:00
|
|
|
// Rise time sample delays for 100% signal convergence on full range step
|
|
|
|
// (1 : 13, 2 : 32, 3 : 67, 4 : 139, 5 : 281, 6 : 565, 7 : 1135, 8 : 2273)
|
|
|
|
// K = 6, 565 samples, 500Hz sample rate, 1.13s convergence on full range step
|
|
|
|
// Memory usage per ADC channel (bytes): 4 (32 Bytes for 8 channels)
|
|
|
|
|
2020-06-18 15:23:03 -05:00
|
|
|
#define HAL_ADC_VREF 3.3 // ADC voltage reference
|
|
|
|
|
2019-11-10 17:30:28 -06:00
|
|
|
#define HAL_ADC_RESOLUTION 12 // 15 bit maximum, raw temperature is stored as int16_t
|
2019-11-06 17:49:17 -06:00
|
|
|
#define HAL_ADC_FILTERED // Disable oversampling done in Marlin as ADC values already filtered in HAL
|
2019-11-02 21:34:09 -05:00
|
|
|
|
2019-11-06 17:49:17 -06:00
|
|
|
using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
|
2019-11-02 21:34:09 -05:00
|
|
|
extern uint32_t HAL_adc_reading;
|
|
|
|
[[gnu::always_inline]] inline void HAL_start_adc(const pin_t pin) {
|
2019-11-10 17:30:28 -06:00
|
|
|
HAL_adc_reading = FilteredADC::read(pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits
|
2019-11-02 21:34:09 -05:00
|
|
|
}
|
|
|
|
[[gnu::always_inline]] inline uint16_t HAL_read_adc() {
|
|
|
|
return HAL_adc_reading;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HAL_adc_init()
|
2018-08-22 19:08:39 -05:00
|
|
|
#define HAL_ANALOG_SELECT(pin) FilteredADC::enable_channel(pin)
|
2019-11-02 21:34:09 -05:00
|
|
|
#define HAL_START_ADC(pin) HAL_start_adc(pin)
|
|
|
|
#define HAL_READ_ADC() HAL_read_adc()
|
|
|
|
#define HAL_ADC_READY() (true)
|
|
|
|
|
|
|
|
// Test whether the pin is valid
|
|
|
|
constexpr bool VALID_PIN(const pin_t pin) {
|
|
|
|
return LPC176x::pin_is_valid(pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the analog index for a digital pin
|
|
|
|
constexpr int8_t DIGITAL_PIN_TO_ANALOG_PIN(const pin_t pin) {
|
|
|
|
return (LPC176x::pin_is_valid(pin) && LPC176x::pin_has_adc(pin)) ? pin : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the index of a pin number
|
|
|
|
constexpr int16_t GET_PIN_MAP_INDEX(const pin_t pin) {
|
|
|
|
return LPC176x::pin_index(pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pin number at the given index
|
|
|
|
constexpr pin_t GET_PIN_MAP_PIN(const int16_t index) {
|
|
|
|
return LPC176x::pin_index(index);
|
|
|
|
}
|
2018-08-22 19:08:39 -05:00
|
|
|
|
|
|
|
// Parse a G-code word into a pin index
|
|
|
|
int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
|
2018-09-21 05:24:53 -05:00
|
|
|
// P0.6 thru P0.9 are for the onboard SD card
|
|
|
|
#define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09
|
2017-06-17 16:19:42 -05:00
|
|
|
|
2018-09-19 12:33:20 -05:00
|
|
|
#define HAL_IDLETASK 1
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_idletask();
|
2019-03-01 20:11:50 -06:00
|
|
|
|
|
|
|
#define PLATFORM_M997_SUPPORT
|
2020-03-23 17:11:00 -05:00
|
|
|
void flashFirmware(const int16_t);
|
2019-03-26 01:03:23 -05:00
|
|
|
|
2020-04-02 19:31:08 -05:00
|
|
|
#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
|
|
|
|
|
2019-03-26 01:03:23 -05:00
|
|
|
/**
|
|
|
|
* set_pwm_frequency
|
|
|
|
* Set the frequency of the timer corresponding to the provided pin
|
|
|
|
* All Hardware PWM pins run at the same frequency and all
|
|
|
|
* Software PWM pins run at the same frequency
|
|
|
|
*/
|
|
|
|
void set_pwm_frequency(const pin_t pin, int f_desired);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_pwm_duty
|
|
|
|
* Set 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);
|
2019-09-29 17:57:29 -05:00
|
|
|
|
|
|
|
// Reset source
|
|
|
|
void HAL_clear_reset_source(void);
|
|
|
|
uint8_t HAL_get_reset_source(void);
|
2020-05-25 15:36:37 -05:00
|
|
|
|
2020-10-09 06:25:23 -05:00
|
|
|
inline void HAL_reboot() {} // reboot the board or restart the bootloader
|
|
|
|
|
2020-05-25 15:36:37 -05:00
|
|
|
// Add strcmp_P if missing
|
|
|
|
#ifndef strcmp_P
|
|
|
|
#define strcmp_P(a, b) strcmp((a), (b))
|
|
|
|
#endif
|
2020-12-07 05:06:09 -06:00
|
|
|
|
|
|
|
#ifndef strcat_P
|
|
|
|
#define strcat_P(a, b) strcat((a), (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef strcpy_P
|
|
|
|
#define strcpy_P(a, b) strcpy((a), (b))
|
|
|
|
#endif
|