Fixed GenericHIDHost demo report write routine incorrect for control type requests...
[pub/USBasp.git] / Demos / Device / AudioInput / AudioInput.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 Audio Input demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "AudioInput.h"
38
39 /* Scheduler Task List */
40 TASK_LIST
41 {
42 { Task: USB_USBTask , TaskStatus: TASK_STOP },
43 { Task: USB_Audio_Task , TaskStatus: TASK_STOP },
44 };
45
46
47 /** Main program entry point. This routine configures the hardware required by the application, then
48 * starts the scheduler to run the application tasks.
49 */
50 int main(void)
51 {
52 /* Disable watchdog if enabled by bootloader/fuses */
53 MCUSR &= ~(1 << WDRF);
54 wdt_disable();
55
56 /* Disable clock division */
57 clock_prescale_set(clock_div_1);
58
59 /* Hardware Initialization */
60 LEDs_Init();
61 ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
62 ADC_SetupChannel(MIC_IN_ADC_CHANNEL);
63
64 /* Start the ADC conversion in free running mode */
65 ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | MIC_IN_ADC_CHANNEL);
66
67 /* Indicate USB not ready */
68 UpdateStatus(Status_USBNotReady);
69
70 /* Initialize Scheduler so that it can be used */
71 Scheduler_Init();
72
73 /* Initialize USB Subsystem */
74 USB_Init();
75
76 /* Scheduling - routine never returns, so put this last in the main function */
77 Scheduler_Start();
78 }
79
80 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs, and
81 * configures the sample update and PWM timers.
82 */
83 EVENT_HANDLER(USB_Connect)
84 {
85 /* Start USB management task */
86 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
87
88 /* Indicate USB enumerating */
89 UpdateStatus(Status_USBEnumerating);
90
91 /* Sample reload timer initialization */
92 OCR0A = (F_CPU / AUDIO_SAMPLE_FREQUENCY) - 1;
93 TCCR0A = (1 << WGM01); // CTC mode
94 TCCR0B = (1 << CS00); // Fcpu speed
95 }
96
97 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
98 * the status LEDs, disables the sample update and PWM output timers and stops the USB and Audio management tasks.
99 */
100 EVENT_HANDLER(USB_Disconnect)
101 {
102 /* Stop the sample reload timer */
103 TCCR0B = 0;
104
105 /* Stop running audio and USB management tasks */
106 Scheduler_SetTaskMode(USB_Audio_Task, TASK_STOP);
107 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
108
109 /* Indicate USB not ready */
110 UpdateStatus(Status_USBNotReady);
111 }
112
113 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
114 * of the USB device after enumeration - the device endpoints are configured.
115 */
116 EVENT_HANDLER(USB_ConfigurationChanged)
117 {
118 /* Setup audio stream endpoint */
119 Endpoint_ConfigureEndpoint(AUDIO_STREAM_EPNUM, EP_TYPE_ISOCHRONOUS,
120 ENDPOINT_DIR_IN, AUDIO_STREAM_EPSIZE,
121 ENDPOINT_BANK_DOUBLE);
122
123 /* Indicate USB connected and ready */
124 UpdateStatus(Status_USBReady);
125 }
126
127 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
128 * control requests that are not handled internally by the USB library (including the Audio class-specific
129 * requests) so that they can be handled appropriately for the application.
130 */
131 EVENT_HANDLER(USB_UnhandledControlPacket)
132 {
133 /* Process General and Audio specific control requests */
134 switch (bRequest)
135 {
136 case REQ_SetInterface:
137 /* Set Interface is not handled by the library, as its function is application-specific */
138 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
139 {
140 uint16_t wValue = Endpoint_Read_Word_LE();
141
142 Endpoint_ClearControlSETUP();
143
144 /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
145 if (wValue)
146 {
147 /* Start audio task */
148 Scheduler_SetTaskMode(USB_Audio_Task, TASK_RUN);
149 }
150 else
151 {
152 /* Stop audio task */
153 Scheduler_SetTaskMode(USB_Audio_Task, TASK_STOP);
154 }
155
156 /* Acknowledge status stage */
157 while (!(Endpoint_IsINReady()));
158 Endpoint_ClearControlIN();
159 }
160
161 break;
162 }
163 }
164
165 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
166 * log to a serial port, or anything else that is suitable for status updates.
167 *
168 * \param CurrentStatus Current status of the system, from the AudioInput_StatusCodes_t enum
169 */
170 void UpdateStatus(uint8_t CurrentStatus)
171 {
172 uint8_t LEDMask = LEDS_NO_LEDS;
173
174 /* Set the LED mask to the appropriate LED mask based on the given status code */
175 switch (CurrentStatus)
176 {
177 case Status_USBNotReady:
178 LEDMask = (LEDS_LED1);
179 break;
180 case Status_USBEnumerating:
181 LEDMask = (LEDS_LED1 | LEDS_LED2);
182 break;
183 case Status_USBReady:
184 LEDMask = (LEDS_LED2 | LEDS_LED4);
185 break;
186 }
187
188 /* Set the board LEDs to the new LED mask */
189 LEDs_SetAllLEDs(LEDMask);
190 }
191
192 /** Task to manage the Audio interface, reading in ADC samples from the microphone, and them to the host. */
193 TASK(USB_Audio_Task)
194 {
195 /* Select the audio stream endpoint */
196 Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM);
197
198 /* Check if the current endpoint can be written to and that the next sample should be stored */
199 if (Endpoint_IsINReady() && (TIFR0 & (1 << OCF0A)))
200 {
201 /* Clear the sample reload timer */
202 TIFR0 |= (1 << OCF0A);
203
204 /* Audio sample is ADC value scaled to fit the entire range */
205 int16_t AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
206
207 #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
208 /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
209 AudioSample -= (SAMPLE_MAX_RANGE / 2));
210 #endif
211
212 /* Write the sample to the buffer */
213 Endpoint_Write_Word_LE(AudioSample);
214
215 /* Check to see if the bank is now full */
216 if (!(Endpoint_IsReadWriteAllowed()))
217 {
218 /* Send the full packet to the host */
219 Endpoint_ClearIN();
220 }
221 }
222 }