USB_Init() no longer calls sei() to enable global interrupts - this must now be done...
[pub/USBasp.git] / Bootloaders / Incomplete / MIDI / BootloaderMIDI.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #include "BootloaderMIDI.h"
38
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.
41 */
42 int main(void)
43 {
44 SetupHardware();
45
46 for (;;)
47 {
48 MIDI_Task();
49 USB_USBTask();
50 }
51 }
52
53 /** Configures the board hardware and chip peripherals for the demo's functionality. */
54 void SetupHardware(void)
55 {
56 /* Disable watchdog if enabled by bootloader/fuses */
57 MCUSR &= ~(1 << WDRF);
58 wdt_disable();
59
60 /* Disable clock division */
61 clock_prescale_set(clock_div_1);
62
63 /* Hardware Initialization */
64 USB_Init();
65 }
66
67 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
68 * of the USB device after enumeration - the device endpoints are configured and the MIDI management task started.
69 */
70 void EVENT_USB_Device_ConfigurationChanged(void)
71 {
72 /* Setup MIDI stream endpoints */
73 Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPNUM, EP_TYPE_BULK,
74 ENDPOINT_DIR_OUT, MIDI_STREAM_EPSIZE,
75 ENDPOINT_BANK_SINGLE);
76
77 Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPNUM, EP_TYPE_BULK,
78 ENDPOINT_DIR_IN, MIDI_STREAM_EPSIZE,
79 ENDPOINT_BANK_SINGLE);
80 }
81
82 /** Task to handle the generation of MIDI note change events in response to presses of the board joystick, and send them
83 * to the host.
84 */
85 void MIDI_Task(void)
86 {
87 /* Device must be connected and configured for the task to run */
88 if (USB_DeviceState != DEVICE_STATE_Configured)
89 return;
90
91 /* Select the MIDI OUT stream */
92 Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM);
93
94 if (Endpoint_IsOUTReceived())
95 {
96 USB_MIDI_EventPacket_t InPacket;
97 Endpoint_Read_Stream_LE(&InPacket, sizeof(InPacket));
98 Endpoint_ClearOUT();
99
100 uint8_t Channel = InPacket.Data1;
101 uint8_t Data = ((InPacket.Data2 & 0x7F) | ((InPacket.Data3 == 64) ? 0x80 : 0x00));
102
103 if ((Channel == MIDI_CONTROL_CHANNEL) && (Data == CONTROL_ENTER_PROG_MODE))
104 {
105 USB_MIDI_EventPacket_t MIDIIn = (USB_MIDI_EventPacket_t)
106 {
107 .CableNumber = 0,
108 .Command = (MIDI_COMMAND_NOTE_ON >> 4),
109
110 .Data1 = MIDI_COMMAND_NOTE_ON | MIDI_CONTROL_CHANNEL,
111 .Data2 = CONTROL_DEVICE_READY,
112 .Data3 = 32,
113 };
114
115 Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);
116 Endpoint_Write_Stream_LE(&MIDIIn, sizeof(MIDIIn));
117 Endpoint_ClearIN();
118 }
119 }
120 }