2017-10-08 11:38:10 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 15:06:53 -06:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-10-08 11:38:10 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Includes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "HAL.h"
|
|
|
|
#include <rom/rtc.h>
|
|
|
|
#include <driver/adc.h>
|
|
|
|
#include <esp_adc_cal.h>
|
|
|
|
|
|
|
|
#include "../../inc/MarlinConfigPre.h"
|
|
|
|
|
2019-05-25 17:06:00 -05:00
|
|
|
#if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
|
|
|
|
#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
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Externals
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Local defines
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define V_REF 1100
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
uint16_t HAL_adc_result;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Private Variables
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
esp_adc_cal_characteristics_t characteristics;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Function prototypes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Private functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Public functions
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void HAL_init(void) {
|
2019-05-25 17:06:00 -05:00
|
|
|
i2s_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_init_board(void) {
|
|
|
|
#if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_idletask(void) {
|
2019-03-13 00:48:08 -05:00
|
|
|
#if ENABLED(OTASUPPORT)
|
2017-10-08 11:38:10 -05:00
|
|
|
OTA_handle();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_clear_reset_source(void) { }
|
|
|
|
|
2019-05-25 17:06:00 -05:00
|
|
|
uint8_t HAL_get_reset_source(void) { 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
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ADC
|
|
|
|
// --------------------------------------------------------------------------
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_0_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_1
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_1_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_2
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_2_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_3
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_3_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_4
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_4_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_ADC_5
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_5_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if HAS_HEATED_BED
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if HAS_TEMP_CHAMBER
|
|
|
|
adc1_config_channel_atten(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db);
|
|
|
|
#endif
|
|
|
|
#if ENABLED(FILAMENT_WIDTH_SENSOR)
|
|
|
|
adc1_config_channel_atten(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db);
|
|
|
|
#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
|
|
|
|
|
|
|
// Calculate ADC characteristics i.e. gain and offset factors
|
|
|
|
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, V_REF, &characteristics);
|
|
|
|
}
|
|
|
|
|
2019-04-12 13:35:30 -05:00
|
|
|
void HAL_adc_start_conversion(uint8_t adc_pin) {
|
2017-10-08 11:38:10 -05:00
|
|
|
uint32_t mv;
|
|
|
|
esp_adc_cal_get_voltage((adc_channel_t)get_channel(adc_pin), &characteristics, &mv);
|
|
|
|
|
|
|
|
HAL_adc_result = mv*1023.0/3300.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void analogWrite(int pin, int value) {
|
2019-06-10 23:22:19 -05:00
|
|
|
|
|
|
|
if (!PWM_PIN(pin)) return;
|
|
|
|
|
2019-05-25 17:06:00 -05:00
|
|
|
static int cnt_channel = 1,
|
|
|
|
pin_to_channel[40] = {};
|
2017-10-08 11:38:10 -05:00
|
|
|
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);
|
|
|
|
}
|
2019-05-25 17:06:00 -05:00
|
|
|
|
2017-10-08 11:38:10 -05:00
|
|
|
#endif // ARDUINO_ARCH_ESP32
|