Various fixes for ESP32 (#14102)

2.0.x
felixstorm 6 years ago committed by Scott Lahteine
parent e1bf34bdc9
commit bc5a1fe562

@ -33,6 +33,10 @@
#include "../../inc/MarlinConfigPre.h" #include "../../inc/MarlinConfigPre.h"
#if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
#include "spiffs.h"
#endif
#if ENABLED(WIFISUPPORT) #if ENABLED(WIFISUPPORT)
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include "wifi.h" #include "wifi.h"
@ -41,10 +45,7 @@
#endif #endif
#if ENABLED(WEBSUPPORT) #if ENABLED(WEBSUPPORT)
#include "web.h" #include "web.h"
#include "spiffs.h"
#endif #endif
#elif ENABLED(EEPROM_SETTINGS)
#include "spiffs.h"
#endif #endif
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -92,21 +93,24 @@ esp_adc_cal_characteristics_t characteristics;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
void HAL_init(void) { void HAL_init(void) {
i2s_init();
}
void HAL_init_board(void) {
#if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
spiffs_init();
#endif
#if ENABLED(WIFISUPPORT) #if ENABLED(WIFISUPPORT)
wifi_init(); wifi_init();
#if ENABLED(OTASUPPORT) #if ENABLED(OTASUPPORT)
OTA_init(); OTA_init();
#endif #endif
#if ENABLED(WEBSUPPORT) #if ENABLED(WEBSUPPORT)
spiffs_init();
web_init(); web_init();
#endif #endif
server.begin(); server.begin();
#elif ENABLED(EEPROM_SETTINGS)
spiffs_init();
#endif #endif
i2s_init();
} }
void HAL_idletask(void) { void HAL_idletask(void) {
@ -117,18 +121,12 @@ void HAL_idletask(void) {
void HAL_clear_reset_source(void) { } void HAL_clear_reset_source(void) { }
uint8_t HAL_get_reset_source(void) { uint8_t HAL_get_reset_source(void) { return rtc_get_reset_reason(1); }
return rtc_get_reset_reason(1);
}
void _delay_ms(int delay_ms) { void _delay_ms(int delay_ms) { delay(delay_ms); }
delay(delay_ms);
}
// return free memory between end of heap (or end bss) and whatever is current // return free memory between end of heap (or end bss) and whatever is current
int freeMemory() { int freeMemory() { return ESP.getFreeHeap(); }
return ESP.getFreeHeap();
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ADC // ADC
@ -144,19 +142,41 @@ adc1_channel_t get_channel(int pin) {
case 33: return ADC1_CHANNEL(33); case 33: return ADC1_CHANNEL(33);
case 32: return ADC1_CHANNEL(32); case 32: return ADC1_CHANNEL(32);
} }
return ADC1_CHANNEL_MAX; return ADC1_CHANNEL_MAX;
} }
void HAL_adc_init() { void HAL_adc_init() {
// Configure ADC // Configure ADC
adc1_config_width(ADC_WIDTH_12Bit); adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(get_channel(39), ADC_ATTEN_11db);
adc1_config_channel_atten(get_channel(36), ADC_ATTEN_11db); // Configure channels only if used as (re-)configuring a pin for ADC that is used elsewhere might have adverse effects
adc1_config_channel_atten(get_channel(35), ADC_ATTEN_11db); #if HAS_TEMP_ADC_0
adc1_config_channel_atten(get_channel(34), ADC_ATTEN_11db); adc1_config_channel_atten(get_channel(TEMP_0_PIN), ADC_ATTEN_11db);
adc1_config_channel_atten(get_channel(33), ADC_ATTEN_11db); #endif
adc1_config_channel_atten(get_channel(32), ADC_ATTEN_11db); #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
// Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail. // 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. // That's why we're not setting it up here.
@ -172,9 +192,9 @@ void HAL_adc_start_conversion(uint8_t adc_pin) {
HAL_adc_result = mv*1023.0/3300.0; HAL_adc_result = mv*1023.0/3300.0;
} }
int pin_to_channel[40] = {};
int cnt_channel = 1;
void analogWrite(int pin, int value) { void analogWrite(int pin, int value) {
static int cnt_channel = 1,
pin_to_channel[40] = {};
if (pin_to_channel[pin] == 0) { if (pin_to_channel[pin] == 0) {
ledcAttachPin(pin, cnt_channel); ledcAttachPin(pin, cnt_channel);
ledcSetup(cnt_channel, 490, 8); ledcSetup(cnt_channel, 490, 8);
@ -185,4 +205,5 @@ void analogWrite(int pin, int value) {
ledcWrite(pin_to_channel[pin], value); ledcWrite(pin_to_channel[pin], value);
} }
#endif // ARDUINO_ARCH_ESP32 #endif // ARDUINO_ARCH_ESP32

@ -122,5 +122,7 @@ void HAL_adc_start_conversion(uint8_t adc_pin);
// Enable hooks into idle and setup for HAL // Enable hooks into idle and setup for HAL
#define HAL_IDLETASK 1 #define HAL_IDLETASK 1
#define HAL_INIT 1 #define HAL_INIT 1
#define BOARD_INIT() HAL_init_board();
void HAL_idletask(void); void HAL_idletask(void);
void HAL_init(void); void HAL_init(void);
void HAL_init_board(void);

@ -132,7 +132,7 @@ void HAL_timer_start(const uint8_t timer_num, uint32_t frequency) {
timer_enable_intr(timer.group, timer.idx); timer_enable_intr(timer.group, timer.idx);
// TODO need to deal with timer_group1_isr // TODO need to deal with timer_group1_isr
timer_isr_register(timer.group, timer.idx, timer_group0_isr, (void*)timer.idx, ESP_INTR_FLAG_INTRDISABLED, nullptr); timer_isr_register(timer.group, timer.idx, timer_group0_isr, (void*)timer.idx, 0, nullptr);
timer_start(timer.group, timer.idx); timer_start(timer.group, timer.idx);
} }
@ -169,10 +169,8 @@ hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
*/ */
hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
const tTimerConfig timer = TimerConfig[timer_num]; const tTimerConfig timer = TimerConfig[timer_num];
uint64_t counter_value; uint64_t counter_value;
timer_get_counter_value(timer.group, timer.idx, &counter_value); timer_get_counter_value(timer.group, timer.idx, &counter_value);
return counter_value; return counter_value;
} }
@ -181,7 +179,7 @@ hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
* @param timer_num timer number to enable interrupts on * @param timer_num timer number to enable interrupts on
*/ */
void HAL_timer_enable_interrupt(const uint8_t timer_num) { void HAL_timer_enable_interrupt(const uint8_t timer_num) {
const tTimerConfig timer = TimerConfig[timer_num]; //const tTimerConfig timer = TimerConfig[timer_num];
//timer_enable_intr(timer.group, timer.idx); //timer_enable_intr(timer.group, timer.idx);
} }
@ -190,8 +188,8 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num) {
* @param timer_num timer number to disable interrupts on * @param timer_num timer number to disable interrupts on
*/ */
void HAL_timer_disable_interrupt(const uint8_t timer_num) { void HAL_timer_disable_interrupt(const uint8_t timer_num) {
const tTimerConfig timer = TimerConfig[timer_num]; //const tTimerConfig timer = TimerConfig[timer_num];
// timer_disable_intr(timer.group, timer.idx); //timer_disable_intr(timer.group, timer.idx);
} }
bool HAL_timer_interrupt_enabled(const uint8_t timer_num) { bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {

@ -28,6 +28,11 @@
#include <stdint.h> #include <stdint.h>
#include "driver/timer.h" #include "driver/timer.h"
// Includes needed to get I2S_STEPPER_STREAM. Note that pins.h
// is included in case this header is being included early.
#include "../../inc/MarlinConfig.h"
#include "../../pins/pins.h"
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Defines // Defines
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

@ -38,9 +38,9 @@ AsyncWebSocket ws("/ws"); // TODO Move inside the class.
RingBuffer::RingBuffer(ring_buffer_pos_t size) RingBuffer::RingBuffer(ring_buffer_pos_t size)
: data(new uint8_t[size]), : data(new uint8_t[size]),
size(size),
read_index(0), read_index(0),
write_index(0), write_index(0)
size(size)
{} {}
RingBuffer::~RingBuffer() { delete[] data; } RingBuffer::~RingBuffer() { delete[] data; }

@ -28,25 +28,38 @@
#include "../shared/persistent_store_api.h" #include "../shared/persistent_store_api.h"
#include "SPIFFS.h" #include <SPIFFS.h>
#include "FS.h" #include <FS.h>
#include "spiffs.h" #include "spiffs.h"
#define HAL_ESP32_EEPROM_SIZE 4096 #define HAL_ESP32_EEPROM_SIZE 4096
#define HAL_ESP32_EEPROM_FILE_PATH "/eeprom.dat"
File eeprom_file; File eeprom_file;
bool PersistentStore::access_start() { bool PersistentStore::access_start() {
if (spiffs_initialized) { if (spiffs_initialized) {
eeprom_file = SPIFFS.open("/eeprom.dat", "r+"); eeprom_file = SPIFFS.open(HAL_ESP32_EEPROM_FILE_PATH, "r+");
size_t file_size = eeprom_file.size(); size_t file_size = eeprom_file.size();
if (file_size < HAL_ESP32_EEPROM_SIZE) { if (file_size < HAL_ESP32_EEPROM_SIZE) {
bool write_ok = eeprom_file.seek(file_size); SERIAL_ECHO_MSG("SPIFFS EEPROM settings file " HAL_ESP32_EEPROM_FILE_PATH " is too small or did not exist, expanding.");
SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(" file size: ", file_size, ", required size: ", HAL_ESP32_EEPROM_SIZE);
while (write_ok && file_size < HAL_ESP32_EEPROM_SIZE) {
write_ok = eeprom_file.write(0xFF) == 1; // mode r+ does not allow to expand the file (at least on ESP32 SPIFFS9, so we close, reopen "a", append, close, reopen "r+"
file_size++; eeprom_file.close();
eeprom_file = SPIFFS.open(HAL_ESP32_EEPROM_FILE_PATH, "a");
for (size_t i = eeprom_file.size(); i < HAL_ESP32_EEPROM_SIZE; i++)
eeprom_file.write(0xFF);
eeprom_file.close();
eeprom_file = SPIFFS.open(HAL_ESP32_EEPROM_FILE_PATH, "r+");
file_size = eeprom_file.size();
if (file_size < HAL_ESP32_EEPROM_SIZE) {
SERIAL_ERROR_MSG("Failed to expand " HAL_ESP32_EEPROM_FILE_PATH " to required size. SPIFFS partition full?");
SERIAL_ERROR_START(); SERIAL_ECHOLNPAIR(" file size: ", file_size, ", required size: ", HAL_ESP32_EEPROM_SIZE);
SERIAL_ERROR_START(); SERIAL_ECHOLNPAIR(" SPIFFS used bytes: ", SPIFFS.usedBytes(), ", total bytes: ", SPIFFS.totalBytes());
} }
} }
return true; return true;

@ -28,16 +28,16 @@
#include "../../core/serial.h" #include "../../core/serial.h"
#include "FS.h" #include <FS.h>
#include "SPIFFS.h" #include <SPIFFS.h>
bool spiffs_initialized; bool spiffs_initialized;
void spiffs_init() { void spiffs_init() {
if (SPIFFS.begin()) if (SPIFFS.begin(true)) // formatOnFail = true
spiffs_initialized = true; spiffs_initialized = true;
else else
SERIAL_ECHO_MSG("SPIFFS mount failed"); SERIAL_ERROR_MSG("SPIFFS mount failed");
} }
#endif // WEBSUPPORT #endif // WEBSUPPORT

@ -26,7 +26,7 @@
#if ENABLED(WEBSUPPORT) #if ENABLED(WEBSUPPORT)
#include "SPIFFS.h" #include <SPIFFS.h>
#include "wifi.h" #include "wifi.h"
AsyncEventSource events("/events"); // event source (Server-Sent events) AsyncEventSource events("/events"); // event source (Server-Sent events)

@ -22,6 +22,7 @@
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
#include "../../core/serial.h"
#include "../../inc/MarlinConfigPre.h" #include "../../inc/MarlinConfigPre.h"
#if ENABLED(WIFISUPPORT) #if ENABLED(WIFISUPPORT)
@ -38,20 +39,28 @@ AsyncWebServer server(80);
#endif #endif
void wifi_init() { void wifi_init() {
SERIAL_ECHO_MSG("Starting WiFi...");
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PWD); WiFi.begin(WIFI_SSID, WIFI_PWD);
while (WiFi.waitForConnectResult() != WL_CONNECTED) { while (WiFi.waitForConnectResult() != WL_CONNECTED) {
SERIAL_ERROR_MSG("Unable to connect to WiFi with SSID '" WIFI_SSID "', restarting.");
delay(5000); delay(5000);
ESP.restart(); ESP.restart();
} }
delay(10); delay(10);
if (!MDNS.begin(WIFI_HOSTNAME)) {
// Loop forever (watchdog kill) on failure SERIAL_ERROR_MSG("Unable to start mDNS with hostname '" WIFI_HOSTNAME "', restarting.");
if (!MDNS.begin(WIFI_HOSTNAME)) for(;;) delay(5000); delay(5000);
ESP.restart();
}
MDNS.addService("http", "tcp", 80); MDNS.addService("http", "tcp", 80);
SERIAL_ECHOLNPAIR("Successfully connected to WiFi with SSID '" WIFI_SSID "', hostname: '" WIFI_HOSTNAME "', IP address: ", WiFi.localIP().toString().c_str());
} }
#endif // WIFISUPPORT #endif // WIFISUPPORT

@ -40,6 +40,8 @@
// //
// Steppers // Steppers
// //
#define I2S_STEPPER_STREAM
#define X_STEP_PIN 128 #define X_STEP_PIN 128
#define X_DIR_PIN 129 #define X_DIR_PIN 129
#define X_ENABLE_PIN 130 #define X_ENABLE_PIN 130

Loading…
Cancel
Save