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.

AudioOutputHost.c 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 AudioOutputHost demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "AudioOutputHost.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_Host_t Speaker_Audio_Interface =
  37. {
  38. .Config =
  39. {
  40. .DataOUTPipe =
  41. {
  42. .Address = (PIPE_DIR_OUT | 2),
  43. },
  44. },
  45. };
  46. /** Main program entry point. This routine configures the hardware required by the application, then
  47. * enters a loop to run the application tasks in sequence.
  48. */
  49. int main(void)
  50. {
  51. SetupHardware();
  52. puts_P(PSTR(ESC_FG_CYAN "Audio Output Host Demo running.\r\n" ESC_FG_WHITE));
  53. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  54. GlobalInterruptEnable();
  55. for (;;)
  56. {
  57. Audio_Host_USBTask(&Speaker_Audio_Interface);
  58. USB_USBTask();
  59. }
  60. }
  61. /** ISR to handle the reloading of the PWM timer with the next sample. */
  62. ISR(TIMER0_COMPA_vect, ISR_BLOCK)
  63. {
  64. uint8_t PrevPipe = Pipe_GetCurrentPipe();
  65. /* Check that the USB bus is ready for the next sample to write */
  66. if (Audio_Host_IsReadyForNextSample(&Speaker_Audio_Interface))
  67. {
  68. int16_t AudioSample;
  69. #if defined(USE_TEST_TONE)
  70. static uint8_t SquareWaveSampleCount;
  71. static int16_t CurrentWaveValue;
  72. /* In test tone mode, generate a square wave at 1/256 of the sample rate */
  73. if (SquareWaveSampleCount++ == 0xFF)
  74. CurrentWaveValue ^= 0x8000;
  75. /* Only generate audio if the board button is being pressed */
  76. AudioSample = (Buttons_GetStatus() & BUTTONS_BUTTON1) ? CurrentWaveValue : 0;
  77. #else
  78. /* Audio sample is ADC value scaled to fit the entire range */
  79. AudioSample = ((SAMPLE_MAX_RANGE / ADC_MAX_RANGE) * ADC_GetResult());
  80. #if defined(MICROPHONE_BIASED_TO_HALF_RAIL)
  81. /* Microphone is biased to half rail voltage, subtract the bias from the sample value */
  82. AudioSample -= (SAMPLE_MAX_RANGE / 2);
  83. #endif
  84. #endif
  85. Audio_Host_WriteSample16(&Speaker_Audio_Interface, AudioSample);
  86. Audio_Host_WriteSample16(&Speaker_Audio_Interface, AudioSample);
  87. }
  88. Pipe_SelectPipe(PrevPipe);
  89. }
  90. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  91. void SetupHardware(void)
  92. {
  93. #if (ARCH == ARCH_AVR8)
  94. /* Disable watchdog if enabled by bootloader/fuses */
  95. MCUSR &= ~(1 << WDRF);
  96. wdt_disable();
  97. /* Disable clock division */
  98. clock_prescale_set(clock_div_1);
  99. #endif
  100. /* Hardware Initialization */
  101. Serial_Init(9600, false);
  102. LEDs_Init();
  103. Buttons_Init();
  104. ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32);
  105. ADC_SetupChannel(MIC_IN_ADC_CHANNEL);
  106. USB_Init();
  107. /* Create a stdio stream for the serial port for stdin and stdout */
  108. Serial_CreateStream(NULL);
  109. /* Start the ADC conversion in free running mode */
  110. ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_GET_CHANNEL_MASK(MIC_IN_ADC_CHANNEL));
  111. }
  112. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  113. * starts the library USB task to begin the enumeration and USB management process.
  114. */
  115. void EVENT_USB_Host_DeviceAttached(void)
  116. {
  117. puts_P(PSTR("Device Attached.\r\n"));
  118. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  119. }
  120. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  121. * stops the library USB task management process.
  122. */
  123. void EVENT_USB_Host_DeviceUnattached(void)
  124. {
  125. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  126. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  127. }
  128. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  129. * enumerated by the host and is now ready to be used by the application.
  130. */
  131. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  132. {
  133. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  134. uint16_t ConfigDescriptorSize;
  135. uint8_t ConfigDescriptorData[512];
  136. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  137. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  138. {
  139. puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
  140. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  141. return;
  142. }
  143. if (Audio_Host_ConfigurePipes(&Speaker_Audio_Interface,
  144. ConfigDescriptorSize, ConfigDescriptorData) != AUDIO_ENUMERROR_NoError)
  145. {
  146. puts_P(PSTR("Attached Device Not a Valid Audio Output Device.\r\n"));
  147. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  148. return;
  149. }
  150. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  151. {
  152. puts_P(PSTR("Error Setting Device Configuration.\r\n"));
  153. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  154. return;
  155. }
  156. if (Audio_Host_StartStopStreaming(&Speaker_Audio_Interface, true) != HOST_SENDCONTROL_Successful)
  157. {
  158. puts_P(PSTR("Error Enabling Audio Stream.\r\n"));
  159. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  160. USB_Host_SetDeviceConfiguration(0);
  161. return;
  162. }
  163. USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
  164. if (Audio_Host_GetSetEndpointProperty(&Speaker_Audio_Interface, Speaker_Audio_Interface.Config.DataOUTPipe.Address,
  165. AUDIO_REQ_SetCurrent, AUDIO_EPCONTROL_SamplingFreq,
  166. sizeof(SampleRate), &SampleRate) != HOST_SENDCONTROL_Successful)
  167. {
  168. puts_P(PSTR("Error Setting Audio Sampling Frequency.\r\n"));
  169. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  170. USB_Host_SetDeviceConfiguration(0);
  171. return;
  172. }
  173. /* Sample reload timer initialization */
  174. TIMSK0 = (1 << OCIE0A);
  175. OCR0A = ((F_CPU / 8 / 48000) - 1);
  176. TCCR0A = (1 << WGM01); // CTC mode
  177. TCCR0B = (1 << CS01); // Fcpu/8 speed
  178. puts_P(PSTR("Audio Device Enumerated.\r\n"));
  179. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  180. }
  181. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  182. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  183. {
  184. USB_Disable();
  185. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  186. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  187. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  188. for(;;);
  189. }
  190. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  191. * enumerating an attached USB device.
  192. */
  193. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  194. const uint8_t SubErrorCode)
  195. {
  196. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  197. " -- Error Code %d\r\n"
  198. " -- Sub Error Code %d\r\n"
  199. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  200. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  201. }