瀏覽代碼

Various fixes for ESP32 (#14102)

felixstorm 6 年之前
父節點
當前提交
bc5a1fe562

+ 47
- 26
Marlin/src/HAL/HAL_ESP32/HAL.cpp 查看文件

@@ -33,6 +33,10 @@
33 33
 
34 34
 #include "../../inc/MarlinConfigPre.h"
35 35
 
36
+#if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
37
+  #include "spiffs.h"
38
+#endif
39
+
36 40
 #if ENABLED(WIFISUPPORT)
37 41
   #include <ESPAsyncWebServer.h>
38 42
   #include "wifi.h"
@@ -41,10 +45,7 @@
41 45
   #endif
42 46
   #if ENABLED(WEBSUPPORT)
43 47
     #include "web.h"
44
-    #include "spiffs.h"
45 48
   #endif
46
-#elif ENABLED(EEPROM_SETTINGS)
47
-  #include "spiffs.h"
48 49
 #endif
49 50
 
50 51
 // --------------------------------------------------------------------------
@@ -92,21 +93,24 @@ esp_adc_cal_characteristics_t characteristics;
92 93
 // --------------------------------------------------------------------------
93 94
 
94 95
 void HAL_init(void) {
96
+  i2s_init();
97
+}
98
+
99
+void HAL_init_board(void) {
100
+  #if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
101
+    spiffs_init();
102
+  #endif
103
+
95 104
   #if ENABLED(WIFISUPPORT)
96 105
     wifi_init();
97 106
     #if ENABLED(OTASUPPORT)
98 107
       OTA_init();
99 108
     #endif
100 109
     #if ENABLED(WEBSUPPORT)
101
-      spiffs_init();
102 110
       web_init();
103 111
     #endif
104 112
     server.begin();
105
-  #elif ENABLED(EEPROM_SETTINGS)
106
-    spiffs_init();
107 113
   #endif
108
-
109
-  i2s_init();
110 114
 }
111 115
 
112 116
 void HAL_idletask(void) {
@@ -117,18 +121,12 @@ void HAL_idletask(void) {
117 121
 
118 122
 void HAL_clear_reset_source(void) { }
119 123
 
120
-uint8_t HAL_get_reset_source(void) {
121
-  return rtc_get_reset_reason(1);
122
-}
124
+uint8_t HAL_get_reset_source(void) { return rtc_get_reset_reason(1); }
123 125
 
124
-void _delay_ms(int delay_ms) {
125
-  delay(delay_ms);
126
-}
126
+void _delay_ms(int delay_ms) { delay(delay_ms); }
127 127
 
128 128
 // return free memory between end of heap (or end bss) and whatever is current
129
-int freeMemory() {
130
-  return ESP.getFreeHeap();
131
-}
129
+int freeMemory() { return ESP.getFreeHeap(); }
132 130
 
133 131
 // --------------------------------------------------------------------------
134 132
 // ADC
@@ -144,19 +142,41 @@ adc1_channel_t get_channel(int pin) {
144 142
     case 33: return ADC1_CHANNEL(33);
145 143
     case 32: return ADC1_CHANNEL(32);
146 144
   }
147
-
148 145
   return ADC1_CHANNEL_MAX;
149 146
 }
150 147
 
151 148
 void HAL_adc_init() {
152 149
   // Configure ADC
153 150
   adc1_config_width(ADC_WIDTH_12Bit);
154
-  adc1_config_channel_atten(get_channel(39), ADC_ATTEN_11db);
155
-  adc1_config_channel_atten(get_channel(36), ADC_ATTEN_11db);
156
-  adc1_config_channel_atten(get_channel(35), ADC_ATTEN_11db);
157
-  adc1_config_channel_atten(get_channel(34), ADC_ATTEN_11db);
158
-  adc1_config_channel_atten(get_channel(33), ADC_ATTEN_11db);
159
-  adc1_config_channel_atten(get_channel(32), ADC_ATTEN_11db);
151
+  
152
+  // Configure channels only if used as (re-)configuring a pin for ADC that is used elsewhere might have adverse effects
153
+  #if HAS_TEMP_ADC_0
154
+    adc1_config_channel_atten(get_channel(TEMP_0_PIN), ADC_ATTEN_11db);
155
+  #endif
156
+  #if HAS_TEMP_ADC_1
157
+    adc1_config_channel_atten(get_channel(TEMP_1_PIN), ADC_ATTEN_11db);
158
+  #endif
159
+  #if HAS_TEMP_ADC_2
160
+    adc1_config_channel_atten(get_channel(TEMP_2_PIN), ADC_ATTEN_11db);
161
+  #endif
162
+  #if HAS_TEMP_ADC_3
163
+    adc1_config_channel_atten(get_channel(TEMP_3_PIN), ADC_ATTEN_11db);
164
+  #endif
165
+  #if HAS_TEMP_ADC_4
166
+    adc1_config_channel_atten(get_channel(TEMP_4_PIN), ADC_ATTEN_11db);
167
+  #endif
168
+  #if HAS_TEMP_ADC_5
169
+    adc1_config_channel_atten(get_channel(TEMP_5_PIN), ADC_ATTEN_11db);
170
+  #endif
171
+  #if HAS_HEATED_BED
172
+    adc1_config_channel_atten(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db);
173
+  #endif
174
+  #if HAS_TEMP_CHAMBER
175
+    adc1_config_channel_atten(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db);
176
+  #endif
177
+  #if ENABLED(FILAMENT_WIDTH_SENSOR)
178
+    adc1_config_channel_atten(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db);
179
+  #endif
160 180
 
161 181
   // Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
162 182
   // That's why we're not setting it up here.
@@ -172,9 +192,9 @@ void HAL_adc_start_conversion(uint8_t adc_pin) {
172 192
   HAL_adc_result = mv*1023.0/3300.0;
173 193
 }
174 194
 
175
-int pin_to_channel[40] = {};
176
-int cnt_channel = 1;
177 195
 void analogWrite(int pin, int value) {
196
+  static int cnt_channel = 1,
197
+             pin_to_channel[40] = {};
178 198
   if (pin_to_channel[pin] == 0) {
179 199
     ledcAttachPin(pin, cnt_channel);
180 200
     ledcSetup(cnt_channel, 490, 8);
@@ -185,4 +205,5 @@ void analogWrite(int pin, int value) {
185 205
 
186 206
   ledcWrite(pin_to_channel[pin], value);
187 207
 }
208
+
188 209
 #endif // ARDUINO_ARCH_ESP32

+ 2
- 0
Marlin/src/HAL/HAL_ESP32/HAL.h 查看文件

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

+ 4
- 6
Marlin/src/HAL/HAL_ESP32/HAL_timers_ESP32.cpp 查看文件

@@ -132,7 +132,7 @@ void HAL_timer_start(const uint8_t timer_num, uint32_t frequency) {
132 132
   timer_enable_intr(timer.group, timer.idx);
133 133
 
134 134
   // TODO need to deal with timer_group1_isr
135
-  timer_isr_register(timer.group, timer.idx, timer_group0_isr, (void*)timer.idx, ESP_INTR_FLAG_INTRDISABLED, nullptr);
135
+  timer_isr_register(timer.group, timer.idx, timer_group0_isr, (void*)timer.idx, 0, nullptr);
136 136
 
137 137
   timer_start(timer.group, timer.idx);
138 138
 }
@@ -169,10 +169,8 @@ hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
169 169
  */
170 170
 hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
171 171
   const tTimerConfig timer = TimerConfig[timer_num];
172
-
173 172
   uint64_t counter_value;
174 173
   timer_get_counter_value(timer.group, timer.idx, &counter_value);
175
-
176 174
   return counter_value;
177 175
 }
178 176
 
@@ -181,7 +179,7 @@ hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
181 179
  * @param timer_num timer number to enable interrupts on
182 180
  */
183 181
 void HAL_timer_enable_interrupt(const uint8_t timer_num) {
184
-  const tTimerConfig timer = TimerConfig[timer_num];
182
+  //const tTimerConfig timer = TimerConfig[timer_num];
185 183
   //timer_enable_intr(timer.group, timer.idx);
186 184
 }
187 185
 
@@ -190,8 +188,8 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num) {
190 188
  * @param timer_num timer number to disable interrupts on
191 189
  */
192 190
 void HAL_timer_disable_interrupt(const uint8_t timer_num) {
193
-  const tTimerConfig timer = TimerConfig[timer_num];
194
-  // timer_disable_intr(timer.group, timer.idx);
191
+  //const tTimerConfig timer = TimerConfig[timer_num];
192
+  //timer_disable_intr(timer.group, timer.idx);
195 193
 }
196 194
 
197 195
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {

+ 5
- 0
Marlin/src/HAL/HAL_ESP32/HAL_timers_ESP32.h 查看文件

@@ -28,6 +28,11 @@
28 28
 #include <stdint.h>
29 29
 #include "driver/timer.h"
30 30
 
31
+// Includes needed to get I2S_STEPPER_STREAM. Note that pins.h
32
+// is included in case this header is being included early.
33
+#include "../../inc/MarlinConfig.h"
34
+#include "../../pins/pins.h"
35
+
31 36
 // --------------------------------------------------------------------------
32 37
 // Defines
33 38
 // --------------------------------------------------------------------------

+ 2
- 2
Marlin/src/HAL/HAL_ESP32/WebSocketSerial.cpp 查看文件

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

+ 21
- 8
Marlin/src/HAL/HAL_ESP32/persistent_store_spiffs.cpp 查看文件

@@ -28,25 +28,38 @@
28 28
 
29 29
 #include "../shared/persistent_store_api.h"
30 30
 
31
-#include "SPIFFS.h"
32
-#include "FS.h"
31
+#include <SPIFFS.h>
32
+#include <FS.h>
33 33
 #include "spiffs.h"
34 34
 
35 35
 #define HAL_ESP32_EEPROM_SIZE 4096
36
+#define HAL_ESP32_EEPROM_FILE_PATH "/eeprom.dat"
36 37
 
37 38
 File eeprom_file;
38 39
 
39 40
 bool PersistentStore::access_start() {
40 41
   if (spiffs_initialized) {
41
-    eeprom_file = SPIFFS.open("/eeprom.dat", "r+");
42
+    eeprom_file = SPIFFS.open(HAL_ESP32_EEPROM_FILE_PATH, "r+");
42 43
 
43 44
     size_t file_size = eeprom_file.size();
44 45
     if (file_size < HAL_ESP32_EEPROM_SIZE) {
45
-      bool write_ok = eeprom_file.seek(file_size);
46
-
47
-      while (write_ok && file_size < HAL_ESP32_EEPROM_SIZE) {
48
-        write_ok = eeprom_file.write(0xFF) == 1;
49
-        file_size++;
46
+      SERIAL_ECHO_MSG("SPIFFS EEPROM settings file " HAL_ESP32_EEPROM_FILE_PATH " is too small or did not exist, expanding.");
47
+      SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR(" file size: ", file_size, ", required size: ", HAL_ESP32_EEPROM_SIZE);
48
+
49
+      // mode r+ does not allow to expand the file (at least on ESP32 SPIFFS9, so we close, reopen "a", append, close, reopen "r+"
50
+      eeprom_file.close();
51
+
52
+      eeprom_file = SPIFFS.open(HAL_ESP32_EEPROM_FILE_PATH, "a");
53
+      for (size_t i = eeprom_file.size(); i < HAL_ESP32_EEPROM_SIZE; i++)
54
+        eeprom_file.write(0xFF);
55
+      eeprom_file.close();
56
+
57
+      eeprom_file = SPIFFS.open(HAL_ESP32_EEPROM_FILE_PATH, "r+");
58
+      file_size = eeprom_file.size();
59
+      if (file_size < HAL_ESP32_EEPROM_SIZE) {
60
+        SERIAL_ERROR_MSG("Failed to expand " HAL_ESP32_EEPROM_FILE_PATH " to required size. SPIFFS partition full?");
61
+        SERIAL_ERROR_START(); SERIAL_ECHOLNPAIR(" file size: ", file_size, ", required size: ", HAL_ESP32_EEPROM_SIZE);
62
+        SERIAL_ERROR_START(); SERIAL_ECHOLNPAIR(" SPIFFS used bytes: ", SPIFFS.usedBytes(), ", total bytes: ", SPIFFS.totalBytes());
50 63
       }
51 64
     }
52 65
     return true;

+ 4
- 4
Marlin/src/HAL/HAL_ESP32/spiffs.cpp 查看文件

@@ -28,16 +28,16 @@
28 28
 
29 29
 #include "../../core/serial.h"
30 30
 
31
-#include "FS.h"
32
-#include "SPIFFS.h"
31
+#include <FS.h>
32
+#include <SPIFFS.h>
33 33
 
34 34
 bool spiffs_initialized;
35 35
 
36 36
 void spiffs_init() {
37
-  if (SPIFFS.begin())
37
+  if (SPIFFS.begin(true))  // formatOnFail = true
38 38
     spiffs_initialized = true;
39 39
   else
40
-    SERIAL_ECHO_MSG("SPIFFS mount failed");
40
+    SERIAL_ERROR_MSG("SPIFFS mount failed");
41 41
 }
42 42
 
43 43
 #endif // WEBSUPPORT

+ 1
- 1
Marlin/src/HAL/HAL_ESP32/web.cpp 查看文件

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

+ 12
- 3
Marlin/src/HAL/HAL_ESP32/wifi.cpp 查看文件

@@ -22,6 +22,7 @@
22 22
 
23 23
 #ifdef ARDUINO_ARCH_ESP32
24 24
 
25
+#include "../../core/serial.h"
25 26
 #include "../../inc/MarlinConfigPre.h"
26 27
 
27 28
 #if ENABLED(WIFISUPPORT)
@@ -38,20 +39,28 @@ AsyncWebServer server(80);
38 39
 #endif
39 40
 
40 41
 void wifi_init() {
42
+
43
+  SERIAL_ECHO_MSG("Starting WiFi...");
44
+
41 45
   WiFi.mode(WIFI_STA);
42 46
   WiFi.begin(WIFI_SSID, WIFI_PWD);
43 47
 
44 48
   while (WiFi.waitForConnectResult() != WL_CONNECTED) {
49
+    SERIAL_ERROR_MSG("Unable to connect to WiFi with SSID '" WIFI_SSID "', restarting.");
45 50
     delay(5000);
46 51
     ESP.restart();
47 52
   }
48 53
 
49 54
   delay(10);
50
-
51
-  // Loop forever (watchdog kill) on failure
52
-  if (!MDNS.begin(WIFI_HOSTNAME)) for(;;) delay(5000);
55
+  if (!MDNS.begin(WIFI_HOSTNAME)) {
56
+    SERIAL_ERROR_MSG("Unable to start mDNS with hostname '" WIFI_HOSTNAME "', restarting.");
57
+    delay(5000);
58
+    ESP.restart();
59
+  }
53 60
 
54 61
   MDNS.addService("http", "tcp", 80);
62
+
63
+  SERIAL_ECHOLNPAIR("Successfully connected to WiFi with SSID '" WIFI_SSID "', hostname: '" WIFI_HOSTNAME "', IP address: ", WiFi.localIP().toString().c_str());
55 64
 }
56 65
 
57 66
 #endif // WIFISUPPORT

+ 2
- 0
Marlin/src/pins/pins_ESP32.h 查看文件

@@ -40,6 +40,8 @@
40 40
 //
41 41
 // Steppers
42 42
 //
43
+#define I2S_STEPPER_STREAM
44
+
43 45
 #define X_STEP_PIN         128
44 46
 #define X_DIR_PIN          129
45 47
 #define X_ENABLE_PIN       130

Loading…
取消
儲存