1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-03 03:13:25 +02:00

Memory Leak WIP - SD card

This commit is contained in:
Mitch Bradley
2020-09-27 12:22:41 -10:00
parent c0c3c20548
commit 93821c3f2a
9 changed files with 47 additions and 1 deletions

View File

@@ -478,6 +478,7 @@ void mc_reset() {
//Report print stopped
report_feedback_message(Message::SdFileQuit);
closeFile();
sd_close();
}
#endif
// Kill steppers only if in any motion state, i.e. cycle, actively holding, or homing.

View File

@@ -208,6 +208,10 @@ Error report_ngc(const char* value, WebUI::AuthenticationLevel auth_level, WebUI
report_ngc_parameters(out->client());
return Error::Ok;
}
Error show_heap(const char* value, WebUI::AuthenticationLevel auth_level, WebUI::ESPResponseStream* out) {
report_heap("");
return Error::Ok;
}
Error home(int cycle) {
if (homing_enable->get() == false) {
return Error::SettingDisabled;
@@ -391,6 +395,7 @@ void make_grbl_commands() {
new GrblCommand("NVX", "Settings/Erase", Setting::eraseNVS, idleOrAlarm, WA);
new GrblCommand("V", "Settings/Stats", Setting::report_nvs_stats, idleOrAlarm);
new GrblCommand("#", "GCode/Offsets", report_ngc, idleOrAlarm);
new GrblCommand("M", "Memory", show_heap, anyState);
new GrblCommand("H", "Home", home_all, idleOrAlarm);
#ifdef HOMING_SINGLE_AXIS_COMMANDS
new GrblCommand("HX", "Home/X", home_x, idleOrAlarm);

View File

@@ -243,6 +243,7 @@ void report_status_message(Error status_code, uint8_t client) {
grbl_notifyf("SD print error", "Error:%d during SD file at line: %d", status_code, sd_get_current_line_number());
grbl_sendf(CLIENT_ALL, "error:%d in SD file at line %d\r\n", status_code, sd_get_current_line_number());
closeFile();
sd_close();
}
return;
}
@@ -930,3 +931,7 @@ char report_get_axis_letter(uint8_t axis) {
return '?';
}
}
void report_heap(const char* prefix) {
grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "heap %s %d", prefix, xPortGetFreeHeapSize());
}

View File

@@ -117,3 +117,4 @@ void report_hex_msg(char* buf, const char* prefix, int len);
void report_hex_msg(uint8_t* buf, const char* prefix, int len);
char report_get_axis_letter(uint8_t axis);
void report_heap(const char* prefix);

View File

@@ -125,6 +125,11 @@ uint32_t sd_get_current_line_number() {
uint8_t sd_state = SDCARD_IDLE;
void sd_close() {
SD.end();
report_heap("]");
}
uint8_t get_sd_state(bool refresh) {
#if defined(SDCARD_DET_PIN) && SDCARD_SD_PIN != -1
//no need to go further if SD detect is not correct
@@ -146,11 +151,13 @@ uint8_t get_sd_state(bool refresh) {
sd_state = SDCARD_NOT_PRESENT;
//using default value for speed ? should be parameter
//refresh content if card was removed
report_heap("[");
if (SD.begin((GRBL_SPI_SS == -1) ? SS : GRBL_SPI_SS, SPI, GRBL_SPI_FREQ)) {
if (SD.cardSize() > 0) {
sd_state = SDCARD_IDLE;
}
}
report_heap("|");
return sd_state;
}

View File

@@ -43,3 +43,4 @@ void readFile(fs::FS& fs, const char* path);
float sd_report_perc_complete();
uint32_t sd_get_current_line_number();
void sd_get_current_filename(char* name);
void sd_close(); // Release SD resources

View File

@@ -68,7 +68,20 @@ uint8_t serial_get_rx_buffer_available(uint8_t client) {
return client_buffer[client].availableforwrite();
}
static TaskHandle_t heapCheckTaskHandle = 0;
//#include <esp_heap_caps.h>
void heapCheckTask(void* pvParameters) {
while (true) {
vTaskDelay(3000 / portTICK_RATE_MS); // Yield to other tasks
report_heap("");
}
}
void serial_init() {
// heapCheckTaskHandle = 0;
// xTaskCreatePinnedToCore(heapCheckTask, "heapTask", 8192, NULL, 1, &heapCheckTaskHandle, 1);
Serial.begin(BAUD_RATE);
// reset all buffers
serial_reset_read_buffer(CLIENT_ALL);
@@ -86,7 +99,7 @@ void serial_init() {
}
// this task runs and checks for data on all interfaces
// REaltime stuff is acted upon, then characters are added to the appropriate buffer
// Handles realtime stuff then adds characters to the appropriate buffer
void serialCheckTask(void* pvParameters) {
uint8_t data = 0;
uint8_t client = CLIENT_ALL; // who sent the data

View File

@@ -1517,6 +1517,7 @@ namespace WebUI {
sdUploadFile = SD.open(filename, FILE_READ);
filesize = sdUploadFile.size();
sdUploadFile.close();
sd_close();
if (_webserver->arg(sizeargname) != String(filesize)) {
_upload_status = UploadStatusType::FAILED;
pushError(ESP_ERROR_UPLOAD, "File upload mismatch");
@@ -1542,6 +1543,7 @@ namespace WebUI {
grbl_send(CLIENT_ALL, "[MSG:Upload failed]\r\n");
if (sdUploadFile) {
sdUploadFile.close();
sd_close();
}
return;
}
@@ -1556,6 +1558,7 @@ namespace WebUI {
SD.remove(filename);
}
set_sd_state(SDCARD_IDLE);
sd_close();
}
COMMANDS::wait(0);
}

View File

@@ -660,11 +660,13 @@ namespace WebUI {
}
if (sys.state != State::Idle) {
webPrintln("Busy");
sd_close();
return Error::IdleError;
}
if (!openFile(SD, parameter)) {
report_status_message(Error::SdFailedRead, (espresponse) ? espresponse->client() : CLIENT_ALL);
webPrintln("");
sd_close();
return Error::Ok;
}
char fileLine[255];
@@ -672,6 +674,7 @@ namespace WebUI {
//No need notification here it is just a macro
closeFile();
webPrintln("");
sd_close();
return Error::Ok;
}
SD_client = (espresponse) ? espresponse->client() : CLIENT_ALL;
@@ -679,6 +682,7 @@ namespace WebUI {
(espresponse) ? espresponse->client() : CLIENT_ALL); // execute the first line
report_realtime_status((espresponse) ? espresponse->client() : CLIENT_ALL);
webPrintln("");
sd_close();
return Error::Ok;
}
@@ -700,22 +704,26 @@ namespace WebUI {
File file2del = SD.open(path);
if (!file2del) {
webPrintln("Cannot stat file!");
sd_close();
return Error::SdFileNotFound;
}
if (file2del.isDirectory()) {
if (!SD.rmdir(path)) {
webPrintln("Cannot delete directory! Is directory empty?");
sd_close();
return Error::SdFailedDelDir;
}
webPrintln("Directory deleted.");
} else {
if (!SD.remove(path)) {
webPrintln("Cannot delete file!");
sd_close();
return Error::SdFailedDelFile;
}
webPrintln("File deleted.");
}
file2del.close();
sd_close();
return Error::Ok;
}
@@ -732,6 +740,7 @@ namespace WebUI {
}
webPrintln("");
listDir(SD, "/", 10, espresponse->client());
sd_close();
String ssd = "[SD Free:" + ESPResponseStream::formatBytes(SD.totalBytes() - SD.usedBytes());
ssd += " Used:" + ESPResponseStream::formatBytes(SD.usedBytes());
ssd += " Total:" + ESPResponseStream::formatBytes(SD.totalBytes());
@@ -803,6 +812,7 @@ namespace WebUI {
}
#endif
webPrintln(resp);
sd_close();
return Error::Ok;
}