Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added 'Autodetect' button to automatically detect microcontroller #4

Merged
merged 3 commits into from
Feb 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AVRFuses.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
8C0903A51E4A606100DDE453 /* Signatures.lst in Resources */ = {isa = PBXBuildFile; fileRef = 8C0903A41E4A606100DDE453 /* Signatures.lst */; };
8D11072A0486CEB800E47090 /* MainMenu.nib in Resources */ = {isa = PBXBuildFile; fileRef = 29B97318FDCFA39411CA2CEA /* MainMenu.nib */; };
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
Expand Down Expand Up @@ -46,6 +47,7 @@
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32CA4F630368D1EE00C91783 /* AVRFuses_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AVRFuses_Prefix.pch; sourceTree = "<group>"; };
8C0903A41E4A606100DDE453 /* Signatures.lst */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Signatures.lst; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* AVRFuses.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AVRFuses.app; sourceTree = BUILT_PRODUCTS_DIR; };
960D606D0B6FE9700018E006 /* AVRFusesWindowController.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = AVRFusesWindowController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -141,6 +143,7 @@
29B97317FDCFA39411CA2CEA /* Resources */ = {
isa = PBXGroup;
children = (
8C0903A41E4A606100DDE453 /* Signatures.lst */,
96EE8B940FA02F320024B974 /* dsa_pub.pem */,
9661E70B0D18340400DFABE4 /* appcast.xml */,
962D354B0CFD5922003A6C5A /* AVRFuses2.icns */,
Expand Down Expand Up @@ -218,6 +221,7 @@
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */,
96D8D30A0CEEE5D600C7FDC6 /* AVRFuses.parts in Resources */,
96A329570CFBF63900500BA9 /* Credits.rtf in Resources */,
8C0903A51E4A606100DDE453 /* Signatures.lst in Resources */,
962D354C0CFD5922003A6C5A /* AVRFuses2.icns in Resources */,
9661E70C0D18340400DFABE4 /* appcast.xml in Resources */,
96EE8B950FA02F320024B974 /* dsa_pub.pem in Resources */,
Expand Down
2 changes: 2 additions & 0 deletions AVRFusesController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
NSMutableArray *fuseSettings;
NSMutableArray *lockbitSettings;
NSString *avrdudeVersion;
NSMutableDictionary *signatures;
}

- (void)awakeFromNib;
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *) theApplication;
- (void)loadPartDefinitions;
- (void)loadSignaturesDefinitions;
- (void)loadAvrdudeConfigs;

- (BOOL) avrdudeAvailable;
Expand Down
103 changes: 103 additions & 0 deletions AVRFusesController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ @implementation AVRFusesController
- (void)awakeFromNib
{
parts = [[NSMutableDictionary alloc] init];
signatures = [[NSMutableDictionary alloc] init];
selectedPart = nil;
fuses = [[NSMutableDictionary alloc] init];
fuseSettings = [[NSMutableArray alloc] init];
Expand All @@ -39,6 +40,7 @@ - (void)awakeFromNib
[lockbitsTableView setDoubleAction: @selector(tableViewDoubleClick:)];

[self loadPartDefinitions];
[self loadSignaturesDefinitions];

NSArray *sortedPartNames = [[parts allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (int i = 0; i < [sortedPartNames count]; i++) {
Expand Down Expand Up @@ -166,6 +168,44 @@ - (void)loadPartDefinitions
fclose(file);
}

- (void)loadSignaturesDefinitions
{
NSBundle *thisBundle = [NSBundle mainBundle];
char buffer[1000];
FILE *file = fopen([[thisBundle pathForResource: @"Signatures" ofType: @"lst"] UTF8String], "r");
while(fgets(buffer, 1000, file) != NULL) {
NSString *line = [[NSString alloc] initWithCString: buffer encoding:NSUTF8StringEncoding];
[line autorelease];
NSScanner *scanner = [NSScanner scannerWithString: line];

NSString *name = nil;
unsigned int signature1 = 0;
unsigned int signature2 = 0;
unsigned int signature3 = 0;

[scanner scanUpToString: @"," intoString: &name];
[scanner setScanLocation:[scanner scanLocation] + 1];
[scanner scanHexInt: &signature1];
[scanner setScanLocation:[scanner scanLocation] + 1];
[scanner scanHexInt: &signature2];
[scanner setScanLocation:[scanner scanLocation] + 1];
[scanner scanHexInt: &signature3];

// TODO see if this makes sense since we're adding to arrays and such
[name retain];

Signature *signature = nil;
signature = [[Signature alloc] init];
[signature autorelease];
signature->s1 = signature1;
signature->s2 = signature2;
signature->s3 = signature3;

[signatures setObject: name forKey: signature];
}
fclose(file);
}


- (IBAction)deviceChanged:(id)sender
{
Expand Down Expand Up @@ -1010,6 +1050,69 @@ - (IBAction)eraseDevice:(id)sender
[self logStatus:[task terminationStatus] == 0];
}

- (IBAction)autodetectDevice:(id)sender
{
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];

[avrdudeArguments addObject: @"-F"];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: @"signature:r:/tmp/SIGNATURE.tmp:h"];

[self log: @"Reading signature..."];

NSTask *task = [[NSTask alloc] init];
[task autorelease];
[task setLaunchPath: avrdudePath];
[task setArguments: avrdudeArguments];
NSPipe *pipe = [NSPipe pipe];
[task setStandardError: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[self logCommandLine:task];
[task launch];
[task waitUntilExit];

NSData *data = [file readDataToEndOfFile];
NSString *stdErr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[stdErr autorelease];
NSArray *stdErrLines = [stdErr componentsSeparatedByString: @"\n"];
for (int i = 0; i < [stdErrLines count]; i++) {
NSString *line = [stdErrLines objectAtIndex: i];
if ([line length] > 0) {
[self log: line];
}
}

[self logStatus:[task terminationStatus] == 0];

if ([task terminationStatus] == 0) {
char buffer[1000];
FILE *file = fopen("/tmp/SIGNATURE.tmp", "r");
fgets(buffer, 1000, file);
NSString *line = [[NSString alloc] initWithCString: buffer encoding:NSUTF8StringEncoding];
[line autorelease];
NSScanner *scanner = [NSScanner scannerWithString: line];
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@","];
unsigned int signByte1, signByte2, signByte3;
[scanner setCharactersToBeSkipped:separatorSet];
[scanner scanHexInt: &signByte1];
[scanner scanHexInt: &signByte2];
[scanner scanHexInt: &signByte3];
fclose(file);

Signature *signature = [[Signature alloc] init];
signature->s1 = signByte1;
signature->s2 = signByte2;
signature->s3 = signByte3;


NSString *name = [signatures objectForKey:signature];
[self log: name];
[devicePopUpButton selectItemWithTitle: name];
[self deviceChanged: nil];
}
}

- (void)tableViewDoubleClick: (NSTableView *) tableView
{
if (![self avrdudeAvailable]) {
Expand Down
46 changes: 46 additions & 0 deletions BuildCache/build_signatures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os

import sys


if len(sys.argv) != 2:
print 'Usage: build_signatures.py <path to AVR Studio devices directory>'
sys.exit(1)

atmel_location = sys.argv[1]

sys.stdout = open('Signatures.lst', 'w')


def process_atdf(path):
with open(path) as f:
lines = f.readlines()
name = None
s1 = None
s2 = None
s3 = None
l = len('<property name="SIGNATURE0" value="')
for s in lines:
pn = s.find('<device name="')
if pn >= 0:
name = s[pn+len('<device name="'):]
name = name[:name.find('"')]
p1 = s.find('<property name="SIGNATURE0" value="')
if p1 >= 0:
s1 = s[p1+l:-4]
p2 = s.find('<property name="SIGNATURE1" value="')
if p2 >= 0:
s2 = s[p2+l:-4]
p3 = s.find('<property name="SIGNATURE2" value="')
if p3 >= 0:
s3 = s[p3+l:-4]
print name + "," + s1 + "," + s2 + "," + s3

for filename in os.listdir(atmel_location):
if filename.endswith(".atdf"):
path = os.path.join(atmel_location, filename)
process_atdf(path)


Loading