Sfoglia il codice sorgente

Various fixes for ESP32 (#14102)

felixstorm 6 anni fa
parent
commit
bc5a1fe562

+ 47
- 26
Marlin/src/HAL/HAL_ESP32/HAL.cpp Vedi File

33
 
33
 
34
 #include "../../inc/MarlinConfigPre.h"
34
 #include "../../inc/MarlinConfigPre.h"
35
 
35
 
36
+#if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
37
+  #include "spiffs.h"
38
+#endif
39
+
36
 #if ENABLED(WIFISUPPORT)
40
 #if ENABLED(WIFISUPPORT)
37
   #include <ESPAsyncWebServer.h>
41
   #include <ESPAsyncWebServer.h>
38
   #include "wifi.h"
42
   #include "wifi.h"
41
   #endif
45
   #endif
42
   #if ENABLED(WEBSUPPORT)
46
   #if ENABLED(WEBSUPPORT)
43
     #include "web.h"
47
     #include "web.h"
44
-    #include "spiffs.h"
45
   #endif
48
   #endif
46
-#elif ENABLED(EEPROM_SETTINGS)
47
-  #include "spiffs.h"
48
 #endif
49
 #endif
49
 
50
 
50
 // --------------------------------------------------------------------------
51
 // --------------------------------------------------------------------------
92
 // --------------------------------------------------------------------------
93
 // --------------------------------------------------------------------------
93
 
94
 
94
 void HAL_init(void) {
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
   #if ENABLED(WIFISUPPORT)
104
   #if ENABLED(WIFISUPPORT)
96
     wifi_init();
105
     wifi_init();
97
     #if ENABLED(OTASUPPORT)
106
     #if ENABLED(OTASUPPORT)
98
       OTA_init();
107
       OTA_init();
99
     #endif
108
     #endif
100
     #if ENABLED(WEBSUPPORT)
109
     #if ENABLED(WEBSUPPORT)
101
-      spiffs_init();
102
       web_init();
110
       web_init();
103
     #endif
111
     #endif
104
     server.begin();
112
     server.begin();
105
-  #elif ENABLED(EEPROM_SETTINGS)
106
-    spiffs_init();
107
   #endif
113
   #endif
108
-
109
-  i2s_init();
110
 }
114
 }
111
 
115
 
112
 void HAL_idletask(void) {
116
 void HAL_idletask(void) {
117
 
121
 
118
 void HAL_clear_reset_source(void) { }
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
 // return free memory between end of heap (or end bss) and whatever is current
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
 // ADC
132
 // ADC
144
     case 33: return ADC1_CHANNEL(33);
142
     case 33: return ADC1_CHANNEL(33);
145
     case 32: return ADC1_CHANNEL(32);
143
     case 32: return ADC1_CHANNEL(32);
146
   }
144
   }
147
-
148
   return ADC1_CHANNEL_MAX;
145
   return ADC1_CHANNEL_MAX;
149
 }
146
 }
150
 
147
 
151
 void HAL_adc_init() {
148
 void HAL_adc_init() {
152
   // Configure ADC
149
   // Configure ADC
153
   adc1_config_width(ADC_WIDTH_12Bit);
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
   // Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
181
   // Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
162
   // That's why we're not setting it up here.
182
   // That's why we're not setting it up here.
172
   HAL_adc_result = mv*1023.0/3300.0;
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
 void analogWrite(int pin, int value) {
195
 void analogWrite(int pin, int value) {
196
+  static int cnt_channel = 1,
197
+             pin_to_channel[40] = {};
178
   if (pin_to_channel[pin] == 0) {
198
   if (pin_to_channel[pin] == 0) {
179
     ledcAttachPin(pin, cnt_channel);
199
     ledcAttachPin(pin, cnt_channel);
180
     ledcSetup(cnt_channel, 490, 8);
200
     ledcSetup(cnt_channel, 490, 8);
185
 
205
 
186
   ledcWrite(pin_to_channel[pin], value);
206
   ledcWrite(pin_to_channel[pin], value);
187
 }
207
 }
208
+
188
 #endif // ARDUINO_ARCH_ESP32
209
 #endif // ARDUINO_ARCH_ESP32

+ 2
- 0
Marlin/src/HAL/HAL_ESP32/HAL.h Vedi File

122
 // Enable hooks into idle and setup for HAL
122
 // Enable hooks into idle and setup for HAL
123
 #define HAL_IDLETASK 1
123
 #define HAL_IDLETASK 1
124
 #define HAL_INIT 1
124
 #define HAL_INIT 1
125
+#define BOARD_INIT() HAL_init_board();
125
 void HAL_idletask(void);
126
 void HAL_idletask(void);
126
 void HAL_init(void);
127
 void HAL_init(void);
128
+void HAL_init_board(void);

+ 4
- 6
Marlin/src/HAL/HAL_ESP32/HAL_timers_ESP32.cpp Vedi File

132
   timer_enable_intr(timer.group, timer.idx);
132
   timer_enable_intr(timer.group, timer.idx);
133
 
133
 
134
   // TODO need to deal with timer_group1_isr
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
   timer_start(timer.group, timer.idx);
137
   timer_start(timer.group, timer.idx);
138
 }
138
 }
169
  */
169
  */
170
 hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
170
 hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
171
   const tTimerConfig timer = TimerConfig[timer_num];
171
   const tTimerConfig timer = TimerConfig[timer_num];
172
-
173
   uint64_t counter_value;
172
   uint64_t counter_value;
174
   timer_get_counter_value(timer.group, timer.idx, &counter_value);
173
   timer_get_counter_value(timer.group, timer.idx, &counter_value);
175
-
176
   return counter_value;
174
   return counter_value;
177
 }
175
 }
178
 
176
 
181
  * @param timer_num timer number to enable interrupts on
179
  * @param timer_num timer number to enable interrupts on
182
  */
180
  */
183
 void HAL_timer_enable_interrupt(const uint8_t timer_num) {
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
   //timer_enable_intr(timer.group, timer.idx);
183
   //timer_enable_intr(timer.group, timer.idx);
186
 }
184
 }
187
 
185
 
190
  * @param timer_num timer number to disable interrupts on
188
  * @param timer_num timer number to disable interrupts on
191
  */
189
  */
192
 void HAL_timer_disable_interrupt(const uint8_t timer_num) {
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
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
195
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {

+ 5
- 0
Marlin/src/HAL/HAL_ESP32/HAL_timers_ESP32.h Vedi File

28
 #include <stdint.h>
28
 #include <stdint.h>
29
 #include "driver/timer.h"
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
 // Defines
37
 // Defines
33
 // --------------------------------------------------------------------------
38
 // --------------------------------------------------------------------------

+ 2
- 2
Marlin/src/HAL/HAL_ESP32/WebSocketSerial.cpp Vedi File

38
 
38
 
39
 RingBuffer::RingBuffer(ring_buffer_pos_t size)
39
 RingBuffer::RingBuffer(ring_buffer_pos_t size)
40
   : data(new uint8_t[size]),
40
   : data(new uint8_t[size]),
41
+    size(size),
41
     read_index(0),
42
     read_index(0),
42
-    write_index(0),
43
-    size(size)
43
+    write_index(0)
44
 {}
44
 {}
45
 
45
 
46
 RingBuffer::~RingBuffer() { delete[] data; }
46
 RingBuffer::~RingBuffer() { delete[] data; }

+ 21
- 8
Marlin/src/HAL/HAL_ESP32/persistent_store_spiffs.cpp Vedi File

28
 
28
 
29
 #include "../shared/persistent_store_api.h"
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
 #include "spiffs.h"
33
 #include "spiffs.h"
34
 
34
 
35
 #define HAL_ESP32_EEPROM_SIZE 4096
35
 #define HAL_ESP32_EEPROM_SIZE 4096
36
+#define HAL_ESP32_EEPROM_FILE_PATH "/eeprom.dat"
36
 
37
 
37
 File eeprom_file;
38
 File eeprom_file;
38
 
39
 
39
 bool PersistentStore::access_start() {
40
 bool PersistentStore::access_start() {
40
   if (spiffs_initialized) {
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
     size_t file_size = eeprom_file.size();
44
     size_t file_size = eeprom_file.size();
44
     if (file_size < HAL_ESP32_EEPROM_SIZE) {
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
     return true;
65
     return true;

+ 4
- 4
Marlin/src/HAL/HAL_ESP32/spiffs.cpp Vedi File

28
 
28
 
29
 #include "../../core/serial.h"
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
 bool spiffs_initialized;
34
 bool spiffs_initialized;
35
 
35
 
36
 void spiffs_init() {
36
 void spiffs_init() {
37
-  if (SPIFFS.begin())
37
+  if (SPIFFS.begin(true))  // formatOnFail = true
38
     spiffs_initialized = true;
38
     spiffs_initialized = true;
39
   else
39
   else
40
-    SERIAL_ECHO_MSG("SPIFFS mount failed");
40
+    SERIAL_ERROR_MSG("SPIFFS mount failed");
41
 }
41
 }
42
 
42
 
43
 #endif // WEBSUPPORT
43
 #endif // WEBSUPPORT

+ 1
- 1
Marlin/src/HAL/HAL_ESP32/web.cpp Vedi File

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

+ 12
- 3
Marlin/src/HAL/HAL_ESP32/wifi.cpp Vedi File

22
 
22
 
23
 #ifdef ARDUINO_ARCH_ESP32
23
 #ifdef ARDUINO_ARCH_ESP32
24
 
24
 
25
+#include "../../core/serial.h"
25
 #include "../../inc/MarlinConfigPre.h"
26
 #include "../../inc/MarlinConfigPre.h"
26
 
27
 
27
 #if ENABLED(WIFISUPPORT)
28
 #if ENABLED(WIFISUPPORT)
38
 #endif
39
 #endif
39
 
40
 
40
 void wifi_init() {
41
 void wifi_init() {
42
+
43
+  SERIAL_ECHO_MSG("Starting WiFi...");
44
+
41
   WiFi.mode(WIFI_STA);
45
   WiFi.mode(WIFI_STA);
42
   WiFi.begin(WIFI_SSID, WIFI_PWD);
46
   WiFi.begin(WIFI_SSID, WIFI_PWD);
43
 
47
 
44
   while (WiFi.waitForConnectResult() != WL_CONNECTED) {
48
   while (WiFi.waitForConnectResult() != WL_CONNECTED) {
49
+    SERIAL_ERROR_MSG("Unable to connect to WiFi with SSID '" WIFI_SSID "', restarting.");
45
     delay(5000);
50
     delay(5000);
46
     ESP.restart();
51
     ESP.restart();
47
   }
52
   }
48
 
53
 
49
   delay(10);
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
   MDNS.addService("http", "tcp", 80);
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
 #endif // WIFISUPPORT
66
 #endif // WIFISUPPORT

+ 2
- 0
Marlin/src/pins/pins_ESP32.h Vedi File

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

Loading…
Annulla
Salva