From 71b74d37409b17a2d431933cd4c5e96cb986d0f7 Mon Sep 17 00:00:00 2001 From: Michael Copland Date: Wed, 22 Jun 2016 13:36:18 +0100 Subject: Properly close and deinitialize the USB connection --- commandline/builds/Windows/micronucleus.exe | Bin 104447 -> 105712 bytes commandline/library/micronucleus_lib.c | 13 +++++++------ commandline/library/micronucleus_lib.h | 6 ++++++ commandline/micronucleus.c | 8 ++++++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/commandline/builds/Windows/micronucleus.exe b/commandline/builds/Windows/micronucleus.exe index da02390..b4cb629 100644 Binary files a/commandline/builds/Windows/micronucleus.exe and b/commandline/builds/Windows/micronucleus.exe differ diff --git a/commandline/library/micronucleus_lib.c b/commandline/library/micronucleus_lib.c index 58ed10a..6cddb7c 100644 --- a/commandline/library/micronucleus_lib.c +++ b/commandline/library/micronucleus_lib.c @@ -148,17 +148,12 @@ int micronucleus_eraseFlash(micronucleus* deviceHandle, micronucleus_callback pr is disconnecting and reconnecting. Under Windows, micronucleus can see this and automatically reconnects prior to uploading the program. To get the the same functionality, we must flag this state (the "-84" error result) by - converting the return to -2 for the upper layer. + converting the return to 1 for the upper layer. On Mac OS a common error is -34 = epipe, but adding it to this list causes: Assertion failed: (res >= 4), function micronucleus_connect, file library/micronucleus_lib.c, line 63. */ if (res == -5 || res == -34 || res == -84) { - if (res == -34) { - libusb_close(deviceHandle->device); - deviceHandle->device = NULL; - } - return 1; // recoverable errors } else { return res; @@ -319,3 +314,9 @@ int micronucleus_startApp(micronucleus* deviceHandle) { else return 0; } + +void micronucleus_disconnect(micronucleus* deviceHandle) { + libusb_close(deviceHandle->device); + libusb_exit(NULL); + free(deviceHandle); +} diff --git a/commandline/library/micronucleus_lib.h b/commandline/library/micronucleus_lib.h index 9e1e0f6..ce31f97 100644 --- a/commandline/library/micronucleus_lib.h +++ b/commandline/library/micronucleus_lib.h @@ -107,4 +107,10 @@ int micronucleus_writeFlash(micronucleus* deviceHandle, unsigned int program_len int micronucleus_startApp(micronucleus* deviceHandle); /*******************************************************************************/ +/******************************************************************************** +* Disconnects from the device and deinitializes libusb +********************************************************************************/ +void micronucleus_disconnect(micronucleus* deviceHandle); +/*******************************************************************************/ + #endif diff --git a/commandline/micronucleus.c b/commandline/micronucleus.c index 14c1761..91dbac4 100644 --- a/commandline/micronucleus.c +++ b/commandline/micronucleus.c @@ -3,7 +3,7 @@ Created: September 2012 (c) 2012 by ihsan Kehribar Changes for Micronucleus protocol version V2.x (c) 2014 T. Bo"scke - + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to @@ -76,7 +76,7 @@ int main(int argc, char **argv) { char* usage = "usage: micronucleus [--help] [--run] [--dump-progress] [--fast-mode] [--type intel-hex|raw] [--timeout integer] [--erase-only] filename"; #else char* usage = "usage: micronucleus [--help] [--run] [--dump-progress] [--fast-mode] [--type intel-hex|raw] [--timeout integer] [--erase-only] filename [--no-ansi]"; - #endif + #endif progress_step = 0; progress_total_steps = 5; // steps: waiting, connecting, parsing, erasing, writing, (running)? dump_progress = 0; @@ -238,6 +238,8 @@ int main(int argc, char **argv) { if (res == 1) { // erase disconnection bug workaround printf(">> Eep! Connection to device lost during erase! Not to worry\n"); printf(">> This happens on some computers - reconnecting...\n"); + + micronucleus_disconnect(my_device); my_device = NULL; delay(CONNECT_WAIT); @@ -290,6 +292,8 @@ int main(int argc, char **argv) { printProgress(1.0); } + micronucleus_disconnect(my_device); + printf(">> Micronucleus done. Thank you!\n"); return EXIT_SUCCESS; -- cgit v1.2.3