diff options
Diffstat (limited to 'Bootloaders/HID')
-rw-r--r-- | Bootloaders/HID/BootloaderHID.c | 27 | ||||
-rw-r--r-- | Bootloaders/HID/BootloaderHID.h | 6 | ||||
-rw-r--r-- | Bootloaders/HID/Config/LUFAConfig.h | 4 | ||||
-rw-r--r-- | Bootloaders/HID/Descriptors.c | 4 | ||||
-rw-r--r-- | Bootloaders/HID/Descriptors.h | 4 | ||||
-rw-r--r-- | Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py | 45 | ||||
-rw-r--r-- | Bootloaders/HID/makefile | 2 |
7 files changed, 58 insertions, 34 deletions
diff --git a/Bootloaders/HID/BootloaderHID.c b/Bootloaders/HID/BootloaderHID.c index fa1dd5873..68a7cb5e5 100644 --- a/Bootloaders/HID/BootloaderHID.c +++ b/Bootloaders/HID/BootloaderHID.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -84,6 +84,9 @@ int main(void) while (RunBootloader) USB_USBTask(); + /* Wait a short time to end all USB transactions and then disconnect */ + _delay_us(1000); + /* Disconnect from the host - USB interface will be reset later along with the AVR */ USB_Detach(); @@ -152,6 +155,10 @@ void EVENT_USB_Device_ControlRequest(void) uint16_t PageAddress = Endpoint_Read_16_LE(); #endif + /* Determine if the given page address is correctly aligned to the + start of a flash page. */ + bool PageAddressIsAligned = !(PageAddress & (SPM_PAGESIZE - 1)); + /* Check if the command is a program page command, or a start application command */ #if (FLASHEND > 0xFFFF) if ((uint16_t)(PageAddress >> 8) == COMMAND_STARTAPPLICATION) @@ -161,11 +168,14 @@ void EVENT_USB_Device_ControlRequest(void) { RunBootloader = false; } - else if (PageAddress < BOOT_START_ADDR) + else if ((PageAddress < BOOT_START_ADDR) && PageAddressIsAligned) { /* Erase the given FLASH page, ready to be programmed */ - boot_page_erase(PageAddress); - boot_spm_busy_wait(); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_erase(PageAddress); + boot_spm_busy_wait(); + } /* Write each of the FLASH page's bytes in sequence */ for (uint8_t PageWord = 0; PageWord < (SPM_PAGESIZE / 2); PageWord++) @@ -182,8 +192,11 @@ void EVENT_USB_Device_ControlRequest(void) } /* Write the filled FLASH page to memory */ - boot_page_write(PageAddress); - boot_spm_busy_wait(); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_write(PageAddress); + boot_spm_busy_wait(); + } /* Re-enable RWW section */ boot_rww_enable(); diff --git a/Bootloaders/HID/BootloaderHID.h b/Bootloaders/HID/BootloaderHID.h index 62ee07de3..8e0c7685f 100644 --- a/Bootloaders/HID/BootloaderHID.h +++ b/Bootloaders/HID/BootloaderHID.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -42,6 +42,8 @@ #include <avr/boot.h> #include <avr/power.h> #include <avr/interrupt.h> + #include <util/atomic.h> + #include <util/delay.h> #include <stdbool.h> #include "Descriptors.h" diff --git a/Bootloaders/HID/Config/LUFAConfig.h b/Bootloaders/HID/Config/LUFAConfig.h index 5aa0e765b..33d50a786 100644 --- a/Bootloaders/HID/Config/LUFAConfig.h +++ b/Bootloaders/HID/Config/LUFAConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/HID/Descriptors.c b/Bootloaders/HID/Descriptors.c index 854ae1b63..19d4ec47e 100644 --- a/Bootloaders/HID/Descriptors.c +++ b/Bootloaders/HID/Descriptors.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/HID/Descriptors.h b/Bootloaders/HID/Descriptors.h index 5516b1635..151920dc2 100644 --- a/Bootloaders/HID/Descriptors.h +++ b/Bootloaders/HID/Descriptors.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py b/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py index cb824f582..4bbb52f70 100644 --- a/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py +++ b/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py @@ -1,6 +1,8 @@ +#!/usr/bin/env python + """ LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org @@ -15,12 +17,12 @@ Example: python hid_bootloader_loader.py at90usb1287 Mouse.hex - Requires the pywinusb (https://pypi.python.org/pypi/pywinusb/) and - IntelHex (https://pypi.python.org/pypi/IntelHex/) libraries. + Requires the hidapi (https://pypi.python.org/pypi/hidapi) and + IntelHex (https://pypi.python.org/pypi/IntelHex) libraries. """ import sys -from pywinusb import hid +import hid from intelhex import IntelHex @@ -40,26 +42,29 @@ device_info_map['at90usb82'] = {'page_size': 128, 'flash_kb': 8} def get_hid_device_handle(): - hid_device_filter = hid.HidDeviceFilter(vendor_id=0x03EB, - product_id=0x2067) + all_hid_devices = hid.enumerate() - valid_hid_devices = hid_device_filter.get_devices() + lufa_hid_devices = [d for d in all_hid_devices if d['vendor_id'] == 0x03EB and d['product_id'] == 0x2067] - if len(valid_hid_devices) is 0: + if len(lufa_hid_devices) is 0: return None - else: - return valid_hid_devices[0] + + device_handle = hid.device() + device_handle.open_path(lufa_hid_devices[0]['path']) + return device_handle def send_page_data(hid_device, address, data): # Bootloader page data should be the HID Report ID (always zero) followed # by the starting address to program, then one device's flash page worth # of data - output_report_data = [0] - output_report_data.extend([address & 0xFF, address >> 8]) - output_report_data.extend(data) + output_report_data = bytearray(65) + output_report_data[0] = 0 + output_report_data[1] = address & 0xFF + output_report_data[2] = address >> 8 + output_report_data[3 : ] = data - hid_device.send_output_report(output_report_data) + hid_device.write(output_report_data) def program_device(hex_data, device_info): @@ -70,8 +75,7 @@ def program_device(hex_data, device_info): sys.exit(1) try: - hid_device.open() - print("Connected to bootloader.") + print("Connected to bootloader.", flush=True) # Program in all data from the loaded HEX file, in a number of device # page sized chunks @@ -83,7 +87,7 @@ def program_device(hex_data, device_info): # address and convert it to a regular list of bytes page_data = [hex_data[i] for i in current_page_range] - print("Writing address 0x%04X-0x%04X" % (current_page_range[0], current_page_range[-1])) + print("Writing address 0x%04X-0x%04X" % (current_page_range[0], current_page_range[-1]), flush=True) # Devices with more than 64KB of flash should shift down the page # address so that it is 16-bit (page size is guaranteed to be @@ -95,7 +99,7 @@ def program_device(hex_data, device_info): # Once programming is complete, start the application via a dummy page # program to the page address 0xFFFF - print("Programming complete, starting application.") + print("Programming complete, starting application.", flush=True) send_page_data(hid_device, 0xFFFF, [0] * device_info['page_size']) finally: @@ -103,6 +107,11 @@ def program_device(hex_data, device_info): if __name__ == '__main__': + if len(sys.argv) != 3: + print("Usage:") + print("\t{} device file.hex".format(sys.argv[0])) + sys.exit(1) + # Load the specified HEX file try: hex_data = IntelHex(sys.argv[2]) diff --git a/Bootloaders/HID/makefile b/Bootloaders/HID/makefile index 12cfadb5d..238823fe3 100644 --- a/Bootloaders/HID/makefile +++ b/Bootloaders/HID/makefile @@ -1,6 +1,6 @@ # # LUFA Library -# Copyright (C) Dean Camera, 2017. +# Copyright (C) Dean Camera, 2019. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org |