Skip to content

Commit 7cfbfd9

Browse files
committedMay 21, 2017
gpdevboard: Now reset all devices from bootloader to normal mode before initiating discovery, so we don't have devices change ID under us. Fixes #71.
1 parent 163b1f5 commit 7cfbfd9

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed
 

‎src/gpdevboard/gpdevboard.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef struct libusb_device_handle* hdevice;
3333
bool USBSetup();
3434
void USBCleanup(hdevice hdev);
3535

36+
bool ResetDevicesInBootloader();
3637
hdevice OpenDevice(uint16_t idVendor, uint16_t idProduct, int nboard);
3738
bool GetStringDescriptor(hdevice hdev, uint8_t index, std::string &desc);
3839
bool SendInterruptTransfer(hdevice hdev, const uint8_t* buf, size_t size);

‎src/gpdevboard/utils.cpp

+46-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <unistd.h>
2020
#include <cmath>
2121
#include <cstring>
22+
#include <libusb-1.0/libusb.h>
2223

2324
#include <log.h>
2425
#include "gpdevboard.h"
@@ -27,6 +28,8 @@
2728

2829
using namespace std;
2930

31+
static bool g_devicesResetFromBootloader = false;
32+
3033
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3134
// Status check
3235

@@ -54,6 +57,39 @@ bool CheckStatus(hdevice hdev)
5457
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5558
// Initialization
5659

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+
5793
/**
5894
@brief Connect to the board, but don't change anything
5995
@@ -63,44 +99,31 @@ bool CheckStatus(hdevice hdev)
6399
*/
64100
hdevice OpenBoard(int nboard, bool test)
65101
{
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)
77103
{
78-
//Change the board into "orange" mode
79-
LogVerbose("Switching developer board from bootloader mode\n");
80-
if(!SwitchMode(hdev))
104+
if(!ResetDevicesInBootloader())
81105
return NULL;
82-
83-
//Takes a while to switch and re-enumerate
84-
switching = true;
85106
}
107+
108+
LogNotice("Searching for developer board at index %d\n", nboard);
109+
LogIndenter li;
86110

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
89114
for(int i=0; i<10; i++)
90115
{
91116
hdev = OpenDevice(0x0f0f, 0x0006, nboard);
92-
if(hdev || !switching)
117+
if(hdev)
93118
break;
94119

95120
// usleep is only guaranteed to work with <1000000 us of sleeping
96121
usleep(1000 * 1000 - 1);
97122
}
98123

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
100125
if(!hdev)
101126
{
102-
if(switching)
103-
LogError("Device failed to switch mode\n");
104127
if(!test)
105128
LogError("No device found, giving up\n");
106129
return NULL;

0 commit comments

Comments
 (0)
Please sign in to comment.