1 /* Teensy Loader, Command Line Interface
2 * Program and Reboot Teensy Board with HalfKay Bootloader
3 * http://www.pjrc.com/teensy/loader_cli.html
4 * Copyright 2008-2010, PJRC.COM, LLC
6 * You may redistribute this program and/or modify it under the terms
7 * of the GNU General Public License as published by the Free Software
8 * Foundation, version 3 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/
19 /* Want to incorporate this code into a proprietary application??
20 * Just email paul@pjrc.com to ask. Usually it's not a problem,
21 * but you do need to ask to use this code in any way other than
22 * those permitted by the GNU General Public License, version 3 */
24 /* For non-root permissions on ubuntu or similar udev-based linux
25 * http://www.pjrc.com/teensy/49-teensy.rules
38 fprintf(stderr
, "Usage: teensy_loader_cli -mmcu=<MCU> [-w] [-h] [-n] [-v] <file.hex>\n");
39 fprintf(stderr
, "\t-w : Wait for device to appear\n");
40 fprintf(stderr
, "\t-r : Use hard reboot if device not online\n");
41 fprintf(stderr
, "\t-n : No reboot after programming\n");
42 fprintf(stderr
, "\t-v : Verbose output\n");
43 fprintf(stderr
, "\n<MCU> = atmega32u4 | at90usb162 | at90usb646 | at90usb1286\n");
44 fprintf(stderr
, "\nFor more information, please visit:\n");
45 fprintf(stderr
, "http://www.pjrc.com/teensy/loader_cli.html\n");
49 // USB Access Functions
50 int teensy_open(void);
51 int teensy_write(void *buf
, int len
, double timeout
);
52 void teensy_close(void);
53 int hard_reboot(void);
55 // Intel Hex File Functions
56 int read_intel_hex(const char *filename
);
57 int ihex_bytes_within_range(int begin
, int end
);
58 void ihex_get_data(int addr
, int len
, unsigned char *bytes
);
61 int printf_verbose(const char *format
, ...);
62 void delay(double seconds
);
63 void die(const char *str
, ...);
64 void parse_options(int argc
, char **argv
);
66 // options (from user via command line args)
67 int wait_for_device_to_appear
= 0;
68 int hard_reboot_device
= 0;
69 int reboot_after_programming
= 1;
71 int code_size
= 0, block_size
= 0;
72 const char *filename
=NULL
;
75 /****************************************************************/
79 /****************************************************************/
81 int main(int argc
, char **argv
)
83 unsigned char buf
[260];
84 int num
, addr
, r
, first_block
=1, waited
=0;
86 // parse command line arguments
87 parse_options(argc
, argv
);
89 fprintf(stderr
, "Filename must be specified\n\n");
93 fprintf(stderr
, "MCU type must be specified\n\n");
96 printf_verbose("Teensy Loader, Command Line, Version 2.0\n");
98 // read the intel hex file
99 // this is done first so any error is reported before using USB
100 num
= read_intel_hex(filename
);
101 if (num
< 0) die("error reading intel hex file \"%s\"", filename
);
102 printf_verbose("Read \"%s\": %d bytes, %.1f%% usage\n",
103 filename
, num
, (double)num
/ (double)code_size
* 100.0);
105 // open the USB device
107 if (teensy_open()) break;
108 if (hard_reboot_device
) {
109 if (!hard_reboot()) die("Unable to find rebootor\n");
110 printf_verbose("Hard Reboot performed\n");
111 hard_reboot_device
= 0; // only hard reboot once
112 wait_for_device_to_appear
= 1;
114 if (!wait_for_device_to_appear
) die("Unable to open device\n");
116 printf_verbose("Waiting for Teensy device...\n");
117 printf_verbose(" (hint: press the reset button)\n");
122 printf_verbose("Found HalfKay Bootloader\n");
124 // if we waited for the device, read the hex file again
125 // perhaps it changed while we were waiting?
127 num
= read_intel_hex(filename
);
128 if (num
< 0) die("error reading intel hex file \"%s\"", filename
);
129 printf_verbose("Read \"%s\": %d bytes, %.1f%% usage\n",
130 filename
, num
, (double)num
/ (double)code_size
* 100.0);
134 printf_verbose("Programming");
136 for (addr
= 0; addr
< code_size
; addr
+= block_size
) {
137 if (addr
> 0 && !ihex_bytes_within_range(addr
, addr
+ block_size
- 1)) {
138 // don't waste time on blocks that are unused,
139 // but always do the first one to erase the chip
143 if (code_size
< 0x10000) {
145 buf
[1] = (addr
>> 8) & 255;
147 buf
[0] = (addr
>> 8) & 255;
148 buf
[1] = (addr
>> 16) & 255;
150 ihex_get_data(addr
, block_size
, buf
+ 2);
151 r
= teensy_write(buf
, block_size
+ 2, first_block ?
3.0 : 0.25);
152 if (!r
) die("error writing to Teensy\n");
155 printf_verbose("\n");
157 // reboot to the user's new code
158 if (reboot_after_programming
) {
159 printf_verbose("Booting\n");
162 memset(buf
+ 2, 0, sizeof(buf
) - 2);
163 teensy_write(buf
, block_size
+ 2, 0.25);
172 /****************************************************************/
174 /* USB Access - libusb (Linux & FreeBSD) */
176 /****************************************************************/
178 #if defined(USE_LIBUSB)
180 // http://libusb.sourceforge.net/doc/index.html
183 usb_dev_handle
* open_usb_device(int vid
, int pid
)
186 struct usb_device
*dev
;
194 //printf_verbose("\nSearching for USB device:\n");
195 for (bus
= usb_get_busses(); bus
; bus
= bus
->next
) {
196 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
197 //printf_verbose("bus \"%s\", device \"%s\" vid=%04X, pid=%04X\n",
198 // bus->dirname, dev->filename,
199 // dev->descriptor.idVendor,
200 // dev->descriptor.idProduct
202 if (dev
->descriptor
.idVendor
!= vid
) continue;
203 if (dev
->descriptor
.idProduct
!= pid
) continue;
206 printf_verbose("Found device but unable to open");
209 #ifdef LIBUSB_HAS_GET_DRIVER_NP
210 r
= usb_get_driver_np(h
, 0, buf
, sizeof(buf
));
212 r
= usb_detach_kernel_driver_np(h
, 0);
215 printf_verbose("Device is in use by \"%s\" driver", buf
);
220 // Mac OS-X - removing this call to usb_claim_interface() might allow
221 // this to work, even though it is a clear misuse of the libusb API.
222 // normally Apple's IOKit should be used on Mac OS-X
223 r
= usb_claim_interface(h
, 0);
226 printf_verbose("Unable to claim interface, check USB permissions");
235 static usb_dev_handle
*libusb_teensy_handle
= NULL
;
237 int teensy_open(void)
240 libusb_teensy_handle
= open_usb_device(0x16C0, 0x0478);
241 if (libusb_teensy_handle
) return 1;
245 int teensy_write(void *buf
, int len
, double timeout
)
249 if (!libusb_teensy_handle
) return 0;
250 r
= usb_control_msg(libusb_teensy_handle
, 0x21, 9, 0x0200, 0, (char *)buf
,
251 len
, (int)(timeout
* 1000.0));
256 void teensy_close(void)
258 if (!libusb_teensy_handle
) return;
259 usb_release_interface(libusb_teensy_handle
, 0);
260 usb_close(libusb_teensy_handle
);
261 libusb_teensy_handle
= NULL
;
264 int hard_reboot(void)
266 usb_dev_handle
*rebootor
;
269 rebootor
= open_usb_device(0x16C0, 0x0477);
270 if (!rebootor
) return 0;
271 r
= usb_control_msg(rebootor
, 0x21, 9, 0x0200, 0, "reboot", 6, 100);
272 usb_release_interface(rebootor
, 0);
281 /****************************************************************/
283 /* USB Access - Microsoft WIN32 */
285 /****************************************************************/
287 #if defined(USE_WIN32)
289 // http://msdn.microsoft.com/en-us/library/ms790932.aspx
291 #include <setupapi.h>
292 #include <ddk/hidsdi.h>
293 #include <ddk/hidclass.h>
295 HANDLE
open_usb_device(int vid
, int pid
)
299 DWORD index
, required_size
;
300 SP_DEVICE_INTERFACE_DATA iface
;
301 SP_DEVICE_INTERFACE_DETAIL_DATA
*details
;
302 HIDD_ATTRIBUTES attrib
;
306 HidD_GetHidGuid(&guid
);
307 info
= SetupDiGetClassDevs(&guid
, NULL
, NULL
, DIGCF_PRESENT
| DIGCF_DEVICEINTERFACE
);
308 if (info
== INVALID_HANDLE_VALUE
) return NULL
;
309 for (index
=0; 1 ;index
++) {
310 iface
.cbSize
= sizeof(SP_DEVICE_INTERFACE_DATA
);
311 ret
= SetupDiEnumDeviceInterfaces(info
, NULL
, &guid
, index
, &iface
);
313 SetupDiDestroyDeviceInfoList(info
);
316 SetupDiGetInterfaceDeviceDetail(info
, &iface
, NULL
, 0, &required_size
, NULL
);
317 details
= (SP_DEVICE_INTERFACE_DETAIL_DATA
*)malloc(required_size
);
318 if (details
== NULL
) continue;
319 memset(details
, 0, required_size
);
320 details
->cbSize
= sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA
);
321 ret
= SetupDiGetDeviceInterfaceDetail(info
, &iface
, details
,
322 required_size
, NULL
, NULL
);
327 h
= CreateFile(details
->DevicePath
, GENERIC_READ
|GENERIC_WRITE
,
328 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
,
329 FILE_FLAG_OVERLAPPED
, NULL
);
331 if (h
== INVALID_HANDLE_VALUE
) continue;
332 attrib
.Size
= sizeof(HIDD_ATTRIBUTES
);
333 ret
= HidD_GetAttributes(h
, &attrib
);
338 if (attrib
.VendorID
!= vid
|| attrib
.ProductID
!= pid
) {
342 SetupDiDestroyDeviceInfoList(info
);
348 int write_usb_device(HANDLE h
, void *buf
, int len
, int timeout
)
350 static HANDLE event
= NULL
;
351 unsigned char tmpbuf
[1040];
355 if (len
> sizeof(tmpbuf
) - 1) return 0;
357 event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
358 if (!event
) return 0;
361 memset(&ov
, 0, sizeof(ov
));
364 memcpy(tmpbuf
+ 1, buf
, len
);
365 if (!WriteFile(h
, tmpbuf
, len
+ 1, NULL
, &ov
)) {
366 if (GetLastError() != ERROR_IO_PENDING
) return 0;
367 r
= WaitForSingleObject(event
, timeout
);
368 if (r
== WAIT_TIMEOUT
) {
372 if (r
!= WAIT_OBJECT_0
) return 0;
374 if (!GetOverlappedResult(h
, &ov
, &n
, FALSE
)) return 0;
375 if (n
<= 0) return 0;
379 void print_win32_err(void)
384 err
= GetLastError();
385 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, err
,
386 0, buf
, sizeof(buf
), NULL
);
387 printf("err %ld: %s\n", err
, buf
);
390 static HANDLE win32_teensy_handle
= NULL
;
392 int teensy_open(void)
395 win32_teensy_handle
= open_usb_device(0x16C0, 0x0478);
396 if (win32_teensy_handle
) return 1;
400 int teensy_write(void *buf
, int len
, double timeout
)
403 if (!win32_teensy_handle
) return 0;
404 r
= write_usb_device(win32_teensy_handle
, buf
, len
, (int)(timeout
* 1000.0));
405 //if (!r) print_win32_err();
409 void teensy_close(void)
411 if (!win32_teensy_handle
) return;
412 CloseHandle(win32_teensy_handle
);
413 win32_teensy_handle
= NULL
;
416 int hard_reboot(void)
421 rebootor
= open_usb_device(0x16C0, 0x0477);
422 if (!rebootor
) return 0;
423 r
= write_usb_device(rebootor
, "reboot", 6, 100);
424 CloseHandle(rebootor
);
432 /****************************************************************/
434 /* USB Access - Apple's IOKit, Mac OS-X */
436 /****************************************************************/
438 #if defined(USE_APPLE_IOKIT)
440 // http://developer.apple.com/technotes/tn2007/tn2187.html
441 #include <IOKit/IOKitLib.h>
442 #include <IOKit/hid/IOHIDLib.h>
443 #include <IOKit/hid/IOHIDDevice.h>
445 struct usb_list_struct
{
449 struct usb_list_struct
*next
;
452 static struct usb_list_struct
*usb_list
=NULL
;
453 static IOHIDManagerRef hid_manager
=NULL
;
455 void attach_callback(void *context
, IOReturn r
, void *hid_mgr
, IOHIDDeviceRef dev
)
458 struct usb_list_struct
*n
, *p
;
462 type
= IOHIDDeviceGetProperty(dev
, CFSTR(kIOHIDVendorIDKey
));
463 if (!type
|| CFGetTypeID(type
) != CFNumberGetTypeID()) return;
464 if (!CFNumberGetValue((CFNumberRef
)type
, kCFNumberSInt32Type
, &vid
)) return;
465 type
= IOHIDDeviceGetProperty(dev
, CFSTR(kIOHIDProductIDKey
));
466 if (!type
|| CFGetTypeID(type
) != CFNumberGetTypeID()) return;
467 if (!CFNumberGetValue((CFNumberRef
)type
, kCFNumberSInt32Type
, &pid
)) return;
468 n
= (struct usb_list_struct
*)malloc(sizeof(struct usb_list_struct
));
470 //printf("attach callback: vid=%04X, pid=%04X\n", vid, pid);
475 if (usb_list
== NULL
) {
478 for (p
= usb_list
; p
->next
; p
= p
->next
) ;
483 void detach_callback(void *context
, IOReturn r
, void *hid_mgr
, IOHIDDeviceRef dev
)
485 struct usb_list_struct
*p
, *tmp
, *prev
=NULL
;
491 prev
->next
= p
->next
;
505 void init_hid_manager(void)
507 CFMutableDictionaryRef dict
;
510 if (hid_manager
) return;
511 hid_manager
= IOHIDManagerCreate(kCFAllocatorDefault
, kIOHIDOptionsTypeNone
);
512 if (hid_manager
== NULL
|| CFGetTypeID(hid_manager
) != IOHIDManagerGetTypeID()) {
513 if (hid_manager
) CFRelease(hid_manager
);
514 printf_verbose("no HID Manager - maybe this is a pre-Leopard (10.5) system?\n");
517 dict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
518 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
520 IOHIDManagerSetDeviceMatching(hid_manager
, dict
);
522 IOHIDManagerScheduleWithRunLoop(hid_manager
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
523 IOHIDManagerRegisterDeviceMatchingCallback(hid_manager
, attach_callback
, NULL
);
524 IOHIDManagerRegisterDeviceRemovalCallback(hid_manager
, detach_callback
, NULL
);
525 ret
= IOHIDManagerOpen(hid_manager
, kIOHIDOptionsTypeNone
);
526 if (ret
!= kIOReturnSuccess
) {
527 IOHIDManagerUnscheduleFromRunLoop(hid_manager
,
528 CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
529 CFRelease(hid_manager
);
530 printf_verbose("Error opening HID Manager");
534 static void do_run_loop(void)
536 while (CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 0, true) == kCFRunLoopRunHandledSource
) ;
539 IOHIDDeviceRef
open_usb_device(int vid
, int pid
)
541 struct usb_list_struct
*p
;
546 for (p
= usb_list
; p
; p
= p
->next
) {
547 if (p
->vid
== vid
&& p
->pid
== pid
) {
548 ret
= IOHIDDeviceOpen(p
->ref
, kIOHIDOptionsTypeNone
);
549 if (ret
== kIOReturnSuccess
) return p
->ref
;
555 void close_usb_device(IOHIDDeviceRef dev
)
557 struct usb_list_struct
*p
;
560 for (p
= usb_list
; p
; p
= p
->next
) {
562 IOHIDDeviceClose(dev
, kIOHIDOptionsTypeNone
);
568 static IOHIDDeviceRef iokit_teensy_reference
= NULL
;
570 int teensy_open(void)
573 iokit_teensy_reference
= open_usb_device(0x16C0, 0x0478);
574 if (iokit_teensy_reference
) return 1;
578 int teensy_write(void *buf
, int len
, double timeout
)
582 // timeouts do not work on OS-X
583 // IOHIDDeviceSetReportWithCallback is not implemented
584 // even though Apple documents it with a code example!
585 // submitted to Apple on 22-sep-2009, problem ID 7245050
586 if (!iokit_teensy_reference
) return 0;
587 ret
= IOHIDDeviceSetReport(iokit_teensy_reference
,
588 kIOHIDReportTypeOutput
, 0, buf
, len
);
589 if (ret
== kIOReturnSuccess
) return 1;
593 void teensy_close(void)
595 if (!iokit_teensy_reference
) return;
596 close_usb_device(iokit_teensy_reference
);
597 iokit_teensy_reference
= NULL
;
600 int hard_reboot(void)
602 IOHIDDeviceRef rebootor
;
605 rebootor
= open_usb_device(0x16C0, 0x0477);
606 if (!rebootor
) return 0;
607 ret
= IOHIDDeviceSetReport(rebootor
,
608 kIOHIDReportTypeOutput
, 0, (uint8_t *)("reboot"), 6);
609 close_usb_device(rebootor
);
610 if (ret
== kIOReturnSuccess
) return 1;
618 /****************************************************************/
620 /* USB Access - BSD's UHID driver */
622 /****************************************************************/
624 #if defined(USE_UHID)
626 // Thanks to Todd T Fries for help getting this working on OpenBSD
627 // and to Chris Kuethe for the initial patch to use UHID.
629 #include <sys/ioctl.h>
632 #include <dev/usb/usb.h>
633 #ifndef USB_GET_DEVICEINFO
634 #include <dev/usb/usb_ioctl.h>
637 int open_usb_device(int vid
, int pid
)
642 struct usb_device_info info
;
645 dir
= opendir("/dev");
647 while ((d
= readdir(dir
)) != NULL
) {
648 if (strncmp(d
->d_name
, "uhid", 4) != 0) continue;
649 snprintf(buf
, sizeof(buf
), "/dev/%s", d
->d_name
);
650 fd
= open(buf
, O_RDWR
);
651 if (fd
< 0) continue;
652 r
= ioctl(fd
, USB_GET_DEVICEINFO
, &info
);
654 // NetBSD: added in 2004
655 // OpenBSD: added November 23, 2009
656 // FreeBSD: missing (FreeBSD 8.0) - USE_LIBUSB works!
657 die("Error: your uhid driver does not support"
658 " USB_GET_DEVICEINFO, please upgrade!\n");
663 //printf("%s: v=%d, p=%d\n", buf, info.udi_vendorNo, info.udi_productNo);
664 if (info
.udi_vendorNo
== vid
&& info
.udi_productNo
== pid
) {
674 static int uhid_teensy_fd
= -1;
676 int teensy_open(void)
679 uhid_teensy_fd
= open_usb_device(0x16C0, 0x0478);
680 if (uhid_teensy_fd
< 0) return 0;
684 int teensy_write(void *buf
, int len
, double timeout
)
688 // TODO: imeplement timeout... how??
689 r
= write(uhid_teensy_fd
, buf
, len
);
690 if (r
== len
) return 1;
694 void teensy_close(void)
696 if (uhid_teensy_fd
>= 0) {
697 close(uhid_teensy_fd
);
702 int hard_reboot(void)
706 rebootor_fd
= open_usb_device(0x16C0, 0x0477);
707 if (rebootor_fd
< 0) return 0;
708 r
= write(rebootor_fd
, "reboot", 6);
711 if (r
== 6) return 1;
719 /****************************************************************/
721 /* Read Intel Hex File */
723 /****************************************************************/
725 // the maximum flash image size we can support
726 // chips with larger memory may be used, but only this
727 // much intel-hex data can be loaded into memory!
728 #define MAX_MEMORY_SIZE 0x10000
730 static unsigned char firmware_image
[MAX_MEMORY_SIZE
];
731 static unsigned char firmware_mask
[MAX_MEMORY_SIZE
];
732 static int end_record_seen
=0;
733 static int byte_count
;
734 static unsigned int extended_addr
= 0;
735 static int parse_hex_line(char *line
);
737 int read_intel_hex(const char *filename
)
745 for (i
=0; i
<MAX_MEMORY_SIZE
; i
++) {
746 firmware_image
[i
] = 0xFF;
747 firmware_mask
[i
] = 0;
751 fp
= fopen(filename
, "r");
753 //printf("Unable to read file %s\n", filename);
758 if (!fgets(buf
, sizeof(buf
), fp
)) break;
761 if (parse_hex_line(buf
) == 0) {
762 //printf("Warning, parse error line %d\n", lineno);
766 if (end_record_seen
) break;
767 if (feof(stdin
)) break;
774 /* from ihex.c, at http://www.pjrc.com/tech/8051/pm2_docs/intel-hex.html */
776 /* parses a line of intel hex code, stores the data in bytes[] */
777 /* and the beginning address in addr, and returns a 1 if the */
778 /* line was valid, or a 0 if an error occured. The variable */
779 /* num gets the number of bytes that were stored into bytes[] */
783 parse_hex_line(char *line
)
786 int sum
, len
, cksum
, i
;
790 if (line
[0] != ':') return 0;
791 if (strlen(line
) < 11) return 0;
793 if (!sscanf(ptr
, "%02x", &len
)) return 0;
795 if ((int)strlen(line
) < (11 + (len
* 2)) ) return 0;
796 if (!sscanf(ptr
, "%04x", &addr
)) return 0;
798 /* printf("Line: length=%d Addr=%d\n", len, addr); */
799 if (!sscanf(ptr
, "%02x", &code
)) return 0;
800 if (addr
+ extended_addr
+ len
>= MAX_MEMORY_SIZE
) return 0;
802 sum
= (len
& 255) + ((addr
>> 8) & 255) + (addr
& 255) + (code
& 255);
808 if (code
== 2 && len
== 2) {
809 if (!sscanf(ptr
, "%04x", &i
)) return 1;
811 sum
+= ((i
>> 8) & 255) + (i
& 255);
812 if (!sscanf(ptr
, "%02x", &cksum
)) return 1;
813 if (((sum
& 255) + (cksum
& 255)) & 255) return 1;
814 extended_addr
= i
<< 4;
815 //printf("ext addr = %05X\n", extended_addr);
817 if (code
== 4 && len
== 2) {
818 if (!sscanf(ptr
, "%04x", &i
)) return 1;
820 sum
+= ((i
>> 8) & 255) + (i
& 255);
821 if (!sscanf(ptr
, "%02x", &cksum
)) return 1;
822 if (((sum
& 255) + (cksum
& 255)) & 255) return 1;
823 extended_addr
= i
<< 16;
824 //printf("ext addr = %08X\n", extended_addr);
826 return 1; // non-data line
830 if (sscanf(ptr
, "%02x", &i
) != 1) return 0;
832 firmware_image
[addr
+ extended_addr
+ num
] = i
;
833 firmware_mask
[addr
+ extended_addr
+ num
] = 1;
837 if (num
>= 256) return 0;
839 if (!sscanf(ptr
, "%02x", &cksum
)) return 0;
840 if (((sum
& 255) + (cksum
& 255)) & 255) return 0; /* checksum error */
844 int ihex_bytes_within_range(int begin
, int end
)
848 if (begin
< 0 || begin
>= MAX_MEMORY_SIZE
||
849 end
< 0 || end
>= MAX_MEMORY_SIZE
) {
852 for (i
=begin
; i
<=end
; i
++) {
853 if (firmware_mask
[i
]) return 1;
858 void ihex_get_data(int addr
, int len
, unsigned char *bytes
)
862 if (addr
< 0 || len
< 0 || addr
+ len
>= MAX_MEMORY_SIZE
) {
863 for (i
=0; i
<len
; i
++) {
868 for (i
=0; i
<len
; i
++) {
869 if (firmware_mask
[addr
]) {
870 bytes
[i
] = firmware_image
[addr
];
878 /****************************************************************/
882 /****************************************************************/
884 int printf_verbose(const char *format
, ...)
889 va_start(ap
, format
);
891 r
= vprintf(format
, ap
);
898 void delay(double seconds
)
901 Sleep(seconds
* 1000.0);
903 usleep(seconds
* 1000000.0);
907 void die(const char *str
, ...)
912 vfprintf(stderr
, str
, ap
);
913 fprintf(stderr
, "\n");
918 #define strcasecmp stricmp
921 void parse_options(int argc
, char **argv
)
926 for (i
=1; i
<argc
; i
++) {
928 //printf("arg: %s\n", arg);
930 if (strcmp(arg
, "-w") == 0) {
931 wait_for_device_to_appear
= 1;
932 } else if (strcmp(arg
, "-r") == 0) {
933 hard_reboot_device
= 1;
934 } else if (strcmp(arg
, "-n") == 0) {
935 reboot_after_programming
= 0;
936 } else if (strcmp(arg
, "-v") == 0) {
938 } else if (strncmp(arg
, "-mmcu=", 6) == 0) {
939 if (strcasecmp(arg
+6, "at90usb162") == 0) {
942 } else if (strcasecmp(arg
+6, "atmega32u4") == 0) {
945 } else if (strcasecmp(arg
+6, "at90usb646") == 0) {
948 } else if (strcasecmp(arg
+6, "at90usb1286") == 0) {
952 die("Unknown MCU type\n");