From 3bd36f811c00b79ac35266460dbdcc0921821d4d Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Mon, 20 Jun 2011 14:46:09 +0000
Subject: [PATCH] Update Mouse demo to support the UC3 devices in the
 MultiArchDemos branch.

---
 Demos/Device/ClassDriver/Mouse/Descriptors.c |  48 +-
 Demos/Device/ClassDriver/Mouse/Descriptors.h |   9 +-
 Demos/Device/ClassDriver/Mouse/Mouse.c       |  42 +-
 Demos/Device/ClassDriver/Mouse/Mouse.h       |  28 +-
 Demos/Device/ClassDriver/Mouse/makefile.uc3  | 493 +++++++++++++++++++
 5 files changed, 584 insertions(+), 36 deletions(-)
 create mode 100644 Demos/Device/ClassDriver/Mouse/makefile.uc3

diff --git a/Demos/Device/ClassDriver/Mouse/Descriptors.c b/Demos/Device/ClassDriver/Mouse/Descriptors.c
index b3c9e38c4..4c5e6cf0b 100644
--- a/Demos/Device/ClassDriver/Mouse/Descriptors.c
+++ b/Demos/Device/ClassDriver/Mouse/Descriptors.c
@@ -72,13 +72,13 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
 
 	.Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,
 
-	.VendorID               = 0x03EB,
-	.ProductID              = 0x2041,
-	.ReleaseNumber          = VERSION_BCD(00.01),
+	.VendorID               = CPU_TO_LE16(0x03EB),
+	.ProductID              = CPU_TO_LE16(0x2041),
+	.ReleaseNumber          = VERSION_BCD(00.02),
 
 	.ManufacturerStrIndex   = 0x01,
 	.ProductStrIndex        = 0x02,
-	.SerialNumStrIndex      = NO_DESCRIPTOR,
+	.SerialNumStrIndex      = USE_INTERNAL_SERIAL,
 
 	.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
 };
@@ -94,7 +94,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 		{
 			.Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
 
-			.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
+			.TotalConfigurationSize = CPU_TO_LE16(sizeof(USB_Descriptor_Configuration_t)),
 			.TotalInterfaces        = 1,
 
 			.ConfigurationNumber    = 1,
@@ -129,7 +129,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 			.CountryCode            = 0x00,
 			.TotalReportDescriptors = 1,
 			.HIDReportType          = HID_DTYPE_Report,
-			.HIDReportLength        = sizeof(MouseReport)
+			.HIDReportLength        = CPU_TO_LE16(sizeof(MouseReport))
 		},
 
 	.HID_ReportINEndpoint =
@@ -138,7 +138,7 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
 
 			.EndpointAddress        = (ENDPOINT_DESCRIPTOR_DIR_IN | MOUSE_EPNUM),
 			.Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
-			.EndpointSize           = MOUSE_EPSIZE,
+			.EndpointSize           = CPU_TO_LE16(MOUSE_EPSIZE),
 			.PollingIntervalMS      = 0x01
 		}
 };
@@ -162,7 +162,17 @@ const USB_Descriptor_String_t PROGMEM ManufacturerString =
 {
 	.Header                 = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
 
-	.UnicodeString          = L"Dean Camera"
+	.UnicodeString          = {CPU_TO_LE16('D'),
+	                           CPU_TO_LE16('e'),
+	                           CPU_TO_LE16('a'),
+	                           CPU_TO_LE16('n'),
+	                           CPU_TO_LE16(' '),
+	                           CPU_TO_LE16('C'),
+	                           CPU_TO_LE16('a'),
+	                           CPU_TO_LE16('m'),
+	                           CPU_TO_LE16('e'),
+	                           CPU_TO_LE16('r'),
+	                           CPU_TO_LE16('a')}
 };
 
 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
@@ -173,7 +183,21 @@ const USB_Descriptor_String_t PROGMEM ProductString =
 {
 	.Header                 = {.Size = USB_STRING_LEN(15), .Type = DTYPE_String},
 
-	.UnicodeString          = L"LUFA Mouse Demo"
+	.UnicodeString          = {CPU_TO_LE16('L'),
+	                           CPU_TO_LE16('U'),
+	                           CPU_TO_LE16('F'),
+	                           CPU_TO_LE16('A'),
+	                           CPU_TO_LE16(' '),
+	                           CPU_TO_LE16('M'),
+	                           CPU_TO_LE16('o'),
+	                           CPU_TO_LE16('u'),
+	                           CPU_TO_LE16('s'),
+	                           CPU_TO_LE16('e'),
+	                           CPU_TO_LE16(' '),
+	                           CPU_TO_LE16('D'),
+	                           CPU_TO_LE16('e'),
+	                           CPU_TO_LE16('m'),
+	                           CPU_TO_LE16('o')}
 };
 
 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
@@ -207,15 +231,15 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
 			{
 				case 0x00:
 					Address = &LanguageString;
-					Size    = pgm_read_byte(&LanguageString.Header.Size);
+					Size    = LanguageString.Header.Size;
 					break;
 				case 0x01:
 					Address = &ManufacturerString;
-					Size    = pgm_read_byte(&ManufacturerString.Header.Size);
+					Size    = ManufacturerString.Header.Size;
 					break;
 				case 0x02:
 					Address = &ProductString;
-					Size    = pgm_read_byte(&ProductString.Header.Size);
+					Size    = ProductString.Header.Size;
 					break;
 			}
 
diff --git a/Demos/Device/ClassDriver/Mouse/Descriptors.h b/Demos/Device/ClassDriver/Mouse/Descriptors.h
index 8ea34e014..e8754621f 100644
--- a/Demos/Device/ClassDriver/Mouse/Descriptors.h
+++ b/Demos/Device/ClassDriver/Mouse/Descriptors.h
@@ -37,10 +37,13 @@
 #define _DESCRIPTORS_H_
 
 	/* Includes: */
+		#include <LUFA/Common/Common.h>
 		#include <LUFA/Drivers/USB/USB.h>
 
-		#include <avr/pgmspace.h>
-
+		#if (ARCH == ARCH_AVR8)
+			#include <avr/pgmspace.h>
+		#endif
+		
 	/* Type Defines: */
 		/** Type define for the device configuration descriptor structure. This must be defined in the
 		 *  application code, as the configuration descriptor contains several sub-descriptors which
@@ -52,7 +55,7 @@
 			USB_Descriptor_Interface_t            HID_Interface;
 			USB_HID_Descriptor_HID_t              HID_MouseHID;
 	        USB_Descriptor_Endpoint_t             HID_ReportINEndpoint;
-		} USB_Descriptor_Configuration_t;
+		} ATTR_PACKED USB_Descriptor_Configuration_t;
 
 	/* Macros: */
 		/** Endpoint number of the Mouse HID reporting IN endpoint. */
diff --git a/Demos/Device/ClassDriver/Mouse/Mouse.c b/Demos/Device/ClassDriver/Mouse/Mouse.c
index 070b48199..f972644f6 100644
--- a/Demos/Device/ClassDriver/Mouse/Mouse.c
+++ b/Demos/Device/ClassDriver/Mouse/Mouse.c
@@ -67,7 +67,7 @@ int main(void)
 	SetupHardware();
 
 	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
-	sei();
+	GlobalInterruptEnable();
 
 	for (;;)
 	{
@@ -79,13 +79,35 @@ int main(void)
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
-	/* Disable watchdog if enabled by bootloader/fuses */
-	MCUSR &= ~(1 << WDRF);
-	wdt_disable();
-
-	/* Disable clock division */
-	clock_prescale_set(clock_div_1);
-
+	#if (ARCH == ARCH_AVR8)
+		/* Disable watchdog if enabled by bootloader/fuses */
+		MCUSR &= ~(1 << WDRF);
+		wdt_disable();
+
+		/* Disable clock division */
+		clock_prescale_set(clock_div_1);
+	#elif (ARCH == ARCH_UC3)
+		/* Initialize interrupt subsystem */
+		INTC_Init();
+		INTC_RegisterGroupHandler(AVR32_USBB_IRQ, AVR32_INTC_INT0, USB_GEN_vect);
+
+		/* Select slow startup, external high frequency crystal attached to OSC0 */
+		AVR32_PM.OSCCTRL0.startup = 6;
+		AVR32_PM.OSCCTRL0.mode    = 7;
+		AVR32_PM.MCCTRL.osc0en    = true;
+		while (!(AVR32_PM.POSCSR.osc0rdy));
+
+		/* Switch CPU core to use OSC0 as the system clock */
+		AVR32_PM.MCCTRL.mcsel     = 1;
+
+		/* Start PLL1 to feed into the USB generic clock module */
+		AVR32_PM.PLL[1].pllmul    = (F_USB / F_CPU) ? (((F_USB / F_CPU) - 1) / 2) : 0;
+		AVR32_PM.PLL[1].plldiv    = 0;
+		AVR32_PM.PLL[1].pllosc    = 0;	
+		AVR32_PM.PLL[1].pllen     = true;
+		while (!(AVR32_PM.POSCSR.lock1));	
+	#endif
+	
 	/* Hardware Initialization */
 	Joystick_Init();
 	LEDs_Init();
@@ -147,8 +169,8 @@ bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDIn
 {
 	USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;
 
-	uint8_t JoyStatus_LCL    = Joystick_GetStatus();
-	uint8_t ButtonStatus_LCL = Buttons_GetStatus();
+	uint_reg_t JoyStatus_LCL    = Joystick_GetStatus();
+	uint_reg_t ButtonStatus_LCL = Buttons_GetStatus();
 
 	if (JoyStatus_LCL & JOY_UP)
 	  MouseReport->Y = -1;
diff --git a/Demos/Device/ClassDriver/Mouse/Mouse.h b/Demos/Device/ClassDriver/Mouse/Mouse.h
index c85932afb..6e1827512 100644
--- a/Demos/Device/ClassDriver/Mouse/Mouse.h
+++ b/Demos/Device/ClassDriver/Mouse/Mouse.h
@@ -37,22 +37,28 @@
 #define _MOUSE_H_
 
 	/* Includes: */
-		#include <avr/io.h>
-		#include <avr/wdt.h>
-		#include <avr/interrupt.h>
-		#include <avr/power.h>
-		#include <avr/interrupt.h>
-		#include <stdbool.h>
-		#include <string.h>
-
-		#include "Descriptors.h"
-
-		#include <LUFA/Version.h>
+		#include <LUFA/Common/Common.h>
 		#include <LUFA/Drivers/Board/Joystick.h>
 		#include <LUFA/Drivers/Board/LEDs.h>
 		#include <LUFA/Drivers/Board/Buttons.h>
 		#include <LUFA/Drivers/USB/USB.h>
 
+		#if (ARCH == ARCH_AVR8)
+			#include <avr/io.h>
+			#include <avr/wdt.h>
+			#include <avr/interrupt.h>
+			#include <avr/power.h>
+			#include <avr/interrupt.h>
+			#include <stdbool.h>
+			#include <string.h>
+		#elif (ARCH == ARCH_UC3)
+			#include <LUFA/Platform/UC3/INTC.h>
+
+			#include <avr32/io.h>			
+		#endif
+
+		#include "Descriptors.h"
+
 	/* Macros: */
 		/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
 		#define LEDMASK_USB_NOTREADY      LEDS_LED1
diff --git a/Demos/Device/ClassDriver/Mouse/makefile.uc3 b/Demos/Device/ClassDriver/Mouse/makefile.uc3
new file mode 100644
index 000000000..fc9438e94
--- /dev/null
+++ b/Demos/Device/ClassDriver/Mouse/makefile.uc3
@@ -0,0 +1,493 @@
+# Hey Emacs, this is a -*- makefile -*-
+#----------------------------------------------------------------------------
+# WinAVR Makefile Template written by Eric B. Weddington, J�rg Wunsch, et al.
+#  >> Modified for use with the LUFA project. <<
+#
+# Released to the Public Domain
+#
+# Additional material for this makefile was written by:
+# Peter Fleury
+# Tim Henigan
+# Colin O'Flynn
+# Reiner Patommel
+# Markus Pfaff
+# Sander Pool
+# Frederik Rouleau
+# Carlos Lamas
+# Dean Camera
+# Opendous Inc.
+# Denver Gingerich
+#
+#----------------------------------------------------------------------------
+# On command line:
+#
+# make all = Make software.
+#
+# make clean = Clean out built project files.
+#
+# make dfu = Download the hex file to the device, using dfu-programmer (must
+#            have dfu-programmer installed).
+#
+# make flip = Download the hex file to the device, using Atmel FLIP (must
+#             have Atmel FLIP installed).
+#
+# make doxygen = Generate DoxyGen documentation for the project (must have
+#                DoxyGen installed)
+#
+# make filename.s = Just compile filename.c into the assembler code only.
+#
+# make filename.i = Create a preprocessed source file for use in submitting
+#                   bug reports to the GCC project.
+#
+# To rebuild project do "make clean" then "make all".
+#----------------------------------------------------------------------------
+
+
+# MCU name
+MCU = uc3b0256
+
+
+# Target board (see library "Board Types" documentation, NONE for projects not requiring
+# LUFA board drivers). If USER is selected, put custom board drivers in a directory called
+# "Board" inside the application directory.
+BOARD = EVK1101
+
+
+# Targeted chip architecture (see library "Architectures" documentation)
+ARCH = UC3
+
+
+# Processor frequency.
+#     This will define a symbol, F_CPU, in all source code files equal to the
+#     processor frequency in Hz. You can then use this symbol in your source code to
+#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
+#     automatically to create a 32-bit value in your source code.
+#
+#     This will be an integer division of F_USB below, as it is sourced by
+#     F_USB after it has run through any CPU prescalers. Note that this value
+#     does not *change* the processor frequency - it should merely be updated to
+#     reflect the processor speed set externally so that the code can use accurate
+#     software delays.
+F_CPU = 12000000
+
+
+# Input clock frequency.
+#     This will define a symbol, F_USB, in all source code files equal to the
+#     input clock frequency (before any prescaling is performed) in Hz. This value may
+#     differ from F_CPU if prescaling is used on the latter, and is required as the
+#     raw input clock is fed directly to the PLL sections of the AVR for high speed
+#     clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
+#     at the end, this will be done automatically to create a 32-bit value in your
+#     source code.
+#
+#     If no clock division is performed on the input clock inside the AVR (via the
+#     CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
+F_USB = 48000000
+
+
+# Output format. (can be srec, ihex, binary)
+FORMAT = ihex
+
+
+# Target file name (without extension).
+TARGET = Mouse
+
+
+# Object files directory
+#     To put object files in current directory, use a dot (.), do NOT make
+#     this an empty or blank macro!
+OBJDIR = .
+
+
+# Path to the LUFA library
+LUFA_PATH = ../../../../
+
+
+# LUFA library compile-time options and predefined tokens
+LUFA_OPTS  = -D USB_DEVICE_ONLY
+LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=32
+LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
+LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_GCLK_SRC_PLL | USB_OPT_GCLK_CHANNEL_1)"
+
+
+# Create the LUFA source path variables by including the LUFA root makefile
+include $(LUFA_PATH)/LUFA/makefile
+
+
+# List C source files here. (C dependencies are automatically generated.)
+SRC = $(TARGET).c                                                 \
+	  Descriptors.c                                               \
+	  $(LUFA_PATH)/LUFA/Platform/UC3/INTC.c                       \
+	  $(LUFA_SRC_USB)                                             \
+	  $(LUFA_SRC_USBCLASS)
+
+
+# List C++ source files here. (C dependencies are automatically generated.)
+CPPSRC =
+
+
+# List Assembler source files here.
+#     Make them always end in a capital .S.  Files ending in a lowercase .s
+#     will not be considered source files but generated files (assembler
+#     output from the compiler), and will be deleted upon "make clean"!
+#     Even though the DOS/Win* filesystem matches both .s and .S the same,
+#     it will preserve the spelling of the filenames, and gcc itself does
+#     care about how the name is spelled on its command-line.
+ASRC = $(LUFA_PATH)/LUFA/Platform/UC3/Exception.S
+
+
+# Optimization level, can be [0, 1, 2, 3, s].
+#     0 = turn off optimization. s = optimize for size.
+#     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
+OPT = 0
+
+
+# Debugging level.
+DEBUG = 3
+
+
+# List any extra directories to look for include files here.
+#     Each directory must be seperated by a space.
+#     Use forward slashes for directory separators.
+#     For a directory that has spaces, enclose it in quotes.
+EXTRAINCDIRS = $(LUFA_PATH)/
+
+
+# Compiler flag to set the C Standard level.
+#     c89   = "ANSI" C
+#     gnu89 = c89 plus GCC extensions
+#     c99   = ISO C99 standard (not yet fully implemented)
+#     gnu99 = c99 plus GCC extensions
+CSTANDARD = -std=gnu99
+
+
+# Place -D or -U options here for C sources
+CDEFS  = -DF_CPU=$(F_CPU)UL
+CDEFS += -DF_USB=$(F_USB)UL
+CDEFS += -DBOARD=BOARD_$(BOARD)
+CDEFS += -DARCH=ARCH_$(ARCH)
+CDEFS += $(LUFA_OPTS)
+
+
+# Place -D or -U options here for ASM sources
+ADEFS  = -DF_CPU=$(F_CPU)
+ADEFS += -DF_USB=$(F_USB)UL
+ADEFS += -DBOARD=BOARD_$(BOARD)
+ADEFS += -DARCH=ARCH_$(ARCH)
+ADEFS += $(LUFA_OPTS)
+
+# Place -D or -U options here for C++ sources
+CPPDEFS  = -DF_CPU=$(F_CPU)UL
+CPPDEFS += -DF_USB=$(F_USB)UL
+CPPDEFS += -DBOARD=BOARD_$(BOARD)
+CPPDEFS += -DARCH=ARCH_$(ARCH)
+CPPDEFS += $(LUFA_OPTS)
+#CPPDEFS += -D__STDC_LIMIT_MACROS
+#CPPDEFS += -D__STDC_CONSTANT_MACROS
+
+
+
+#---------------- Compiler Options C ----------------
+#  -g*:          generate debugging information
+#  -O*:          optimization level
+#  -f...:        tuning, see GCC manual and avr-libc documentation
+#  -Wall...:     warning level
+#  -Wa,...:      tell GCC to pass this to the assembler.
+#    -adhlns...: create assembler listing
+CFLAGS = -g$(DEBUG)
+CFLAGS += $(CDEFS)
+CFLAGS += -O$(OPT)
+CFLAGS += -funsigned-char
+CFLAGS += -funsigned-bitfields
+CFLAGS += -ffunction-sections
+CFLAGS += -fno-strict-aliasing
+CFLAGS += -masm-addr-pseudos
+CFLAGS += -Wall
+CFLAGS += -Wstrict-prototypes
+CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
+CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
+CFLAGS += $(CSTANDARD)
+
+
+#---------------- Compiler Options C++ ----------------
+#  -g*:          generate debugging information
+#  -O*:          optimization level
+#  -f...:        tuning, see GCC manual and avr-libc documentation
+#  -Wall...:     warning level
+#  -Wa,...:      tell GCC to pass this to the assembler.
+#    -adhlns...: create assembler listing
+CPPFLAGS = -g$(DEBUG)
+CPPFLAGS += $(CPPDEFS)
+CPPFLAGS += -O$(OPT)
+CPPFLAGS += -funsigned-char
+CPPFLAGS += -funsigned-bitfields
+CPPFLAGS += -ffunction-sections
+CPPFLAGS += -fno-strict-aliasing
+CPPFLAGS += -fno-exceptions
+CPPFLAGS += -masm-addr-pseudos
+CPPFLAGS += -Wall
+CPPFLAGS += -Wundef
+CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
+CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
+#CPPFLAGS += $(CSTANDARD)
+
+
+#---------------- Assembler Options ----------------
+#  -Wa,...:   tell GCC to pass this to the assembler.
+#  -adhlns:   create listing
+#  -gstabs:   have the assembler create line number information; note that
+#             for use in COFF files, additional information about filenames
+#             and function names needs to be present in the assembler source
+#             files -- see avr-libc docs [FIXME: not yet described there]
+#  -listing-cont-lines: Sets the maximum number of continuation lines of hex
+#       dump that will be displayed for a given single line of source input.
+ASFLAGS = -g3 $(ADEFS)
+
+
+#---------------- Linker Options ----------------
+#  -Wl,...:     tell GCC to pass this to linker.
+#    -Map:      create map file
+#    --cref:    add cross reference to  map file
+LDFLAGS  = -Wl,-Map=$(TARGET).map,--cref
+LDFLAGS += -Wl,--gc-sections --rodata-writable
+LDFLAGS += -Wl,--direct-data
+#LDFLAGS += -T avr32elf_uc3b0256.x
+
+
+#============================================================================
+
+
+# Define programs and commands.
+SHELL = sh
+CC = avr32-gcc
+OBJCOPY = avr32-objcopy
+OBJDUMP = avr32-objdump
+SIZE = avr32-size
+AR = avr32-ar rcs
+NM = avr32-nm
+REMOVE = rm -f
+REMOVEDIR = rm -rf
+COPY = cp
+WINSHELL = cmd
+
+
+# Define Messages
+# English
+MSG_ERRORS_NONE = Errors: none
+MSG_BEGIN = -------- begin --------
+MSG_END = --------  end  --------
+MSG_SIZE_BEFORE = Size before:
+MSG_SIZE_AFTER = Size after:
+MSG_COFF = Converting to AVR COFF:
+MSG_FLASH = Creating load file for Flash:
+MSG_EEPROM = Creating load file for EEPROM:
+MSG_EXTENDED_LISTING = Creating Extended Listing:
+MSG_SYMBOL_TABLE = Creating Symbol Table:
+MSG_LINKING = Linking:
+MSG_COMPILING = Compiling C:
+MSG_COMPILING_CPP = Compiling C++:
+MSG_ASSEMBLING = Assembling:
+MSG_CLEANING = Cleaning project:
+MSG_CREATING_LIBRARY = Creating library:
+
+
+
+
+# Define all object files.
+OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
+
+# Define all listing files.
+LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
+
+
+# Compiler flags to generate dependency files.
+GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
+
+
+# Combine all necessary flags and optional flags.
+# Add target processor to flags.
+ALL_CFLAGS = -mpart=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
+ALL_CPPFLAGS = -mpart=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
+ALL_ASFLAGS = -mpart=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
+
+
+
+
+
+# Default target.
+all: begin gccversion sizebefore build sizeafter end
+
+# Change the build target to build a HEX file or a library.
+build: elf hex lss sym
+#build: lib
+
+
+elf: $(TARGET).elf
+hex: $(TARGET).hex
+lss: $(TARGET).lss
+sym: $(TARGET).sym
+LIBNAME=lib$(TARGET).a
+lib: $(LIBNAME)
+
+
+
+# Eye candy.
+# AVR Studio 3.x does not check make's exit code but relies on
+# the following magic strings to be generated by the compile job.
+begin:
+	@echo
+	@echo $(MSG_BEGIN)
+
+end:
+	@echo $(MSG_END)
+	@echo
+
+
+# Display size of file.
+HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
+ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
+MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
+FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
+
+
+sizebefore:
+	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
+	2>/dev/null; echo; fi
+
+sizeafter:
+	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
+	2>/dev/null; echo; fi
+
+
+
+# Display compiler version information.
+gccversion :
+	@$(CC) --version
+
+
+# Program the device.
+flip: $(TARGET).hex
+	batchisp -hardware usb -device $(MCU) -operation erase f
+	batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
+	batchisp -hardware usb -device $(MCU) -operation start reset 0
+
+dfu: $(TARGET).hex
+	dfu-programmer $(MCU) erase
+	dfu-programmer $(MCU) flash $(TARGET).hex
+	dfu-programmer $(MCU) reset
+
+
+# Create final output files (.hex, .eep) from ELF output file.
+%.hex: %.elf
+	@echo
+	@echo $(MSG_FLASH) $@
+	$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
+
+# Create extended listing file from ELF output file.
+%.lss: %.elf
+	@echo
+	@echo $(MSG_EXTENDED_LISTING) $@
+	$(OBJDUMP) -h -S $< > $@
+
+# Create a symbol table from ELF output file.
+%.sym: %.elf
+	@echo
+	@echo $(MSG_SYMBOL_TABLE) $@
+	$(NM) -n $< > $@
+
+
+
+# Create library from object files.
+.SECONDARY : $(TARGET).a
+.PRECIOUS : $(OBJ)
+%.a: $(OBJ)
+	@echo
+	@echo $(MSG_CREATING_LIBRARY) $@
+	$(AR) $@ $(OBJ)
+
+
+# Link: create ELF output file from object files.
+.SECONDARY : $(TARGET).elf
+.PRECIOUS : $(OBJ)
+%.elf: $(OBJ)
+	@echo
+	@echo $(MSG_LINKING) $@
+	$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
+
+
+# Compile: create object files from C source files.
+$(OBJDIR)/%.o : %.c
+	@echo
+	@echo $(MSG_COMPILING) $<
+	$(CC) -c $(ALL_CFLAGS) $< -o $@
+
+
+# Compile: create object files from C++ source files.
+$(OBJDIR)/%.o : %.cpp
+	@echo
+	@echo $(MSG_COMPILING_CPP) $<
+	$(CC) -c $(ALL_CPPFLAGS) $< -o $@
+
+
+# Compile: create assembler files from C source files.
+%.s : %.c
+	$(CC) -S $(ALL_CFLAGS) $< -o $@
+
+
+# Compile: create assembler files from C++ source files.
+%.s : %.cpp
+	$(CC) -S $(ALL_CPPFLAGS) $< -o $@
+
+
+# Assemble: create object files from assembler source files.
+$(OBJDIR)/%.o : %.S
+	@echo
+	@echo $(MSG_ASSEMBLING) $<
+	$(CC) -c $(ALL_ASFLAGS) $< -o $@
+
+
+# Create preprocessed source for use in sending a bug report.
+%.i : %.c
+	$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
+
+
+# Target: clean project.
+clean: begin clean_list end
+
+clean_list :
+	@echo
+	@echo $(MSG_CLEANING)
+	$(REMOVE) $(TARGET).hex
+	$(REMOVE) $(TARGET).cof
+	$(REMOVE) $(TARGET).elf
+	$(REMOVE) $(TARGET).map
+	$(REMOVE) $(TARGET).sym
+	$(REMOVE) $(TARGET).lss
+	$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
+	$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
+	$(REMOVE) $(SRC:.c=.s)
+	$(REMOVE) $(SRC:.c=.d)
+	$(REMOVE) $(SRC:.c=.i)
+	$(REMOVEDIR) .dep
+
+doxygen:
+	@echo Generating Project Documentation...
+	@doxygen Doxygen.conf
+	@echo Documentation Generation Complete.
+
+clean_doxygen:
+	rm -rf Documentation
+
+# Create object files directory
+$(shell mkdir $(OBJDIR) 2>/dev/null)
+
+
+# Include the dependency files.
+-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
+
+
+# Listing of phony targets.
+.PHONY : all begin finish end sizebefore sizeafter gccversion \
+build elf hex lss sym doxygen clean clean_list clean_doxygen  \
+dfu flip
+
-- 
GitLab