]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Host/ClassDriver/AudioInputHost/AudioInputHost.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Host / ClassDriver / AudioInputHost / AudioInputHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 disclaims 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 AudioInputHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "AudioInputHost.h"
38
39 /** LUFA Audio Class driver interface configuration and state information. This structure is
40 * passed to all Audio Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
42 */
43 USB_ClassInfo_Audio_Host_t Microphone_Audio_Interface =
44 {
45 .Config =
46 {
47 .DataINPipe =
48 {
49 .Address = (PIPE_DIR_IN | 1),
50 },
51 },
52 };
53
54
55 /** Main program entry point. This routine configures the hardware required by the application, then
56 * enters a loop to run the application tasks in sequence.
57 */
58 int main(void)
59 {
60 SetupHardware();
61
62 puts_P(PSTR(ESC_FG_CYAN "Audio Input Host Demo running.\r\n" ESC_FG_WHITE));
63
64 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
65 GlobalInterruptEnable();
66
67 for (;;)
68 {
69 Audio_Host_USBTask(&Microphone_Audio_Interface);
70 USB_USBTask();
71 }
72 }
73
74 /** ISR to handle the reloading of the PWM timer with the next sample. */
75 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
76 {
77 uint8_t PrevPipe = Pipe_GetCurrentPipe();
78
79 /* Check that the USB bus is ready for the next sample to read */
80 if (Audio_Host_IsSampleReceived(&Microphone_Audio_Interface))
81 {
82 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
83 int8_t Sample_8Bit = (Audio_Host_ReadSample16(&Microphone_Audio_Interface) >> 8);
84
85 /* Load the sample into the PWM timer channel */
86 OCR3A = (Sample_8Bit ^ (1 << 7));
87
88 uint8_t LEDMask = LEDS_NO_LEDS;
89
90 /* Turn on LEDs as the sample amplitude increases */
91 if (Sample_8Bit > 16)
92 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
93 else if (Sample_8Bit > 8)
94 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
95 else if (Sample_8Bit > 4)
96 LEDMask = (LEDS_LED1 | LEDS_LED2);
97 else if (Sample_8Bit > 2)
98 LEDMask = (LEDS_LED1);
99
100 LEDs_SetAllLEDs(LEDMask);
101 }
102
103 Pipe_SelectPipe(PrevPipe);
104 }
105
106 /** Configures the board hardware and chip peripherals for the demo's functionality. */
107 void SetupHardware(void)
108 {
109 #if (ARCH == ARCH_AVR8)
110 /* Disable watchdog if enabled by bootloader/fuses */
111 MCUSR &= ~(1 << WDRF);
112 wdt_disable();
113
114 /* Disable clock division */
115 clock_prescale_set(clock_div_1);
116 #endif
117
118 /* Hardware Initialization */
119 Serial_Init(9600, false);
120 LEDs_Init();
121 USB_Init();
122
123 /* Create a stdio stream for the serial port for stdin and stdout */
124 Serial_CreateStream(NULL);
125 }
126
127 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
128 * starts the library USB task to begin the enumeration and USB management process.
129 */
130 void EVENT_USB_Host_DeviceAttached(void)
131 {
132 puts_P(PSTR("Device Attached.\r\n"));
133 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
134 }
135
136 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
137 * stops the library USB task management process.
138 */
139 void EVENT_USB_Host_DeviceUnattached(void)
140 {
141 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
142 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
143 }
144
145 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
146 * enumerated by the host and is now ready to be used by the application.
147 */
148 void EVENT_USB_Host_DeviceEnumerationComplete(void)
149 {
150 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
151
152 uint16_t ConfigDescriptorSize;
153 uint8_t ConfigDescriptorData[512];
154
155 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
156 sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
157 {
158 puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
159 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
160 return;
161 }
162
163 if (Audio_Host_ConfigurePipes(&Microphone_Audio_Interface,
164 ConfigDescriptorSize, ConfigDescriptorData) != AUDIO_ENUMERROR_NoError)
165 {
166 puts_P(PSTR("Attached Device Not a Valid Audio Input Device.\r\n"));
167 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
168 return;
169 }
170
171 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
172 {
173 puts_P(PSTR("Error Setting Device Configuration.\r\n"));
174 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
175 return;
176 }
177
178 if (Audio_Host_StartStopStreaming(&Microphone_Audio_Interface, true) != HOST_SENDCONTROL_Successful)
179 {
180 puts_P(PSTR("Error Enabling Audio Stream.\r\n"));
181 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
182 USB_Host_SetDeviceConfiguration(0);
183 return;
184 }
185
186 USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
187 if (Audio_Host_GetSetEndpointProperty(&Microphone_Audio_Interface, Microphone_Audio_Interface.Config.DataINPipe.Address,
188 AUDIO_REQ_SetCurrent, AUDIO_EPCONTROL_SamplingFreq,
189 sizeof(SampleRate), &SampleRate) != HOST_SENDCONTROL_Successful)
190 {
191 puts_P(PSTR("Error Setting Audio Sampling Frequency.\r\n"));
192 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
193 USB_Host_SetDeviceConfiguration(0);
194 return;
195 }
196
197 /* Sample reload timer initialization */
198 TIMSK0 = (1 << OCIE0A);
199 OCR0A = ((F_CPU / 8 / 48000) - 1);
200 TCCR0A = (1 << WGM01); // CTC mode
201 TCCR0B = (1 << CS01); // Fcpu/8 speed
202
203 /* Set speaker as output */
204 DDRC |= (1 << 6);
205
206 /* PWM speaker timer initialization */
207 TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)); // Set on match, clear on TOP
208 TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
209
210 puts_P(PSTR("Audio Device Enumerated.\r\n"));
211 LEDs_SetAllLEDs(LEDMASK_USB_READY);
212 }
213
214 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
215 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
216 {
217 USB_Disable();
218
219 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
220 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
221
222 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
223 for(;;);
224 }
225
226 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
227 * enumerating an attached USB device.
228 */
229 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
230 const uint8_t SubErrorCode)
231 {
232 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
233 " -- Error Code %d\r\n"
234 " -- Sub Error Code %d\r\n"
235 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
236
237 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
238 }
239
Imprint / Impressum