Skip to content

Commit b72cd4c

Browse files
authored
Use less memory (#644)
a) closeFile() now does SD.end() to release memory after running a file from SD. b) Several task stacks are smaller c) All tasks now check their free space if DEBUG_REPORT_STACK_FREE is defined. platformio.ini has a commented-out line that can be uncommented to turn that on. d) Similarly, platformio.ini can turn on DEBUG_REPORT_HEAP_SIZE e) Fixed a small leak that occurred when listing local files. With these changes, the heap size tends to hover around 53K, dropping to about 37K when running a file from SD.
1 parent ab36c5a commit b72cd4c

File tree

13 files changed

+221
-175
lines changed

13 files changed

+221
-175
lines changed

Grbl_Esp32/src/I2SOut.cpp

Lines changed: 131 additions & 120 deletions
Large diffs are not rendered by default.

Grbl_Esp32/src/Limits.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,8 @@ void limits_init() {
330330
}
331331

332332
if (limit_sw_queue == NULL) {
333-
grbl_msg_sendf(CLIENT_SERIAL,
334-
MsgLevel::Info,
335-
"%s limit switch on pin %s",
336-
reportAxisNameMsg(axis, gang_index),
337-
pinName(pin).c_str());
333+
grbl_msg_sendf(
334+
CLIENT_SERIAL, MsgLevel::Info, "%s limit switch on pin %s", reportAxisNameMsg(axis, gang_index), pinName(pin).c_str());
338335
}
339336
}
340337
}
@@ -427,17 +424,19 @@ void limitCheckTask(void* pvParameters) {
427424
mc_reset(); // Initiate system kill.
428425
sys_rt_exec_alarm = ExecAlarm::HardLimit; // Indicate hard limit critical event
429426
}
427+
static UBaseType_t uxHighWaterMark = 0;
428+
reportTaskStackSize(uxHighWaterMark);
430429
}
431430
}
432431

433432
float limitsMaxPosition(uint8_t axis) {
434-
float mpos = axis_settings[axis]->home_mpos->get();
433+
float mpos = axis_settings[axis]->home_mpos->get();
435434

436435
return bitnum_istrue(homing_dir_mask->get(), axis) ? mpos + axis_settings[axis]->max_travel->get() : mpos;
437436
}
438437

439438
float limitsMinPosition(uint8_t axis) {
440-
float mpos = axis_settings[axis]->home_mpos->get();
439+
float mpos = axis_settings[axis]->home_mpos->get();
441440

442441
return bitnum_istrue(homing_dir_mask->get(), axis) ? mpos : mpos - axis_settings[axis]->max_travel->get();
443442
}

Grbl_Esp32/src/Motors/Servo.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,14 @@ namespace Motors {
6060
xLastWakeTime = xTaskGetTickCount(); // Initialise the xLastWakeTime variable with the current time.
6161
vTaskDelay(2000); // initial delay
6262
while (true) { // don't ever return from this or the task dies
63-
64-
// static UBaseType_t uxHighWaterMark = 0;
65-
// if (uxHighWaterMark != uxTaskGetStackHighWaterMark(NULL)) {
66-
// uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
67-
// grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Servo Task Min Stack Space: %d", uxHighWaterMark);
68-
// }
69-
7063
for (Servo* p = List; p; p = p->link) {
7164
p->update();
7265
}
7366

7467
vTaskDelayUntil(&xLastWakeTime, xUpdate);
68+
69+
static UBaseType_t uxHighWaterMark = 0;
70+
reportTaskStackSize(uxHighWaterMark);
7571
}
7672
}
7773

Grbl_Esp32/src/Motors/TrinamicDriver.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,10 @@ namespace Motors {
360360
} // sys.state
361361
} // if mask
362362

363-
// static UBaseType_t uxHighWaterMark = 0;
364-
// if (uxHighWaterMark != uxTaskGetStackHighWaterMark(NULL)) {
365-
// uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
366-
// grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "SG Task Stack Space: %d", uxHighWaterMark);
367-
// }
368-
369363
vTaskDelayUntil(&xLastWakeTime, xreadSg);
364+
365+
static UBaseType_t uxHighWaterMark = 0;
366+
reportTaskStackSize(uxHighWaterMark);
370367
}
371368
}
372369
}

Grbl_Esp32/src/Report.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,3 +938,13 @@ char* reportAxisNameMsg(uint8_t axis) {
938938
sprintf(name, "%c Axis", report_get_axis_letter(axis));
939939
return name;
940940
}
941+
942+
void reportTaskStackSize(UBaseType_t& saved) {
943+
#ifdef DEBUG_REPORT_STACK_FREE
944+
UBaseType_t newHighWater = uxTaskGetStackHighWaterMark(NULL);
945+
if (newHighWater != saved) {
946+
saved = newHighWater;
947+
grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "%s Min Stack Space: %d", pcTaskGetTaskName(NULL), saved);
948+
}
949+
#endif
950+
}

Grbl_Esp32/src/Report.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ char report_get_axis_letter(uint8_t axis);
127127
char* reportAxisLimitsMsg(uint8_t axis);
128128
char* reportAxisNameMsg(uint8_t axis);
129129
char* reportAxisNameMsg(uint8_t axis, uint8_t dual_axis);
130+
131+
void reportTaskStackSize(UBaseType_t& saved);

Grbl_Esp32/src/SDCard.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ boolean closeFile() {
8181
SD_ready_next = false;
8282
sd_current_line_number = 0;
8383
myFile.close();
84+
SD.end();
8485
return true;
8586
}
8687

@@ -147,7 +148,7 @@ uint8_t get_sd_state(bool refresh) {
147148
sd_state = SDCARD_NOT_PRESENT;
148149
//using default value for speed ? should be parameter
149150
//refresh content if card was removed
150-
if (SD.begin((GRBL_SPI_SS == -1) ? SS : GRBL_SPI_SS, SPI, GRBL_SPI_FREQ)) {
151+
if (SD.begin((GRBL_SPI_SS == -1) ? SS : GRBL_SPI_SS, SPI, GRBL_SPI_FREQ, "/sd", 2)) {
151152
if (SD.cardSize() > 0) {
152153
sd_state = SDCARD_IDLE;
153154
}

Grbl_Esp32/src/Serial.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,39 @@ uint8_t serial_get_rx_buffer_available(uint8_t client) {
6868
return client_buffer[client].availableforwrite();
6969
}
7070

71+
void heapCheckTask(void* pvParameters) {
72+
static uint32_t heapSize = 0;
73+
while (true) {
74+
uint32_t newHeapSize = xPortGetFreeHeapSize();
75+
if (newHeapSize != heapSize) {
76+
heapSize = newHeapSize;
77+
grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "heap %d", heapSize);
78+
}
79+
vTaskDelay(3000 / portTICK_RATE_MS); // Yield to other tasks
80+
81+
static UBaseType_t uxHighWaterMark = 0;
82+
reportTaskStackSize(uxHighWaterMark);
83+
}
84+
}
85+
7186
void serial_init() {
87+
#ifdef DEBUG_REPORT_HEAP_SIZE
88+
// For a 2000-word stack, uxTaskGetStackHighWaterMark reports 288 words available
89+
xTaskCreatePinnedToCore(heapCheckTask, "heapTask", 2000, NULL, 1, NULL, 1);
90+
#endif
91+
7292
Serial.begin(BAUD_RATE);
7393
Serial.setRxBufferSize(256);
7494
// reset all buffers
7595
serial_reset_read_buffer(CLIENT_ALL);
7696
grbl_send(CLIENT_SERIAL, "\r\n"); // create some white space after ESP32 boot info
7797
serialCheckTaskHandle = 0;
7898
// create a task to check for incoming data
99+
// For a 4096-word stack, uxTaskGetStackHighWaterMark reports 244 words available
100+
// after WebUI attaches.
79101
xTaskCreatePinnedToCore(serialCheckTask, // task
80102
"serialCheckTask", // name for task
81-
8192, // size of task stack
103+
4096, // size of task stack
82104
NULL, // parameters
83105
1, // priority
84106
&serialCheckTaskHandle,
@@ -89,9 +111,10 @@ void serial_init() {
89111
// this task runs and checks for data on all interfaces
90112
// REaltime stuff is acted upon, then characters are added to the appropriate buffer
91113
void serialCheckTask(void* pvParameters) {
92-
uint8_t data = 0;
93-
uint8_t client = CLIENT_ALL; // who sent the data
94-
while (true) { // run continuously
114+
uint8_t data = 0;
115+
uint8_t client = CLIENT_ALL; // who sent the data
116+
static UBaseType_t uxHighWaterMark = 0;
117+
while (true) { // run continuously
95118
while (any_client_has_data()) {
96119
if (Serial.available()) {
97120
client = CLIENT_SERIAL;
@@ -149,7 +172,10 @@ void serialCheckTask(void* pvParameters) {
149172
WebUI::Serial2Socket.handle_flush();
150173
#endif
151174
vTaskDelay(1 / portTICK_RATE_MS); // Yield to other tasks
152-
} // while(true)
175+
176+
static UBaseType_t uxHighWaterMark = 0;
177+
reportTaskStackSize(uxHighWaterMark);
178+
} // while(true)
153179
}
154180

155181
void serial_reset_read_buffer(uint8_t client) {

Grbl_Esp32/src/Spindles/VFDSpindle.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ namespace Spindles {
185185
// Wait a bit before we retry. Set the delay to poll-rate. Not sure
186186
// if we should use a different value...
187187
vTaskDelay(VFD_RS485_POLL_RATE);
188+
189+
static UBaseType_t uxHighWaterMark = 0;
190+
reportTaskStackSize(uxHighWaterMark);
188191
}
189192
}
190193

Grbl_Esp32/src/System.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ void controlCheckTask(void* pvParameters) {
123123
system_exec_control_pin(pins);
124124
}
125125
debouncing = false;
126+
127+
static UBaseType_t uxHighWaterMark = 0;
128+
reportTaskStackSize(uxHighWaterMark);
126129
}
127130
}
128131
#endif

0 commit comments

Comments
 (0)