SPIFFS-backed PersistentStore for ESP32 (#13566)
This commit is contained in:
		
				
					committed by
					
						 Scott Lahteine
						Scott Lahteine
					
				
			
			
				
	
			
			
			
						parent
						
							af92ee9dd6
						
					
				
				
					commit
					b21ca53dfc
				
			| @@ -41,7 +41,10 @@ | |||||||
|   #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 | ||||||
|  |  | ||||||
| // -------------------------------------------------------------------------- | // -------------------------------------------------------------------------- | ||||||
| @@ -95,9 +98,12 @@ void HAL_init(void) { | |||||||
|       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(); |   i2s_init(); | ||||||
|   | |||||||
							
								
								
									
										93
									
								
								Marlin/src/HAL/HAL_ESP32/persistent_store_spiffs.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								Marlin/src/HAL/HAL_ESP32/persistent_store_spiffs.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,93 @@ | |||||||
|  | /** | ||||||
|  |  * Marlin 3D Printer Firmware | ||||||
|  |  * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] | ||||||
|  |  * | ||||||
|  |  * Based on Sprinter and grbl. | ||||||
|  |  * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm | ||||||
|  |  * | ||||||
|  |  * This program is free software: you can redistribute it and/or modify | ||||||
|  |  * it under the terms of the GNU General Public License as published by | ||||||
|  |  * the Free Software Foundation, either version 3 of the License, or | ||||||
|  |  * (at your option) any later version. | ||||||
|  |  * | ||||||
|  |  * This program is distributed in the hope that it will be useful, | ||||||
|  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  |  * GNU General Public License for more details. | ||||||
|  |  * | ||||||
|  |  * You should have received a copy of the GNU General Public License | ||||||
|  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #ifdef ARDUINO_ARCH_ESP32 | ||||||
|  |  | ||||||
|  | #include "../../inc/MarlinConfig.h" | ||||||
|  |  | ||||||
|  | #if ENABLED(EEPROM_SETTINGS) && DISABLED(FLASH_EEPROM_EMULATION) | ||||||
|  |  | ||||||
|  | #include "../shared/persistent_store_api.h" | ||||||
|  |  | ||||||
|  | #include "SPIFFS.h" | ||||||
|  | #include "FS.h" | ||||||
|  | #include "spiffs.h" | ||||||
|  |  | ||||||
|  | #define HAL_ESP32_EEPROM_SIZE 4096 | ||||||
|  |  | ||||||
|  | File eeprom_file; | ||||||
|  |  | ||||||
|  | bool PersistentStore::access_start() { | ||||||
|  |   if (spiffs_initialized) { | ||||||
|  |     eeprom_file = SPIFFS.open("/eeprom.dat", "r+"); | ||||||
|  |  | ||||||
|  |     size_t file_size = eeprom_file.size(); | ||||||
|  |     if (file_size < HAL_ESP32_EEPROM_SIZE) { | ||||||
|  |       bool write_ok = eeprom_file.seek(file_size); | ||||||
|  |  | ||||||
|  |       while (write_ok && file_size < HAL_ESP32_EEPROM_SIZE) { | ||||||
|  |         write_ok = eeprom_file.write(0xFF) == 1; | ||||||
|  |         file_size++; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return true; | ||||||
|  |   } | ||||||
|  |   return false; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool PersistentStore::access_finish() { | ||||||
|  |   eeprom_file.close(); | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) { | ||||||
|  |   if (!eeprom_file.seek(pos)) return true; // return true for any error | ||||||
|  |   if (eeprom_file.write(value, size) != size) return true; | ||||||
|  |  | ||||||
|  |   crc16(crc, value, size); | ||||||
|  |   pos += size; | ||||||
|  |  | ||||||
|  |   return false; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) { | ||||||
|  |   if (!eeprom_file.seek(pos)) return true; // return true for any error | ||||||
|  |  | ||||||
|  |   if (writing) { | ||||||
|  |     if (eeprom_file.read(value, size) != size) return true; | ||||||
|  |     crc16(crc, value, size); | ||||||
|  |   } | ||||||
|  |   else { | ||||||
|  |     uint8_t tmp[size]; | ||||||
|  |     if (eeprom_file.read(tmp, size) != size) return true; | ||||||
|  |     crc16(crc, tmp, size); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   pos += size; | ||||||
|  |  | ||||||
|  |   return false; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | size_t PersistentStore::capacity() { return HAL_ESP32_EEPROM_SIZE; } | ||||||
|  |  | ||||||
|  | #endif // EEPROM_SETTINGS | ||||||
|  | #endif // ARDUINO_ARCH_ESP32 | ||||||
							
								
								
									
										44
									
								
								Marlin/src/HAL/HAL_ESP32/spiffs.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								Marlin/src/HAL/HAL_ESP32/spiffs.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  | /** | ||||||
|  |  * Marlin 3D Printer Firmware | ||||||
|  |  * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] | ||||||
|  |  * | ||||||
|  |  * Based on Sprinter and grbl. | ||||||
|  |  * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm | ||||||
|  |  * | ||||||
|  |  * This program is free software: you can redistribute it and/or modify | ||||||
|  |  * it under the terms of the GNU General Public License as published by | ||||||
|  |  * the Free Software Foundation, either version 3 of the License, or | ||||||
|  |  * (at your option) any later version. | ||||||
|  |  * | ||||||
|  |  * This program is distributed in the hope that it will be useful, | ||||||
|  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  |  * GNU General Public License for more details. | ||||||
|  |  * | ||||||
|  |  * You should have received a copy of the GNU General Public License | ||||||
|  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #ifdef ARDUINO_ARCH_ESP32 | ||||||
|  |  | ||||||
|  | #include "../../inc/MarlinConfigPre.h" | ||||||
|  |  | ||||||
|  | #if EITHER(WEBSUPPORT, EEPROM_SETTINGS) | ||||||
|  |  | ||||||
|  | #include "../../core/serial.h" | ||||||
|  |  | ||||||
|  | #include "FS.h" | ||||||
|  | #include "SPIFFS.h" | ||||||
|  |  | ||||||
|  | bool spiffs_initialized; | ||||||
|  |  | ||||||
|  | void spiffs_init() { | ||||||
|  |   if (SPIFFS.begin()) | ||||||
|  |     spiffs_initialized = true; | ||||||
|  |   else | ||||||
|  |     SERIAL_ECHO_MSG("SPIFFS mount failed"); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #endif // WEBSUPPORT | ||||||
|  | #endif // ARDUINO_ARCH_ESP32 | ||||||
							
								
								
									
										26
									
								
								Marlin/src/HAL/HAL_ESP32/spiffs.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								Marlin/src/HAL/HAL_ESP32/spiffs.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | |||||||
|  | /** | ||||||
|  |  * Marlin 3D Printer Firmware | ||||||
|  |  * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] | ||||||
|  |  * | ||||||
|  |  * Based on Sprinter and grbl. | ||||||
|  |  * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm | ||||||
|  |  * | ||||||
|  |  * This program is free software: you can redistribute it and/or modify | ||||||
|  |  * it under the terms of the GNU General Public License as published by | ||||||
|  |  * the Free Software Foundation, either version 3 of the License, or | ||||||
|  |  * (at your option) any later version. | ||||||
|  |  * | ||||||
|  |  * This program is distributed in the hope that it will be useful, | ||||||
|  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
|  |  * GNU General Public License for more details. | ||||||
|  |  * | ||||||
|  |  * You should have received a copy of the GNU General Public License | ||||||
|  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | extern bool spiffs_initialized; | ||||||
|  |  | ||||||
|  | void spiffs_init(); | ||||||
| @@ -23,9 +23,6 @@ | |||||||
|  |  | ||||||
| #if ENABLED(WEBSUPPORT) | #if ENABLED(WEBSUPPORT) | ||||||
|  |  | ||||||
| #include "../../core/serial.h" |  | ||||||
|  |  | ||||||
| #include "FS.h" |  | ||||||
| #include "SPIFFS.h" | #include "SPIFFS.h" | ||||||
| #include "wifi.h" | #include "wifi.h" | ||||||
|  |  | ||||||
| @@ -37,13 +34,9 @@ void onNotFound(AsyncWebServerRequest *request){ | |||||||
|  |  | ||||||
| void web_init() { | void web_init() { | ||||||
|   server.addHandler(&events);       // attach AsyncEventSource |   server.addHandler(&events);       // attach AsyncEventSource | ||||||
|   if (SPIFFS.begin()) { |  | ||||||
|   server.serveStatic("/", SPIFFS, "/www").setDefaultFile("index.html"); |   server.serveStatic("/", SPIFFS, "/www").setDefaultFile("index.html"); | ||||||
|   server.onNotFound(onNotFound); |   server.onNotFound(onNotFound); | ||||||
| } | } | ||||||
|   else |  | ||||||
|     SERIAL_ECHO_MSG("SPIFFS Mount Failed"); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| #endif // WEBSUPPORT | #endif // WEBSUPPORT | ||||||
| #endif // ARDUINO_ARCH_ESP32 | #endif // ARDUINO_ARCH_ESP32 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user