2018-04-05 03:13:27 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-04-05 03:13:27 +02: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
|
2020-07-23 05:20:14 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-07-28 08:04:44 +02:00
|
|
|
*
|
2018-04-05 03:13:27 +02:00
|
|
|
*/
|
2017-07-19 01:29:06 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-03 00:05:45 +02:00
|
|
|
* HAL for Arduino Due and compatible (SAM3X8E)
|
2017-07-19 01:29:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_SAM
|
|
|
|
|
2018-10-03 07:47:27 +02:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2018-04-13 03:25:08 +02:00
|
|
|
#include "HAL.h"
|
2017-07-19 01:29:06 +02:00
|
|
|
|
|
|
|
#include <Wire.h>
|
2018-03-22 01:04:45 +01:00
|
|
|
#include "usb/usb_task.h"
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2019-07-10 05:30:06 +02:00
|
|
|
// ------------------------
|
2017-07-19 01:29:06 +02:00
|
|
|
// Public Variables
|
2019-07-10 05:30:06 +02:00
|
|
|
// ------------------------
|
2017-07-19 01:29:06 +02:00
|
|
|
|
|
|
|
uint16_t HAL_adc_result;
|
|
|
|
|
2019-07-10 05:30:06 +02:00
|
|
|
// ------------------------
|
2017-07-19 01:29:06 +02:00
|
|
|
// Public functions
|
2019-07-10 05:30:06 +02:00
|
|
|
// ------------------------
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2021-02-21 03:22:20 +01:00
|
|
|
TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
|
|
|
|
|
2018-03-22 01:04:45 +01:00
|
|
|
// HAL initialization task
|
2019-09-17 03:31:08 +02:00
|
|
|
void HAL_init() {
|
2018-03-22 01:04:45 +01:00
|
|
|
// Initialize the USB stack
|
2018-04-05 03:13:27 +02:00
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
|
|
|
|
#endif
|
2018-03-22 01:04:45 +01:00
|
|
|
usb_task_init();
|
2021-02-21 03:22:20 +01:00
|
|
|
TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler
|
2018-03-22 01:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// HAL idle task
|
2019-09-17 03:31:08 +02:00
|
|
|
void HAL_idletask() {
|
2018-03-22 01:04:45 +01:00
|
|
|
// Perform USB stack housekeeping
|
|
|
|
usb_task_idle();
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:13:27 +02:00
|
|
|
// Disable interrupts
|
2019-09-17 03:31:08 +02:00
|
|
|
void cli() { noInterrupts(); }
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2018-04-05 03:13:27 +02:00
|
|
|
// Enable interrupts
|
2019-09-17 03:31:08 +02:00
|
|
|
void sei() { interrupts(); }
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
void HAL_clear_reset_source() { }
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
uint8_t HAL_get_reset_source() {
|
2018-03-22 01:04:45 +01:00
|
|
|
switch ((RSTC->RSTC_SR >> 8) & 0x07) {
|
|
|
|
case 0: return RST_POWER_ON;
|
|
|
|
case 1: return RST_BACKUP;
|
|
|
|
case 2: return RST_WATCHDOG;
|
|
|
|
case 3: return RST_SOFTWARE;
|
|
|
|
case 4: return RST_EXTERNAL;
|
|
|
|
default: return 0;
|
2017-07-19 01:29:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 09:53:52 +02:00
|
|
|
void HAL_reboot() { rstc_start_software_reset(RSTC); }
|
|
|
|
|
2017-09-27 11:57:33 +02:00
|
|
|
void _delay_ms(const int delay_ms) {
|
2018-04-05 03:13:27 +02:00
|
|
|
// Todo: port for Due?
|
2017-09-01 00:30:43 +02:00
|
|
|
delay(delay_ms);
|
2017-07-19 01:29:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
extern unsigned int _ebss; // end of bss section
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:13:27 +02:00
|
|
|
// Return free memory between end of heap (or end bss) and whatever is current
|
2017-07-19 01:29:06 +02:00
|
|
|
int freeMemory() {
|
|
|
|
int free_memory, heap_end = (int)_sbrk(0);
|
2019-10-02 02:59:48 +02:00
|
|
|
return (int)&free_memory - (heap_end ?: (int)&_ebss);
|
2017-07-19 01:29:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 05:30:06 +02:00
|
|
|
// ------------------------
|
2017-07-19 01:29:06 +02:00
|
|
|
// ADC
|
2019-07-10 05:30:06 +02:00
|
|
|
// ------------------------
|
2017-07-19 01:29:06 +02:00
|
|
|
|
2020-01-17 09:39:22 +01:00
|
|
|
void HAL_adc_start_conversion(const uint8_t ch) {
|
|
|
|
HAL_adc_result = analogRead(ch);
|
2017-07-19 01:29:06 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 03:31:08 +02:00
|
|
|
uint16_t HAL_adc_get_result() {
|
2017-07-19 01:29:06 +02:00
|
|
|
// nop
|
|
|
|
return HAL_adc_result;
|
|
|
|
}
|
|
|
|
|
2021-02-14 04:04:22 +01:00
|
|
|
// Forward the default serial ports
|
2021-03-25 22:19:03 +01:00
|
|
|
#if USING_HW_SERIAL0
|
2021-03-10 19:05:05 +01:00
|
|
|
DefaultSerial1 MSerial0(false, Serial);
|
2021-02-14 04:04:22 +01:00
|
|
|
#endif
|
2021-03-25 22:19:03 +01:00
|
|
|
#if USING_HW_SERIAL1
|
2021-03-10 19:05:05 +01:00
|
|
|
DefaultSerial2 MSerial1(false, Serial1);
|
2021-02-14 04:04:22 +01:00
|
|
|
#endif
|
2021-03-25 22:19:03 +01:00
|
|
|
#if USING_HW_SERIAL2
|
2021-03-10 19:05:05 +01:00
|
|
|
DefaultSerial3 MSerial2(false, Serial2);
|
2021-02-14 04:04:22 +01:00
|
|
|
#endif
|
2021-03-25 22:19:03 +01:00
|
|
|
#if USING_HW_SERIAL3
|
2021-03-10 19:05:05 +01:00
|
|
|
DefaultSerial4 MSerial3(false, Serial3);
|
2021-02-14 04:04:22 +01:00
|
|
|
#endif
|
2021-01-28 09:02:06 +01:00
|
|
|
|
2017-07-19 01:29:06 +02:00
|
|
|
#endif // ARDUINO_ARCH_SAM
|