Skip to content

Commit

Permalink
✨ Firmware Upload via Binary Transfer (#23462)
Browse files Browse the repository at this point in the history
  • Loading branch information
GHGiampy authored Jan 9, 2022
1 parent 54f218e commit 1363b43
Show file tree
Hide file tree
Showing 8 changed files with 770 additions and 24 deletions.
5 changes: 5 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,11 @@
// Add an optimized binary file transfer mode, initiated with 'M28 B1'
//#define BINARY_FILE_TRANSFER

#if ENABLED(BINARY_FILE_TRANSFER)
// Include extra facilities (e.g., 'M20 F') supporting firmware upload via BINARY_FILE_TRANSFER
//#define CUSTOM_FIRMWARE_UPLOAD
#endif

/**
* Set this option to one of the following (or the board's defaults apply):
*
Expand Down
8 changes: 7 additions & 1 deletion Marlin/src/gcode/sd/M20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
void GcodeSuite::M20() {
if (card.flag.mounted) {
SERIAL_ECHOLNPGM(STR_BEGIN_FILE_LIST);
card.ls(TERN_(LONG_FILENAME_HOST_SUPPORT, parser.boolval('L')));
card.ls(
TERN_(CUSTOM_FIRMWARE_UPLOAD, parser.boolval('F'))
#if BOTH(CUSTOM_FIRMWARE_UPLOAD, LONG_FILENAME_HOST_SUPPORT)
,
#endif
TERN_(LONG_FILENAME_HOST_SUPPORT, parser.boolval('L'))
);
SERIAL_ECHOLNPGM(STR_END_FILE_LIST);
}
else
Expand Down
16 changes: 8 additions & 8 deletions Marlin/src/pins/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,21 +558,21 @@
#elif MB(CHITU3D_V9)
#include "stm32f1/pins_CHITU3D_V9.h" // STM32F1 env:chitu_f103 env:chitu_f103_maple
#elif MB(CREALITY_V4)
#include "stm32f1/pins_CREALITY_V4.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_maple
#include "stm32f1/pins_CREALITY_V4.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer env:STM32F103RET6_creality_maple
#elif MB(CREALITY_V4210)
#include "stm32f1/pins_CREALITY_V4210.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_maple
#include "stm32f1/pins_CREALITY_V4210.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer env:STM32F103RET6_creality_maple
#elif MB(CREALITY_V423)
#include "stm32f1/pins_CREALITY_V423.h" // STM32F1 env:STM32F103RET6_creality
#include "stm32f1/pins_CREALITY_V423.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer
#elif MB(CREALITY_V427)
#include "stm32f1/pins_CREALITY_V427.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_maple
#include "stm32f1/pins_CREALITY_V427.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer env:STM32F103RET6_creality_maple
#elif MB(CREALITY_V431, CREALITY_V431_A, CREALITY_V431_B, CREALITY_V431_C, CREALITY_V431_D)
#include "stm32f1/pins_CREALITY_V431.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_maple
#include "stm32f1/pins_CREALITY_V431.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer env:STM32F103RET6_creality_maple
#elif MB(CREALITY_V452)
#include "stm32f1/pins_CREALITY_V452.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_maple
#include "stm32f1/pins_CREALITY_V452.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer env:STM32F103RET6_creality_maple
#elif MB(CREALITY_V453)
#include "stm32f1/pins_CREALITY_V453.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_maple
#include "stm32f1/pins_CREALITY_V453.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer env:STM32F103RET6_creality_maple
#elif MB(CREALITY_V24S1)
#include "stm32f1/pins_CREALITY_V24S1.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_maple
#include "stm32f1/pins_CREALITY_V24S1.h" // STM32F1 env:STM32F103RET6_creality env:STM32F103RET6_creality_xfer env:STM32F103RET6_creality_maple
#elif MB(TRIGORILLA_PRO)
#include "stm32f1/pins_TRIGORILLA_PRO.h" // STM32F1 env:trigorilla_pro env:trigorilla_pro_maple
#elif MB(FLY_MINI)
Expand Down
39 changes: 27 additions & 12 deletions Marlin/src/sd/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,15 @@ char *createFilename(char * const buffer, const dir_t &p) {
}

//
// Return 'true' if the item is a folder or G-code file
// Return 'true' if the item is something Marlin can read
//
bool CardReader::is_dir_or_gcode(const dir_t &p) {
bool CardReader::is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD, bool onlyBin/*=false*/)) {
//uint8_t pn0 = p.name[0];

#if DISABLED(CUSTOM_FIRMWARE_UPLOAD)
constexpr bool onlyBin = false;
#endif

if ( (p.attributes & DIR_ATT_HIDDEN) // Hidden by attribute
// When readDir() > 0 these must be false:
//|| pn0 == DIR_NAME_FREE || pn0 == DIR_NAME_DELETED // Clear or Deleted entry
Expand All @@ -211,7 +215,11 @@ bool CardReader::is_dir_or_gcode(const dir_t &p) {

return (
flag.filenameIsDir // All Directories are ok
|| (p.name[8] == 'G' && p.name[9] != '~') // Non-backup *.G* files are accepted
|| (!onlyBin && p.name[8] == 'G'
&& p.name[9] != '~') // Non-backup *.G* files are accepted
|| ( onlyBin && p.name[8] == 'B'
&& p.name[9] == 'I'
&& p.name[10] == 'N') // BIN files are accepted
);
}

Expand All @@ -222,7 +230,7 @@ int CardReader::countItems(SdFile dir) {
dir_t p;
int c = 0;
while (dir.readDir(&p, longFilename) > 0)
c += is_dir_or_gcode(p);
c += is_visible_entity(p);

#if ALL(SDCARD_SORT_ALPHA, SDSORT_USES_RAM, SDSORT_CACHE_NAMES)
nrFiles = c;
Expand All @@ -237,7 +245,7 @@ int CardReader::countItems(SdFile dir) {
void CardReader::selectByIndex(SdFile dir, const uint8_t index) {
dir_t p;
for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0;) {
if (is_dir_or_gcode(p)) {
if (is_visible_entity(p)) {
if (cnt == index) {
createFilename(filename, p);
return; // 0 based index
Expand All @@ -253,7 +261,7 @@ void CardReader::selectByIndex(SdFile dir, const uint8_t index) {
void CardReader::selectByName(SdFile dir, const char * const match) {
dir_t p;
for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0; cnt++) {
if (is_dir_or_gcode(p)) {
if (is_visible_entity(p)) {
createFilename(filename, p);
if (strcasecmp(match, filename) == 0) return;
}
Expand All @@ -272,6 +280,7 @@ void CardReader::selectByName(SdFile dir, const char * const match) {
*/
void CardReader::printListing(
SdFile parent, const char * const prepend
OPTARG(CUSTOM_FIRMWARE_UPLOAD, bool onlyBin/*=false*/)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames/*=false*/)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong/*=nullptr*/)
) {
Expand All @@ -297,20 +306,20 @@ void CardReader::printListing(
char pathLong[lenPrependLong + strlen(longFilename) + 1];
if (prependLong) { strcpy(pathLong, prependLong); pathLong[lenPrependLong - 1] = '/'; }
strcpy(pathLong + lenPrependLong, longFilename);
printListing(child, path, true, pathLong);
printListing(child, path OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin), true, pathLong);
}
else
printListing(child, path);
printListing(child, path OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin));
#else
printListing(child, path);
printListing(child, path OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin));
#endif
}
else {
SERIAL_ECHO_MSG(STR_SD_CANT_OPEN_SUBDIR, dosFilename);
return;
}
}
else if (is_dir_or_gcode(p)) {
else if (is_visible_entity(p OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin))) {
if (prepend) { SERIAL_ECHO(prepend); SERIAL_CHAR('/'); }
SERIAL_ECHO(createFilename(filename, p));
SERIAL_CHAR(' ');
Expand All @@ -330,10 +339,16 @@ void CardReader::printListing(
//
// List all files on the SD card
//
void CardReader::ls(TERN_(LONG_FILENAME_HOST_SUPPORT, bool includeLongNames/*=false*/)) {
void CardReader::ls(
TERN_(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin/*=false*/)
#if BOTH(CUSTOM_FIRMWARE_UPLOAD, LONG_FILENAME_HOST_SUPPORT)
,
#endif
TERN_(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames/*=false*/)
) {
if (flag.mounted) {
root.rewind();
printListing(root, nullptr OPTARG(LONG_FILENAME_HOST_SUPPORT, includeLongNames));
printListing(root, nullptr OPTARG(CUSTOM_FIRMWARE_UPLOAD, onlyBin) OPTARG(LONG_FILENAME_HOST_SUPPORT, includeLongNames));
}
}

Expand Down
11 changes: 9 additions & 2 deletions Marlin/src/sd/cardreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ class CardReader {
FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { selectFileByIndex(nr); }
#endif

static void ls(TERN_(LONG_FILENAME_HOST_SUPPORT, bool includeLongNames=false));
static void ls(
TERN_(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin=false)
#if BOTH(CUSTOM_FIRMWARE_UPLOAD, LONG_FILENAME_HOST_SUPPORT)
,
#endif
TERN_(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames=false)
);

#if ENABLED(POWER_LOSS_RECOVERY)
static bool jobRecoverFileExists();
Expand Down Expand Up @@ -331,12 +337,13 @@ class CardReader {
//
// Directory items
//
static bool is_dir_or_gcode(const dir_t &p);
static bool is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin=false));
static int countItems(SdFile dir);
static void selectByIndex(SdFile dir, const uint8_t index);
static void selectByName(SdFile dir, const char * const match);
static void printListing(
SdFile parent, const char * const prepend
OPTARG(CUSTOM_FIRMWARE_UPLOAD, const bool onlyBin=false)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames=false)
OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong=nullptr)
);
Expand Down
Loading

0 comments on commit 1363b43

Please sign in to comment.