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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /** 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 Microphone_Audio_Interface =
  37. {
  38. .Config =
  39. {
  40. .DataINPipe =
  41. {
  42. .Address = (PIPE_DIR_IN | 1),
  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 Input Host Demo running.\r\n" ESC_FG_WHITE));
  53. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  54. GlobalInterruptEnable();
  55. for (;;)
  56. {
  57. Audio_Host_USBTask(&Microphone_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 read */
  66. if (Audio_Host_IsSampleReceived(&Microphone_Audio_Interface))
  67. {
  68. /* Retrieve the signed 16-bit audio sample, convert to 8-bit */
  69. int8_t Sample_8Bit = (Audio_Host_ReadSample16(&Microphone_Audio_Interface) >> 8);
  70. /* Load the sample into the PWM timer channel */
  71. OCR3A = (Sample_8Bit ^ (1 << 7));
  72. uint8_t LEDMask = LEDS_NO_LEDS;
  73. /* Turn on LEDs as the sample amplitude increases */
  74. if (Sample_8Bit > 16)
  75. LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
  76. else if (Sample_8Bit > 8)
  77. LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
  78. else if (Sample_8Bit > 4)
  79. LEDMask = (LEDS_LED1 | LEDS_LED2);
  80. else if (Sample_8Bit > 2)
  81. LEDMask = (LEDS_LED1);
  82. LEDs_SetAllLEDs(LEDMask);
  83. }
  84. Pipe_SelectPipe(PrevPipe);
  85. }
  86. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  87. void SetupHardware(void)
  88. {
  89. #if (ARCH == ARCH_AVR8)
  90. /* Disable watchdog if enabled by bootloader/fuses */
  91. MCUSR &= ~(1 << WDRF);
  92. wdt_disable();
  93. /* Disable clock division */
  94. clock_prescale_set(clock_div_1);
  95. #endif
  96. /* Hardware Initialization */
  97. Serial_Init(9600, false);
  98. LEDs_Init();
  99. USB_Init();
  100. /* Create a stdio stream for the serial port for stdin and stdout */
  101. Serial_CreateStream(NULL);
  102. }
  103. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  104. * starts the library USB task to begin the enumeration and USB management process.
  105. */
  106. void EVENT_USB_Host_DeviceAttached(void)
  107. {
  108. puts_P(PSTR("Device Attached.\r\n"));
  109. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  110. }
  111. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  112. * stops the library USB task management process.
  113. */
  114. void EVENT_USB_Host_DeviceUnattached(void)
  115. {
  116. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  117. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  118. }
  119. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  120. * enumerated by the host and is now ready to be used by the application.
  121. */
  122. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  123. {
  124. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  125. uint16_t ConfigDescriptorSize;
  126. uint8_t ConfigDescriptorData[512];
  127. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  128. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  129. {
  130. puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
  131. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  132. return;
  133. }
  134. if (Audio_Host_ConfigurePipes(&Microphone_Audio_Interface,
  135. ConfigDescriptorSize, ConfigDescriptorData) != AUDIO_ENUMERROR_NoError)
  136. {
  137. puts_P(PSTR("Attached Device Not a Valid Audio Input Device.\r\n"));
  138. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  139. return;
  140. }
  141. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  142. {
  143. puts_P(PSTR("Error Setting Device Configuration.\r\n"));
  144. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  145. return;
  146. }
  147. if (Audio_Host_StartStopStreaming(&Microphone_Audio_Interface, true) != HOST_SENDCONTROL_Successful)
  148. {
  149. puts_P(PSTR("Error Enabling Audio Stream.\r\n"));
  150. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  151. USB_Host_SetDeviceConfiguration(0);
  152. return;
  153. }
  154. USB_Audio_SampleFreq_t SampleRate = AUDIO_SAMPLE_FREQ(48000);
  155. if (Audio_Host_GetSetEndpointProperty(&Microphone_Audio_Interface, Microphone_Audio_Interface.Config.DataINPipe.Address,
  156. AUDIO_REQ_SetCurrent, AUDIO_EPCONTROL_SamplingFreq,
  157. sizeof(SampleRate), &SampleRate) != HOST_SENDCONTROL_Successful)
  158. {
  159. puts_P(PSTR("Error Setting Audio Sampling Frequency.\r\n"));
  160. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  161. USB_Host_SetDeviceConfiguration(0);
  162. return;
  163. }
  164. /* Sample reload timer initialization */
  165. TIMSK0 = (1 << OCIE0A);
  166. OCR0A = ((F_CPU / 8 / 48000) - 1);
  167. TCCR0A = (1 << WGM01); // CTC mode
  168. TCCR0B = (1 << CS01); // Fcpu/8 speed
  169. /* Set speaker as output */
  170. DDRC |= (1 << 6);
  171. /* PWM speaker timer initialization */
  172. TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)); // Set on match, clear on TOP
  173. TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
  174. puts_P(PSTR("Audio Device Enumerated.\r\n"));
  175. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  176. }
  177. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  178. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  179. {
  180. USB_Disable();
  181. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  182. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  183. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  184. for(;;);
  185. }
  186. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  187. * enumerating an attached USB device.
  188. */
  189. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  190. const uint8_t SubErrorCode)
  191. {
  192. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  193. " -- Error Code %d\r\n"
  194. " -- Sub Error Code %d\r\n"
  195. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  196. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  197. }