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 8.0KB

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