@@ -48,7 +48,7 @@ const int dfuStateNameCount = sizeof(dfuStateName) / sizeof(dfuStateName[0]);
4848const char * dfuEqualSigns = " ==================================================" ;
4949
5050// ----------------------------------------
51- // Constants
51+ // Locals
5252// ----------------------------------------
5353
5454static bool dfuLoopInUpdate; // Loop in deviceFirmwareUpdate while set
@@ -60,7 +60,7 @@ void deviceFirmwareActionMenu(DEVICE_FIRMWARE_CTX * ctx)
6060{
6161 if (ctx->_doAll == false )
6262 {
63- inMainMenu = true ;
63+ // inMainMenu = true;
6464 systemPrintf (" \r\n Action:\r\n " );
6565
6666 // Display the menu
@@ -74,7 +74,7 @@ void deviceFirmwareActionMenu(DEVICE_FIRMWARE_CTX * ctx)
7474 systemPrintf (" x) Exit\r\n " );
7575
7676 // Discard the input
77- serialInputClear ();
77+ serialInputClear (&Serial );
7878
7979 // Output the prompt
8080 systemPrintf (" Select an action: " );
@@ -114,11 +114,15 @@ bool deviceFirmwareBufferAllocate(DEVICE_FIRMWARE_CTX * ctx)
114114 }
115115
116116 // Return buffer allocation status
117- return (dfuFirmwareData._address && dfuFirmwareFileNamesNet._address
117+ return (dfuFirmwareData._address
118+ && dfuFirmwareFileNamesNet._address
118119 && ((ctx->_doAll == true )
119120 || (dfuFirmwareFileNamesNvm._address
120121 && ((present.microSd == false )
121- || dfuFirmwareFileNamesSd._address ))));
122+ || dfuFirmwareFileNamesSd._address )
123+ )
124+ )
125+ );
122126}
123127
124128// ----------------------------------------
@@ -220,7 +224,7 @@ void deviceFirmwareCleanup(DEVICE_FIRMWARE_CTX * ctx)
220224 systemPrintf (" Freeing device firmware update context, %d bytes\r\n " , sizeof (*ctx));
221225 rtkFree (ctx, " Device firmware context" );
222226 dfuContext = nullptr ;
223- inMainMenu = false ;
227+ // inMainMenu = false;
224228}
225229
226230// ----------------------------------------
@@ -267,7 +271,7 @@ void deviceFirmwareClose(DEVICE_FIRMWARE_CTX * ctx, uint32_t currentMsec)
267271
268272 // Programming a device
269273 else
270- deviceFirmwareStateSet (ctx, ctx->_reboot ? DFUS_REBOOT : DFUS_DONE );
274+ deviceFirmwareStateSet (ctx, ctx->_reboot ? DFUS_REBOOT : DFUS_NEXT_DEVICE );
271275 }
272276}
273277
@@ -409,7 +413,7 @@ void deviceFirmwareDeviceListMenu(DEVICE_FIRMWARE_CTX * ctx)
409413{
410414 if (ctx->_doAll == false )
411415 {
412- inMainMenu = true ;
416+ // inMainMenu = true;
413417 systemPrintf (" \r\n Device List:\r\n " );
414418
415419 // Walk the list of devices
@@ -426,7 +430,7 @@ void deviceFirmwareDeviceListMenu(DEVICE_FIRMWARE_CTX * ctx)
426430 systemPrintf (" x) Exit\r\n " );
427431
428432 // Discard the input
429- serialInputClear ();
433+ serialInputClear (&Serial );
430434 ctx->_buffer [0 ] = 0 ;
431435 ctx->_validDataBytes = 0 ;
432436
@@ -465,7 +469,7 @@ void deviceFirmwareFileListMenu(DEVICE_FIRMWARE_CTX * ctx)
465469
466470 if (ctx->_doAll == false )
467471 {
468- inMainMenu = true ;
472+ // inMainMenu = true;
469473
470474 // Display the firmware version
471475 if (ctx->_deviceInfo ->_version )
@@ -492,7 +496,7 @@ void deviceFirmwareFileListMenu(DEVICE_FIRMWARE_CTX * ctx)
492496 systemPrintf (" x) Exit\r\n " );
493497
494498 // Discard the input
495- serialInputClear ();
499+ serialInputClear (&Serial );
496500 ctx->_buffer [0 ] = 0 ;
497501 ctx->_validDataBytes = 0 ;
498502
@@ -828,17 +832,14 @@ void deviceFirmwareOpenOutput(DEVICE_FIRMWARE_CTX * ctx, uint32_t currentMsec)
828832 // Determine if there is an open routine
829833 if (ctx->_deviceInfo ->_open )
830834 {
831- // Yes, prepare the device for firmware updates
832- if (settings.debugFirmwareUpdate )
833- systemPrintf (" Preparing the %s for firmware update\r\n " ,
834- ctx->_deviceInfo ->_deviceName );
835835 result = ctx->_deviceInfo ->_open (ctx);
836836 if (result)
837837 break ;
838838
839839 // Display the error
840840 systemPrintf (" ERROR: %s firmware open failed!\r\n " ,
841841 ctx->_deviceInfo ->_deviceName );
842+ rtkTaskList (&Serial);
842843 }
843844 else
844845 {
@@ -1615,60 +1616,59 @@ bool deviceFirmwareUpdate(uint32_t currentMsec)
16151616 bool running;
16161617 const char * stateName;
16171618
1618- do
1619+ // Get the context instance
1620+ running = false ;
1621+ ctx = dfuContext;
1622+ if (ctx)
16191623 {
1620- running = false ;
1621-
1622- // Get the context instance
1623- ctx = dfuContext;
1624- if (ctx == nullptr )
1625- break ;
1626-
1627- running = true ;
1624+ do
1625+ {
1626+ running = true ;
16281627
1629- // Blink the LED
1630- deviceFirmwareLedBlink (ctx, currentMsec);
1628+ // Blink the LED
1629+ deviceFirmwareLedBlink (ctx, currentMsec);
16311630
1632- // Perform the firmware update
1633- switch (ctx->_state )
1634- {
1635- case DFUS_INIT : deviceFirmwareInit (ctx, currentMsec); break ;
1636- case DFUS_WAIT_NETWORK : deviceFirmwareWaitForNetwork (ctx, currentMsec); break ;
1637- case DFUS_GET_DEVICE : deviceFirmwareSelectDevice (ctx, currentMsec); break ;
1638- case DFUS_GET_NETWORK_FILES : dfuNetworkFileListBuildUrl (ctx); break ;
1639- case DFUS_GET_HTTP_FILE_LIST_REQ : dfuNetworkFileListHtmlRequest (ctx, currentMsec); break ;
1640- case DFUS_GET_NETWORK_FILE_LIST : dfuNetworkFileListGetFileName (ctx, currentMsec); break ;
1641- case DFUS_GET_NVM_FILE_LIST : dfuNvmGetFiles (ctx, currentMsec); break ;
1642- case DFUS_GET_SD_FILE_LIST : dfuSdGetFiles (ctx, currentMsec); break ;
1643- case DFUS_SELECT_FILE : deviceFirmwareSelectFile (ctx, currentMsec); break ;
1644- case DFUS_SELECT_ACTION : deviceFirmwareSelectAction (ctx, currentMsec); break ;
1645- case DFUS_CRC_OPEN_INPUT : deviceFirmwareCrcOpen (ctx, currentMsec); break ;
1646- case DFUS_CRC_READ_DATA : deviceFirmwareCrcReadData (ctx, currentMsec); break ;
1647- case DFUS_CRC_CLOSE : deviceFirmwareCrcClose (ctx, currentMsec); break ;
1648- case DFUS_DEVICE_OPEN_INPUT : deviceFirmwareOpenFirmwareFile (ctx, currentMsec); break ;
1649- case DFUS_DEVICE_FILL_BUFFER : deviceFirmwareReadFillBuffer (ctx, currentMsec); break ;
1650- case DFUS_DEVICE_RESET : deviceFirmwareReset (ctx, currentMsec); break ;
1651- case DFUS_DEVICE_OPEN_OUTPUT : deviceFirmwareOpenOutput (ctx, currentMsec); break ;
1652- case DFUS_DEVICE_PROGRAM_FIRMWARE : deviceFirmwareWrite (ctx, currentMsec); break ;
1653- case DFUS_READ_FIRMWARE_DATA : deviceFirmwareReadFirmwareData (ctx, currentMsec); break ;
1654- case DFUS_DEVICE_CLOSE : deviceFirmwareClose (ctx, currentMsec); break ;
1655- case DFUS_NEXT_DEVICE : deviceFirmwareNextDevice (ctx, currentMsec); break ;
1656- case DFUS_REBOOT : dfuEsp32Reboot (); break ;
1657-
1658- case DFUS_DONE :
1659- deviceFirmwareBufferFree (ctx, true );
1660- deviceFirmwareCleanup (ctx);
1661- running = false ;
1662- break ;
1631+ // Perform the firmware update
1632+ switch (ctx->_state )
1633+ {
1634+ case DFUS_INIT : deviceFirmwareInit (ctx, currentMsec); break ;
1635+ case DFUS_WAIT_NETWORK : deviceFirmwareWaitForNetwork (ctx, currentMsec); break ;
1636+ case DFUS_GET_DEVICE : deviceFirmwareSelectDevice (ctx, currentMsec); break ;
1637+ case DFUS_GET_NETWORK_FILES : dfuNetworkFileListBuildUrl (ctx); break ;
1638+ case DFUS_GET_HTTP_FILE_LIST_REQ : dfuNetworkFileListHtmlRequest (ctx, currentMsec); break ;
1639+ case DFUS_GET_NETWORK_FILE_LIST : dfuNetworkFileListGetFileName (ctx, currentMsec); break ;
1640+ case DFUS_GET_NVM_FILE_LIST : dfuNvmGetFiles (ctx, currentMsec); break ;
1641+ case DFUS_GET_SD_FILE_LIST : dfuSdGetFiles (ctx, currentMsec); break ;
1642+ case DFUS_SELECT_FILE : deviceFirmwareSelectFile (ctx, currentMsec); break ;
1643+ case DFUS_SELECT_ACTION : deviceFirmwareSelectAction (ctx, currentMsec); break ;
1644+ case DFUS_CRC_OPEN_INPUT : deviceFirmwareCrcOpen (ctx, currentMsec); break ;
1645+ case DFUS_CRC_READ_DATA : deviceFirmwareCrcReadData (ctx, currentMsec); break ;
1646+ case DFUS_CRC_CLOSE : deviceFirmwareCrcClose (ctx, currentMsec); break ;
1647+ case DFUS_DEVICE_OPEN_INPUT : deviceFirmwareOpenFirmwareFile (ctx, currentMsec); break ;
1648+ case DFUS_DEVICE_FILL_BUFFER : deviceFirmwareReadFillBuffer (ctx, currentMsec); break ;
1649+ case DFUS_DEVICE_RESET : deviceFirmwareReset (ctx, currentMsec); break ;
1650+ case DFUS_DEVICE_OPEN_OUTPUT : deviceFirmwareOpenOutput (ctx, currentMsec); break ;
1651+ case DFUS_DEVICE_PROGRAM_FIRMWARE : deviceFirmwareWrite (ctx, currentMsec); break ;
1652+ case DFUS_READ_FIRMWARE_DATA : deviceFirmwareReadFirmwareData (ctx, currentMsec); break ;
1653+ case DFUS_DEVICE_CLOSE : deviceFirmwareClose (ctx, currentMsec); break ;
1654+ case DFUS_NEXT_DEVICE : deviceFirmwareNextDevice (ctx, currentMsec); break ;
1655+ case DFUS_REBOOT : dfuEsp32Reboot (); break ;
1656+
1657+ case DFUS_DONE :
1658+ deviceFirmwareBufferFree (ctx, true );
1659+ deviceFirmwareCleanup (ctx);
1660+ running = false ;
1661+ break ;
16631662
1664- default :
1665- stateName = deviceFirmwareStateGetName (ctx->_state );
1666- systemPrintf (" Device firmware update state: %d (%s)\r\n " , ctx->_state , stateName);
1667- reportFatalError (" Device firmware update state not implemented!" );
1668- deviceFirmwareStateSet (ctx, DFUS_DONE );
1669- break ;
1670- }
1671- } while (dfuLoopInUpdate);
1663+ default :
1664+ stateName = deviceFirmwareStateGetName (ctx->_state );
1665+ systemPrintf (" Device firmware update state: %d (%s)\r\n " , ctx->_state , stateName);
1666+ reportFatalError (" Device firmware update state not implemented!" );
1667+ deviceFirmwareStateSet (ctx, DFUS_DONE );
1668+ break ;
1669+ }
1670+ } while (dfuLoopInUpdate);
1671+ }
16721672 return running;
16731673}
16741674
@@ -1798,7 +1798,8 @@ bool deviceFirmwareWaitForNetwork(DEVICE_FIRMWARE_CTX * ctx, uint32_t currentMse
17981798void deviceFirmwareWrite (DEVICE_FIRMWARE_CTX * ctx, uint32_t currentMsec)
17991799{
18001800 size_t bytesToWrite;
1801- size_t bytesWritten;
1801+ ssize_t bytesWritten;
1802+ bool done;
18021803 int percentage;
18031804 DEVICE_WRITE write;
18041805
@@ -1826,37 +1827,41 @@ void deviceFirmwareWrite(DEVICE_FIRMWARE_CTX * ctx, uint32_t currentMsec)
18261827 bytesWritten = dfuNvmWrite (ctx, ctx->_data , bytesToWrite);
18271828 else if (ctx->_outputDeviceType == DFU_ODT_SD )
18281829 bytesWritten = dfuSdWrite (ctx, ctx->_data , bytesToWrite);
1829- if (bytesWritten >= 0 )
1830+
1831+ // Handle the error case
1832+ if (bytesWritten <= 0 )
1833+ break ;
1834+
1835+ // Account for the data written
1836+ ctx->_validDataBytes -= bytesWritten;
1837+ ctx->_data += bytesWritten;
1838+ ctx->_bytesWritten += bytesWritten;
1839+ if (ctx->_bytesWritten == ctx->_fileBytes )
1840+ ctx->_complete = true ;
1841+
1842+ // Display the number of bytes written
1843+ if (settings.debugFirmwareUpdate && ctx->_debugVerbose )
1844+ systemPrintf (" bytesWritten: %d\r\n " , bytesWritten);
1845+
1846+ // Display the percentage changes
1847+ percentage = ctx->_bytesWritten * 100 / ctx->_fileBytes ;
1848+ if (percentage != ctx->_percentage )
18301849 {
1831- // Account for the data written
1832- ctx->_validDataBytes -= bytesWritten;
1833- ctx->_data += bytesWritten;
1834- ctx->_bytesWritten += bytesWritten;
1835- if (ctx->_bytesWritten == ctx->_fileBytes )
1836- ctx->_complete = true ;
1837-
1838- // Display the number of bytes written
1839- if (settings.debugFirmwareUpdate && ctx->_debugVerbose )
1840- systemPrintf (" bytesWritten: %d\r\n " , bytesWritten);
1850+ ctx->_percentage = percentage;
1851+ displayFirmwareUpdateProgress (percentage);
1852+ systemPrintf (" \r [%s %d%%%s" ,
1853+ &dfuEqualSigns[strlen (dfuEqualSigns) - (percentage >> 1 )],
1854+ percentage,
1855+ settings.debugFirmwareUpdate ? " \r\n " : " " );
18411856 }
18421857 }
18431858
1844- // Display the percentage changes
1845- percentage = ctx->_bytesWritten * 100 / ctx->_fileBytes ;
1846- if (percentage != ctx->_percentage )
1847- {
1848- ctx->_percentage = percentage;
1849- displayFirmwareUpdateProgress (percentage);
1850- systemPrintf (" \r [%s %d%%" ,
1851- &dfuEqualSigns[strlen (dfuEqualSigns) - (percentage >> 1 )],
1852- percentage);
1853- }
1854-
18551859 // Read more data
1856- if (ctx->_complete || settings.debugFirmwareUpdate )
1860+ done = ctx->_complete || (bytesWritten <= 0 );
1861+ if (ctx->_complete && (settings.debugFirmwareUpdate == false ))
18571862 systemPrintln ();
1858- deviceFirmwareStateSet (ctx, ctx-> _complete ? DFUS_DEVICE_CLOSE
1859- : DFUS_READ_FIRMWARE_DATA );
1863+ deviceFirmwareStateSet (ctx, done ? DFUS_DEVICE_CLOSE
1864+ : DFUS_READ_FIRMWARE_DATA );
18601865}
18611866
18621867#endif // COMPILE_MENU_FIRMWARE
0 commit comments