3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
37 /* --- Project Configuration --- */
38 //#define MICROPHONE_BIASED_TO_HALF_RAIL
39 /* --- --- --- --- --- --- --- --- */
41 #include "AudioInput.h"
43 /* Project Tags, for reading out using the ButtLoad project */
44 BUTTLOADTAG(ProjName
, "LUFA AudioIn App");
45 BUTTLOADTAG(BuildTime
, __TIME__
);
46 BUTTLOADTAG(BuildDate
, __DATE__
);
47 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
);
49 /* Scheduler Task List */
52 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
53 { Task
: USB_Audio_Task
, TaskStatus
: TASK_STOP
},
57 /** Main program entry point. This routine configures the hardware required by the application, then
58 * starts the scheduler to run the application tasks.
62 /* Disable watchdog if enabled by bootloader/fuses */
63 MCUSR
&= ~(1 << WDRF
);
66 /* Disable Clock Division */
67 SetSystemClockPrescaler(0);
69 /* Hardware Initialization */
71 ADC_Init(ADC_FREE_RUNNING
| ADC_PRESCALE_32
);
72 ADC_SetupChannel(MIC_IN_ADC_CHANNEL
);
74 /* Start the ADC conversion in free running mode */
75 ADC_StartReading(ADC_REFERENCE_AVCC
| ADC_RIGHT_ADJUSTED
| MIC_IN_ADC_CHANNEL
);
77 /* Indicate USB not ready */
78 UpdateStatus(Status_USBNotReady
);
80 /* Initialize Scheduler so that it can be used */
83 /* Initialize USB Subsystem */
86 /* Scheduling - routine never returns, so put this last in the main function */
90 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs, and
91 * configures the sample update and PWM timers.
93 EVENT_HANDLER(USB_Connect
)
95 /* Start USB management task */
96 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
98 /* Indicate USB enumerating */
99 UpdateStatus(Status_USBEnumerating
);
101 /* Sample reload timer initialization */
102 OCR0A
= (F_CPU
/ AUDIO_SAMPLE_FREQUENCY
);
103 TCCR0A
= (1 << WGM01
); // CTC mode
104 TCCR0B
= (1 << CS00
); // Fcpu speed
107 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
108 * the status LEDs, disables the sample update and PWM output timers and stops the USB and Audio management tasks.
110 EVENT_HANDLER(USB_Disconnect
)
112 /* Stop the sample reload timer */
115 /* Stop running audio and USB management tasks */
116 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_STOP
);
117 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
119 /* Indicate USB not ready */
120 UpdateStatus(Status_USBNotReady
);
123 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
124 * of the USB device after enumeration - the device endpoints are configured.
126 EVENT_HANDLER(USB_ConfigurationChanged
)
128 /* Setup audio stream endpoint */
129 Endpoint_ConfigureEndpoint(AUDIO_STREAM_EPNUM
, EP_TYPE_ISOCHRONOUS
,
130 ENDPOINT_DIR_IN
, AUDIO_STREAM_EPSIZE
,
131 ENDPOINT_BANK_DOUBLE
);
133 /* Indicate USB connected and ready */
134 UpdateStatus(Status_USBReady
);
137 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
138 * control requests that are not handled internally by the USB library (including the Audio class-specific
139 * requests) so that they can be handled appropriately for the application.
141 EVENT_HANDLER(USB_UnhandledControlPacket
)
143 /* Process General and Audio specific control requests */
146 case REQ_SetInterface
:
147 /* Set Interface is not handled by the library, as its function is application-specific */
148 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
))
150 uint16_t wValue
= Endpoint_Read_Word_LE();
152 Endpoint_ClearSetupReceived();
154 /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
157 /* Start audio task */
158 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_RUN
);
162 /* Stop audio task */
163 Scheduler_SetTaskMode(USB_Audio_Task
, TASK_STOP
);
166 /* Handshake the request */
167 Endpoint_ClearSetupIN();
174 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
175 * log to a serial port, or anything else that is suitable for status updates.
177 * \param CurrentStatus Current status of the system, from the AudioInput_StatusCodes_t enum
179 void UpdateStatus(uint8_t CurrentStatus
)
181 uint8_t LEDMask
= LEDS_NO_LEDS
;
183 /* Set the LED mask to the appropriate LED mask based on the given status code */
184 switch (CurrentStatus
)
186 case Status_USBNotReady
:
187 LEDMask
= (LEDS_LED1
);
189 case Status_USBEnumerating
:
190 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
192 case Status_USBReady
:
193 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
197 /* Set the board LEDs to the new LED mask */
198 LEDs_SetAllLEDs(LEDMask
);
201 /** Task to manage the Audio interface, reading in ADC samples from the microphone, and them to the host. */
204 /* Select the audio stream endpoint */
205 Endpoint_SelectEndpoint(AUDIO_STREAM_EPNUM
);
207 /* Check if the current endpoint can be read from (contains a packet) and that the next sample should be stored */
208 if (Endpoint_ReadWriteAllowed() && (TIFR0
& (1 << OCF0A
)))
210 /* Clear the sample reload timer */
211 TIFR0
|= (1 << OCF0A
);
213 /* Audio sample is ADC value scaled to fit the entire range */
214 int16_t AudioSample
= ((SAMPLE_MAX_RANGE
/ ADC_MAX_RANGE
) * ADC_GetResult());
216 #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
217 /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
218 AudioSample
-= (SAMPLE_MAX_RANGE
/ 2));
221 /* Write the sample to the buffer */
222 Endpoint_Write_Word_LE(AudioSample
);
224 /* Check to see if the bank is now full */
225 if (!(Endpoint_ReadWriteAllowed()))
227 /* Send the full packet to the host */
228 Endpoint_ClearCurrentBank();