From 298592383ad2a98922064a0d183390e896856483 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Thu, 6 May 2010 02:06:12 +0000
Subject: [PATCH] Fixed TeensyHID bootloader so that it works with the official
 TeensyLoader GUI application with all supported AVR models.

---
 Bootloaders/TeensyHID/Descriptors.c | 24 ++++++++++++++----------
 Bootloaders/TeensyHID/Descriptors.h | 23 +++++++++++++++++++++++
 Bootloaders/TeensyHID/TeensyHID.c   |  2 +-
 Bootloaders/TeensyHID/TeensyHID.txt |  2 ++
 Bootloaders/TeensyHID/makefile      |  4 ++--
 5 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/Bootloaders/TeensyHID/Descriptors.c b/Bootloaders/TeensyHID/Descriptors.c
index 9cd987c4d..d421d90f4 100644
--- a/Bootloaders/TeensyHID/Descriptors.c
+++ b/Bootloaders/TeensyHID/Descriptors.c
@@ -45,16 +45,20 @@
  */
 USB_Descriptor_HIDReport_Datatype_t HIDReport[] =
 {
-	0x06, 0x9c, 0xff,     /* Usage Page (Vendor Defined)                     */
-	0x09, 0x1B,           /* Usage (Vendor Defined)                          */
-	0xa1, 0x01,           /* Collection (Vendor Defined)                     */
-	0x0a, 0x19, 0x00,     /*   Usage (Vendor Defined)                        */
-	0x75, 0x08,           /*   Report Size (8)                               */
-	0x95, 0x82,           /*   Report Count (130)                            */
-	0x15, 0x00,           /*   Logical Minimum (0)                           */
-	0x25, 0xff,           /*   Logical Maximum (255)                         */
-	0x91, 0x02,           /*   Output (Data, Variable, Absolute)             */
-	0xc0                  /* End Collection                                  */
+	0x06, 0x9c, 0xff,        /* Usage Page (Vendor Defined)                     */
+	0x09, TEENSY_USAGEPAGE,  /* Usage (Vendor Defined)                          */
+	0xa1, 0x01,              /* Collection (Vendor Defined)                     */
+	0x0a, 0x19, 0x00,        /*   Usage (Vendor Defined)                        */
+	0x75, 0x08,              /*   Report Size (8)                               */
+#if (SPM_PAGESIZE == 128)    /*   Report Count (SPM_PAGESIZE + 2)               */
+	0x95, (SPM_PAGESIZE + 2)
+#else
+	0x96, ((SPM_PAGESIZE + 2) & 0xFF), ((SPM_PAGESIZE + 2) >> 8),
+#endif
+	0x15, 0x00,              /*   Logical Minimum (0)                           */
+	0x25, 0xff,              /*   Logical Maximum (255)                         */
+	0x91, 0x02,              /*   Output (Data, Variable, Absolute)             */
+	0xc0                     /* End Collection                                  */
 };
 
 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
diff --git a/Bootloaders/TeensyHID/Descriptors.h b/Bootloaders/TeensyHID/Descriptors.h
index 0ea7f3935..37a2b644e 100644
--- a/Bootloaders/TeensyHID/Descriptors.h
+++ b/Bootloaders/TeensyHID/Descriptors.h
@@ -83,6 +83,29 @@
 		
 		/** Descriptor header type value, to indicate a HID class HID report descriptor. */
 		#define DTYPE_Report              0x22
+		
+		/** Vendor usage page for the Teensy 1.0 board */
+		#define TEENSY_USAGEPAGE_10       0x19
+
+		/** Vendor usage page for the Teensy++ 1.0 board */
+		#define TEENSY_USAGEPAGE_10PP     0x1A
+
+		/** Vendor usage page for the Teensy 2.0 board */
+		#define TEENSY_USAGEPAGE_20       0x1B
+		
+		/** Vendor usage page for the Teensy++ 1.0 board */
+		#define TEENSY_USAGEPAGE_20PP     0x1C
+		
+		#if defined(USB_SERIES_2_AVR)
+			#define TEENSY_USAGEPAGE      TEENSY_USAGEPAGE_10
+		#elif defined(USB_SERIES_4_AVR)
+			#define TEENSY_USAGEPAGE      TEENSY_USAGEPAGE_20
+			#define TEENSY_USAGE          TEENSY_USAGE_20
+		#elif defined(USB_SERIES_6_AVR)
+			#define TEENSY_USAGEPAGE      TEENSY_USAGEPAGE_10PP
+		#elif defined(USB_SERIES_7_AVR)
+			#define TEENSY_USAGEPAGE      TEENSY_USAGEPAGE_20PP
+		#endif
 
 	/* Function Prototypes: */
 		uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress)
diff --git a/Bootloaders/TeensyHID/TeensyHID.c b/Bootloaders/TeensyHID/TeensyHID.c
index 5431a5700..4e287bcf4 100644
--- a/Bootloaders/TeensyHID/TeensyHID.c
+++ b/Bootloaders/TeensyHID/TeensyHID.c
@@ -120,7 +120,7 @@ void EVENT_USB_Device_UnhandledControlRequest(void)
 				boot_spm_busy_wait();
 				
 				/* Write each of the FLASH page's bytes in sequence */
-				for (uint8_t PageByte = 0; PageByte < SPM_PAGESIZE; PageByte += 2)
+				for (uint16_t PageByte = 0; PageByte < SPM_PAGESIZE; PageByte += 2)
 				{
 					/* Check if endpoint is empty - if so clear it and wait until ready for next packet */
 					if (!(Endpoint_BytesInEndpoint()))
diff --git a/Bootloaders/TeensyHID/TeensyHID.txt b/Bootloaders/TeensyHID/TeensyHID.txt
index 52b6327dc..9dac8f5d0 100644
--- a/Bootloaders/TeensyHID/TeensyHID.txt
+++ b/Bootloaders/TeensyHID/TeensyHID.txt
@@ -11,7 +11,9 @@
  *  The following list indicates what microcontrollers are compatible with this demo.
  *
  *  - AT90USB162 (Teensy 1.0)
+ *  - AT90USB646 (Teensy++ 1.0)
  *  - ATMEGA32U4 (Teensy 2.0)
+ *  - AT90USB1287 (Teensy++ 2.0)
  *
  *  \section SSec_Info USB Information:
  *
diff --git a/Bootloaders/TeensyHID/makefile b/Bootloaders/TeensyHID/makefile
index 404b55f9c..0d12374b5 100644
--- a/Bootloaders/TeensyHID/makefile
+++ b/Bootloaders/TeensyHID/makefile
@@ -48,7 +48,7 @@
 
 
 # MCU name
-MCU = atmega32u4
+MCU = at90usb1287
 
 
 # Target board (see library "Board Types" documentation, NONE for projects not requiring
@@ -86,7 +86,7 @@ F_CLOCK = $(F_CPU)
 
 
 # Starting byte address of the bootloader
-BOOT_START = 0x7000
+BOOT_START = 0x1e000
 
 
 # Output format. (can be srec, ihex, binary)
-- 
GitLab