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
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-10-08 11:38:10 -05:00
|
|
|
*
|
|
|
|
* 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-10-08 11:38:10 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
|
2020-06-01 18:33:30 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
|
2017-10-08 11:38:10 -05:00
|
|
|
#include <rom/rtc.h>
|
|
|
|
#include <driver/adc.h>
|
|
|
|
#include <esp_adc_cal.h>
|
2020-02-22 04:00:10 -06:00
|
|
|
#include <HardwareSerial.h>
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
#if ENABLED(WIFISUPPORT)
|
2019-03-13 00:48:08 -05:00
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
#include "wifi.h"
|
|
|
|
#if ENABLED(OTASUPPORT)
|
|
|
|
#include "ota.h"
|
|
|
|
#endif
|
|
|
|
#if ENABLED(WEBSUPPORT)
|
2020-01-10 17:15:05 -06:00
|
|
|
#include "spiffs.h"
|
2019-03-13 00:48:08 -05:00
|
|
|
#include "web.h"
|
|
|
|
#endif
|
2017-10-08 11:38:10 -05:00
|
|
|
#endif
|
|
|
|
|
2021-01-28 02:02:06 -06:00
|
|
|
#if ENABLED(ESP3D_WIFISUPPORT)
|
|
|
|
DefaultSerial MSerial(false, Serial2Socket);
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2019-07-20 14:40:49 -05:00
|
|
|
// Externs
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
// Local defines
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
#define V_REF 1100
|
|
|
|
|
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
|
|
|
|
|
|
|
uint16_t HAL_adc_result;
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
// Private Variables
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2019-07-14 23:32:48 -05:00
|
|
|
esp_adc_cal_characteristics_t characteristics[ADC_ATTEN_MAX];
|
|
|
|
adc_atten_t attenuations[ADC1_CHANNEL_MAX] = {};
|
|
|
|
uint32_t thresholds[ADC_ATTEN_MAX];
|
2019-07-14 10:08:14 -05:00
|
|
|
volatile int numPWMUsed = 0,
|
|
|
|
pwmPins[MAX_PWM_PINS],
|
|
|
|
pwmValues[MAX_PWM_PINS];
|
2017-10-08 11:38:10 -05:00
|
|
|
|
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
|
|
|
|
2020-01-10 17:15:05 -06:00
|
|
|
#if ENABLED(WIFI_CUSTOM_COMMAND)
|
|
|
|
|
|
|
|
bool wifi_custom_command(char * const command_ptr) {
|
|
|
|
#if ENABLED(ESP3D_WIFISUPPORT)
|
|
|
|
return esp3dlib.parse(command_ptr);
|
|
|
|
#else
|
|
|
|
UNUSED(command_ptr);
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_init_board() {
|
2019-05-25 17:06:00 -05:00
|
|
|
|
2020-01-10 17:15:05 -06:00
|
|
|
#if ENABLED(ESP3D_WIFISUPPORT)
|
|
|
|
esp3dlib.init();
|
|
|
|
#elif ENABLED(WIFISUPPORT)
|
2019-03-13 00:48:08 -05:00
|
|
|
wifi_init();
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(OTASUPPORT, OTA_init());
|
2019-03-13 00:48:08 -05:00
|
|
|
#if ENABLED(WEBSUPPORT)
|
2020-01-10 17:15:05 -06:00
|
|
|
spiffs_init();
|
2019-03-13 00:48:08 -05:00
|
|
|
web_init();
|
|
|
|
#endif
|
|
|
|
server.begin();
|
2017-10-08 11:38:10 -05:00
|
|
|
#endif
|
2020-02-22 04:00:10 -06:00
|
|
|
|
|
|
|
// ESP32 uses a GPIO matrix that allows pins to be assigned to hardware serial ports.
|
|
|
|
// The following code initializes hardware Serial1 and Serial2 to use user-defined pins
|
|
|
|
// if they have been defined.
|
|
|
|
#if defined(HARDWARE_SERIAL1_RX) && defined(HARDWARE_SERIAL1_TX)
|
|
|
|
HardwareSerial Serial1(1);
|
|
|
|
#ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
|
|
|
|
Serial1.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
|
|
|
|
#else // use default BAUDRATE if TMC_BAUD_RATE not defined
|
|
|
|
Serial1.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if defined(HARDWARE_SERIAL2_RX) && defined(HARDWARE_SERIAL2_TX)
|
|
|
|
HardwareSerial Serial2(2);
|
|
|
|
#ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
|
|
|
|
Serial2.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
|
|
|
|
#else // use default BAUDRATE if TMC_BAUD_RATE not defined
|
|
|
|
Serial2.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-02-07 16:56:39 -06:00
|
|
|
// Initialize the i2s peripheral only if the I2S stepper stream is enabled.
|
|
|
|
// The following initialization is performed after Serial1 and Serial2 are defined as
|
|
|
|
// their native pins might conflict with the i2s stream even when they are remapped.
|
|
|
|
TERN_(I2S_STEPPER_STREAM, i2s_init());
|
2017-10-08 11:38:10 -05:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_idletask() {
|
2020-01-10 17:15:05 -06:00
|
|
|
#if BOTH(WIFISUPPORT, OTASUPPORT)
|
2017-10-08 11:38:10 -05:00
|
|
|
OTA_handle();
|
|
|
|
#endif
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(ESP3D_WIFISUPPORT, esp3dlib.idletask());
|
2017-10-08 11:38:10 -05:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_clear_reset_source() { }
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
uint8_t HAL_get_reset_source() { return rtc_get_reset_reason(1); }
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2019-05-25 17:06:00 -05:00
|
|
|
void _delay_ms(int delay_ms) { delay(delay_ms); }
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
// return free memory between end of heap (or end bss) and whatever is current
|
2019-05-25 17:06:00 -05:00
|
|
|
int freeMemory() { return ESP.getFreeHeap(); }
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2017-10-08 11:38:10 -05:00
|
|
|
// ADC
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2019-04-18 13:01:24 -05:00
|
|
|
#define ADC1_CHANNEL(pin) ADC1_GPIO ## pin ## _CHANNEL
|
2017-10-08 11:38:10 -05:00
|
|
|
|
|
|
|
adc1_channel_t get_channel(int pin) {
|
|
|
|
switch (pin) {
|
2019-04-18 13:01:24 -05:00
|
|
|
case 39: return ADC1_CHANNEL(39);
|
|
|
|
case 36: return ADC1_CHANNEL(36);
|
|
|
|
case 35: return ADC1_CHANNEL(35);
|
|
|
|
case 34: return ADC1_CHANNEL(34);
|
|
|
|
case 33: return ADC1_CHANNEL(33);
|
|
|
|
case 32: return ADC1_CHANNEL(32);
|
2017-10-08 11:38:10 -05:00
|
|
|
}
|
|
|
|
return ADC1_CHANNEL_MAX;
|
|
|
|
}
|
|
|
|
|
2019-07-14 23:32:48 -05:00
|
|
|
void adc1_set_attenuation(adc1_channel_t chan, adc_atten_t atten) {
|
|
|
|
if (attenuations[chan] != atten) {
|
|
|
|
adc1_config_channel_atten(chan, atten);
|
|
|
|
attenuations[chan] = atten;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 11:38:10 -05:00
|
|
|
void HAL_adc_init() {
|
|
|
|
// Configure ADC
|
|
|
|
adc1_config_width(ADC_WIDTH_12Bit);
|
2019-05-26 15:24:10 -05:00
|
|
|
|
2019-05-25 17:06:00 -05:00
|
|
|
// Configure channels only if used as (re-)configuring a pin for ADC that is used elsewhere might have adverse effects
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(HAS_TEMP_ADC_0, adc1_set_attenuation(get_channel(TEMP_0_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_ADC_1, adc1_set_attenuation(get_channel(TEMP_1_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_ADC_2, adc1_set_attenuation(get_channel(TEMP_2_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_ADC_3, adc1_set_attenuation(get_channel(TEMP_3_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_ADC_4, adc1_set_attenuation(get_channel(TEMP_4_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_ADC_5, adc1_set_attenuation(get_channel(TEMP_5_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_ADC_6, adc2_set_attenuation(get_channel(TEMP_6_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_ADC_7, adc3_set_attenuation(get_channel(TEMP_7_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_HEATED_BED, adc1_set_attenuation(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db));
|
|
|
|
TERN_(HAS_TEMP_CHAMBER, adc1_set_attenuation(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db));
|
2021-03-06 14:13:28 -06:00
|
|
|
TERN_(HAS_TEMP_COOLER, adc1_set_attenuation(get_channel(TEMP_COOLER_PIN), ADC_ATTEN_11db));
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(FILAMENT_WIDTH_SENSOR, adc1_set_attenuation(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db));
|
2019-04-18 13:01:24 -05:00
|
|
|
|
|
|
|
// Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
|
|
|
|
// That's why we're not setting it up here.
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2019-07-14 23:32:48 -05:00
|
|
|
// Calculate ADC characteristics (i.e., gain and offset factors for each attenuation level)
|
|
|
|
for (int i = 0; i < ADC_ATTEN_MAX; i++) {
|
|
|
|
esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t)i, ADC_WIDTH_BIT_12, V_REF, &characteristics[i]);
|
2019-07-20 14:40:49 -05:00
|
|
|
|
2019-07-14 23:32:48 -05:00
|
|
|
// Change attenuation 100mV below the calibrated threshold
|
|
|
|
thresholds[i] = esp_adc_cal_raw_to_voltage(4095, &characteristics[i]);
|
|
|
|
}
|
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) {
|
2019-07-14 23:32:48 -05:00
|
|
|
const adc1_channel_t chan = get_channel(adc_pin);
|
2017-10-08 11:38:10 -05:00
|
|
|
uint32_t mv;
|
2019-07-14 23:32:48 -05:00
|
|
|
esp_adc_cal_get_voltage((adc_channel_t)chan, &characteristics[attenuations[chan]], &mv);
|
2019-10-15 17:05:32 -05:00
|
|
|
HAL_adc_result = mv * 1023.0 / 3300.0;
|
2019-07-14 23:32:48 -05:00
|
|
|
|
|
|
|
// Change the attenuation level based on the new reading
|
|
|
|
adc_atten_t atten;
|
|
|
|
if (mv < thresholds[ADC_ATTEN_DB_0] - 100)
|
2019-10-15 17:05:32 -05:00
|
|
|
atten = ADC_ATTEN_DB_0;
|
2019-07-14 23:32:48 -05:00
|
|
|
else if (mv > thresholds[ADC_ATTEN_DB_0] - 50 && mv < thresholds[ADC_ATTEN_DB_2_5] - 100)
|
2019-10-15 17:05:32 -05:00
|
|
|
atten = ADC_ATTEN_DB_2_5;
|
2019-07-14 23:32:48 -05:00
|
|
|
else if (mv > thresholds[ADC_ATTEN_DB_2_5] - 50 && mv < thresholds[ADC_ATTEN_DB_6] - 100)
|
2019-10-15 17:05:32 -05:00
|
|
|
atten = ADC_ATTEN_DB_6;
|
2019-07-14 23:32:48 -05:00
|
|
|
else if (mv > thresholds[ADC_ATTEN_DB_6] - 50)
|
2019-10-15 17:05:32 -05:00
|
|
|
atten = ADC_ATTEN_DB_11;
|
|
|
|
else return;
|
2019-07-14 23:32:48 -05:00
|
|
|
|
2019-10-15 17:05:32 -05:00
|
|
|
adc1_set_attenuation(chan, atten);
|
2017-10-08 11:38:10 -05:00
|
|
|
}
|
|
|
|
|
2019-07-02 04:04:49 -05:00
|
|
|
void analogWrite(pin_t pin, int value) {
|
2019-07-14 10:08:14 -05:00
|
|
|
// Use ledc hardware for internal pins
|
|
|
|
if (pin < 34) {
|
|
|
|
static int cnt_channel = 1, pin_to_channel[40] = { 0 };
|
|
|
|
if (pin_to_channel[pin] == 0) {
|
|
|
|
ledcAttachPin(pin, cnt_channel);
|
|
|
|
ledcSetup(cnt_channel, 490, 8);
|
|
|
|
ledcWrite(cnt_channel, value);
|
|
|
|
pin_to_channel[pin] = cnt_channel++;
|
|
|
|
}
|
|
|
|
ledcWrite(pin_to_channel[pin], value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int idx = -1;
|
2019-06-10 23:22:19 -05:00
|
|
|
|
2019-07-14 10:08:14 -05:00
|
|
|
// Search Pin
|
|
|
|
for (int i = 0; i < numPWMUsed; ++i)
|
|
|
|
if (pwmPins[i] == pin) { idx = i; break; }
|
2019-06-10 23:22:19 -05:00
|
|
|
|
2019-07-14 10:08:14 -05:00
|
|
|
// not found ?
|
|
|
|
if (idx < 0) {
|
|
|
|
// No slots remaining
|
|
|
|
if (numPWMUsed >= MAX_PWM_PINS) return;
|
2017-10-08 11:38:10 -05:00
|
|
|
|
2019-07-14 10:08:14 -05:00
|
|
|
// Take new slot for pin
|
|
|
|
idx = numPWMUsed;
|
|
|
|
pwmPins[idx] = pin;
|
|
|
|
// Start timer on first use
|
|
|
|
if (idx == 0) HAL_timer_start(PWM_TIMER_NUM, PWM_TIMER_FREQUENCY);
|
|
|
|
|
|
|
|
++numPWMUsed;
|
2017-10-08 11:38:10 -05:00
|
|
|
}
|
|
|
|
|
2019-07-14 10:08:14 -05:00
|
|
|
// Use 7bit internal value - add 1 to have 100% high at 255
|
|
|
|
pwmValues[idx] = (value + 1) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle PWM timer interrupt
|
|
|
|
HAL_PWM_TIMER_ISR() {
|
|
|
|
HAL_timer_isr_prologue(PWM_TIMER_NUM);
|
|
|
|
|
|
|
|
static uint8_t count = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < numPWMUsed; ++i) {
|
|
|
|
if (count == 0) // Start of interval
|
|
|
|
WRITE(pwmPins[i], pwmValues[i] ? HIGH : LOW);
|
|
|
|
else if (pwmValues[i] == count) // End of duration
|
|
|
|
WRITE(pwmPins[i], LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 128 for 7 Bit resolution
|
|
|
|
count = (count + 1) & 0x7F;
|
|
|
|
|
|
|
|
HAL_timer_isr_epilogue(PWM_TIMER_NUM);
|
2017-10-08 11:38:10 -05:00
|
|
|
}
|
2019-05-25 17:06:00 -05:00
|
|
|
|
2017-10-08 11:38:10 -05:00
|
|
|
#endif // ARDUINO_ARCH_ESP32
|