19
19
#include < unistd.h>
20
20
#include < cmath>
21
21
#include < cstring>
22
+ #include < libusb-1.0/libusb.h>
22
23
23
24
#include < log.h>
24
25
#include " gpdevboard.h"
27
28
28
29
using namespace std ;
29
30
31
+ static bool g_devicesResetFromBootloader = false ;
32
+
30
33
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
31
34
// Status check
32
35
@@ -54,6 +57,39 @@ bool CheckStatus(hdevice hdev)
54
57
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55
58
// Initialization
56
59
60
+ /* *
61
+ @brief If any devices are in bootloader, move them to normal mode
62
+ */
63
+ bool ResetDevicesInBootloader ()
64
+ {
65
+ // Set up libusb
66
+ if (!USBSetup ())
67
+ return false ;
68
+
69
+ LogVerbose (" Resetting any devices in bootloader mode\n " );
70
+ LogIndenter li;
71
+
72
+ // Find all of the devices in "white" (bootloader) mode
73
+ vector<hdevice> handles;
74
+ for (int i=100 ; i>=0 ; i--)
75
+ {
76
+ hdevice hdev = OpenDevice (0x0f0f , 0x8006 , i);
77
+ if (hdev)
78
+ handles.push_back (hdev);
79
+ }
80
+
81
+ // Switch all of them into normal mode
82
+ for (auto h : handles)
83
+ {
84
+ SwitchMode (h);
85
+ libusb_close (h);
86
+ }
87
+
88
+ g_devicesResetFromBootloader = true ;
89
+
90
+ return true ;
91
+ }
92
+
57
93
/* *
58
94
@brief Connect to the board, but don't change anything
59
95
@@ -63,44 +99,31 @@ bool CheckStatus(hdevice hdev)
63
99
*/
64
100
hdevice OpenBoard (int nboard, bool test)
65
101
{
66
- // Set up libusb
67
- if (!USBSetup ())
68
- return NULL ;
69
-
70
- LogNotice (" Searching for developer board at index %d\n " , nboard);
71
- LogIndenter li;
72
-
73
- // Try opening the board in "white" mode first
74
- hdevice hdev = OpenDevice (0x0f0f , 0x8006 , nboard);
75
- bool switching = false ;
76
- if (hdev)
102
+ if (!g_devicesResetFromBootloader)
77
103
{
78
- // Change the board into "orange" mode
79
- LogVerbose (" Switching developer board from bootloader mode\n " );
80
- if (!SwitchMode (hdev))
104
+ if (!ResetDevicesInBootloader ())
81
105
return NULL ;
82
-
83
- // Takes a while to switch and re-enumerate
84
- switching = true ;
85
106
}
107
+
108
+ LogNotice (" Searching for developer board at index %d\n " , nboard);
109
+ LogIndenter li;
86
110
87
- // Try up to ten times to open the board if we're switching mode.
88
- // Stop after the first failure if we're not switching b/c there's no point.
111
+ hdevice hdev = NULL ;
112
+
113
+ // Try up to ten times to open the board
89
114
for (int i=0 ; i<10 ; i++)
90
115
{
91
116
hdev = OpenDevice (0x0f0f , 0x0006 , nboard);
92
- if (hdev || !switching )
117
+ if (hdev)
93
118
break ;
94
119
95
120
// usleep is only guaranteed to work with <1000000 us of sleeping
96
121
usleep (1000 * 1000 - 1 );
97
122
}
98
123
99
- // By this point, it should be in "orange" mode
124
+ // By this point, it should be in "orange" mode even if it wasn't initially
100
125
if (!hdev)
101
126
{
102
- if (switching)
103
- LogError (" Device failed to switch mode\n " );
104
127
if (!test)
105
128
LogError (" No device found, giving up\n " );
106
129
return NULL ;
0 commit comments