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