diff --git a/module_usb_aud_shared/usb_buffer/decouple.xc b/module_usb_aud_shared/usb_buffer/decouple.xc index fd59d611..9e9640d0 100644 --- a/module_usb_aud_shared/usb_buffer/decouple.xc +++ b/module_usb_aud_shared/usb_buffer/decouple.xc @@ -675,6 +675,8 @@ void decouple(chanend c_mix_out, int iap_waiting_on_send_to_host = 0; int iap_to_host_flag = 0; int iap_from_host_flag = 0; + int iap_expecting_length = 1; + int iap_expecting_data_length = 0; #endif int t = array_to_xc_ptr(outAudioBuff); @@ -1168,6 +1170,7 @@ void decouple(chanend c_mix_out, if (iap_reset) { iap_send_reset(c_iap); // What if this happen in the middle of a send/ack? iap_reset = 0; + iap_expecting_length = 1; SET_SHARED_GLOBAL(g_iap_reset, iap_reset); // Reset has been signalled } // Need to handle sending ZLP on iap_to_host_usb_ep_int to signal iOS device to collect from bulk endpoint. @@ -1209,30 +1212,30 @@ void decouple(chanend c_mix_out, /* The buffer() thread has filled up a buffer */ /* Reset flag */ SET_SHARED_GLOBAL(g_iap_from_host_flag, 0); - + /* Read length from buffer[0] */ read_via_xc_ptr(iap_data_remaining_to_device, iap_from_host_buffer); // Send length first so iAP thread knows how much data to expect // Don't expect ack from this to make it simpler outuint(c_iap, iap_data_remaining_to_device); - + /* Increment read pointer - buffer[0] is length */ iap_from_host_rdptr = iap_from_host_buffer + 4; - + if (iap_data_remaining_to_device) { read_byte_via_xc_ptr(datum_iap, iap_from_host_rdptr); outuint(c_iap, datum_iap); iap_from_host_rdptr += 1; iap_data_remaining_to_device -= 1; - } + } } } - + select - { - /* Received word from iap thread - Check for ACK or Data */ + { + /* Received word from iap thread - Check for ACK or Data */ case iap_get_ack_or_reset_or_data(c_iap, is_ack_iap, is_reset, datum_iap): if (is_ack_iap) { @@ -1241,7 +1244,7 @@ void decouple(chanend c_mix_out, if (iap_data_remaining_to_device == 0) { /* We have read an entire packet - Mark ready to receive another */ - XUD_SetReady(iap_from_host_usb_ep, 1); + XUD_SetReady(iap_from_host_usb_ep, 1); } else { @@ -1250,40 +1253,46 @@ void decouple(chanend c_mix_out, outuint(c_iap, datum_iap); iap_from_host_rdptr += 1; iap_data_remaining_to_device -= 1; - } + } } - else + else { /* The iap/uart thread has sent us some data - handshake back */ iap_send_ack(c_iap); - if (iap_data_collected_from_device < IAP_USB_BUFFER_TO_HOST_SIZE) - { - /* There is room in the collecting buffer for the data */ - xc_ptr p = (iap_to_host_buffer_being_collected + 4) + iap_data_collected_from_device; - // Add data to the buffer - write_byte_via_xc_ptr(p, datum_iap); - iap_data_collected_from_device += 1; + if (iap_expecting_length) { + iap_expecting_data_length = datum_iap; + iap_expecting_length = 0; + } else { + if (iap_data_collected_from_device < IAP_USB_BUFFER_TO_HOST_SIZE) + { + /* There is room in the collecting buffer for the data */ + xc_ptr p = (iap_to_host_buffer_being_collected + 4) + iap_data_collected_from_device; + // Add data to the buffer + write_byte_via_xc_ptr(p, datum_iap); + iap_data_collected_from_device += 1; + } + else + { + // Too many events from device - drop + } + + // If we are not sending data to the host then initiate it (ONLY IF GOT WHOLE MESSAGE) + if (!iap_waiting_on_send_to_host && (iap_data_collected_from_device == iap_expecting_data_length)) + { + // Set first element of buffer to length i.e. iap_to_host_buffer_being_collected[0] = iap_data_collected_from_device; + write_via_xc_ptr(iap_to_host_buffer_being_collected, iap_data_collected_from_device); + + swap(iap_to_host_buffer_being_collected, iap_to_host_buffer_being_sent); + + // Signal other side to swap + XUD_SetReady_In(iap_to_host_int_usb_ep, 0, zero_buffer, 0); + XUD_SetReady_In(iap_to_host_usb_ep, 0, iap_to_host_buffer_being_sent+4, iap_data_collected_from_device); + iap_data_collected_from_device = 0; + iap_waiting_on_send_to_host = 1; + iap_expecting_length = 1; + } } - else - { - // Too many events from device - drop - } - - // If we are not sending data to the host then initiate it - if (!iap_waiting_on_send_to_host) - { - // Set first element of buffer to length i.e. iap_to_host_buffer_being_collected[0] = iap_data_collected_from_device; - write_via_xc_ptr(iap_to_host_buffer_being_collected, iap_data_collected_from_device); - - swap(iap_to_host_buffer_being_collected, iap_to_host_buffer_being_sent); - - // Signal other side to swap - XUD_SetReady_In(iap_to_host_int_usb_ep, 0, zero_buffer, 0); - XUD_SetReady_In(iap_to_host_usb_ep, 0, iap_to_host_buffer_being_sent+4, iap_data_collected_from_device); - iap_data_collected_from_device = 0; - iap_waiting_on_send_to_host = 1; - } - } + } break; default: break;