3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this  
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in  
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting  
  17   documentation, and that the name of the author not be used in  
  18   advertising or publicity pertaining to distribution of the  
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Main source file for the MIDI bootloader. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  37 #include "BootloaderMIDI.h" 
  39 /** Main program entry point. This routine configures the hardware required by the application, then 
  40  *  enters a loop to run the application tasks in sequence. 
  46         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  55 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  56 void SetupHardware(void) 
  58         /* Disable watchdog if enabled by bootloader/fuses */ 
  59         MCUSR 
&= ~(1 << WDRF
); 
  62         /* Disable clock division */ 
  63         clock_prescale_set(clock_div_1
); 
  65         /* Hardware Initialization */ 
  70 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */ 
  71 void EVENT_USB_Device_Connect(void) 
  73         /* Indicate USB enumerating */ 
  74         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  77 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
  78  *  the status LEDs, disables the sample update and PWM output timers and stops the USB and MIDI management tasks. 
  80 void EVENT_USB_Device_Disconnect(void) 
  82         /* Indicate USB not ready */ 
  83         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  86 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration 
  87  *  of the USB device after enumeration - the device endpoints are configured and the MIDI management task started. 
  89 void EVENT_USB_Device_ConfigurationChanged(void) 
  91         /* Indicate USB connected and ready */ 
  92         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
  94         /* Setup MIDI stream endpoints */ 
  95         if (!(Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPNUM
, EP_TYPE_BULK
, 
  96                                              ENDPOINT_DIR_OUT
, MIDI_STREAM_EPSIZE
, 
  97                                          ENDPOINT_BANK_SINGLE
))) 
  99                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 102         if (!(Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPNUM
, EP_TYPE_BULK
, 
 103                                              ENDPOINT_DIR_IN
, MIDI_STREAM_EPSIZE
, 
 104                                          ENDPOINT_BANK_SINGLE
))) 
 106                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 110 /** Task to handle the generation of MIDI note change events in response to presses of the board joystick, and send them 
 115         /* Device must be connected and configured for the task to run */ 
 116         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 119         /* Select the MIDI OUT stream */ 
 120         Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM
); 
 122         if (Endpoint_IsOUTReceived()) 
 124                 USB_MIDI_EventPacket_t InPacket
; 
 125                 Endpoint_Read_Stream_LE(&InPacket
, sizeof(InPacket
)); 
 127                 LEDs_SetAllLEDs(InPacket
.Data2 
> 64 ? LEDS_LED1 
: LEDS_LED2
);    
 130                 uint8_t Channel 
= InPacket
.Data1
; 
 131                 uint8_t Data    
= ((InPacket
.Data2 
& 0x7F) | ((InPacket
.Data3 
== 64) ? 
0x80 : 0x00)); 
 133                 if ((Channel 
== MIDI_CONTROL_CHANNEL
) && (Data 
== CONTROL_ENTER_PROG_MODE
)) 
 135                         USB_MIDI_EventPacket_t MIDIIn 
= (USB_MIDI_EventPacket_t
) 
 138                                         .Command     
= (MIDI_COMMAND_NOTE_ON 
>> 4), 
 140                                         .Data1       
= MIDI_COMMAND_NOTE_ON 
| MIDI_CONTROL_CHANNEL
, 
 141                                         .Data2       
= CONTROL_DEVICE_READY
, 
 145                         Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM
); 
 146                         Endpoint_Write_Stream_LE(&MIDIIn
, sizeof(MIDIIn
));