adds a USB serial port to your design

Dependents:   Example_WatchDog_Timer

Revision:
70:2c525a50f1b6
Parent:
47:a0cd9646ecd1
diff -r c5e178adb138 -r 2c525a50f1b6 USBAudio/USBAudio.cpp
--- a/USBAudio/USBAudio.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBAudio/USBAudio.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -85,7 +85,7 @@
     SOF_handler = false;
     writeIN = false;
     if (interruptIN) {
-        USBDevice::writeNB(EP3IN, buf_write, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
+        USBDevice::writeNB(EPISO_IN, buf_write, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
     } else {
         buf_stream_out = buf_write;
     }
@@ -102,7 +102,7 @@
     writeIN = false;
     SOF_handler = false;
     if (interruptIN) {
-        USBDevice::writeNB(EP3IN, buf, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
+        USBDevice::writeNB(EPISO_IN, buf, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
     } else {
         buf_stream_out = buf;
     }
@@ -113,6 +113,21 @@
     return true;
 }
 
+void USBAudio::writeSync(uint8_t *buf, AudioSampleCorrectType jitter_nb)
+{
+    if ((jitter_nb != RemoveOneSample) && (jitter_nb != AddOneSample)) {
+        jitter_nb = NoCorrection;
+    }
+    /* each sample is 2 bytes */
+    USBDevice::writeNB(EPISO_IN, buf, PACKET_SIZE_ISO_OUT + jitter_nb *(this->channel_nb_out*2), PACKET_SIZE_ISO_OUT+this->channel_nb_out*2);
+}
+
+uint32_t USBAudio::readSync(uint8_t *buf)
+{
+    uint32_t size = 0;
+    USBDevice::readEP(EPISO_OUT, (uint8_t *)buf, &size, PACKET_SIZE_ISO_IN);
+    return size;
+}
 
 float USBAudio::getVolume() {
     return (mute) ? 0.0 : volume;
@@ -123,11 +138,15 @@
     uint32_t size = 0;
     interruptOUT = true;
     if (buf_stream_in != NULL) {
-        readEP(EP3OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN);
+        readEP(EPISO_OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN);
         available = true;
         buf_stream_in = NULL;
     }
-    readStart(EP3OUT, PACKET_SIZE_ISO_IN);
+    else  {
+        if (rxDone)
+            rxDone.call();
+    }
+    readStart(EPISO_OUT, PACKET_SIZE_ISO_IN);
     return false;
 }
 
@@ -135,6 +154,8 @@
 bool USBAudio::EPISO_IN_callback() {
     interruptIN = true;
     writeIN = true;
+    if (txDone) 
+        txDone.call();
     return true;
 }
 
@@ -147,10 +168,10 @@
     if (!interruptOUT) {
         // read the isochronous endpoint
         if (buf_stream_in != NULL) {
-            if (USBDevice::readEP_NB(EP3OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN)) {
+            if (USBDevice::readEP_NB(EPISO_OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN)) {
                 if (size) {
                     available = true;
-                    readStart(EP3OUT, PACKET_SIZE_ISO_IN);
+                    readStart(EPISO_OUT, PACKET_SIZE_ISO_IN);
                     buf_stream_in = NULL;
                 }
             }
@@ -160,7 +181,7 @@
     if (!interruptIN) {
         // write if needed
         if (buf_stream_out != NULL) {
-            USBDevice::writeNB(EP3IN, (uint8_t *)buf_stream_out, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
+            USBDevice::writeNB(EPISO_IN, (uint8_t *)buf_stream_out, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
             buf_stream_out = NULL;
         }
     }
@@ -177,11 +198,11 @@
     }
 
     // Configure isochronous endpoint
-    realiseEndpoint(EP3OUT, PACKET_SIZE_ISO_IN, ISOCHRONOUS);
-    realiseEndpoint(EP3IN, PACKET_SIZE_ISO_OUT, ISOCHRONOUS);
+    realiseEndpoint(EPISO_OUT, PACKET_SIZE_ISO_IN, ISOCHRONOUS);
+    realiseEndpoint(EPISO_IN, PACKET_SIZE_ISO_OUT+this->channel_nb_out*2, ISOCHRONOUS);
 
     // activate readings on this endpoint
-    readStart(EP3OUT, PACKET_SIZE_ISO_IN);
+    readStart(EPISO_OUT, PACKET_SIZE_ISO_IN);
     return true;
 }
 
@@ -313,7 +334,8 @@
                 switch (transfer->setup.bRequest) {
                     case REQUEST_SET_CUR:
                         mute = data & 0xff;
-                        updateVol.call();
+                        if (updateVol)
+                            updateVol.call();
                         break;
                     default:
                         break;
@@ -324,7 +346,8 @@
                     case REQUEST_SET_CUR:
                         volCur = data;
                         volume = (float)volCur/(float)volMax;
-                        updateVol.call();
+                        if (updateVol)
+                            updateVol.call();
                         break;
                     default:
                         break;
@@ -578,8 +601,8 @@
         ENDPOINT_DESCRIPTOR,                    // bDescriptorType
         PHY_TO_DESC(EPISO_IN),                  // bEndpointAddress
         E_ISOCHRONOUS,                          // bmAttributes
-        (uint8_t)(LSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
-        (uint8_t)(MSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
+        (uint8_t)(LSB(PACKET_SIZE_ISO_OUT+channel_nb_out*2)),                   // wMaxPacketSize
+        (uint8_t)(MSB(PACKET_SIZE_ISO_OUT+channel_nb_out*2)),                   // wMaxPacketSize
         0x01,                                   // bInterval
         0x00,                                   // bRefresh
         0x00,                                   // bSynchAddress