Skip to content

Commit

Permalink
Improve detection and filtering of serial devices for Caterina (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Aug 5, 2020
1 parent c8daa44 commit 1a13852
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 114 deletions.
4 changes: 2 additions & 2 deletions osx/qmk_toolbox/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ - (IBAction) clearEEPROMButtonClick:(id) sender {
}
}

- (void)setCaterinaPort:(NSString *)port {
_flasher.caterinaPort = port;
- (void)setSerialPort:(NSString *)port {
_flasher.serialPort = port;
}

- (void)deviceConnected:(Chipset)chipset {
Expand Down
11 changes: 6 additions & 5 deletions osx/qmk_toolbox/Flashing.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

typedef enum {
AtmelSAMBA,
DFU,
AtmelDFU,
Halfkay,
Caterina,
CaterinaAlt,
FeatherBLE32u4,
STM32,
SparkfunVID,
AdafruitVID,
DogHunterVID,
STM32DFU,
Kiibohd,
AVRISP,
USBTiny,
Expand All @@ -41,7 +42,7 @@ typedef enum {
- (void)reset:(NSString *)mcu;
- (BOOL)canReset;
- (void)clearEEPROM:(NSString *)mcu;
@property NSString * caterinaPort;
@property NSString * serialPort;

@property (nonatomic, assign) id <FlashingDelegate> delegate;

Expand Down
40 changes: 20 additions & 20 deletions osx/qmk_toolbox/Flashing.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ @interface Flashing ()
@end

@implementation Flashing
@synthesize caterinaPort;
@synthesize serialPort;

- (id)initWithPrinter:(Printing *)p {
if (self = [super init]) {
Expand Down Expand Up @@ -51,14 +51,14 @@ - (NSString *)runProcess:(NSString *)command withArgs:(NSArray<NSString *> *)arg
}

- (void)flash:(NSString *)mcu withFile:(NSString *)file {
if ([USB canFlash:DFU])
[self flashDFU:mcu withFile:file];
if ([USB canFlash:AtmelDFU])
[self flashAtmelDFU:mcu withFile:file];
if ([USB canFlash:Caterina])
[self flashCaterina:mcu withFile:file];
if ([USB canFlash:Halfkay])
[self flashHalfkay:mcu withFile:file];
if ([USB canFlash:STM32])
[self flashSTM32WithFile:file];
if ([USB canFlash:STM32DFU])
[self flashSTM32DFUWithFile:file];
if ([USB canFlash:Kiibohd])
[self flashKiibohdWithFile:file];
if ([USB canFlash:AVRISP])
Expand All @@ -74,8 +74,8 @@ - (void)flash:(NSString *)mcu withFile:(NSString *)file {
}

- (void)reset:(NSString *)mcu {
if ([USB canFlash:DFU])
[self resetDFU:mcu];
if ([USB canFlash:AtmelDFU])
[self resetAtmelDFU:mcu];
if ([USB canFlash:Halfkay])
[self resetHalfkay:mcu];
if ([USB canFlash:AtmelSAMBA])
Expand All @@ -86,7 +86,7 @@ - (void)reset:(NSString *)mcu {

- (BOOL)canReset {
NSArray<NSNumber *> *resettable = @[
@(DFU),
@(AtmelDFU),
@(Halfkay),
@(AtmelSAMBA),
@(BootloadHID)
Expand All @@ -99,15 +99,15 @@ - (BOOL)canReset {
}

- (void)clearEEPROM:(NSString *)mcu {
if ([USB canFlash:DFU])
[self clearEEPROMDFU:mcu];
if ([USB canFlash:AtmelDFU])
[self clearEEPROMAtmelDFU:mcu];
if ([USB canFlash:Caterina])
[self clearEEPROMCaterina:mcu];
if ([USB canFlash:USBAsp])
[self clearEEPROMUSBAsp:mcu];
}

- (void)flashDFU:(NSString *)mcu withFile:(NSString *)file {
- (void)flashAtmelDFU:(NSString *)mcu withFile:(NSString *)file {
NSString * result;
result = [self runProcess:@"dfu-programmer" withArgs:@[mcu, @"erase", @"--force"]];
result = [self runProcess:@"dfu-programmer" withArgs:@[mcu, @"flash", file]];
Expand All @@ -118,11 +118,11 @@ - (void)flashDFU:(NSString *)mcu withFile:(NSString *)file {
}
}

- (void)resetDFU:(NSString *)mcu {
- (void)resetAtmelDFU:(NSString *)mcu {
[self runProcess:@"dfu-programmer" withArgs:@[mcu, @"reset"]];
}

- (void)clearEEPROMDFU:(NSString *)mcu {
- (void)clearEEPROMAtmelDFU:(NSString *)mcu {
NSString * result;
NSString * file = [[NSBundle mainBundle] pathForResource:@"reset" ofType:@"eep"];
result = [self runProcess:@"dfu-programmer" withArgs:@[mcu, @"erase", @"--force"]];
Expand All @@ -131,13 +131,13 @@ - (void)clearEEPROMDFU:(NSString *)mcu {
}

- (void)flashCaterina:(NSString *)mcu withFile:(NSString *)file {
[self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"avr109", @"-U", [NSString stringWithFormat:@"flash:w:%@:i", file], @"-P", caterinaPort, @"-C", @"avrdude.conf"]];
[self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"avr109", @"-U", [NSString stringWithFormat:@"flash:w:%@:i", file], @"-P", serialPort, @"-C", @"avrdude.conf"]];
}

- (void)clearEEPROMCaterina:(NSString *)mcu {
NSString * result;
NSString * file = [[NSBundle mainBundle] pathForResource:@"reset" ofType:@"eep"];
result = [self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"avr109", @"-U", [NSString stringWithFormat:@"eeprom:w:%@:i", file], @"-P", caterinaPort, @"-C", @"avrdude.conf"]];
result = [self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"avr109", @"-U", [NSString stringWithFormat:@"eeprom:w:%@:i", file], @"-P", serialPort, @"-C", @"avrdude.conf"]];
}

- (void)clearEEPROMUSBAsp:(NSString *)mcu {
Expand All @@ -154,7 +154,7 @@ - (void)resetHalfkay:(NSString *)mcu {
[self runProcess:@"teensy_loader_cli" withArgs:@[[@"-mmcu=" stringByAppendingString:mcu], @"-bv"]];
}

- (void)flashSTM32WithFile:(NSString *)file {
- (void)flashSTM32DFUWithFile:(NSString *)file {
if([[[file pathExtension] lowercaseString] isEqualToString:@"bin"]) {
[self runProcess:@"dfu-util" withArgs:@[@"-a", @"0", @"-d", @"0482:df11", @"-s", @"0x8000000:leave", @"-D", file]];
} else {
Expand All @@ -171,23 +171,23 @@ - (void)flashKiibohdWithFile:(NSString *)file {
}

- (void)flashAVRISP:(NSString *)mcu withFile:(NSString *)file {
[self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"avrisp", @"-U", [NSString stringWithFormat:@"flash:w:%@:i", file], @"-P", caterinaPort, @"-C", @"avrdude.conf"]];
[self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"avrisp", @"-U", [NSString stringWithFormat:@"flash:w:%@:i", file], @"-P", serialPort, @"-C", @"avrdude.conf"]];
}

- (void)flashUSBTiny:(NSString *)mcu withFile:(NSString *)file {
[self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"usbtiny", @"-U", [NSString stringWithFormat:@"flash:w:%@:i", file], @"-P", caterinaPort, @"-C", @"avrdude.conf"]];
[self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"usbtiny", @"-U", [NSString stringWithFormat:@"flash:w:%@:i", file], @"-P", serialPort, @"-C", @"avrdude.conf"]];
}

- (void)flashUSBAsp:(NSString *)mcu withFile:(NSString *)file {
[self runProcess:@"avrdude" withArgs:@[@"-p", mcu, @"-c", @"usbasp", @"-U", [NSString stringWithFormat:@"flash:w:%@:i", file], @"-C", @"avrdude.conf"]];
}

- (void)flashAtmelSAMBAwithFile: (NSString *)file {
[self runProcess:@"mdloader_mac" withArgs:@[@"-p", caterinaPort, @"-D", file, @"--restart"]];
[self runProcess:@"mdloader_mac" withArgs:@[@"-p", serialPort, @"-D", file, @"--restart"]];
}

- (void)resetAtmelSAMBA {
[self runProcess:@"mdloader_mac" withArgs:@[@"-p", caterinaPort, @"--restart"]];
[self runProcess:@"mdloader_mac" withArgs:@[@"-p", serialPort, @"--restart"]];
}

- (void)flashBootloadHIDwithFile: (NSString *)file {
Expand Down
2 changes: 1 addition & 1 deletion osx/qmk_toolbox/USB.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@optional
- (void)deviceConnected:(Chipset)chipset;
- (void)deviceDisconnected:(Chipset)chipset;
- (void)setCaterinaPort:(NSString *)port;
- (void)setSerialPort:(NSString *)port;
@end

@interface USB : NSObject
Expand Down
54 changes: 27 additions & 27 deletions osx/qmk_toolbox/USB.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
//Global variables
static IONotificationPortRef gNotifyPort;
DEFINE_ITER(AtmelSAMBA);
DEFINE_ITER(DFU);
DEFINE_ITER(AtmelDFU);
DEFINE_ITER(Caterina);
DEFINE_ITER(Halfkay);
DEFINE_ITER(STM32);
DEFINE_ITER(STM32DFU);
DEFINE_ITER(Kiibohd);
DEFINE_ITER(AVRISP);
DEFINE_ITER(USBAsp);
Expand All @@ -50,13 +50,13 @@ + (void)setupWithPrinter:(Printing *)printer {
_printer = printer;
mach_port_t masterPort;
CFMutableDictionaryRef AtmelSAMBAMatchingDict;
CFMutableDictionaryRef DFUMatchingDict;
CFMutableDictionaryRef AtmelDFUMatchingDict;
CFMutableDictionaryRef CaterinaMatchingDict;
CFMutableDictionaryRef CaterinaAltMatchingDict;
CFMutableDictionaryRef CaterinaDogHunterMatchingDict;
CFMutableDictionaryRef FeatherBLE32u4MatchingDict;
CFMutableDictionaryRef SparkfunVIDMatchingDict;
CFMutableDictionaryRef DogHunterVIDMatchingDict;
CFMutableDictionaryRef AdafruitVIDMatchingDict;
CFMutableDictionaryRef HalfkayMatchingDict;
CFMutableDictionaryRef STM32MatchingDict;
CFMutableDictionaryRef STM32DFUMatchingDict;
CFMutableDictionaryRef KiibohdMatchingDict;
CFMutableDictionaryRef AVRISPMatchingDict;
CFMutableDictionaryRef USBAspMatchingDict;
Expand Down Expand Up @@ -109,13 +109,13 @@ + (void)setupWithPrinter:(Printing *)printer {
dest##DeviceRemoved(NULL, g##dest##RemovedIter)

VID_PID_MATCH(0x03EB, 0x6124, AtmelSAMBA);
VID_MATCH(0x03EB, DFU);
VID_MATCH(0x03EB, AtmelDFU);
VID_MATCH(0x2341, Caterina);
VID_MATCH_MAP(0x1B4F, CaterinaAlt, Caterina);
VID_MATCH_MAP(0x2a03, CaterinaDogHunter, Caterina);
VID_MATCH_MAP(0x239a, FeatherBLE32u4, Caterina);
VID_MATCH_MAP(0x1B4F, SparkfunVID, Caterina);
VID_MATCH_MAP(0x2A03, DogHunterVID, Caterina);
VID_MATCH_MAP(0x239A, AdafruitVID, Caterina);
VID_PID_MATCH(0x16C0, 0x0478, Halfkay);
VID_PID_MATCH(0x0483, 0xDF11, STM32);
VID_PID_MATCH(0x0483, 0xDF11, STM32DFU);
VID_PID_MATCH(0x1C11, 0xB007, Kiibohd);
VID_PID_MATCH(0x16C0, 0x0483, AVRISP);
VID_PID_MATCH(0x16C0, 0x05DC, USBAsp);
Expand All @@ -133,11 +133,11 @@ + (void)setupWithPrinter:(Printing *)printer {
#define STR2(x) #x
#define STR(x) STR2(x)

#define DEVICE_EVENTS(type) \
#define DEVICE_EVENTS(type, name) \
static void type##DeviceAdded(void *refCon, io_iterator_t iterator) { \
io_service_t object; \
while ((object = IOIteratorNext(iterator))) { \
[_printer print:[NSString stringWithFormat:@"%@ %@", @(STR(type)), @"device connected"] withType:MessageType_Bootloader]; \
[_printer print:[NSString stringWithFormat:@"%@ %@", name, @"device connected"] withType:MessageType_Bootloader]; \
deviceConnected(type); \
} \
} \
Expand All @@ -156,14 +156,14 @@ + (void)setupWithPrinter:(Printing *)printer {
} \
} \
}
#define DEVICE_EVENTS_PORT(type) \
#define DEVICE_EVENTS_PORT(type, name) \
static void type##DeviceAdded(void *refCon, io_iterator_t iterator) { \
io_service_t object; \
while ((object = IOIteratorNext(iterator))) { \
double delayInSeconds = 2.; \
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); \
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ \
[_printer print:[NSString stringWithFormat:@"%@ %@", @(STR(type)), @"device connected"] withType:MessageType_Bootloader]; \
[_printer print:[NSString stringWithFormat:@"%@ %@", name, @"device connected"] withType:MessageType_Bootloader]; \
deviceConnected(type); \
io_iterator_t serialPortIterator; \
char deviceFilePath[FILEPATH_SIZE]; \
Expand All @@ -173,7 +173,7 @@ + (void)setupWithPrinter:(Printing *)printer {
printf("No modem port found.\n"); \
[_printer printResponse:@"No modem port found, try again." withType:MessageType_Bootloader]; \
} else { \
[delegate setCaterinaPort:[NSString stringWithFormat:@"%s", deviceFilePath]]; \
[delegate setSerialPort:[NSString stringWithFormat:@"%s", deviceFilePath]]; \
[_printer printResponse:[NSString stringWithFormat:@"Found port: %s", deviceFilePath] withType:MessageType_Bootloader]; \
} \
IOObjectRelease(serialPortIterator); \
Expand All @@ -196,16 +196,16 @@ + (void)setupWithPrinter:(Printing *)printer {
} \
}

DEVICE_EVENTS_PORT(AtmelSAMBA);
DEVICE_EVENTS(DFU);
DEVICE_EVENTS_PORT(Caterina);
DEVICE_EVENTS(Halfkay);
DEVICE_EVENTS(STM32);
DEVICE_EVENTS(Kiibohd);
DEVICE_EVENTS_PORT(AVRISP);
DEVICE_EVENTS(USBAsp);
DEVICE_EVENTS_PORT(USBTiny);
DEVICE_EVENTS(BootloadHID);
DEVICE_EVENTS_PORT(AtmelSAMBA, @"Atmel SAM-BA");
DEVICE_EVENTS(AtmelDFU, @"Atmel DFU");
DEVICE_EVENTS_PORT(Caterina, @"Caterina");
DEVICE_EVENTS(Halfkay, @"Halfkay");
DEVICE_EVENTS(STM32DFU, @"STM32 DFU");
DEVICE_EVENTS(Kiibohd, @"Kiibohd");
DEVICE_EVENTS_PORT(AVRISP, @"AVRISP");
DEVICE_EVENTS(USBAsp, @"USBAsp");
DEVICE_EVENTS_PORT(USBTiny, @"USBTiny");
DEVICE_EVENTS(BootloadHID, @"BootloadHID");

static kern_return_t MyFindModems(io_iterator_t *matchingServices)
{
Expand Down
14 changes: 7 additions & 7 deletions windows/QMK Toolbox/Flashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Flashing : EventArgs

public const ushort ConsoleUsagePage = 0xFF31;
public const int ConsoleUsage = 0x0074;
public string CaterinaPort = "";
public string ComPort = "";

private readonly Printing _printer;
public Usb Usb;
Expand Down Expand Up @@ -211,9 +211,9 @@ private void ClearEepromDfu(string mcu)
_printer.Print("Please reflash device with firmware now", MessageType.Bootloader);
}

private void FlashCaterina(string mcu, string file) => RunProcess("avrdude.exe", $"-p {mcu} -c avr109 -U flash:w:\"{file}\":i -P {CaterinaPort}");
private void FlashCaterina(string mcu, string file) => RunProcess("avrdude.exe", $"-p {mcu} -c avr109 -U flash:w:\"{file}\":i -P {ComPort}");

private void ClearEepromCaterina(string mcu) => RunProcess("avrdude.exe", $"-p {mcu} -c avr109 -U eeprom:w:\"reset.eep\":i -P {CaterinaPort}");
private void ClearEepromCaterina(string mcu) => RunProcess("avrdude.exe", $"-p {mcu} -c avr109 -U eeprom:w:\"reset.eep\":i -P {ComPort}");

private void ClearEepromUsbAsp(string mcu) => RunProcess("avrdude.exe", $"-p {mcu} -c usbasp -U eeprom:w:\"reset.eep\":i");

Expand Down Expand Up @@ -241,7 +241,7 @@ private void FlashKiibohd(string file)

private void FlashAvrIsp(string mcu, string file)
{
RunProcess("avrdude.exe", $"-p {mcu} -c avrisp -U flash:w:\"{file}\":i -P {CaterinaPort}");
RunProcess("avrdude.exe", $"-p {mcu} -c avrisp -U flash:w:\"{file}\":i -P {ComPort}");
_printer.Print("Flash complete", MessageType.Bootloader);
}

Expand All @@ -253,15 +253,15 @@ private void FlashUsbAsp(string mcu, string file)

private void FlashUsbTiny(string mcu, string file)
{
RunProcess("avrdude.exe", $"-p {mcu} -c usbtiny -U flash:w:\"{file}\":i -P {CaterinaPort}");
RunProcess("avrdude.exe", $"-p {mcu} -c usbtiny -U flash:w:\"{file}\":i -P {ComPort}");
_printer.Print("Flash complete", MessageType.Bootloader);
}

private void FlashBootloadHid(string file) => RunProcess("bootloadHID.exe", $"-r \"{file}\"");
private void ResetBootloadHid() => RunProcess("bootloadHID.exe", $"-r");

private void FlashAtmelSamBa(string file) => RunProcess("mdloader_windows.exe", $"-p {CaterinaPort} -D \"{file}\" --restart");
private void FlashAtmelSamBa(string file) => RunProcess("mdloader_windows.exe", $"-p {ComPort} -D \"{file}\" --restart");

private void ResetAtmelSamBa() => RunProcess("mdloader_windows.exe", $"-p {CaterinaPort} --restart");
private void ResetAtmelSamBa() => RunProcess("mdloader_windows.exe", $"-p {ComPort} --restart");
}
}
Loading

0 comments on commit 1a13852

Please sign in to comment.