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.

AudioOutput.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 AudioOutput demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "AudioOutput.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 Speaker_Audio_Interface =
  37. {
  38. .Config =
  39. {
  40. .ControlInterfaceNumber = INTERFACE_ID_AudioControl,
  41. .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,
  42. .DataOUTEndpoint =
  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(&Speaker_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. USB_Init();
  79. }
  80. /** ISR to handle the reloading of the PWM timer with the next sample. */
  81. ISR(TIMER0_COMPA_vect, ISR_BLOCK)
  82. {
  83. uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();
  84. /* Check that the USB bus is ready for the next sample to read */
  85. if (Audio_Device_IsSampleReceived(&Speaker_Audio_Interface))
  86. {
  87. /* Retrieve the signed 16-bit left and right audio samples, convert to 8-bit */
  88. int8_t LeftSample_8Bit = (Audio_Device_ReadSample16(&Speaker_Audio_Interface) >> 8);
  89. int8_t RightSample_8Bit = (Audio_Device_ReadSample16(&Speaker_Audio_Interface) >> 8);
  90. /* Mix the two channels together to produce a mono, 8-bit sample */
  91. int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);
  92. #if defined(AUDIO_OUT_MONO)
  93. /* Load the sample into the PWM timer channel */
  94. OCR3A = (MixedSample_8Bit ^ (1 << 7));
  95. #elif defined(AUDIO_OUT_STEREO)
  96. /* Load the dual 8-bit samples into the PWM timer channels */
  97. OCR3A = (LeftSample_8Bit ^ (1 << 7));
  98. OCR3B = (RightSample_8Bit ^ (1 << 7));
  99. #elif defined(AUDIO_OUT_PORTC)
  100. /* Load the 8-bit mixed sample into PORTC */
  101. PORTC = MixedSample_8Bit;
  102. #endif
  103. uint8_t LEDMask = LEDS_NO_LEDS;
  104. /* Turn on LEDs as the sample amplitude increases */
  105. if (MixedSample_8Bit > 16)
  106. LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);
  107. else if (MixedSample_8Bit > 8)
  108. LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);
  109. else if (MixedSample_8Bit > 4)
  110. LEDMask = (LEDS_LED1 | LEDS_LED2);
  111. else if (MixedSample_8Bit > 2)
  112. LEDMask = (LEDS_LED1);
  113. LEDs_SetAllLEDs(LEDMask);
  114. }
  115. Endpoint_SelectEndpoint(PrevEndpoint);
  116. }
  117. /** Event handler for the library USB Connection event. */
  118. void EVENT_USB_Device_Connect(void)
  119. {
  120. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  121. /* Sample reload timer initialization */
  122. TIMSK0 = (1 << OCIE0A);
  123. OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);
  124. TCCR0A = (1 << WGM01); // CTC mode
  125. TCCR0B = (1 << CS01); // Fcpu/8 speed
  126. #if defined(AUDIO_OUT_MONO)
  127. /* Set speaker as output */
  128. DDRC |= (1 << 6);
  129. #elif defined(AUDIO_OUT_STEREO)
  130. /* Set speakers as outputs */
  131. DDRC |= ((1 << 6) | (1 << 5));
  132. #elif defined(AUDIO_OUT_PORTC)
  133. /* Set PORTC as outputs */
  134. DDRC |= 0xFF;
  135. #endif
  136. #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
  137. /* PWM speaker timer initialization */
  138. TCCR3A = ((1 << WGM30) | (1 << COM3A1) | (1 << COM3A0)
  139. | (1 << COM3B1) | (1 << COM3B0)); // Set on match, clear on TOP
  140. TCCR3B = ((1 << WGM32) | (1 << CS30)); // Fast 8-Bit PWM, F_CPU speed
  141. #endif
  142. }
  143. /** Event handler for the library USB Disconnection event. */
  144. void EVENT_USB_Device_Disconnect(void)
  145. {
  146. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  147. /* Stop the sample reload timer */
  148. TCCR0B = 0;
  149. #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))
  150. /* Stop the PWM generation timer */
  151. TCCR3B = 0;
  152. #endif
  153. #if defined(AUDIO_OUT_MONO)
  154. /* Set speaker as input to reduce current draw */
  155. DDRC &= ~(1 << 6);
  156. #elif defined(AUDIO_OUT_STEREO)
  157. /* Set speakers as inputs to reduce current draw */
  158. DDRC &= ~((1 << 6) | (1 << 5));
  159. #elif defined(AUDIO_OUT_PORTC)
  160. /* Set PORTC low */
  161. PORTC = 0x00;
  162. #endif
  163. }
  164. /** Event handler for the library USB Configuration Changed event. */
  165. void EVENT_USB_Device_ConfigurationChanged(void)
  166. {
  167. bool ConfigSuccess = true;
  168. ConfigSuccess &= Audio_Device_ConfigureEndpoints(&Speaker_Audio_Interface);
  169. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  170. }
  171. /** Event handler for the library USB Control Request reception event. */
  172. void EVENT_USB_Device_ControlRequest(void)
  173. {
  174. Audio_Device_ProcessControlRequest(&Speaker_Audio_Interface);
  175. }
  176. /** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
  177. * in the user application to handle property manipulations on streaming audio endpoints.
  178. *
  179. * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
  180. * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
  181. * to indicate the size of the retrieved data.
  182. *
  183. * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
  184. * of the \c DataLength parameter.
  185. *
  186. * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
  187. * \param[in] EndpointProperty Property of the endpoint to get or set, a value from Audio_ClassRequests_t.
  188. * \param[in] EndpointAddress Address of the streaming endpoint whose property is being referenced.
  189. * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from Audio_EndpointControls_t.
  190. * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
  191. * length of the retrieved data. When NULL, the function should return whether the given property
  192. * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
  193. * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
  194. * the retrieved data is to be stored for GET operations.
  195. *
  196. * \return Boolean \c true if the property get/set was successful, \c false otherwise
  197. */
  198. bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
  199. const uint8_t EndpointProperty,
  200. const uint8_t EndpointAddress,
  201. const uint8_t EndpointControl,
  202. uint16_t* const DataLength,
  203. uint8_t* Data)
  204. {
  205. /* Check the requested endpoint to see if a supported endpoint is being manipulated */
  206. if (EndpointAddress == Speaker_Audio_Interface.Config.DataOUTEndpoint.Address)
  207. {
  208. /* Check the requested control to see if a supported control is being manipulated */
  209. if (EndpointControl == AUDIO_EPCONTROL_SamplingFreq)
  210. {
  211. switch (EndpointProperty)
  212. {
  213. case AUDIO_REQ_SetCurrent:
  214. /* Check if we are just testing for a valid property, or actually adjusting it */
  215. if (DataLength != NULL)
  216. {
  217. /* Set the new sampling frequency to the value given by the host */
  218. CurrentAudioSampleFrequency = (((uint32_t)Data[2] << 16) | ((uint32_t)Data[1] << 8) | (uint32_t)Data[0]);
  219. /* Adjust sample reload timer to the new frequency */
  220. OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);
  221. }
  222. return true;
  223. case AUDIO_REQ_GetCurrent:
  224. /* Check if we are just testing for a valid property, or actually reading it */
  225. if (DataLength != NULL)
  226. {
  227. *DataLength = 3;
  228. Data[2] = (CurrentAudioSampleFrequency >> 16);
  229. Data[1] = (CurrentAudioSampleFrequency >> 8);
  230. Data[0] = (CurrentAudioSampleFrequency & 0xFF);
  231. }
  232. return true;
  233. }
  234. }
  235. }
  236. return false;
  237. }
  238. /** Audio class driver callback for the setting and retrieval of streaming interface properties. This callback must be implemented
  239. * in the user application to handle property manipulations on streaming audio interfaces.
  240. *
  241. * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
  242. * the given entity and should return as fast as possible. When non-NULL, this value may be altered for GET operations
  243. * to indicate the size of the retrieved data.
  244. *
  245. * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
  246. * of the \c DataLength parameter.
  247. *
  248. * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
  249. * \param[in] Property Property of the interface to get or set, a value from Audio_ClassRequests_t.
  250. * \param[in] EntityAddress Address of the audio entity whose property is being referenced.
  251. * \param[in] Parameter Parameter of the entity to get or set, specific to each type of entity (see USB Audio specification).
  252. * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
  253. * length of the retrieved data. When NULL, the function should return whether the given property
  254. * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
  255. * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
  256. * the retrieved data is to be stored for GET operations.
  257. *
  258. * \return Boolean \c true if the property GET/SET was successful, \c false otherwise
  259. */
  260. bool CALLBACK_Audio_Device_GetSetInterfaceProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
  261. const uint8_t Property,
  262. const uint8_t EntityAddress,
  263. const uint16_t Parameter,
  264. uint16_t* const DataLength,
  265. uint8_t* Data)
  266. {
  267. /* No audio interface entities in the device descriptor, thus no properties to get or set. */
  268. return false;
  269. }