描述
<p>* 1、项目功能介绍</p>
<p>这次制作一个带有USB声卡功能的鼠标,表面上看着是一个鼠标,其实它内置喇叭能够直接发声。</p>
<p>*2、项目属性<br>首次公开,原创。</p>
<p><br>* 3、开源协议<br>GPL3.0开源协议</p>
<p>*4、硬件部分</p>
<p>硬件上使用了 USB Hub 芯片对鼠标的 USB 信号进行扩展,转出2个USB接口,一个继续给鼠标使用,另外一个给 Arduino Leonardo (32U4)使用。理论上可以将32u4和USB Hub 芯片放在同一个 PCB 上,但是目前 32U4太贵了(立创商城 65元)。所以选择独立的"Badusb迷你型开发板 Beetle USB ATMEGA32U4虚拟键盘模块 "(22.88 元)。</p>
<p>32u4 带有 USB Device 功能,通过编程,让它报告自己为一个 USB 声卡设备。这样插入电脑后,Windows 会增加一个音频输出设备,选择从这个设备输出后, Windows 会将音频发送到 32U4上。最终我们使用 PWM pin 作为模拟输出驱动喇叭发声。</p>
<p>代码基于 Lufa USB 库开发,主要代码如下:</p>
<p> </p>
<p>#include "AudioOutput.h"</p>
<p>/** LUFA Audio Class driver interface configuration and state information. This structure is<br> * passed to all Audio Class driver functions, so that multiple instances of the same class<br> * within a device can be differentiated from one another.<br> */<br>USB_ClassInfo_Audio_Device_t Speaker_Audio_Interface =<br> {<br> .Config =<br> {<br> .ControlInterfaceNumber = INTERFACE_ID_AudioControl,<br> .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,<br> .DataOUTEndpoint =<br> {<br> .Address = AUDIO_STREAM_EPADDR,<br> .Size = AUDIO_STREAM_EPSIZE,<br> .Banks = 2,<br> },<br> },<br> };</p>
<p>/** Current audio sampling frequency of the streaming audio endpoint. */<br>static uint32_t CurrentAudioSampleFrequency = 48000;</p>
<p><br>/** Main program entry point. This routine contains the overall program flow, including initial<br> * setup of all components and the main program loop.<br> */<br>int main(void)<br>{<br> SetupHardware();</p>
<p> //LAB_ZDebug LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);<br> GlobalInterruptEnable();</p>
<p> for (;;)<br> {<br> Audio_Device_USBTask(&Speaker_Audio_Interface);<br> USB_USBTask();<br> }<br>}</p>
<p>/** Configures the board hardware and chip peripherals for the demo's functionality. */<br>void SetupHardware(void)<br>{<br>#if (ARCH == ARCH_AVR8)<br> /* Disable watchdog if enabled by bootloader/fuses */<br> MCUSR &= ~(1 << WDRF);<br> wdt_disable();</p>
<p> /* Disable clock division */<br> clock_prescale_set(clock_div_1);<br>#endif</p>
<p> /* Hardware Initialization */<br> //LAB_ZDebug LEDs_Init();<br> USB_Init();<br>}</p>
<p>/** ISR to handle the reloading of the PWM timer with the next sample. */<br>ISR(TIMER0_COMPA_vect, ISR_BLOCK)<br>{<br> uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();</p>
<p> /* Check that the USB bus is ready for the next sample to read */<br> if (Audio_Device_IsSampleReceived(&Speaker_Audio_Interface))<br> {<br> /* Retrieve the signed 16-bit left and right audio samples, convert to 8-bit */<br> int8_t LeftSample_8Bit = (Audio_Device_ReadSample16(&Speaker_Audio_Interface) >> 8);<br> int8_t RightSample_8Bit = (Audio_Device_ReadSample16(&Speaker_Audio_Interface) >> 8);</p>
<p> /* Mix the two channels together to produce a mono, 8-bit sample */<br> int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);</p>
<p> #if defined(AUDIO_OUT_MONO)<br> /* Load the sample into the PWM timer channel */<br> OCR1A = (MixedSample_8Bit ^ (1 << 7));<br> #elif defined(AUDIO_OUT_STEREO)<br> /* Load the dual 8-bit samples into the PWM timer channels */<br> OCR1A = (LeftSample_8Bit ^ (1 << 7));<br> OCR1B = (RightSample_8Bit ^ (1 << 7));<br> #elif defined(AUDIO_OUT_PORTC)<br> /* Load the 8-bit mixed sample into PORTC */<br> PORTC = MixedSample_8Bit;<br> #endif</p>
<p> //LAB_ZDebug uint8_t LEDMask = LEDS_NO_LEDS;<br> </p>
<p> /* Turn on LEDs as the sample amplitude increases <br> if (MixedSample_8Bit > 16)<br> LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3 | LEDS_LED4);<br> else if (MixedSample_8Bit > 8)<br> LEDMask = (LEDS_LED1 | LEDS_LED2 | LEDS_LED3);<br> else if (MixedSample_8Bit > 4)<br> LEDMask = (LEDS_LED1 | LEDS_LED2);<br> else if (MixedSample_8Bit > 2)<br> LEDMask = (LEDS_LED1);</p>
<p> LEDs_SetAllLEDs(LEDMask);<br> */<br> }</p>
<p> Endpoint_SelectEndpoint(PrevEndpoint);<br>}</p>
<p>/** Event handler for the library USB Connection event. */<br>void EVENT_USB_Device_Connect(void)<br>{<br> //LAB_ZDebug LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);</p>
<p> /* Sample reload timer initialization */<br> TIMSK0 = (1 << OCIE0A);<br> OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);<br> TCCR0A = (1 << WGM01); // CTC mode<br> TCCR0B = (1 << CS01); // Fcpu/8 speed</p>
<p> #if defined(AUDIO_OUT_MONO)<br> /* Set speaker as output */<br> DDRB |= (1 << 6);<br> #elif defined(AUDIO_OUT_STEREO)<br> /* Set speakers as outputs */<br> DDRB |= ((1 << 6) | (1 << 5));<br> #elif defined(AUDIO_OUT_PORTC)<br> /* Set PORTC as outputs */<br> DDRB |= 0xFF;<br> #endif</p>
<p> #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))<br> /* PWM speaker timer initialization */<br> TCCR1A = ((1 << WGM10) | (1 << COM1A1) | (1 << COM1A0)<br> | (1 << COM1B1) | (1 << COM1B0)); // Set on match, clear on TOP<br> TCCR1B = ((1 << WGM12) | (1 << CS10)); // Fast 8-Bit PWM, F_CPU speed<br> #endif<br>}</p>
<p>/** Event handler for the library USB Disconnection event. */<br>void EVENT_USB_Device_Disconnect(void)<br>{<br> //LAB_ZDebug LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);</p>
<p> /* Stop the sample reload timer */<br> TCCR0B = 0;</p>
<p> #if (defined(AUDIO_OUT_MONO) || defined(AUDIO_OUT_STEREO))<br> /* Stop the PWM generation timer */<br> TCCR1B = 0;<br> #endif</p>
<p> #if defined(AUDIO_OUT_MONO)<br> /* Set speaker as input to reduce current draw */<br> DDRB &= ~(1 << 6);<br> #elif defined(AUDIO_OUT_STEREO)<br> /* Set speakers as inputs to reduce current draw */<br> DDRB &= ~((1 << 6) | (1 << 5));<br> #elif defined(AUDIO_OUT_PORTC)<br> /* Set PORTC low */<br> PORTB = 0x00;<br> #endif<br>}</p>
<p>/** Event handler for the library USB Configuration Changed event. */<br>void EVENT_USB_Device_ConfigurationChanged(void)<br>{<br> bool ConfigSuccess = true;</p>
<p> ConfigSuccess &= Audio_Device_ConfigureEndpoints(&Speaker_Audio_Interface);</p>
<p> //LAB_ZDebug LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);<br>}</p>
<p>/** Event handler for the library USB Control Request reception event. */<br>void EVENT_USB_Device_ControlRequest(void)<br>{<br> Audio_Device_ProcessControlRequest(&Speaker_Audio_Interface);<br>}</p>
<p>/** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented<br> * in the user application to handle property manipulations on streaming audio endpoints.<br> *<br> * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for<br> * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations<br> * to indicate the size of the retrieved data.<br> *<br> * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value<br> * of the \c DataLength parameter.<br> *<br> * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.<br> * \param[in] EndpointProperty Property of the endpoint to get or set, a value from Audio_ClassRequests_t.<br> * \param[in] EndpointAddress Address of the streaming endpoint whose property is being referenced.<br> * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from Audio_EndpointControls_t.<br> * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum<br> * length of the retrieved data. When NULL, the function should return whether the given property<br> * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.<br> * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where<br> * the retrieved data is to be stored for GET operations.<br> *<br> * \return Boolean \c true if the property get/set was successful, \c false otherwise<br> */<br>bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,<br> const uint8_t EndpointProperty,<br> const uint8_t EndpointAddress,<br> const uint8_t EndpointControl,<br> uint16_t* const DataLength,<br> uint8_t* Data)<br>{<br> /* Check the requested endpoint to see if a supported endpoint is being manipulated */<br> if (EndpointAddress == Speaker_Audio_Interface.Config.DataOUTEndpoint.Address)<br> {<br> /* Check the requested control to see if a supported control is being manipulated */<br> if (EndpointControl == AUDIO_EPCONTROL_SamplingFreq)<br> {<br> switch (EndpointProperty)<br> {<br> case AUDIO_REQ_SetCurrent:<br> /* Check if we are just testing for a valid property, or actually adjusting it */<br> if (DataLength != NULL)<br> {<br> /* Set the new sampling frequency to the value given by the host */<br> CurrentAudioSampleFrequency = (((uint32_t)Data[2] << 16) | ((uint32_t)Data[1] << 8) | (uint32_t)Data[0]);</p>
<p> /* Adjust sample reload timer to the new frequency */<br> OCR0A = ((F_CPU / 8 / CurrentAudioSampleFrequency) - 1);<br> }</p>
<p> return true;<br> case AUDIO_REQ_GetCurrent:<br> /* Check if we are just testing for a valid property, or actually reading it */<br> if (DataLength != NULL)<br> {<br> *DataLength = 3;</p>
<p> Data[2] = (CurrentAudioSampleFrequency >> 16);<br> Data[1] = (CurrentAudioSampleFrequency >> 8);<br> Data[0] = (CurrentAudioSampleFrequency & 0xFF);<br> }</p>
<p> return true;<br> }<br> }<br> }</p>
<p> return false;<br>}</p>
<p>/** Audio class driver callback for the setting and retrieval of streaming interface properties. This callback must be implemented<br> * in the user application to handle property manipulations on streaming audio interfaces.<br> *<br> * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for<br> * the given entity and should return as fast as possible. When non-NULL, this value may be altered for GET operations<br> * to indicate the size of the retrieved data.<br> *<br> * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value<br> * of the \c DataLength parameter.<br> *<br> * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.<br> * \param[in] Property Property of the interface to get or set, a value from Audio_ClassRequests_t.<br> * \param[in] EntityAddress Address of the audio entity whose property is being referenced.<br> * \param[in] Parameter Parameter of the entity to get or set, specific to each type of entity (see USB Audio specification).<br> * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum<br> * length of the retrieved data. When NULL, the function should return whether the given property<br> * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.<br> * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where<br> * the retrieved data is to be stored for GET operations.<br> *<br> * \return Boolean \c true if the property GET/SET was successful, \c false otherwise<br> */<br>bool CALLBACK_Audio_Device_GetSetInterfaceProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,<br> const uint8_t Property,<br> const uint8_t EntityAddress,<br> const uint16_t Parameter,</p>
<p> uint16_t* const DataLength,<br> uint8_t* Data)<br>{<br> /* No audio interface entities in the device descriptor, thus no properties to get or set. */<br> return false;<br>}</p>
<p> </p>
<p>*6、BOM清单</p>
<p> </p>
<p><img src="//image.lceda.cn/pullimage/gA00tD5f2xQqBXGrw8BIP2M67MrwhC3TkM47WSVt.png" alt="" width="1752" height="364"></p>
<p> </p>
<p>*7、大赛LOGO验证</p>
<p><img src="//image.lceda.cn/pullimage/6QE0yGEmR1wS8w6hH29OS6Qpz7pDFuCwuo0IFF6u.jpeg" alt="" width="1382" height="1036"></p>
<p> </p>
<p>* 8、演示您的项目并录制成视频上传</p>
<p> </p>
<p><img src="//image.lceda.cn/pullimage/uQPsjJ4MNiCiPSqQpMVtm5JYTyF7ReBGDE4iCDNp.jpeg" alt="" width="3648" height="2736"></p>
<p> </p>
<p><img src="//image.lceda.cn/pullimage/SV4KID20tU4SYb4DlUuZWDD1ccbfTxKRZsAus90v.jpeg" alt="" width="3648" height="2736"></p>
评论(1)