Pseudo-port the projects and dual role demos for possible later multi-arch support.
[pub/USBasp.git] / Demos / Host / LowLevel / AudioInputHost / AudioInputHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 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 /** 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 Input 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 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR &= ~(1 << WDRF);
62 wdt_disable();
63
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1);
66
67 /* Hardware Initialization */
68 Serial_Init(9600, false);
69 LEDs_Init();
70 USB_Init();
71
72 /* Create a stdio stream for the serial port for stdin and stdout */
73 Serial_CreateStream(NULL);
74 }
75
76 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
77 * starts the library USB task to begin the enumeration and USB management process.
78 */
79 void EVENT_USB_Host_DeviceAttached(void)
80 {
81 puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
82 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
83 }
84
85 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
86 * stops the library USB task management process.
87 */
88 void EVENT_USB_Host_DeviceUnattached(void)
89 {
90 puts_P(PSTR(ESC_FG_GREEN "Device Unattached.\r\n" ESC_FG_WHITE));
91 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
92 }
93
94 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
95 * enumerated by the host and is now ready to be used by the application.
96 */
97 void EVENT_USB_Host_DeviceEnumerationComplete(void)
98 {
99 puts_P(PSTR("Getting Config Data.\r\n"));
100
101 uint8_t ErrorCode;
102
103 /* Get and process the configuration descriptor data */
104 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
105 {
106 if (ErrorCode == ControlError)
107 puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
108 else
109 puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
110
111 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
112
113 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
114 return;
115 }
116
117 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
118 if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
119 {
120 printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
121 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
122
123 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
124 return;
125 }
126
127 if ((ErrorCode = USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex,
128 StreamingInterfaceAltSetting)) != HOST_SENDCONTROL_Successful)
129 {
130 printf_P(PSTR(ESC_FG_RED "Could not set alternative streaming interface setting.\r\n"
131 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
132
133 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
134 USB_Host_SetDeviceConfiguration(0);
135 return;
136 }
137
138 USB_ControlRequest = (USB_Request_Header_t)
139 {
140 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT),
141 .bRequest = AUDIO_REQ_SetCurrent,
142 .wValue = (AUDIO_EPCONTROL_SamplingFreq << 8),
143 .wIndex = StreamingEndpointAddress,
144 .wLength = sizeof(USB_Audio_SampleFreq_t),
145 };
146
147 USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
148
149 /* Select the control pipe for the request transfer */
150 Pipe_SelectPipe(PIPE_CONTROLPIPE);
151
152 /* Set the sample rate on the streaming interface endpoint */
153 if ((ErrorCode = USB_Host_SendControlRequest(&SampleRate)) != HOST_SENDCONTROL_Successful)
154 {
155 printf_P(PSTR(ESC_FG_RED "Could not set requested Audio sample rate.\r\n"
156 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
157
158 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
159 USB_Host_SetDeviceConfiguration(0);
160 return;
161 }
162
163 /* Sample reload timer initialization */
164 TIMSK0 = (1 << OCIE0A);
165 OCR0A = ((F_CPU / 8 / 48000) - 1);
166 TCCR0A = (1 << WGM01); // CTC mode
167 TCCR0B = (1 << CS01); // Fcpu/8 speed
168
169 /* Set speaker as output */
170 DDRC |= (1 << 6);
171
172 /* PWM speaker timer initialization */
173 TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)); // Set on match, clear on TOP
174 TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
175
176 puts_P(PSTR("Microphone Enumerated.\r\n"));
177 LEDs_SetAllLEDs(LEDMASK_USB_READY);
178 }
179
180 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
181 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
182 {
183 USB_Disable();
184
185 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
186 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
187
188 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
189 for(;;);
190 }
191
192 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
193 * enumerating an attached USB device.
194 */
195 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
196 const uint8_t SubErrorCode)
197 {
198 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
199 " -- Error Code %d\r\n"
200 " -- Sub Error Code %d\r\n"
201 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
202
203 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
204 }
205
206 /** ISR to handle the reloading of the PWM timer with the next sample. */
207 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
208 {
209 uint8_t PrevPipe = Pipe_GetCurrentPipe();
210
211 Pipe_SelectPipe(AUDIO_DATA_IN_PIPE);
212 Pipe_Unfreeze();
213
214 /* Check if the current pipe can be read from (contains a packet) and the device is sending data */
215 if (Pipe_IsINReceived())
216 {
217 /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
218 int8_t Sample_8Bit = (Pipe_Read_16_LE() >> 8);
219
220 /* Check to see if the bank is now empty */
221 if (!(Pipe_IsReadWriteAllowed()))
222 {
223 /* Acknowledge the packet, clear the bank ready for the next packet */
224 Pipe_ClearIN();
225 }
226
227 /* Load the sample into the PWM timer channel */
228 OCR3A = (Sample_8Bit ^ (1 << 7));
229
230 uint8_t LEDMask = LEDS_NO_LEDS;
231
232 /* Turn on LEDs as the sample amplitude increases */
233 if (Sample_8Bit > 16)
234 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
235 else if (Sample_8Bit > 8)
236 LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
237 else if (Sample_8Bit > 4)
238 LEDMask = (LEDS_LED1 | LEDS_LED2);
239 else if (Sample_8Bit > 2)
240 LEDMask = (LEDS_LED1);
241
242 LEDs_SetAllLEDs(LEDMask);
243 }
244
245 Pipe_Freeze();
246 Pipe_SelectPipe(PrevPipe);
247 }
248