Get rid of the redundant ATTR_NEVER_INLINE macro which translated to the same as...
[pub/USBasp.git] / Projects / MIDIToneGenerator / MIDIToneGenerator.c
index 851db25..ed2d334 100644 (file)
@@ -1,24 +1,24 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2011.
-              
+     Copyright (C) Dean Camera, 2013.
+
   dean [at] fourwalledcubicle [dot] com
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
 */
 
 /*
-  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
 
-  Permission to use, copy, modify, distribute, and sell this 
+  Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
   software and its documentation for any purpose is hereby granted
-  without fee, provided that the above copyright notice appear in 
+  without fee, provided that the above copyright notice appear in
   all copies and that both that the copyright notice and this
   all copies and that both that the copyright notice and this
-  permission notice and warranty disclaimer appear in supporting 
-  documentation, and that the name of the author not be used in 
-  advertising or publicity pertaining to distribution of the 
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
   software without specific, written prior permission.
 
-  The author disclaim all warranties with regard to this
+  The author disclaims all warranties with regard to this
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
@@ -44,15 +44,19 @@ USB_ClassInfo_MIDI_Device_t Keyboard_MIDI_Interface =
        {
                .Config =
                        {
        {
                .Config =
                        {
-                               .StreamingInterfaceNumber = 1,
-
-                               .DataINEndpointNumber      = MIDI_STREAM_IN_EPNUM,
-                               .DataINEndpointSize        = MIDI_STREAM_EPSIZE,
-                               .DataINEndpointDoubleBank  = false,
-
-                               .DataOUTEndpointNumber     = MIDI_STREAM_OUT_EPNUM,
-                               .DataOUTEndpointSize       = MIDI_STREAM_EPSIZE,
-                               .DataOUTEndpointDoubleBank = false,
+                               .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,
+                               .DataINEndpoint           =
+                                       {
+                                               .Address          = MIDI_STREAM_IN_EPADDR,
+                                               .Size             = MIDI_STREAM_EPSIZE,
+                                               .Banks            = 1,
+                                       },
+                               .DataOUTEndpoint           =
+                                       {
+                                               .Address          = MIDI_STREAM_OUT_EPADDR,
+                                               .Size             = MIDI_STREAM_EPSIZE,
+                                               .Banks            = 1,
+                                       },
                        },
        };
 
                        },
        };
 
@@ -89,17 +93,17 @@ int main(void)
        SetupHardware();
 
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
        SetupHardware();
 
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
-       sei();
-       
+       GlobalInterruptEnable();
+
        for (;;)
        {
                MIDI_EventPacket_t ReceivedMIDIEvent;
                if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent))
                {
        for (;;)
        {
                MIDI_EventPacket_t ReceivedMIDIEvent;
                if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent))
                {
-                       if ((ReceivedMIDIEvent.Command == (MIDI_COMMAND_NOTE_ON >> 4)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0))
+                       if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_ON)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0))
                        {
                                DDSNoteData* LRUNoteStruct = &NoteData[0];
                        {
                                DDSNoteData* LRUNoteStruct = &NoteData[0];
-                       
+
                                /* Find a free entry in the note table to use for the note being turned on */
                                for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
                                {
                                /* Find a free entry in the note table to use for the note being turned on */
                                for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
                                {
@@ -112,13 +116,13 @@ int main(void)
                                        }
                                        else if (NoteData[i].LRUAge >= LRUNoteStruct->LRUAge)
                                        {
                                        }
                                        else if (NoteData[i].LRUAge >= LRUNoteStruct->LRUAge)
                                        {
-                                               /* If an older entry that the current entry has been found, prefer overwriting that one */                                              
+                                               /* If an older entry that the current entry has been found, prefer overwriting that one */
                                                LRUNoteStruct = &NoteData[i];
                                        }
                                                LRUNoteStruct = &NoteData[i];
                                        }
-                                       
+
                                        NoteData[i].LRUAge++;
                                }
                                        NoteData[i].LRUAge++;
                                }
-                               
+
                                /* Update the oldest note entry with the new note data and reset its age */
                                LRUNoteStruct->Pitch          = ReceivedMIDIEvent.Data2;
                                LRUNoteStruct->TableIncrement = (uint32_t)(BASE_INCREMENT * SCALE_FACTOR) +
                                /* Update the oldest note entry with the new note data and reset its age */
                                LRUNoteStruct->Pitch          = ReceivedMIDIEvent.Data2;
                                LRUNoteStruct->TableIncrement = (uint32_t)(BASE_INCREMENT * SCALE_FACTOR) +
@@ -130,10 +134,10 @@ int main(void)
                                /* Turn on indicator LED to indicate note generation activity */
                                LEDs_SetAllLEDs(LEDS_LED1);
                        }
                                /* Turn on indicator LED to indicate note generation activity */
                                LEDs_SetAllLEDs(LEDS_LED1);
                        }
-                       else if ((ReceivedMIDIEvent.Command == (MIDI_COMMAND_NOTE_OFF >> 4)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0))
+                       else if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_OFF)) && ((ReceivedMIDIEvent.Data1 & 0x0F) == 0))
                        {
                                bool FoundActiveNote = false;
                        {
                                bool FoundActiveNote = false;
-                       
+
                                /* Find the note in the note table to turn off */
                                for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
                                {
                                /* Find the note in the note table to turn off */
                                for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
                                {
@@ -142,13 +146,13 @@ int main(void)
                                        else if (NoteData[i].Pitch)
                                          FoundActiveNote   = true;
                                }
                                        else if (NoteData[i].Pitch)
                                          FoundActiveNote   = true;
                                }
-                               
+
                                /* If all notes off, turn off the indicator LED */
                                if (!(FoundActiveNote))
                                  LEDs_SetAllLEDs(LEDS_NO_LEDS);
                        }
                }
                                /* If all notes off, turn off the indicator LED */
                                if (!(FoundActiveNote))
                                  LEDs_SetAllLEDs(LEDS_NO_LEDS);
                        }
                }
-       
+
                MIDI_Device_USBTask(&Keyboard_MIDI_Interface);
                USB_USBTask();
        }
                MIDI_Device_USBTask(&Keyboard_MIDI_Interface);
                USB_USBTask();
        }
@@ -158,7 +162,7 @@ int main(void)
 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
 {
        uint16_t MixedSample = 0;
 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
 {
        uint16_t MixedSample = 0;
-       
+
        /* Sum together all the active notes to form a single sample */
        for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
        {
        /* Sum together all the active notes to form a single sample */
        for (uint8_t i = 0; i < MAX_SIMULTANEOUS_NOTES; i++)
        {
@@ -167,13 +171,13 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
                {
                        /* Use the top 8 bits of the table position as the sample table index */
                        uint8_t TableIndex = (NoteData[i].TablePosition >> 24);
                {
                        /* Use the top 8 bits of the table position as the sample table index */
                        uint8_t TableIndex = (NoteData[i].TablePosition >> 24);
-                       
+
                        /* Add the new tone sample to the accumulator and increment the table position */
                        MixedSample += SineTable[TableIndex];
                        NoteData[i].TablePosition += NoteData[i].TableIncrement;
                }
        }
                        /* Add the new tone sample to the accumulator and increment the table position */
                        MixedSample += SineTable[TableIndex];
                        NoteData[i].TablePosition += NoteData[i].TableIncrement;
                }
        }
-       
+
        /* Output clamped mixed sample value to the PWM */
        OCR3A = (MixedSample <= 0xFF) ? MixedSample : 0xFF;
 }
        /* Output clamped mixed sample value to the PWM */
        OCR3A = (MixedSample <= 0xFF) ? MixedSample : 0xFF;
 }
@@ -181,13 +185,15 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK)
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
+#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);
        /* Disable watchdog if enabled by bootloader/fuses */
        MCUSR &= ~(1 << WDRF);
        wdt_disable();
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
-       
+#endif
+
        /* Hardware Initialization */
        LEDs_Init();
        USB_Init();
        /* Hardware Initialization */
        LEDs_Init();
        USB_Init();
@@ -234,7 +240,7 @@ void EVENT_USB_Device_ConfigurationChanged(void)
        bool ConfigSuccess = true;
 
        ConfigSuccess &= MIDI_Device_ConfigureEndpoints(&Keyboard_MIDI_Interface);
        bool ConfigSuccess = true;
 
        ConfigSuccess &= MIDI_Device_ConfigureEndpoints(&Keyboard_MIDI_Interface);
-       
+
        LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
 }
 
        LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
 }
 
@@ -243,3 +249,4 @@ void EVENT_USB_Device_ControlRequest(void)
 {
        MIDI_Device_ProcessControlRequest(&Keyboard_MIDI_Interface);
 }
 {
        MIDI_Device_ProcessControlRequest(&Keyboard_MIDI_Interface);
 }
+