| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | /* 
 | 
					
						
							|  |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2020 Reinhard Panhuber | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-15 12:08:50 +07:00
										 |  |  | /* plot_audio_samples.py requires following modules:
 | 
					
						
							|  |  |  |  * $ sudo apt install libportaudio | 
					
						
							|  |  |  |  * $ pip3 install sounddevice matplotlib | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Then run | 
					
						
							|  |  |  |  * $ python3 plot_audio_samples.py | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "bsp/board.h"
 | 
					
						
							|  |  |  | #include "tusb.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO CONSTANT TYPEDEF PROTYPES
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-13 00:23:13 +07:00
										 |  |  | #ifndef AUDIO_SAMPLE_RATE
 | 
					
						
							|  |  |  | #define AUDIO_SAMPLE_RATE   48000
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | /* Blink pattern
 | 
					
						
							|  |  |  |  * - 250 ms  : device not mounted | 
					
						
							|  |  |  |  * - 1000 ms : device mounted | 
					
						
							|  |  |  |  * - 2500 ms : device is suspended | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | enum  { | 
					
						
							|  |  |  |   BLINK_NOT_MOUNTED = 250, | 
					
						
							|  |  |  |   BLINK_MOUNTED = 1000, | 
					
						
							|  |  |  |   BLINK_SUSPENDED = 2500, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Audio controls
 | 
					
						
							|  |  |  | // Current states
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:48:09 +02:00
										 |  |  | bool mute[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX + 1]; 				          // +1 for master channel 0
 | 
					
						
							| 
									
										
										
										
											2021-04-03 09:49:27 +02:00
										 |  |  | uint16_t volume[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX + 1]; 					// +1 for master channel 0
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | uint32_t sampFreq; | 
					
						
							|  |  |  | uint8_t clkValid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Range states
 | 
					
						
							| 
									
										
										
										
											2021-04-03 09:49:27 +02:00
										 |  |  | audio_control_range_2_n_t(1) volumeRng[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX+1]; 			// Volume range state
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | audio_control_range_4_n_t(1) sampleFreqRng; 						// Sample frequency range state
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 20:15:57 +02:00
										 |  |  | // Audio test data
 | 
					
						
							| 
									
										
										
										
											2021-04-03 09:49:27 +02:00
										 |  |  | uint16_t test_buffer_audio[CFG_TUD_AUDIO_EP_SZ_IN/2]; | 
					
						
							| 
									
										
										
										
											2020-10-09 20:15:57 +02:00
										 |  |  | uint16_t startVal = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | void led_blinking_task(void); | 
					
						
							|  |  |  | void audio_task(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*------------- MAIN -------------*/ | 
					
						
							|  |  |  | int main(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   board_init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   tusb_init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Init values
 | 
					
						
							| 
									
										
										
										
											2021-03-10 19:32:13 +01:00
										 |  |  |   sampFreq = AUDIO_SAMPLE_RATE; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   clkValid = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sampleFreqRng.wNumSubRanges = 1; | 
					
						
							| 
									
										
										
										
											2021-03-10 19:32:13 +01:00
										 |  |  |   sampleFreqRng.subrange[0].bMin = AUDIO_SAMPLE_RATE; | 
					
						
							|  |  |  |   sampleFreqRng.subrange[0].bMax = AUDIO_SAMPLE_RATE; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   sampleFreqRng.subrange[0].bRes = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (1) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     tud_task(); // tinyusb device task
 | 
					
						
							|  |  |  |     led_blinking_task(); | 
					
						
							|  |  |  |     audio_task(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Device callbacks
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when device is mounted
 | 
					
						
							|  |  |  | void tud_mount_cb(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   blink_interval_ms = BLINK_MOUNTED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when device is unmounted
 | 
					
						
							|  |  |  | void tud_umount_cb(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   blink_interval_ms = BLINK_NOT_MOUNTED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when usb bus is suspended
 | 
					
						
							|  |  |  | // remote_wakeup_en : if host allow us  to perform remote wakeup
 | 
					
						
							|  |  |  | // Within 7ms, device must draw an average of current less than 2.5 mA from bus
 | 
					
						
							|  |  |  | void tud_suspend_cb(bool remote_wakeup_en) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) remote_wakeup_en; | 
					
						
							|  |  |  |   blink_interval_ms = BLINK_SUSPENDED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when usb bus is resumed
 | 
					
						
							|  |  |  | void tud_resume_cb(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   blink_interval_ms = BLINK_MOUNTED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // AUDIO Task
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void audio_task(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   // Yet to be filled - e.g. put meas data into TX FIFOs etc.
 | 
					
						
							| 
									
										
										
										
											2021-11-02 21:46:23 +07:00
										 |  |  |   // asm("nop");
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Application Callback API Implementations
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when audio class specific set request received for an EP
 | 
					
						
							|  |  |  | bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   (void) pBuff; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We do not support any set range requests here, only current value requests
 | 
					
						
							|  |  |  |   TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page 91 in UAC2 specification
 | 
					
						
							|  |  |  |   uint8_t channelNum = TU_U16_LOW(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ep = TU_U16_LOW(p_request->wIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   (void) channelNum; (void) ctrlSel; (void) ep; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   return false; 	// Yet not implemented
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when audio class specific set request received for an interface
 | 
					
						
							|  |  |  | bool tud_audio_set_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   (void) pBuff; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We do not support any set range requests here, only current value requests
 | 
					
						
							|  |  |  |   TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page 91 in UAC2 specification
 | 
					
						
							|  |  |  |   uint8_t channelNum = TU_U16_LOW(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t itf = TU_U16_LOW(p_request->wIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   (void) channelNum; (void) ctrlSel; (void) itf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   return false; 	// Yet not implemented
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when audio class specific set request received for an entity
 | 
					
						
							|  |  |  | bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page 91 in UAC2 specification
 | 
					
						
							|  |  |  |   uint8_t channelNum = TU_U16_LOW(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t itf = TU_U16_LOW(p_request->wIndex); | 
					
						
							|  |  |  |   uint8_t entityID = TU_U16_HIGH(p_request->wIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   (void) itf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   // We do not support any set range requests here, only current value requests
 | 
					
						
							|  |  |  |   TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // If request is for our feature unit
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   if ( entityID == 2 ) | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |     switch ( ctrlSel ) | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       case AUDIO_FU_CTRL_MUTE: | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |         // Request uses format layout 1
 | 
					
						
							|  |  |  |         TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_1_t)); | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |         mute[channelNum] = ((audio_control_cur_1_t*) pBuff)->bCur; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |         TU_LOG2("    Set Mute: %d of channel: %u\r\n", mute[channelNum], channelNum); | 
					
						
							|  |  |  |       return true; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       case AUDIO_FU_CTRL_VOLUME: | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |         // Request uses format layout 2
 | 
					
						
							|  |  |  |         TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_2_t)); | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |         volume[channelNum] = ((audio_control_cur_2_t*) pBuff)->bCur; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |         TU_LOG2("    Set Volume: %d dB of channel: %u\r\n", volume[channelNum], channelNum); | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |       return true; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |         // Unknown/Unsupported control
 | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         TU_BREAKPOINT(); | 
					
						
							|  |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   return false;    // Yet not implemented
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when audio class specific get request received for an EP
 | 
					
						
							|  |  |  | bool tud_audio_get_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page 91 in UAC2 specification
 | 
					
						
							|  |  |  |   uint8_t channelNum = TU_U16_LOW(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ep = TU_U16_LOW(p_request->wIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   (void) channelNum; (void) ctrlSel; (void) ep; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   //	return tud_control_xfer(rhport, p_request, &tmp, 1);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return false; 	// Yet not implemented
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when audio class specific get request received for an interface
 | 
					
						
							|  |  |  | bool tud_audio_get_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page 91 in UAC2 specification
 | 
					
						
							|  |  |  |   uint8_t channelNum = TU_U16_LOW(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t itf = TU_U16_LOW(p_request->wIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 13:36:03 +07:00
										 |  |  |   (void) channelNum; (void) ctrlSel; (void) itf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   return false; 	// Yet not implemented
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Invoked when audio class specific get request received for an entity
 | 
					
						
							|  |  |  | bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Page 91 in UAC2 specification
 | 
					
						
							|  |  |  |   uint8_t channelNum = TU_U16_LOW(p_request->wValue); | 
					
						
							|  |  |  |   uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); | 
					
						
							|  |  |  |   // uint8_t itf = TU_U16_LOW(p_request->wIndex); 			// Since we have only one audio function implemented, we do not need the itf value
 | 
					
						
							|  |  |  |   uint8_t entityID = TU_U16_HIGH(p_request->wIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Input terminal (Microphone input)
 | 
					
						
							|  |  |  |   if (entityID == 1) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |     switch ( ctrlSel ) | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |       case AUDIO_TE_CTRL_CONNECTOR: | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         // The terminal connector control only has a get request with only the CUR attribute.
 | 
					
						
							|  |  |  |         audio_desc_channel_cluster_t ret; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         // Those are dummy values for now
 | 
					
						
							|  |  |  |         ret.bNrChannels = 1; | 
					
						
							|  |  |  |         ret.bmChannelConfig = 0; | 
					
						
							|  |  |  |         ret.iChannelNames = 0; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         TU_LOG2("    Get terminal connector\r\n"); | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, (void*) &ret, sizeof(ret)); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         // Unknown/Unsupported control selector
 | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         TU_BREAKPOINT(); | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Feature unit
 | 
					
						
							|  |  |  |   if (entityID == 2) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |     switch ( ctrlSel ) | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       case AUDIO_FU_CTRL_MUTE: | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         // Audio control mute cur parameter block consists of only one byte - we thus can send it right away
 | 
					
						
							|  |  |  |         // There does not exist a range parameter block for mute
 | 
					
						
							|  |  |  |         TU_LOG2("    Get Mute of channel: %u\r\n", channelNum); | 
					
						
							|  |  |  |         return tud_control_xfer(rhport, p_request, &mute[channelNum], 1); | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       case AUDIO_FU_CTRL_VOLUME: | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         switch ( p_request->bRequest ) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           case AUDIO_CS_REQ_CUR: | 
					
						
							|  |  |  |             TU_LOG2("    Get Volume of channel: %u\r\n", channelNum); | 
					
						
							|  |  |  |             return tud_control_xfer(rhport, p_request, &volume[channelNum], sizeof(volume[channelNum])); | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |           case AUDIO_CS_REQ_RANGE: | 
					
						
							|  |  |  |             TU_LOG2("    Get Volume range of channel: %u\r\n", channelNum); | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |             // Copy values - only for testing - better is version below
 | 
					
						
							|  |  |  |             audio_control_range_2_n_t(1) | 
					
						
							|  |  |  |             ret; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |             ret.wNumSubRanges = 1; | 
					
						
							|  |  |  |             ret.subrange[0].bMin = -90;    // -90 dB
 | 
					
						
							|  |  |  |             ret.subrange[0].bMax = 90;		// +90 dB
 | 
					
						
							|  |  |  |             ret.subrange[0].bRes = 1; 		// 1 dB steps
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |             return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, (void*) &ret, sizeof(ret)); | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |             // Unknown/Unsupported control
 | 
					
						
							|  |  |  |           default: | 
					
						
							|  |  |  |             TU_BREAKPOINT(); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         // Unknown/Unsupported control
 | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         TU_BREAKPOINT(); | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Clock Source unit
 | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |   if ( entityID == 4 ) | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |     switch ( ctrlSel ) | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       case AUDIO_CS_CTRL_SAM_FREQ: | 
					
						
							| 
									
										
										
										
											2021-04-06 15:34:50 +07:00
										 |  |  |         // channelNum is always zero in this case
 | 
					
						
							|  |  |  |         switch ( p_request->bRequest ) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           case AUDIO_CS_REQ_CUR: | 
					
						
							|  |  |  |             TU_LOG2("    Get Sample Freq.\r\n"); | 
					
						
							|  |  |  |             return tud_control_xfer(rhport, p_request, &sampFreq, sizeof(sampFreq)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           case AUDIO_CS_REQ_RANGE: | 
					
						
							|  |  |  |             TU_LOG2("    Get Sample Freq. range\r\n"); | 
					
						
							|  |  |  |             return tud_control_xfer(rhport, p_request, &sampleFreqRng, sizeof(sampleFreqRng)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            // Unknown/Unsupported control
 | 
					
						
							|  |  |  |           default: | 
					
						
							|  |  |  |             TU_BREAKPOINT(); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       case AUDIO_CS_CTRL_CLK_VALID: | 
					
						
							|  |  |  |         // Only cur attribute exists for this request
 | 
					
						
							|  |  |  |         TU_LOG2("    Get Sample Freq. valid\r\n"); | 
					
						
							|  |  |  |         return tud_control_xfer(rhport, p_request, &clkValid, sizeof(clkValid)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Unknown/Unsupported control
 | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         TU_BREAKPOINT(); | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   TU_LOG2("  Unsupported entity: %d\r\n", entityID); | 
					
						
							|  |  |  |   return false; 	// Yet not implemented
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 20:15:57 +02:00
										 |  |  | bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t itf, uint8_t ep_in, uint8_t cur_alt_setting) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							|  |  |  |   (void) itf; | 
					
						
							|  |  |  |   (void) ep_in; | 
					
						
							|  |  |  |   (void) cur_alt_setting; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:48:09 +02:00
										 |  |  |   tud_audio_write ((uint8_t *)test_buffer_audio, CFG_TUD_AUDIO_EP_SZ_IN); | 
					
						
							| 
									
										
										
										
											2020-10-09 20:15:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool tud_audio_tx_done_post_load_cb(uint8_t rhport, uint16_t n_bytes_copied, uint8_t itf, uint8_t ep_in, uint8_t cur_alt_setting) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							| 
									
										
										
										
											2020-10-09 21:59:52 +02:00
										 |  |  |   (void) n_bytes_copied; | 
					
						
							| 
									
										
										
										
											2020-10-09 20:15:57 +02:00
										 |  |  |   (void) itf; | 
					
						
							|  |  |  |   (void) ep_in; | 
					
						
							|  |  |  |   (void) cur_alt_setting; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:48:09 +02:00
										 |  |  |   for (size_t cnt = 0; cnt < CFG_TUD_AUDIO_EP_SZ_IN/2; cnt++) | 
					
						
							| 
									
										
										
										
											2020-10-09 20:15:57 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-10-09 20:36:35 +02:00
										 |  |  |     test_buffer_audio[cnt] = startVal++; | 
					
						
							| 
									
										
										
										
											2020-10-09 20:15:57 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool tud_audio_set_itf_close_EP_cb(uint8_t rhport, tusb_control_request_t const * p_request) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) rhport; | 
					
						
							|  |  |  |   (void) p_request; | 
					
						
							|  |  |  |   startVal = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 21:07:43 +02:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // BLINKING TASK
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | void led_blinking_task(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   static uint32_t start_ms = 0; | 
					
						
							|  |  |  |   static bool led_state = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Blink every interval ms
 | 
					
						
							|  |  |  |   if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
 | 
					
						
							|  |  |  |   start_ms += blink_interval_ms; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   board_led_write(led_state); | 
					
						
							|  |  |  |   led_state = 1 - led_state; // toggle
 | 
					
						
							|  |  |  | } |