-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
AVRFusesController.m
1388 lines (1216 loc) · 49.1 KB
/
AVRFusesController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "AVRFusesController.h"
#import <termios.h>
#import <CoreFoundation/CoreFoundation.h>
#import <IOKit/IOKitLib.h>
#import <IOKit/serial/IOSerialKeys.h>
#import <IOKit/IOBSD.h>
/*
Need to come up with just a few generic methods:
readMemory, verifyMemory, programMemory and use it similar to how avrdude uses -U
*/
/*
TODO removed lockbits for now so I can release 1.4
This method of reading in the configs causes problems in all the other
methods (mainly read) because of the loop that looks at all fusenames
and expects a file. I don't like having to hardcode the fuse names in each
function.
Also, right now it's fine cause we never add lockbits fusename to fuses, but if we do (like
I originally intended) then everything that uses the fuses array needs to change. Should store lockbits
seperately or come up with a more generic method of read/writing/verifying/displaying memory.
*/
@implementation AVRFusesController
- (void)awakeFromNib
{
parts = [[NSMutableDictionary alloc] init];
signatures = [[NSMutableDictionary alloc] init];
selectedPart = nil;
fuses = [[NSMutableDictionary alloc] init];
fuseSettings = [[NSMutableArray alloc] init];
lockbitSettings = [[NSMutableArray alloc] init];
[fusesTableView setDoubleAction: @selector(tableViewDoubleClick:)];
[lockbitsTableView setDoubleAction: @selector(tableViewDoubleClick:)];
[self loadPartDefinitions];
[self loadSignaturesDefinitions];
NSArray *sortedPartNames = [[parts allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (int i = 0; i < [sortedPartNames count]; i++) {
NSString *partName = [sortedPartNames objectAtIndex: i];
[devicePopUpButton addItemWithTitle: partName];
[projectDevicePopUpButton addItemWithTitle: partName];
}
if ([[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedPart"] != nil) {
[devicePopUpButton selectItemWithTitle: [[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedPart"]];
}
[avrdudeSerialBaudPopUpButton removeAllItems];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"[Default]"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"300"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"1200"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"2400"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"4800"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"9600"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"19200"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"38400"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"57600"];
[avrdudeSerialBaudPopUpButton addItemWithTitle: @"115200"];
if ([[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeSerialBaud"] != nil) {
[avrdudeSerialBaudPopUpButton selectItemWithTitle: [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeSerialBaud"]];
}
[avrdudePortPopUpButton removeAllItems];
[avrdudePortPopUpButton addItemWithObjectValue: @"usb"];
NSMutableArray *serialPorts = [NSMutableArray array];
[self addAllSerialPortsToArray: serialPorts];
for (int i = 0; i < [serialPorts count]; i++) {
[avrdudePortPopUpButton addItemWithObjectValue: [serialPorts objectAtIndex: i]];
}
[self deviceChanged: nil];
[mainWindow makeKeyAndOrderFront: nil];
NSString *avrdudeConfig = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeConfig"];
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
if (!avrdudeConfig || !avrdudePath) {
[self showPrefs: nil];
}
else {
[self loadAvrdudeConfigs];
}
self.avrdudeOperationInProgress = NO;
[self updateProjectsMenu];
[projectNameTextField setDelegate: self];
}
- (void)loadPartDefinitions
{
NSBundle *thisBundle = [NSBundle mainBundle];
char buffer[1000];
FILE *file = fopen([[thisBundle pathForResource: @"AVRFuses" ofType: @"parts"] UTF8String], "r");
while(fgets(buffer, 1000, file) != NULL) {
NSString *line = [[NSString alloc] initWithCString: buffer encoding:NSUTF8StringEncoding];
[line autorelease];
NSScanner *scanner = [NSScanner scannerWithString: line];
NSString *settingPart = nil;
NSString *settingFuse = nil;
unsigned int settingMask = 0;
unsigned int settingValue = 0;
NSString *settingText = nil;
[scanner scanUpToString: @"," intoString: &settingPart];
[scanner setScanLocation:[scanner scanLocation] + 1];
[scanner scanUpToString: @"," intoString: &settingFuse];
[scanner setScanLocation:[scanner scanLocation] + 1];
[scanner scanHexInt: &settingMask];
[scanner setScanLocation:[scanner scanLocation] + 1];
[scanner scanHexInt: &settingValue];
[scanner setScanLocation:[scanner scanLocation] + 1];
[scanner scanUpToString: @"\n" intoString: &settingText];
// TODO see if this makes sense since we're adding to arrays and such
[settingPart retain];
[settingFuse retain];
[settingText retain];
PartDefinition *part = nil;
if ([parts objectForKey: settingPart] == nil) {
part = [[PartDefinition alloc] init];
[part autorelease];
part->name = settingPart;
[parts setObject: part forKey: part->name];
}
else {
part = [parts objectForKey: settingPart];
}
FuseDefinition *fuse = nil;
if ([settingFuse isEqualToString: @"LOCKBIT"]) {
if (part->lockbits == nil) {
fuse = [[FuseDefinition alloc] init];
[fuse autorelease];
fuse->name = settingFuse;
part->lockbits = fuse;
}
else {
fuse = part->lockbits;
}
}
else {
if ([part->fuses objectForKey: settingFuse] == nil) {
fuse = [[FuseDefinition alloc] init];
[fuse autorelease];
fuse->name = settingFuse;
[part->fuses setObject: fuse forKey: fuse->name];
}
else {
fuse = [part->fuses objectForKey: settingFuse];
}
}
FuseSetting *fuseSetting = [[FuseSetting alloc] init];
[fuseSetting autorelease];
fuseSetting->fuse = fuse->name;
fuseSetting->mask = settingMask & 0xff;
fuseSetting->value = settingValue & 0xff;
fuseSetting->text = settingText;
[fuse->settings addObject: fuseSetting];
}
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
{
selectedPart = [parts objectForKey: [[devicePopUpButton selectedItem] title]];
[[NSUserDefaults standardUserDefaults] setObject: selectedPart->name forKey: @"lastSelectedPart"];
[fuseSettings removeAllObjects];
[lockbitSettings removeAllObjects];
if ([selectedPart->fuses objectForKey:@"EXTENDED"] != nil) {
FuseDefinition *fuse = [selectedPart->fuses objectForKey: @"EXTENDED"];
for (int i = 0; i < [fuse->settings count]; i++) {
[fuseSettings addObject: [fuse->settings objectAtIndex: i]];
}
}
if ([selectedPart->fuses objectForKey:@"HIGH"] != nil) {
FuseDefinition *fuse = [selectedPart->fuses objectForKey: @"HIGH"];
for (int i = 0; i < [fuse->settings count]; i++) {
[fuseSettings addObject: [fuse->settings objectAtIndex: i]];
}
}
if ([selectedPart->fuses objectForKey:@"LOW"] != nil) {
FuseDefinition *fuse = [selectedPart->fuses objectForKey: @"LOW"];
for (int i = 0; i < [fuse->settings count]; i++) {
[fuseSettings addObject: [fuse->settings objectAtIndex: i]];
}
}
if (selectedPart->lockbits != nil) {
FuseDefinition *fuse = selectedPart->lockbits;
for (int i = 0; i < [fuse->settings count]; i++) {
[lockbitSettings addObject: [fuse->settings objectAtIndex: i]];
}
}
[fuses removeAllObjects];
for (int i = 0; i < [[selectedPart->fuses allKeys] count]; i++) {
[fuses setObject: [NSNumber numberWithUnsignedChar: 0xff] forKey: [[selectedPart->fuses allKeys] objectAtIndex: i]];
}
[fusesTableView reloadData];
[lockbitsTableView reloadData];
[self->lfuseText setHidden:[selectedPart->fuses objectForKey:@"LOW"] == nil];
[self->hfuseText setHidden:[selectedPart->fuses objectForKey:@"HIGH"] == nil];
[self->efuseText setHidden:[selectedPart->fuses objectForKey:@"EXTENDED"] == nil];
[self->lfuseTextLabel setHidden:[selectedPart->fuses objectForKey:@"LOW"] == nil];
[self->hfuseTextLabel setHidden:[selectedPart->fuses objectForKey:@"HIGH"] == nil];
[self->efuseTextLabel setHidden:[selectedPart->fuses objectForKey:@"EXTENDED"] == nil];
[self updateFuseTextFields];
}
- (IBAction)showPrefs:(id)sender
{
[mainWindow beginSheet:prefsWindow completionHandler:nil];
[self willChangeValueForKey: @"avrdudeAvailable"];
[NSApp runModalForWindow:prefsWindow];
[self didChangeValueForKey: @"avrdudeAvailable"];
[NSApp endSheet:prefsWindow];
[prefsWindow orderOut:self];
[self loadAvrdudeConfigs];
}
- (IBAction)closePrefs:(id)sender
{
[NSApp endSheet:prefsWindow];
[prefsWindow orderOut:self];
[self loadAvrdudeConfigs];
[NSApp stopModal];
}
- (IBAction)browseAvrdude:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories: NO];
[openPanel setAllowsMultipleSelection: NO];
[openPanel setMessage: @"Type / to browse to hidden directories."];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:@"/usr/local/bin"]];
if ([openPanel runModal] == NSModalResponseOK) {
[[[NSUserDefaultsController sharedUserDefaultsController] values] setValue: [[openPanel URL] path] forKey: @"avrdudePath"];
[self avrdudeChanged: nil];
}
}
- (IBAction)avrdudeChanged: (id) sender
{
[self loadAvrdudeConfigs];
}
- (IBAction)browseFlash:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories: NO];
[openPanel setAllowsMultipleSelection: NO];
[openPanel setAllowedFileTypes:[NSArray arrayWithObject: @"hex"]];
if ([openPanel runModal] == NSModalResponseOK) {
[[[NSUserDefaultsController sharedUserDefaultsController] values] setValue: [[openPanel URL] path] forKey: @"lastSelectedFlash"];
}
}
- (IBAction)browseEeprom:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories: NO];
[openPanel setAllowsMultipleSelection: NO];
[openPanel setAllowedFileTypes:[NSArray arrayWithObjects: @"hex", @"eep", nil]];
if ([openPanel runModal] == NSModalResponseOK) {
[[[NSUserDefaultsController sharedUserDefaultsController] values] setValue: [[openPanel URL] path] forKey: @"lastSelectedEeprom"];
}
}
- (void)log:(NSString *)s withAttributes:(NSDictionary *)attributes
{
NSAttributedString *a = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n", s] attributes:attributes];
[a autorelease];
[[logTextView textStorage] appendAttributedString: a];
[logTextView scrollRangeToVisible: NSMakeRange([[logTextView textStorage] length], [[logTextView textStorage] length])];
}
- (void)log:(NSString *)s
{
[self log:s withAttributes:[NSDictionary dictionary]];
}
- (void)appendLog:(NSString *)s
{
[self appendLog:s withAttributes:[NSDictionary dictionary]];
}
- (void)appendLog:(NSString *)s withAttributes:(NSDictionary *)attributes
{
NSAttributedString *a = [[NSAttributedString alloc] initWithString:s attributes:attributes];
[a autorelease];
[[logTextView textStorage] appendAttributedString: a];
[logTextView scrollRangeToVisible: NSMakeRange([[logTextView textStorage] length], [[logTextView textStorage] length])];
}
- (void)logStatus:(BOOL)status
{
NSDictionary *green =
[NSDictionary dictionaryWithObject:[NSColor colorWithDeviceRed:0.0 green:1.0 blue:0.0 alpha:1.0]
forKey:NSForegroundColorAttributeName];
NSDictionary *red =
[NSDictionary dictionaryWithObject:[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0]
forKey:NSForegroundColorAttributeName];
[self log:(status ? @"SUCCESS" : @"FAILURE") withAttributes:(status ? green : red)];
}
- (void)logCommandLine:(NSTask *)t
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"showCommandLines"]) {
return;
}
NSDictionary *blue =
[NSDictionary dictionaryWithObject:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:1.0]
forKey:NSForegroundColorAttributeName];
NSMutableString *s = [NSMutableString stringWithFormat:@"\n%@", [t launchPath]];
for (int i = 0, count = (int) [[t arguments] count]; i < count; i++) {
[s appendFormat:@" %@", [[t arguments] objectAtIndex:i]];
}
[s appendString:@"\n"];
[self log:[s description] withAttributes:blue];
}
- (BOOL) avrdudeAvailable
{
NSString *avrdudeConfig = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeConfig"];
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
return (avrdudePath && avrdudeConfig && avrdudeVersion) && !self.avrdudeOperationInProgress;
}
- (void)loadAvrdudeConfigs
{
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
[self willChangeValueForKey: @"avrdudeAvailable"];
if (avrdudePath == nil) {
[self didChangeValueForKey: @"avrdudeAvailable"];
return;
}
avrdudeVersion = nil;
[avrdudeConfigPopUpButton removeAllItems];
NSMutableArray *avrdudeArguments = [[NSMutableArray alloc] init];
[avrdudeArguments addObject: @"-v"];
[avrdudeArguments addObject: @"-c"];
[avrdudeArguments addObject: @"?"];
[self log: @"Loading avrdude config..."];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: avrdudePath];
[task setArguments: avrdudeArguments];
NSPipe *pipe = [NSPipe pipe];
[task setStandardError: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
NS_DURING
[self logCommandLine:task];
[task launch];
[task waitUntilExit];
NSData *data = [file readDataToEndOfFile];
NSString *configs = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSArray *lines = [configs componentsSeparatedByString: @"\n"];
for (int i = 0; i < [lines count]; i++) {
NSString *line = [lines objectAtIndex: i];
NSRange range;
if (!avrdudeVersion) {
range = [line rangeOfString: @"version" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
NSString *substr = [line substringFromIndex: range.location];
NSArray *comps = [substr componentsSeparatedByString: @" "];
comps = [[comps objectAtIndex: 1] componentsSeparatedByString: @","];
avrdudeVersion = [comps objectAtIndex: 0];
}
}
if (![line hasPrefix: @" "] && [line hasPrefix: @" "]) {
NSArray *comps = [line componentsSeparatedByString: @" "];
[avrdudeConfigPopUpButton addItemWithTitle: [comps objectAtIndex: 2]];
}
}
[self logStatus:TRUE];
NS_HANDLER
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText: @"Unable to execute avrdude."];
[alert setInformativeText: @"Check that the path to avrdude is correct and that you are able to execute it normally."];
[alert addButtonWithTitle: @"OK"];
[alert beginSheetModalForWindow:([prefsWindow isVisible] ? prefsWindow : mainWindow)
completionHandler: nil
];
NS_ENDHANDLER
if (avrdudeVersion) {
[mainWindow setTitle: [NSString stringWithFormat: @"AVRFuses (avrdude v%@)", avrdudeVersion]];
if ([[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeConfig"] != nil) {
[avrdudeConfigPopUpButton selectItemWithTitle: [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeConfig"]];
}
}
else {
[self logStatus:FALSE];
}
[self didChangeValueForKey: @"avrdudeAvailable"];
}
- (NSMutableArray *) defaultAvrdudeArguments
{
NSString *avrdudePort = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePort"];
NSString *avrdudeConfig = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeConfig"];
NSString *avrdudeSerialBaud = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeSerialBaud"];
NSString *avrdudeBitClock = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudeBitClock"];
NSMutableArray *avrdudeArguments = [[NSMutableArray alloc] init];
if (avrdudePort != nil && [avrdudePort length] > 0) {
[avrdudeArguments addObject: @"-P"];
[avrdudeArguments addObject: avrdudePort];
}
[avrdudeArguments addObject: @"-c"];
[avrdudeArguments addObject: avrdudeConfig];
if (avrdudeSerialBaud != nil && ![avrdudeSerialBaud isEqualToString: @"[Default]"]) {
[avrdudeArguments addObject: @"-b"];
[avrdudeArguments addObject: avrdudeSerialBaud];
}
[avrdudeArguments addObject: @"-p"];
[avrdudeArguments addObject: selectedPart->name];
//[avrdudeArguments addObject: @"-qq"];
// [avrdudeArguments addObject: @"-s"];
if (avrdudeBitClock != nil && [avrdudeBitClock length] > 0) {
[avrdudeArguments addObject: @"-B"];
[avrdudeArguments addObject: avrdudeBitClock];
}
[avrdudeArguments autorelease];
return avrdudeArguments;
}
- (void) execAvrdude:(NSMutableArray* _Nonnull)avrdudeArguments
completionHandler:(void (^ _Nullable)(int returnCode))handler
{
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
[self willChangeValueForKey: @"avrdudeAvailable"];
self.avrdudeOperationInProgress = YES;
[self didChangeValueForKey: @"avrdudeAvailable"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSTask *task = [[NSTask alloc] init];
[task autorelease];
[task setLaunchPath: avrdudePath];
[task setArguments: avrdudeArguments];
NSPipe *pipe = [NSPipe pipe];
[task setStandardError: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
dispatch_async(dispatch_get_main_queue(), ^{
[self logCommandLine:task];
});
[task launch];
while (task.running) {
[NSThread sleepForTimeInterval:0.005f];
//NSData *data = [file readDataToEndOfFile];
NSData *data = [file availableData];
NSString *stdErr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[self appendLog: stdErr];
});
[stdErr autorelease];
}
//[task waitUntilExit];
dispatch_async(dispatch_get_main_queue(), ^{
[self willChangeValueForKey: @"avrdudeAvailable"];
self.avrdudeOperationInProgress = NO;
[self didChangeValueForKey: @"avrdudeAvailable"];
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];
}
}
int status = [task terminationStatus];
[self logStatus: status == 0];
if (handler != nil) {
handler(status);
}
});
});
}
- (IBAction)programFuses:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
for (int i = 0; i < [[fuses allKeys] count]; i++) {
NSString *fuseName = [[fuses allKeys] objectAtIndex: i];
NSString *avrdudeFuseName = nil;
if ([fuseName isEqualToString: @"EXTENDED"]) {
avrdudeFuseName = @"efuse";
}
else if ([fuseName isEqualToString: @"LOW"]) {
avrdudeFuseName = @"lfuse";
}
else if ([fuseName isEqualToString: @"HIGH"]) {
avrdudeFuseName = @"hfuse";
}
// TODO this pattern in the rest of these functions should change with addition of lockbits
else {
continue;
}
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"%@:w:0x%02x:m", avrdudeFuseName, [[fuses objectForKey: fuseName] unsignedCharValue]]];
}
[self log: @"Programming fuses..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)readFuses:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
for (int i = 0; i < [[fuses allKeys] count]; i++) {
NSString *fuseName = [[fuses allKeys] objectAtIndex: i];
NSString *avrdudeFuseName = nil;
if ([fuseName isEqualToString: @"EXTENDED"]) {
avrdudeFuseName = @"efuse";
}
else if ([fuseName isEqualToString: @"LOW"]) {
avrdudeFuseName = @"lfuse";
}
else if ([fuseName isEqualToString: @"HIGH"]) {
avrdudeFuseName = @"hfuse";
}
else {
continue;
}
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"%@:r:/tmp/%@.tmp:h", avrdudeFuseName, fuseName]];
}
[self log: @"Reading fuses..."];
[self execAvrdude: avrdudeArguments completionHandler: ^(int returnCode) {
if (returnCode == 0) {
for (int i = 0; i < [[fuses allKeys] count]; i++) {
NSString *fuseName = [[fuses allKeys] objectAtIndex: i];
//NSLog(@"%@", fuseName);
char buffer[1000];
FILE *file = fopen([[NSString stringWithFormat: @"/tmp/%@.tmp", fuseName] UTF8String], "r");
fgets(buffer, 1000, file);
NSString *line = [[NSString alloc] initWithCString: buffer encoding:NSUTF8StringEncoding];
[line autorelease];
NSScanner *scanner = [NSScanner scannerWithString: line];
unsigned int fuseValue;
[scanner scanHexInt: &fuseValue];
fclose(file);
[fuses setObject: [NSNumber numberWithUnsignedChar: (fuseValue & 0xff)] forKey: fuseName];
}
}
else {
[self log: @"FAILED"];
}
[self updateFuseTextFields];
[fusesTableView reloadData];
}];
}
- (IBAction)verifyFuses:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
for (int i = 0; i < [[fuses allKeys] count]; i++) {
NSString *fuseName = [[fuses allKeys] objectAtIndex: i];
NSString *avrdudeFuseName = nil;
if ([fuseName isEqualToString: @"EXTENDED"]) {
avrdudeFuseName = @"efuse";
}
else if ([fuseName isEqualToString: @"LOW"]) {
avrdudeFuseName = @"lfuse";
}
else if ([fuseName isEqualToString: @"HIGH"]) {
avrdudeFuseName = @"hfuse";
}
else {
continue;
}
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"%@:v:0x%02x:m", avrdudeFuseName, [[fuses objectForKey: fuseName] unsignedCharValue]]];
}
[self log: @"Verifying fuses..."];
[self execAvrdude: avrdudeArguments completionHandler: ^(int returnCode) {
[fusesTableView reloadData];
}];
}
// TODO
/*
- (IBAction)programLockbits:(id)sender
{
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
for (int i = 0; i < [[fuses allKeys] count]; i++) {
NSString *fuseName = [[fuses allKeys] objectAtIndex: i];
NSString *avrdudeFuseName = nil;
if ([fuseName isEqualToString: @"EXTENDED"]) {
avrdudeFuseName = @"efuse";
}
else if ([fuseName isEqualToString: @"LOW"]) {
avrdudeFuseName = @"lfuse";
}
else if ([fuseName isEqualToString: @"HIGH"]) {
avrdudeFuseName = @"hfuse";
}
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"%@:w:0x%02x:m", avrdudeFuseName, [[fuses objectForKey: fuseName] unsignedCharValue]]];
}
[self log: @"Programming fuses..."];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: avrdudePath];
[task setArguments: avrdudeArguments];
NSPipe *pipe = [NSPipe pipe];
[task setStandardError: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData *data = [file readDataToEndOfFile];
NSString *stdErr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
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];
}
}
if ([task terminationStatus] == 0) {
[self log: @"SUCCESS"];
}
else {
[self log: @"FAILED"];
}
}
- (IBAction)readLockbits:(id)sender
{
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
NSString *fuseName = @"LOCKBIT";
NSString *avrdudeFuseName = @"lock";
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"%@:r:/tmp/%@.tmp:h", avrdudeFuseName, fuseName]];
[self log: @"Reading lock bits..."];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: avrdudePath];
[task setArguments: avrdudeArguments];
NSPipe *pipe = [NSPipe pipe];
[task setStandardError: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData *data = [file readDataToEndOfFile];
NSString *stdErr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
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];
}
}
if ([task terminationStatus] == 0) {
[self log: @"SUCCESS"];
char buffer[1000];
FILE *file = fopen([[NSString stringWithFormat: @"/tmp/%@.tmp", fuseName] cString], "r");
fgets(buffer, 1000, file);
NSString *line = [[NSString alloc] initWithCString: buffer];
NSScanner *scanner = [NSScanner scannerWithString: line];
unsigned int fuseValue;
[scanner scanHexInt: &fuseValue];
fclose(file);
[fuses setObject: [NSNumber numberWithUnsignedChar: (fuseValue & 0xff)] forKey: fuseName];
}
else {
[self log: @"FAILED"];
}
[lockbitsTableView reloadData];
}
- (IBAction)verifyLockbits:(id)sender
{
NSString *avrdudePath = [[NSUserDefaults standardUserDefaults] stringForKey: @"avrdudePath"];
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
for (int i = 0; i < [[fuses allKeys] count]; i++) {
NSString *fuseName = [[fuses allKeys] objectAtIndex: i];
NSString *avrdudeFuseName = nil;
if ([fuseName isEqualToString: @"EXTENDED"]) {
avrdudeFuseName = @"efuse";
}
else if ([fuseName isEqualToString: @"LOW"]) {
avrdudeFuseName = @"lfuse";
}
else if ([fuseName isEqualToString: @"HIGH"]) {
avrdudeFuseName = @"hfuse";
}
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"%@:v:0x%02x:m", avrdudeFuseName, [[fuses objectForKey: fuseName] unsignedCharValue]]];
}
[self log: @"Verifying fuses..."];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: avrdudePath];
[task setArguments: avrdudeArguments];
NSPipe *pipe = [NSPipe pipe];
[task setStandardError: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData *data = [file readDataToEndOfFile];
NSString *stdErr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
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];
}
}
if ([task terminationStatus] == 0) {
[self log: @"SUCCESS"];
}
else {
[self log: @"FAILED"];
}
[fusesTableView reloadData];
}
*/
- (IBAction)verifyFlash:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"flash:v:%@",
[[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedFlash"]]];
[self log: @"Verifying flash..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)programFlash:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"flash:w:%@",
[[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedFlash"]]];
[self log: @"Programming flash..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)readFlash:(id)sender
{
NSString *filename = [[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedFlash"];
if (sender != nil && [[NSFileManager defaultManager] fileExistsAtPath: filename]) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:
[NSString stringWithFormat: @"\"%@\" already exists. Do you want to replace it?",
[[NSFileManager defaultManager] displayNameAtPath: filename]]];
[alert setInformativeText: @"A file or folder with the same name already exists. Replacing it will overwrite it's current contents."];
[alert addButtonWithTitle: @"Replace"];
[alert addButtonWithTitle: @"Cancel"];
[alert beginSheetModalForWindow: mainWindow
completionHandler: ^(NSModalResponse returnCode) {
[[alert window] orderOut: self];
if (returnCode == NSAlertFirstButtonReturn) {
[self readFlash: nil];
}
}];
return;
}
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"flash:r:%@:i", filename]];
[self log: @"Reading flash..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)verifyEeprom:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"eeprom:v:%@",
[[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedEeprom"]]];
[self log: @"Verifying EEPROM..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)programEeprom:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"eeprom:w:%@",
[[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedEeprom"]]];
[self log: @"Programming EEPROM..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)readEeprom:(id)sender
{
NSString *filename = [[NSUserDefaults standardUserDefaults] stringForKey: @"lastSelectedEeprom"];
if (sender != nil && [[NSFileManager defaultManager] fileExistsAtPath: filename]) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:
[NSString stringWithFormat: @"\"%@\" already exists. Do you want to replace it?",
[[NSFileManager defaultManager] displayNameAtPath: filename]]];
[alert setInformativeText: @"A file or folder with the same name already exists. Replacing it will overwrite it's current contents."];
[alert addButtonWithTitle: @"Replace"];
[alert addButtonWithTitle: @"Cancel"];
[alert beginSheetModalForWindow: mainWindow
completionHandler: ^(NSModalResponse returnCode) {
[[alert window] orderOut: self];
if (returnCode == NSAlertFirstButtonReturn) {
[self readEeprom: nil];
}
}];
return;
}
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: [NSString stringWithFormat: @"eeprom:r:%@:i", filename]];
[self log: @"Reading EEPROM..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)eraseDevice:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-e"];
[self log: @"Erasing chip..."];
[self execAvrdude: avrdudeArguments completionHandler: nil];
}
- (IBAction)autodetectDevice:(id)sender
{
NSMutableArray *avrdudeArguments = [self defaultAvrdudeArguments];
[avrdudeArguments addObject: @"-F"];
[avrdudeArguments addObject: @"-U"];
[avrdudeArguments addObject: @"signature:r:/tmp/SIGNATURE.tmp:h"];
[self log: @"Reading signature..."];
[self execAvrdude:avrdudeArguments completionHandler: ^(int returnCode) {
if (returnCode == 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];
if (name != nil) {
[self log: name];
}
NSInteger index = name != nil ? [devicePopUpButton indexOfItemWithTitle: name] : -1;
if (index >= 0) {
[devicePopUpButton selectItemAtIndex: index];
[self deviceChanged: nil];
} else {
[self log: @"Device not found"];
}
}
}];
}
- (void)tableViewDoubleClick: (NSTableView *) tableView
{
if (![self avrdudeAvailable]) {
return;
}
NSMutableArray *settings = nil;
if (tableView == fusesTableView) {
settings = fuseSettings;
}
else if (tableView == lockbitsTableView) {
settings = lockbitSettings;
}
FuseSetting *fuseSetting = [settings objectAtIndex: [tableView selectedRow]];
if (!fuseSetting) {
return;
}
unsigned char fuseValue = [[fuses objectForKey: fuseSetting->fuse] intValue];
BOOL singleElementGroup = YES;
for (int i = 0; i < [settings count]; i++) {
FuseSetting *fuseSetting1 = [settings objectAtIndex: i];
if (fuseSetting1 != fuseSetting &&
[fuseSetting1->fuse isEqualToString: fuseSetting->fuse] &&
fuseSetting1->mask == fuseSetting->mask) {
singleElementGroup = NO;
break;
}
}
if (singleElementGroup) {
fuseValue ^= fuseSetting->mask;
}
else if ((fuseValue & fuseSetting->mask) != fuseSetting->value) {
fuseValue |= fuseSetting->mask;
fuseValue &= (~(fuseSetting->mask) | fuseSetting->value);
}
[fuses setObject: [NSNumber numberWithUnsignedChar: fuseValue] forKey: fuseSetting->fuse];
[tableView reloadData];