2017-10-08 11:38:10 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2019 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
|
|
|
|
#include "HAL.h"
|
2019-09-02 19:49:58 -05:00
|
|
|
#include "timers.h"
|
2017-10-08 11:38:10 -05:00
|
|
|
#include <rom/rtc.h>
|
|
|
|
#include <driver/adc.h>
|
|
|
|
#include <esp_adc_cal.h>
|
|
|
|
|
|
|
|
#include "../../inc/MarlinConfigPre.h"
|
|
|
|
|
2019-12-15 11:39:39 -06:00
|
|
|
#if ENABLED(WEBSUPPORT)
|
2019-05-25 17:06:00 -05:00
|
|
|
#include "spiffs.h"
|
|
|
|
#endif
|
|
|
|
|
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)
|
|
|
|
#include "web.h"
|
|
|
|
#endif
|
2017-10-08 11:38:10 -05:00
|
|
|
#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
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_init() {
|
2019-05-25 17:06:00 -05:00
|
|
|
i2s_init();
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_init_board() {
|
2019-12-15 11:39:39 -06:00
|
|
|
#if ENABLED(WEBSUPPORT)
|
2019-05-25 17:06:00 -05:00
|
|
|
spiffs_init();
|
|
|
|
#endif
|
|
|
|
|
2017-10-08 11:38:10 -05:00
|
|
|
#if ENABLED(WIFISUPPORT)
|
2019-03-13 00:48:08 -05:00
|
|
|
wifi_init();
|
|
|
|
#if ENABLED(OTASUPPORT)
|
|
|
|
OTA_init();
|
|
|
|
#endif
|
|
|
|
#if ENABLED(WEBSUPPORT)
|
|
|
|
web_init();
|
|
|
|
#endif
|
|
|
|
server.begin();
|
2017-10-08 11:38:10 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_idletask() {
|
2019-03-13 00:48:08 -05:00
|
|
|
#if ENABLED(OTASUPPORT)
|
2017-10-08 11:38:10 -05:00
|
|
|
OTA_handle();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
#if HAS_TEMP_ADC_0
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_0_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_1
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_1_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_2
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_2_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_3
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_3_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_4
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_4_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_5
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_5_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if HAS_HEATED_BED
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_CHAMBER
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
|
|
|
#if ENABLED(FILAMENT_WIDTH_SENSOR)
|
2019-07-14 23:32:48 -05:00
|
|
|
adc1_set_attenuation(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db);
|
2019-05-25 17:06:00 -05:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2019-04-12 13:35:30 -05:00
|
|
|
void HAL_adc_start_conversion(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
|