]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Host/LowLevel/AudioOutputHost/AudioOutputHost.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Host / LowLevel / AudioOutputHost / AudioOutputHost.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 AudioOutputHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
35 */
36
37 #include "AudioOutputHost.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 puts_P(PSTR(ESC_FG_CYAN "Audio Output Host Demo running.\r\n" ESC_FG_WHITE));
47
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
49 GlobalInterruptEnable();
50
51 for (;;)
52 {
53 USB_USBTask();
54 }
55 }
56
57 /** Configures the board hardware and chip peripherals for the demo's functionality. */
58 void SetupHardware(void)
59 {
60 #if (ARCH == ARCH_AVR8)
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR &= ~(1 << WDRF);
63 wdt_disable();
64
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1);
67 #endif
68
69 /* Hardware Initialization */
70 Serial_Init(9600, false);
71 Buttons_Init();
72 ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
73 ADC_SetupChannel(MIC_IN_ADC_CHANNEL);
74 LEDs_Init();
75 USB_Init();
76
77 /* Create a stdio stream for the serial port for stdin and stdout */
78 Serial_CreateStream(NULL);
79
80 /* Start the ADC conversion in free running mode */
81 ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_GET_CHANNEL_MASK(MIC_IN_ADC_CHANNEL));
82 }
83
84 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
85 * starts the library USB task to begin the enumeration and USB management process.
86 */
87 void EVENT_USB_Host_DeviceAttached(void)
88 {
89 puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
90 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
91 }
92
93 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
94 * stops the library USB task management process.
95 */
96 void EVENT_USB_Host_DeviceUnattached(void)
97 {
98 puts_P(PSTR(ESC_FG_GREEN "Device Unattached.\r\n" ESC_FG_WHITE));
99 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
100 }
101
102 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
103 * enumerated by the host and is now ready to be used by the application.
104 */
105 void EVENT_USB_Host_DeviceEnumerationComplete(void)
106 {
107 puts_P(PSTR("Getting Config Data.\r\n"));
108
109 uint8_t ErrorCode;
110
111 /* Get and process the configuration descriptor data */
112 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
113 {
114 if (ErrorCode == ControlError)
115 puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
116 else
117 puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
118
119 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
120
121 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
122 return;
123 }
124
125 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
126 if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
127 {
128 printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
129 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
130
131 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
132 return;
133 }
134
135 if ((ErrorCode = USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex,
136 StreamingInterfaceAltSetting)) != HOST_SENDCONTROL_Successful)
137 {
138 printf_P(PSTR(ESC_FG_RED "Could not set alternative streaming interface setting.\r\n"
139 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
140
141 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
142 USB_Host_SetDeviceConfiguration(0);
143 return;
144 }
145
146 USB_ControlRequest = (USB_Request_Header_t)
147 {
148 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT),
149 .bRequest = AUDIO_REQ_SetCurrent,
150 .wValue = (AUDIO_EPCONTROL_SamplingFreq << 8),
151 .wIndex = StreamingEndpointAddress,
152 .wLength = sizeof(USB_Audio_SampleFreq_t),
153 };
154
155 USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
156
157 /* Select the control pipe for the request transfer */
158 Pipe_SelectPipe(PIPE_CONTROLPIPE);
159
160 /* Set the sample rate on the streaming interface endpoint */
161 if ((ErrorCode = USB_Host_SendControlRequest(&SampleRate)) != HOST_SENDCONTROL_Successful)
162 {
163 printf_P(PSTR(ESC_FG_RED "Could not set requested Audio sample rate.\r\n"
164 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
165
166 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
167 USB_Host_SetDeviceConfiguration(0);
168 return;
169 }
170
171 /* Sample reload timer initialization */
172 TIMSK0 = (1 << OCIE0A);
173 OCR0A = ((F_CPU / 8 / 48000) - 1);
174 TCCR0A = (1 << WGM01); // CTC mode
175 TCCR0B = (1 << CS01); // Fcpu/8 speed
176
177 puts_P(PSTR("Speaker Enumerated.\r\n"));
178 LEDs_SetAllLEDs(LEDMASK_USB_READY);
179 }
180
181 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
182 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
183 {
184 USB_Disable();
185
186 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
187 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
188
189 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
190 for(;;);
191 }
192
193 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
194 * enumerating an attached USB device.
195 */
196 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
197 const uint8_t SubErrorCode)
198 {
199 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
200 " -- Error Code %d\r\n"
201 " -- Sub Error Code %d\r\n"
202 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
203
204 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
205 }
206
207 /** ISR to handle the reloading of the endpoint with the next sample. */
208 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
209 {
210 uint8_t PrevPipe = Pipe_GetCurrentPipe();
211
212 Pipe_SelectPipe(AUDIO_DATA_OUT_PIPE);
213 Pipe_Unfreeze();
214
215 /* Check if the current pipe can be written to (device ready for more data) */
216 if (Pipe_IsOUTReady())
217 {
218 int16_t AudioSample;
219
220 #if defined(USE_TEST_TONE)
221 static uint8_t SquareWaveSampleCount;
222 static int16_t CurrentWaveValue;
223
224 /* In test tone mode, generate a square wave at 1/256 of the sample rate */
225 if (SquareWaveSampleCount++ == 0xFF)
226 CurrentWaveValue ^= 0x8000;
227
228 /* Only generate audio if the board button is being pressed */
229 AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
230 #else
231 /* Audio sample is ADC value scaled to fit the entire range */
232 AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
233
234 #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
235 /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
236 AudioSample -= (SAMPLE_MAX_RANGE / 2);
237 #endif
238 #endif
239
240 Pipe_Write_16_LE(AudioSample);
241 Pipe_Write_16_LE(AudioSample);
242
243 if (!(Pipe_IsReadWriteAllowed()))
244 Pipe_ClearOUT();
245 }
246
247 Pipe_Freeze();
248 Pipe_SelectPipe(PrevPipe);
249 }
250
Imprint / Impressum