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.

AudioInputHost.c 7.9KB

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 AudioInputHost demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "AudioInputHost.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 Input 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. LEDs_Init();
  59. USB_Init();
  60. /* Create a stdio stream for the serial port for stdin and stdout */
  61. Serial_CreateStream(NULL);
  62. }
  63. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  64. * starts the library USB task to begin the enumeration and USB management process.
  65. */
  66. void EVENT_USB_Host_DeviceAttached(void)
  67. {
  68. puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
  69. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  70. }
  71. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  72. * stops the library USB task management process.
  73. */
  74. void EVENT_USB_Host_DeviceUnattached(void)
  75. {
  76. puts_P(PSTR(ESC_FG_GREEN "Device Unattached.\r\n" ESC_FG_WHITE));
  77. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  78. }
  79. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  80. * enumerated by the host and is now ready to be used by the application.
  81. */
  82. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  83. {
  84. puts_P(PSTR("Getting Config Data.\r\n"));
  85. uint8_t ErrorCode;
  86. /* Get and process the configuration descriptor data */
  87. if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
  88. {
  89. if (ErrorCode == ControlError)
  90. puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
  91. else
  92. puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
  93. printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  94. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  95. return;
  96. }
  97. /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
  98. if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
  99. {
  100. printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
  101. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  102. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  103. return;
  104. }
  105. if ((ErrorCode = USB_Host_SetInterfaceAltSetting(StreamingInterfaceIndex,
  106. StreamingInterfaceAltSetting)) != HOST_SENDCONTROL_Successful)
  107. {
  108. printf_P(PSTR(ESC_FG_RED "Could not set alternative streaming interface setting.\r\n"
  109. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  110. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  111. USB_Host_SetDeviceConfiguration(0);
  112. return;
  113. }
  114. USB_ControlRequest = (USB_Request_Header_t)
  115. {
  116. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT),
  117. .bRequest = AUDIO_REQ_SetCurrent,
  118. .wValue = (AUDIO_EPCONTROL_SamplingFreq << 8),
  119. .wIndex = StreamingEndpointAddress,
  120. .wLength = sizeof(USB_Audio_SampleFreq_t),
  121. };
  122. USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
  123. /* Select the control pipe for the request transfer */
  124. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  125. /* Set the sample rate on the streaming interface endpoint */
  126. if ((ErrorCode = USB_Host_SendControlRequest(&SampleRate)) != HOST_SENDCONTROL_Successful)
  127. {
  128. printf_P(PSTR(ESC_FG_RED "Could not set requested Audio sample rate.\r\n"
  129. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  130. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  131. USB_Host_SetDeviceConfiguration(0);
  132. return;
  133. }
  134. /* Sample reload timer initialization */
  135. TIMSK0 = (1 << OCIE0A);
  136. OCR0A = ((F_CPU / 8 / 48000) - 1);
  137. TCCR0A = (1 << WGM01); // CTC mode
  138. TCCR0B = (1 << CS01); // Fcpu/8 speed
  139. /* Set speaker as output */
  140. DDRC |= (1 << 6);
  141. /* PWM speaker timer initialization */
  142. TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)); // Set on match, clear on TOP
  143. TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
  144. puts_P(PSTR("Microphone 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 PWM timer with the next sample. */
  169. ISR(TIMER0_COMPA_vect, ISR_BLOCK)
  170. {
  171. uint8_t PrevPipe = Pipe_GetCurrentPipe();
  172. Pipe_SelectPipe(AUDIO_DATA_IN_PIPE);
  173. Pipe_Unfreeze();
  174. /* Check if the current pipe can be read from (contains a packet) and the device is sending data */
  175. if (Pipe_IsINReceived())
  176. {
  177. /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
  178. int8_t Sample_8Bit = (Pipe_Read_16_LE() >> 8);
  179. /* Check to see if the bank is now empty */
  180. if (!(Pipe_IsReadWriteAllowed()))
  181. {
  182. /* Acknowledge the packet, clear the bank ready for the next packet */
  183. Pipe_ClearIN();
  184. }
  185. /* Load the sample into the PWM timer channel */
  186. OCR3A = (Sample_8Bit ^ (1 << 7));
  187. uint8_t LEDMask = LEDS_NO_LEDS;
  188. /* Turn on LEDs as the sample amplitude increases */
  189. if (Sample_8Bit > 16)
  190. LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
  191. else if (Sample_8Bit > 8)
  192. LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
  193. else if (Sample_8Bit > 4)
  194. LEDMask = (LEDS_LED1 | LEDS_LED2);
  195. else if (Sample_8Bit > 2)
  196. LEDMask = (LEDS_LED1);
  197. LEDs_SetAllLEDs(LEDMask);
  198. }
  199. Pipe_Freeze();
  200. Pipe_SelectPipe(PrevPipe);
  201. }