Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioInput.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Main source file for the AudioInput demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "AudioInput.h"
  32. /** LUFA Audio Class driver interface configuration and state information. This structure is
  33. * passed to all Audio Class driver functions, so that multiple instances of the same class
  34. * within a device can be differentiated from one another.
  35. */
  36. USB_ClassInfo_Audio_Device_t Microphone_Audio_Interface =
  37. {
  38. .Config =
  39. {
  40. .ControlInterfaceNumber = INTERFACE_ID_AudioControl,
  41. .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,
  42. .DataINEndpoint =
  43. {
  44. .Address = AUDIO_STREAM_EPADDR,
  45. .Size = AUDIO_STREAM_EPSIZE,
  46. .Banks = 2,
  47. },
  48. },
  49. };
  50. /** Current audio sampling frequency of the streaming audio endpoint. */
  51. static uint32_t CurrentAudioSampleFrequency = 48000;
  52. /** Main program entry point. This routine contains the overall program flow, including initial
  53. * setup of all components and the main program loop.
  54. */
  55. int main(void)
  56. {
  57. SetupHardware();
  58. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  59. GlobalInterruptEnable();
  60. for (;;)
  61. {
  62. Audio_Device_USBTask(&Microphone_Audio_Interface);
  63. USB_USBTask();
  64. }
  65. }
  66. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  67. void SetupHardware(void)
  68. {
  69. #if (ARCH == ARCH_AVR8)
  70. /* Disable watchdog if enabled by bootloader/fuses */
  71. MCUSR &= ~(1 << WDRF);
  72. wdt_disable();
  73. /* Disable clock division */
  74. clock_prescale_set(clock_div_1);
  75. #endif
  76. /* Hardware Initialization */
  77. LEDs_Init();
  78. Buttons_Init();
  79. ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
  80. ADC_SetupChannel(MIC_IN_ADC_CHANNEL);
  81. USB_Init();
  82. /* Start the ADC conversion in free running mode */
  83. ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_GET_CHANNEL_MASK(MIC_IN_ADC_CHANNEL));
  84. }
  85. /** ISR to handle the reloading of the data endpoint with the next sample. */
  86. ISR(TIMER0_COMPA_vect, ISR_BLOCK)
  87. {
  88. uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();
  89. /* Check that the USB bus is ready for the next sample to write */
  90. if (Audio_Device_IsReadyForNextSample(&Microphone_Audio_Interface))
  91. {
  92. int16_t AudioSample;
  93. #if defined(USE_TEST_TONE)
  94. static uint8_t SquareWaveSampleCount;
  95. static int16_t CurrentWaveValue;
  96. /* In test tone mode, generate a square wave at 1/256 of the sample rate */
  97. if (SquareWaveSampleCount++ == 0xFF)
  98. CurrentWaveValue ^= 0x8000;
  99. /* Only generate audio if the board button is being pressed */
  100. AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
  101. #else
  102. /* Audio sample is ADC value scaled to fit the entire range */
  103. AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
  104. #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
  105. /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
  106. AudioSample -= (SAMPLE_MAX_RANGE / 2);
  107. #endif
  108. #endif
  109. Audio_Device_WriteSample16(&Microphone_Audio_Interface, AudioSample);
  110. }
  111. Endpoint_SelectEndpoint(PrevEndpoint);
  112. }
  113. /** Event handler for the library USB Connection event. */
  114. void EVENT_USB_Device_Connect(void)
  115. {
  116. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  117. /* Sample reload timer initialization */
  118. TIMSK0 = (1 << OCIE0A);
  119. OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);
  120. TCCR0A = (1 << WGM01); // CTC mode
  121. TCCR0B = (1 << CS01); // Fcpu/8 speed
  122. }
  123. /** Event handler for the library USB Disconnection event. */
  124. void EVENT_USB_Device_Disconnect(void)
  125. {
  126. /* Stop the sample reload timer */
  127. TCCR0B = 0;
  128. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  129. }
  130. /** Event handler for the library USB Configuration Changed event. */
  131. void EVENT_USB_Device_ConfigurationChanged(void)
  132. {
  133. bool ConfigSuccess = true;
  134. ConfigSuccess &= Audio_Device_ConfigureEndpoints(&Microphone_Audio_Interface);
  135. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  136. }
  137. /** Event handler for the library USB Control Request reception event. */
  138. void EVENT_USB_Device_ControlRequest(void)
  139. {
  140. Audio_Device_ProcessControlRequest(&Microphone_Audio_Interface);
  141. }
  142. /** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
  143. * in the user application to handle property manipulations on streaming audio endpoints.
  144. *
  145. * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
  146. * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
  147. * to indicate the size of the retrieved data.
  148. *
  149. * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
  150. * of the \c DataLength parameter.
  151. *
  152. * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
  153. * \param[in] EndpointProperty Property of the endpoint to get or set, a value from Audio_ClassRequests_t.
  154. * \param[in] EndpointAddress Address of the streaming endpoint whose property is being referenced.
  155. * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from Audio_EndpointControls_t.
  156. * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
  157. * length of the retrieved data. When NULL, the function should return whether the given property
  158. * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
  159. * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
  160. * the retrieved data is to be stored for GET operations.
  161. *
  162. * \return Boolean \c true if the property get/set was successful, \c false otherwise
  163. */
  164. bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
  165. const uint8_t EndpointProperty,
  166. const uint8_t EndpointAddress,
  167. const uint8_t EndpointControl,
  168. uint16_t* const DataLength,
  169. uint8_t* Data)
  170. {
  171. /* Check the requested endpoint to see if a supported endpoint is being manipulated */
  172. if (EndpointAddress == Microphone_Audio_Interface.Config.DataINEndpoint.Address)
  173. {
  174. /* Check the requested control to see if a supported control is being manipulated */
  175. if (EndpointControl == AUDIO_EPCONTROL_SamplingFreq)
  176. {
  177. switch (EndpointProperty)
  178. {
  179. case AUDIO_REQ_SetCurrent:
  180. /* Check if we are just testing for a valid property, or actually adjusting it */
  181. if (DataLength != NULL)
  182. {
  183. /* Set the new sampling frequency to the value given by the host */
  184. CurrentAudioSampleFrequency = (((uint32_t)Data[2] << 16) | ((uint32_t)Data[1] << 8) | (uint32_t)Data[0]);
  185. /* Adjust sample reload timer to the new frequency */
  186. OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);
  187. }
  188. return true;
  189. case AUDIO_REQ_GetCurrent:
  190. /* Check if we are just testing for a valid property, or actually reading it */
  191. if (DataLength != NULL)
  192. {
  193. *DataLength = 3;
  194. Data[2] = (CurrentAudioSampleFrequency >> 16);
  195. Data[1] = (CurrentAudioSampleFrequency >> 8);
  196. Data[0] = (CurrentAudioSampleFrequency & 0xFF);
  197. }
  198. return true;
  199. }
  200. }
  201. }
  202. return false;
  203. }
  204. /** Audio class driver callback for the setting and retrieval of streaming interface properties. This callback must be implemented
  205. * in the user application to handle property manipulations on streaming audio interfaces.
  206. *
  207. * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
  208. * the given entity and should return as fast as possible. When non-NULL, this value may be altered for GET operations
  209. * to indicate the size of the retrieved data.
  210. *
  211. * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
  212. * of the \c DataLength parameter.
  213. *
  214. * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
  215. * \param[in] Property Property of the interface to get or set, a value from Audio_ClassRequests_t.
  216. * \param[in] EntityAddress Address of the audio entity whose property is being referenced.
  217. * \param[in] Parameter Parameter of the entity to get or set, specific to each type of entity (see USB Audio specification).
  218. * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
  219. * length of the retrieved data. When NULL, the function should return whether the given property
  220. * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
  221. * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
  222. * the retrieved data is to be stored for GET operations.
  223. *
  224. * \return Boolean \c true if the property GET/SET was successful, \c false otherwise
  225. */
  226. bool CALLBACK_Audio_Device_GetSetInterfaceProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
  227. const uint8_t Property,
  228. const uint8_t EntityAddress,
  229. const uint16_t Parameter,
  230. uint16_t* const DataLength,
  231. uint8_t* Data)
  232. {
  233. /* No audio interface entities in the device descriptor, thus no properties to get or set. */
  234. return false;
  235. }