diff --git a/module_usb_aud_shared/audio.xc b/module_usb_aud_shared/audio.xc index c261f73e..ef2abb31 100755 --- a/module_usb_aud_shared/audio.xc +++ b/module_usb_aud_shared/audio.xc @@ -579,7 +579,8 @@ void audio(chanend c_mix_out, chanend ?c_dig_rx, chanend ?c_config) { #ifdef SPDIF - { //set_thread_fast_mode_on(); + { + set_thread_fast_mode_on(); SpdifTransmit(p_spdif_tx, c_spdif_out); } #endif diff --git a/module_usb_aud_shared/endpoint0/descriptors_2.h b/module_usb_aud_shared/endpoint0/descriptors_2.h index f81c0b24..19afb760 100644 --- a/module_usb_aud_shared/endpoint0/descriptors_2.h +++ b/module_usb_aud_shared/endpoint0/descriptors_2.h @@ -1284,7 +1284,7 @@ unsigned char cfgDesc_Audio2[] = /* Endpoint descriptor (IN) */ 0x7, /* 0 bLength */ 5, /* 1 bDescriptorType */ - EP_ADR_IN_HID, /* 2 bEndpointAddress */ + EP_ADR_IN_HID, /* 2 bEndpointAddress */ 3, /* 3 bmAttributes (INTERRUPT) */ 64, /* 4 wMaxPacketSize */ 0, /* 5 wMaxPacketSize */ diff --git a/module_usb_aud_shared/endpoint0/endpoint0.xc b/module_usb_aud_shared/endpoint0/endpoint0.xc index fe2d33bb..1a763efd 100755 --- a/module_usb_aud_shared/endpoint0/endpoint0.xc +++ b/module_usb_aud_shared/endpoint0/endpoint0.xc @@ -95,7 +95,7 @@ unsigned g_epStatusOut[NUM_EP_OUT]; unsigned g_epStatusIn[NUM_EP_IN]; /* Global variable for current USB bus speed (i.e. FS/HS) */ -unsigned g_curUsbSpeed = XUD_SPEED_HS; +unsigned g_curUsbSpeed = 0; #ifdef HOST_ACTIVE_CALL void VendorHostActive(int active); @@ -842,6 +842,8 @@ void Endpoint0( chanend c_ep0_out, chanend c_ep0_in, chanend c_audioControl, { g_curUsbSpeed = XUD_ResetEndpoint(ep0_out, ep0_in); + //printintln(g_curUsbSpeed); + g_config = 0; #ifdef DFU diff --git a/module_usb_aud_shared/usb_buffer/decouple.h b/module_usb_aud_shared/usb_buffer/decouple.h index ce83fbc8..2fa7f523 100644 --- a/module_usb_aud_shared/usb_buffer/decouple.h +++ b/module_usb_aud_shared/usb_buffer/decouple.h @@ -12,7 +12,7 @@ * \param c_clk_int Optional chanend connected to the clockGen() thread if present */ void decouple(chanend c_audio_out, - chanend ?c_midi, + // chanend ?c_midi, chanend ?c_clk_int #ifdef IAP , chanend ?c_iap diff --git a/module_usb_aud_shared/usb_buffer/decouple.xc b/module_usb_aud_shared/usb_buffer/decouple.xc index 16f9fc48..1ed40d97 100644 --- a/module_usb_aud_shared/usb_buffer/decouple.xc +++ b/module_usb_aud_shared/usb_buffer/decouple.xc @@ -42,11 +42,13 @@ */ static inline void XUD_Change_ReadyIn_Buffer(XUD_ep e, unsigned bufferPtr, int len) { + int chan_array_ptr; int xud_chan; int my_chan; int tail; + printstr("TODO"); asm ("ldw %0, %1[0]":"=r"(chan_array_ptr):"r"(e)); asm ("ldw %0, %1[2]":"=r"(my_chan):"r"(e)); @@ -173,7 +175,8 @@ static inline void swap(xc_ptr &a, xc_ptr &b) } // shared global midi buffering variables -#ifdef MIDI +#if 0 +//#ifdef MIDI unsigned g_midi_from_host_flag = 0; unsigned g_midi_to_host_flag = 0; int midi_to_host_usb_ep = 0; @@ -193,7 +196,8 @@ int aud_from_host_usb_ep = 0; int aud_to_host_usb_ep = 0; int int_usb_ep = 0; -#ifdef MIDI +#if 0 +//#ifdef MIDI unsigned int g_midi_to_host_buffer_A[MAX_USB_MIDI_PACKET_SIZE/4+4]; unsigned int g_midi_to_host_buffer_B[MAX_USB_MIDI_PACKET_SIZE/4+4]; int g_midi_from_host_buffer[MAX_USB_MIDI_PACKET_SIZE/4+4]; @@ -292,7 +296,7 @@ void handle_audio_request(chanend c_mix_out, chanend ?c_led) slotSize = 3; /* 3 bytes per sample */ } #endif - + /* If in overflow condition then receive samples and throw away */ if(inOverflow || sampsToWrite == 0) { @@ -437,6 +441,7 @@ void handle_audio_request(chanend c_mix_out, chanend ?c_led) read_via_xc_ptr(sample, g_aud_from_host_rdptr); g_aud_from_host_rdptr+=4; + #ifndef OUT_VOLUME_IN_MIXER asm("ldw %0, %1[%2]":"=r"(mult):"r"(p_multOut),"r"(i)); {h, l} = macs(mult, sample, 0, 0); @@ -485,6 +490,14 @@ void handle_audio_request(chanend c_mix_out, chanend ?c_led) break; } unpackState++; + + if(sample!=0) + { //printhexln(sample); + //printintln(g_numUsbChanOut); + //printhexln(g_aud_from_host_rdptr); + //printintln(aud_data_remaining_to_device); + //while(1); + } #ifndef OUT_VOLUME_IN_MIXER asm("ldw %0, %1[%2]":"=r"(mult):"r"(p_multOut),"r"(i)); @@ -588,6 +601,7 @@ void handle_audio_request(chanend c_mix_out, chanend ?c_led) /* Handle any tail - incase a bad driver sent us a datalength not a multiple of chan count */ if (aud_data_remaining_to_device) { + printintln(aud_data_remaining_to_device); /* Round up to nearest word */ aud_data_remaining_to_device +=3; aud_data_remaining_to_device &= (~3); @@ -609,6 +623,7 @@ void handle_audio_request(chanend c_mix_out, chanend ?c_led) if (!outUnderflow) { read_via_xc_ptr(aud_data_remaining_to_device, g_aud_from_host_rdptr); + unpackState = 0; @@ -665,7 +680,8 @@ unsigned char tmpBuffer[1026]; #pragma unsafe arrays void decouple(chanend c_mix_out, - chanend ?c_midi, chanend ?c_clk_int + //chanend ?c_midi, + chanend ?c_clk_int #ifdef IAP , chanend ?c_iap #endif @@ -676,21 +692,6 @@ void decouple(chanend c_mix_out, int aud_to_host_flag=0; xc_ptr released_buffer; -#ifdef MIDI - xc_ptr midi_from_host_rdptr; - xc_ptr midi_from_host_buffer; - xc_ptr midi_to_host_buffer_being_sent = array_to_xc_ptr(g_midi_to_host_buffer_A); - xc_ptr midi_to_host_buffer_being_collected = array_to_xc_ptr(g_midi_to_host_buffer_B); - - int is_ack; - unsigned int datum; - int midi_data_remaining_to_device = 0; - int midi_data_collected_from_device = 0; - int midi_waiting_on_send_to_host = 0; - int midi_to_host_flag = 0; - int midi_from_host_flag = 0; -#endif - #ifdef IAP xc_ptr iap_from_host_rdptr; xc_ptr iap_from_host_buffer; @@ -771,18 +772,19 @@ void decouple(chanend c_mix_out, #ifdef MIDI //asm("ldaw %0, dp[g_midi_to_host_buffer]":"=r"(midi_to_host_buffer)); - asm("ldaw %0, dp[g_midi_from_host_buffer]":"=r"(midi_from_host_buffer)); + //asm("ldaw %0, dp[g_midi_from_host_buffer]":"=r"(midi_from_host_buffer)); // wait for usb_buffer to set up - while(!midi_from_host_flag) { - GET_SHARED_GLOBAL(midi_from_host_flag, g_midi_from_host_flag); - } + //while(!midi_from_host_flag) { + //GET_SHARED_GLOBAL(midi_from_host_flag, g_midi_from_host_flag); + // } +//#if 1 - midi_from_host_flag = 0; - SET_SHARED_GLOBAL(g_midi_from_host_flag, midi_from_host_flag); + //midi_from_host_flag = 0; + //SET_SHARED_GLOBAL(g_midi_from_host_flag, midi_from_host_flag); // send the current host -> device buffer out of the fifo - XUD_SetReady_OutPtr(midi_from_host_usb_ep, midi_from_host_buffer); + //XUD_SetReady_OutPtr(midi_from_host_usb_ep, midi_from_host_buffer); #endif #ifdef IAP @@ -813,7 +815,7 @@ void decouple(chanend c_mix_out, // send the current host -> device buffer out of the fifo SET_SHARED_GLOBAL(g_aud_from_host_buffer, g_aud_from_host_wrptr); - XUD_SetReady_OutPtr(aud_from_host_usb_ep, g_aud_from_host_wrptr); + XUD_SetReady_OutPtr(aud_from_host_usb_ep, g_aud_from_host_wrptr+4); #endif #ifdef INPUT @@ -841,7 +843,7 @@ void decouple(chanend c_mix_out, while(1) { - int tmp; + int tmp; if (!isnull(c_clk_int)) { check_for_interrupt(c_clk_int); @@ -849,18 +851,11 @@ void decouple(chanend c_mix_out, { #ifdef HID_CONTROLS - - p_but :> tmp; - tmp = ~tmp; - tmp &=3; - g_hidData[0] = tmp; - Vendor_ReadHIDButtons(g_hidData); - asm("ldaw %0, dp[g_hidData]":"=r"(tmp)); - if(g_hidFlag==0) + if(g_hidFlag == 0) { - //XUD_SetReady_In(g_ep_hid, 0, tmp, 1); + XUD_SetReady_In(g_ep_hid, g_hidData, 1); g_hidFlag = 1; } #endif @@ -968,13 +963,29 @@ void decouple(chanend c_mix_out, /* Read datalength from buffer */ read_via_xc_ptr(datalength, released_buffer); - + + //printintln(datalength); + /* Ignore bad small packets */ if ((datalength >= (g_numUsbChanOut * slotSize)) && (released_buffer == g_aud_from_host_wrptr)) { + +#if 0 + for(int i = 0; i < (datalength+4); i++) + { + unsigned samp; + read_byte_via_xc_ptr(samp, aud_from_host_wrptr); + aud_from_host_wrptr+=1; + printint(i); + printhexln(samp); + } +#endif /* Move the write pointer of the fifo on - round up to nearest word */ aud_from_host_wrptr = aud_from_host_wrptr + ((datalength+3)&~0x3) + 4; + + + /* Wrap pointer */ if (aud_from_host_wrptr >= aud_from_host_fifo_end) { @@ -997,7 +1008,7 @@ void decouple(chanend c_mix_out, if (space_left <= 0 || space_left >= MAX_USB_AUD_PACKET_SIZE) { SET_SHARED_GLOBAL(g_aud_from_host_buffer, aud_from_host_wrptr); - XUD_SetReady_OutPtr(aud_from_host_usb_ep, aud_from_host_wrptr); + XUD_SetReady_OutPtr(aud_from_host_usb_ep, aud_from_host_wrptr+4); } else { @@ -1024,6 +1035,7 @@ void decouple(chanend c_mix_out, /* Come out of OUT overflow state */ outOverflow = 0; SET_SHARED_GLOBAL(g_aud_from_host_buffer, aud_from_host_wrptr); + printintln(1); //XUD_SetReady(aud_from_host_usb_ep, 1); #ifdef DEBUG_LEDS led(c_led); @@ -1115,121 +1127,6 @@ void decouple(chanend c_mix_out, } #endif // INPUT -#ifdef MIDI - /* Check if buffer() has send MIDI packet to host */ - GET_SHARED_GLOBAL(midi_to_host_flag, g_midi_to_host_flag); - if (midi_to_host_flag) - { - /* The buffer has been sent to the host, so we can ack the midi thread */ - SET_SHARED_GLOBAL(g_midi_to_host_flag, 0); - if (midi_data_collected_from_device != 0) - { - /* We have some more data to send set the amount of data to send */ - write_via_xc_ptr(midi_to_host_buffer_being_collected, midi_data_collected_from_device); - - /* Swap the collecting and sending buffer */ - swap(midi_to_host_buffer_being_collected, midi_to_host_buffer_being_sent); - - /* Request to send packet */ - XUD_SetReady_InPtr(midi_to_host_usb_ep, midi_to_host_buffer_being_sent+4, midi_data_collected_from_device); - - /* Mark as waiting for host to poll us */ - midi_waiting_on_send_to_host = 1; - /* Reset the collected data count */ - midi_data_collected_from_device = 0; - } - else - { - midi_waiting_on_send_to_host = 0; - } - continue; - } - else - { - /* Check if host has sent us MIDI OUT data */ - GET_SHARED_GLOBAL(midi_from_host_flag, g_midi_from_host_flag); - if (midi_from_host_flag) - { - /* The buffer() thread has filled up a buffer */ - /* Reset flag */ - SET_SHARED_GLOBAL(g_midi_from_host_flag, 0); - - /* Read length from buffer[0] */ - read_via_xc_ptr(midi_data_remaining_to_device, midi_from_host_buffer); - - //printintln(midi_data_remaining_to_device); - - /* Increment read pointer - buffer[0] is length */ - midi_from_host_rdptr = midi_from_host_buffer + 4; - - if (midi_data_remaining_to_device) - { - read_via_xc_ptr(datum, midi_from_host_rdptr); - outuint(c_midi, datum); - //printhexln(datum); - midi_from_host_rdptr += 4; - midi_data_remaining_to_device -= 4; - } - } - } - - select - { - /* Received word from MIDI thread - Check for ACK or Data */ - case midi_get_ack_or_data(c_midi, is_ack, datum): - if (is_ack) - { - /* An ack from the midi/uart thread means it has accepted some data we sent it - * we are okay to send another word */ - if (midi_data_remaining_to_device == 0) - { - /* We have read an entire packet - Mark ready to receive another */ - XUD_SetReady_OutPtr(midi_from_host_usb_ep, midi_from_host_buffer+4); - } - else - { - /* Read another word from the fifo and output it to MIDI thread */ - read_via_xc_ptr(datum, midi_from_host_rdptr); - outuint(c_midi, datum); - midi_from_host_rdptr += 4; - midi_data_remaining_to_device -= 4; - } - } - else - { - /* The midi/uart thread has sent us some data - handshake back */ - midi_send_ack(c_midi); - if (midi_data_collected_from_device < MIDI_USB_BUFFER_TO_HOST_SIZE) - { - /* There is room in the collecting buffer for the data */ - xc_ptr p = (midi_to_host_buffer_being_collected + 4) + midi_data_collected_from_device; - // Add data to the buffer - write_via_xc_ptr(p, datum); - midi_data_collected_from_device += 4; - } - else - { - // Too many events from device - drop - } - - // If we are not sending data to the host then initiate it - if (!midi_waiting_on_send_to_host) - { - write_via_xc_ptr(midi_to_host_buffer_being_collected, midi_data_collected_from_device); - - swap(midi_to_host_buffer_being_collected, midi_to_host_buffer_being_sent); - - // Signal other side to swap - XUD_SetReady_InPtr(midi_to_host_usb_ep, midi_to_host_buffer_being_sent+4, midi_data_collected_from_device); - midi_data_collected_from_device = 0; - midi_waiting_on_send_to_host = 1; - } - } - break; - default: - break; - } -#endif // MIDI #ifdef IAP GET_SHARED_GLOBAL(iap_reset, g_iap_reset); diff --git a/module_usb_aud_shared/usb_buffer/usb_buffer.h b/module_usb_aud_shared/usb_buffer/usb_buffer.h index 4fac7b0a..e5cba64d 100644 --- a/module_usb_aud_shared/usb_buffer/usb_buffer.h +++ b/module_usb_aud_shared/usb_buffer/usb_buffer.h @@ -22,6 +22,7 @@ void buffer(chanend c_aud_out, #ifdef MIDI chanend c_midi_from_host, chanend c_midi_to_host, + chanend c_midi, #endif #ifdef IAP #error IAP diff --git a/module_usb_aud_shared/usb_buffer/usb_buffer.xc b/module_usb_aud_shared/usb_buffer/usb_buffer.xc index 4355f418..5408c570 100644 --- a/module_usb_aud_shared/usb_buffer/usb_buffer.xc +++ b/module_usb_aud_shared/usb_buffer/usb_buffer.xc @@ -18,6 +18,8 @@ #include "clockcmds.h" #include "xud.h" #include "testct_byref.h" + +int pktCount = 0; XUD_ep XUD_Init_Ep(chanend c_ep); // @@ -55,6 +57,13 @@ static inline void swap(xc_ptr &a, xc_ptr &b) } #endif +#ifdef MIDI +unsigned int g_midi_to_host_buffer_A[MAX_USB_MIDI_PACKET_SIZE/4+4]; +unsigned int g_midi_to_host_buffer_B[MAX_USB_MIDI_PACKET_SIZE/4+4]; +int g_midi_from_host_buffer[MAX_USB_MIDI_PACKET_SIZE/4+4]; +#endif + + unsigned char fb_clocks[16]; //#define FB_TOLERANCE_TEST @@ -72,7 +81,8 @@ extern unsigned g_numUsbChanIn; void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud_fb, #ifdef MIDI chanend c_midi_from_host, - chanend c_midi_to_host, + chanend c_midi_to_host, + chanend c_midi, #endif #ifdef IAP chanend c_iap_from_host, @@ -132,6 +142,19 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud xc_ptr midi_from_host_buffer = 0; xc_ptr midi_to_host_buffer = 0; xc_ptr midi_to_host_waiting_buffer = 0; + + xc_ptr midi_from_host_rdptr; + xc_ptr midi_to_host_buffer_being_sent = array_to_xc_ptr(g_midi_to_host_buffer_A); + xc_ptr midi_to_host_buffer_being_collected = array_to_xc_ptr(g_midi_to_host_buffer_B); + + + int is_ack; + unsigned int datum; + int midi_data_remaining_to_device = 0; + int midi_data_collected_from_device = 0; + int midi_waiting_on_send_to_host = 0; + int midi_to_host_flag = 0; + int midi_from_host_flag = 0; #endif xc_ptr p_inZeroBuff = array_to_xc_ptr(inZeroBuff); @@ -163,7 +186,10 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud int frameTime; while(usb_speed == 0) + { GET_SHARED_GLOBAL(usb_speed, g_curUsbSpeed); + //printintln(usb_speed); + } GetADCCounts(DEFAULT_FREQ, min, mid, max); asm("stw %0, dp[g_speed]"::"r"(mid << 16)); @@ -189,10 +215,12 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud // pass the midi->XUD chanends to decouple so that thread can // initialize comm with XUD - asm("stw %0, dp[midi_to_host_usb_ep]"::"r"(ep_midi_to_host)); - asm("stw %0, dp[midi_from_host_usb_ep]"::"r"(ep_midi_from_host)); + //asm("stw %0, dp[midi_to_host_usb_ep]"::"r"(ep_midi_to_host)); + //asm("stw %0, dp[midi_from_host_usb_ep]"::"r"(ep_midi_from_host)); swap(midi_to_host_buffer, midi_to_host_waiting_buffer); - SET_SHARED_GLOBAL(g_midi_from_host_flag, 1); + //SET_SHARED_GLOBAL(g_midi_from_host_flag, 1); + + #endif #ifdef IAP @@ -230,9 +258,7 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud if (usb_speed == XUD_SPEED_HS) { - XUD_SetReady_In(ep_aud_fb, fb_clocks, 4); - } else { @@ -241,6 +267,10 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud } } +#ifdef MIDI + XUD_SetReady_OutPtr(ep_midi_from_host, midi_from_host_buffer); +#endif + while(1) { @@ -439,10 +469,6 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud /* DEVICE -> HOST */ case XUD_SetData_Select(c_aud_in, ep_aud_in, tmp): { - //XUD_SetData_Inline(ep_aud_in, c_aud_in); - - //XUD_SetNotReady(ep_aud_in); - /* Inform stream that buffer sent */ SET_SHARED_GLOBAL(g_aud_to_host_flag, bufferIn+1); } @@ -476,41 +502,84 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud /* Audio HOST -> DEVICE */ case XUD_GetData_Select(c_aud_out, ep_aud_out, tmp): - + { + unsigned samp; asm("#h->d aud data"); + pktCount++; + + if(pktCount ==3) + { + // asm("ecallf %0"::"r"(0)); + } + GET_SHARED_GLOBAL(aud_from_host_buffer, g_aud_from_host_buffer); - + + //printintln(tmp); +#if 0 + for(int i = 0; i < (tmp); i++) + { + read_byte_via_xc_ptr(samp, aud_from_host_buffer); + aud_from_host_buffer+=1; + printint(i); + printhexln(samp); + } +#endif write_via_xc_ptr(aud_from_host_buffer, tmp); /* Sync with audio thread */ SET_SHARED_GLOBAL(g_aud_from_host_flag, 1); - + } break; #endif - #ifdef MIDI case XUD_GetData_Select(c_midi_from_host, ep_midi_from_host, tmp): asm("#midi h->d"); /* Get buffer data from host - MIDI OUT from host always into a single buffer */ /* Write datalength (tmp) into buffer[0], data stored in buffer[4] onwards */ - write_via_xc_ptr(midi_from_host_buffer, tmp); + midi_data_remaining_to_device = tmp; - /* release the buffer */ - SET_SHARED_GLOBAL(g_midi_from_host_flag, 1); - + /* Increment read pointer - buffer[0] is length */ + midi_from_host_rdptr = midi_from_host_buffer + 4; + + if (midi_data_remaining_to_device) + { + read_via_xc_ptr(datum, midi_from_host_rdptr); + outuint(c_midi, datum); + midi_from_host_rdptr += 4; + midi_data_remaining_to_device -= 4; + } break; /* MIDI IN to host */ case XUD_SetData_Select(c_midi_to_host, ep_midi_to_host, tmp): asm("#midi d->h"); - // Ack the decouple thread to say it has been sent to host - SET_SHARED_GLOBAL(g_midi_to_host_flag, 1); - swap(midi_to_host_buffer, midi_to_host_waiting_buffer); + /* The buffer has been sent to the host, so we can ack the midi thread */ + if (midi_data_collected_from_device != 0) + { + /* We have some more data to send set the amount of data to send */ + write_via_xc_ptr(midi_to_host_buffer_being_collected, midi_data_collected_from_device); + + /* Swap the collecting and sending buffer */ + swap(midi_to_host_buffer_being_collected, midi_to_host_buffer_being_sent); + + /* Request to send packet */ + XUD_SetReady_InPtr(ep_midi_to_host, midi_to_host_buffer_being_sent+4, midi_data_collected_from_device); + + /* Mark as waiting for host to poll us */ + midi_waiting_on_send_to_host = 1; + /* Reset the collected data count */ + midi_data_collected_from_device = 0; + } + else + { + midi_waiting_on_send_to_host = 0; + } + break; #endif @@ -623,15 +692,73 @@ void buffer(register chanend c_aud_out, register chanend c_aud_in, chanend c_aud #ifdef HID_CONTROLS /* HID Report Data */ - case inuint_byref(c_hid, tmp): + case XUD_SetData_Select(c_hid, ep_hid, tmp): { - //XUD_SetData_Inline(ep_hid, c_hid); - asm("stw %0, dp[g_hidFlag]" :: "r" (0) ); - - //XUD_SetNotReady(ep_hid); } break; +#endif + +#ifdef MIDI +//select + // { + /* Received word from MIDI thread - Check for ACK or Data */ + case midi_get_ack_or_data(c_midi, is_ack, datum): + if (is_ack) + { + /* An ack from the midi/uart thread means it has accepted some data we sent it + * we are okay to send another word */ + if (midi_data_remaining_to_device == 0) + { + /* We have read an entire packet - Mark ready to receive another */ + XUD_SetReady_OutPtr(ep_midi_from_host, midi_from_host_buffer+4); + } + else + { + /* Read another word from the fifo and output it to MIDI thread */ + read_via_xc_ptr(datum, midi_from_host_rdptr); + outuint(c_midi, datum); + midi_from_host_rdptr += 4; + midi_data_remaining_to_device -= 4; + } + } + else + { + /* The midi/uart thread has sent us some data - handshake back */ + midi_send_ack(c_midi); + if (midi_data_collected_from_device < MIDI_USB_BUFFER_TO_HOST_SIZE) + { + /* There is room in the collecting buffer for the data */ + xc_ptr p = (midi_to_host_buffer_being_collected + 4) + midi_data_collected_from_device; + // Add data to the buffer + write_via_xc_ptr(p, datum); + midi_data_collected_from_device += 4; + } + else + { + // Too many events from device - drop + } + + // If we are not sending data to the host then initiate it + if (!midi_waiting_on_send_to_host) + { + write_via_xc_ptr(midi_to_host_buffer_being_collected, midi_data_collected_from_device); + + swap(midi_to_host_buffer_being_collected, midi_to_host_buffer_being_sent); + + // Signal other side to swap + XUD_SetReady_InPtr(ep_midi_to_host, midi_to_host_buffer_being_sent+4, midi_data_collected_from_device); + midi_data_collected_from_device = 0; + midi_waiting_on_send_to_host = 1; + } + } + break; + // default: + // break; + //} + + + #endif }