2017-10-08 11:38:10 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-10-08 11:38:10 -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
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-09-28 14:53:50 -05:00
|
|
|
*
|
2017-10-08 11:38:10 -05:00
|
|
|
*/
|
2018-11-04 02:25:55 -06:00
|
|
|
#pragma once
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
/**
|
2020-10-02 17:05:45 -05:00
|
|
|
* HAL for Espressif ESP32 WiFi
|
2017-10-08 11:38:10 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define CPU_32_BIT
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
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-10-08 11:38:10 -05:00
|
|
|
|
2019-09-02 19:49:58 -05:00
|
|
|
#include "fastio.h"
|
|
|
|
#include "watchdog.h"
|
2019-02-10 05:40:31 -06:00
|
|
|
#include "i2s.h"
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2020-01-10 17:15:05 -06:00
|
|
|
#if ENABLED(WIFISUPPORT)
|
|
|
|
#include "WebSocketSerial.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(ESP3D_WIFISUPPORT)
|
|
|
|
#include "esp3dlib.h"
|
|
|
|
#endif
|
|
|
|
|
2019-04-05 19:04:02 -05:00
|
|
|
#include "FlushableHardwareSerial.h"
|
2019-03-13 00:48:08 -05:00
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
// Defines
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
extern portMUX_TYPE spinlock;
|
|
|
|
|
2019-04-05 19:04:02 -05:00
|
|
|
#define MYSERIAL0 flushableSerial
|
2019-03-13 05:25:54 -05:00
|
|
|
|
2020-01-10 17:15:05 -06:00
|
|
|
#if EITHER(WIFISUPPORT, ESP3D_WIFISUPPORT)
|
|
|
|
#if ENABLED(ESP3D_WIFISUPPORT)
|
|
|
|
#define MYSERIAL1 Serial2Socket
|
|
|
|
#else
|
|
|
|
#define MYSERIAL1 webSocketSerial
|
|
|
|
#endif
|
2019-03-13 05:25:54 -05:00
|
|
|
#endif
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2020-02-11 01:13:02 -06:00
|
|
|
#define CRITICAL_SECTION_START() portENTER_CRITICAL(&spinlock)
|
|
|
|
#define CRITICAL_SECTION_END() portEXIT_CRITICAL(&spinlock)
|
2017-10-08 11:38:10 -05:00
|
|
|
#define ISRS_ENABLED() (spinlock.owner == portMUX_FREE_VAL)
|
|
|
|
#define ENABLE_ISRS() if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock)
|
|
|
|
#define DISABLE_ISRS() portENTER_CRITICAL(&spinlock)
|
|
|
|
|
|
|
|
// Fix bug in pgm_read_ptr
|
|
|
|
#undef pgm_read_ptr
|
|
|
|
#define pgm_read_ptr(addr) (*(addr))
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
// Types
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
typedef int16_t pin_t;
|
|
|
|
|
2019-05-25 18:12:24 -05:00
|
|
|
#define HAL_SERVO_LIB Servo
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
// Public Variables
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
/** result of last ADC conversion */
|
|
|
|
extern uint16_t HAL_adc_result;
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
// Public functions
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
// clear reset reason
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_clear_reset_source();
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
// reset reason
|
2019-09-16 20:31:08 -05:00
|
|
|
uint8_t HAL_get_reset_source();
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2020-10-09 06:25:23 -05:00
|
|
|
inline void HAL_reboot() {} // reboot the board or restart the bootloader
|
|
|
|
|
2017-10-08 11:38:10 -05:00
|
|
|
void _delay_ms(int delay);
|
|
|
|
|
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-10-08 11:38:10 -05:00
|
|
|
|
2019-07-02 04:04:49 -05:00
|
|
|
void analogWrite(pin_t pin, int value);
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
// ADC
|
|
|
|
#define HAL_ANALOG_SELECT(pin)
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_adc_init();
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2020-06-18 15:23:03 -05:00
|
|
|
#define HAL_ADC_VREF 3.3
|
2019-11-06 17:49:17 -06:00
|
|
|
#define HAL_ADC_RESOLUTION 10
|
2020-06-18 15:23:03 -05:00
|
|
|
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
|
2018-07-26 03:59:19 -05:00
|
|
|
#define HAL_READ_ADC() HAL_adc_result
|
|
|
|
#define HAL_ADC_READY() true
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2020-01-17 02:39:22 -06:00
|
|
|
void HAL_adc_start_conversion(const uint8_t adc_pin);
|
2017-10-08 11:38:10 -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)
|
|
|
|
|
|
|
|
// Enable hooks into idle and setup for HAL
|
|
|
|
#define HAL_IDLETASK 1
|
2019-05-25 17:06:00 -05:00
|
|
|
#define BOARD_INIT() HAL_init_board();
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_idletask();
|
|
|
|
void HAL_init();
|
|
|
|
void HAL_init_board();
|
2020-01-16 05:57:14 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Delay in cycles (used by DELAY_NS / DELAY_US)
|
|
|
|
//
|
|
|
|
FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
|
|
|
|
unsigned long start, ccount, stop;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It's important to care for race conditions (and overflows) here.
|
|
|
|
* Race condition example: If `stop` calculates to being close to the upper boundary of
|
|
|
|
* `uint32_t` and if at the same time a longer loop interruption kicks in (e.g. due to other
|
|
|
|
* FreeRTOS tasks or interrupts), `ccount` might overflow (and therefore be below `stop` again)
|
|
|
|
* without the loop ever being able to notice that `ccount` had already been above `stop` once
|
|
|
|
* (and that therefore the number of cycles to delay has already passed).
|
|
|
|
* As DELAY_CYCLES (through DELAY_NS / DELAY_US) is used by software SPI bit banging to drive
|
|
|
|
* LCDs and therefore might be called very, very often, this seemingly improbable situation did
|
|
|
|
* actually happen in reality. It resulted in apparently random print pauses of ~17.9 seconds
|
|
|
|
* (0x100000000 / 240 MHz) or multiples thereof, essentially ruining the current print by causing
|
|
|
|
* large blobs of filament.
|
|
|
|
*/
|
|
|
|
|
|
|
|
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (start) );
|
|
|
|
stop = start + x;
|
|
|
|
ccount = start;
|
|
|
|
|
|
|
|
if (stop >= start) {
|
|
|
|
// no overflow, so only loop while in between start and stop:
|
2020-10-09 16:50:17 -05:00
|
|
|
// 0x00000000 -----------------start****stop-- 0xFFFFFFFF
|
2020-01-16 05:57:14 -06:00
|
|
|
while (ccount >= start && ccount < stop) {
|
|
|
|
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// stop did overflow, so only loop while outside of stop and start:
|
2020-10-09 16:50:17 -05:00
|
|
|
// 0x00000000 **stop-------------------start** 0xFFFFFFFF
|
2020-01-16 05:57:14 -06:00
|
|
|
while (ccount >= start || ccount < stop) {
|
|
|
|
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|