Skip to content

Commit

Permalink
Merge pull request ARMmbed#3339 from jamike/USBAudio_CB_RX_TX
Browse files Browse the repository at this point in the history
USB audio callback  rx and tx
  • Loading branch information
0xc0170 committed Dec 21, 2016
2 parents 92fbad7 + 71f570f commit 08509e7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
17 changes: 17 additions & 0 deletions features/unsupported/USBDevice/USBAudio/USBAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ bool USBAudio::write(uint8_t * buf) {
return true;
}

void USBAudio::writeSync(uint8_t *buf)
{
USBDevice::writeNB(EP3IN, buf, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
}

uint32_t USBAudio::readSync(uint8_t *buf)
{
uint32_t size = 0;
USBDevice::readEP(EP3OUT, (uint8_t *)buf, &size, PACKET_SIZE_ISO_IN);
return size;
}

float USBAudio::getVolume() {
return (mute) ? 0.0 : volume;
Expand All @@ -127,6 +138,10 @@ bool USBAudio::EPISO_OUT_callback() {
available = true;
buf_stream_in = NULL;
}
else {
if (rxDone)
rxDone.call();
}
readStart(EP3OUT, PACKET_SIZE_ISO_IN);
return false;
}
Expand All @@ -135,6 +150,8 @@ bool USBAudio::EPISO_OUT_callback() {
bool USBAudio::EPISO_IN_callback() {
interruptIN = true;
writeIN = true;
if (txDone)
txDone.call();
return true;
}

Expand Down
43 changes: 43 additions & 0 deletions features/unsupported/USBDevice/USBAudio/USBAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ class USBAudio: public USBDevice {
*/
bool readNB(uint8_t * buf);

/**
* read last received packet if some.
* @param buf pointer on a buffer which will be filled if an audio packet is available
*
* @returns the packet length
*/
uint32_t readSync(uint8_t *buf);

/**
* Write an audio packet. During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
*
Expand All @@ -115,6 +123,12 @@ class USBAudio: public USBDevice {
*/
bool write(uint8_t * buf);

/**
* Write packet in endpoint fifo. assuming tx fifo is empty
* @param buf pointer on the audio packet which will be sent
*/
void writeSync(uint8_t *buf);

/**
* Write and read an audio packet at the same time (on the same frame)
*
Expand All @@ -133,6 +147,22 @@ class USBAudio: public USBDevice {
void attach(void(*fptr)(void)) {
updateVol.attach(fptr);
}
/** attach a handler to Tx Done
*
* @param function Function to attach
*
*/
void attachTx(void(*fptr)(void)) {
txDone.attach(fptr);
}
/** attach a handler to Rx Done
*
* @param function Function to attach
*
*/
void attachRx(void(*fptr)(void)) {
rxDone.attach(fptr);
}

/** Attach a nonstatic void/void member function to update the volume
*
Expand All @@ -144,6 +174,14 @@ class USBAudio: public USBDevice {
void attach(T *tptr, void(T::*mptr)(void)) {
updateVol.attach(tptr, mptr);
}
template<typename T>
void attachTx(T *tptr, void(T::*mptr)(void)) {
txDone.attach(tptr, mptr);
}
template<typename T>
void attachRx(T *tptr, void(T::*mptr)(void)) {
rxDone.attach(tptr, mptr);
}


protected:
Expand Down Expand Up @@ -277,6 +315,11 @@ class USBAudio: public USBDevice {
// callback to update volume
Callback<void()> updateVol;

// callback transmit Done
Callback<void()> txDone;
// callback transmit Done
Callback<void()> rxDone;

// boolean showing that the SOF handler has been called. Useful for readNB.
volatile bool SOF_handler;

Expand Down
51 changes: 51 additions & 0 deletions features/unsupported/tests/usb/device/audio_cb/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Playback example with the USBAUDIO library

#include "mbed.h"
#include "USBAudio.h"

// frequency: 48 kHz
#define FREQ_SPK 16000
#define FREQ_MIC 16000

// 2channels: stereo
#define NB_CHA_SPK 2
#define NB_CHA_MIC 2

// length computed: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there are two channels, the length will be 48 * 2 * 2
#define LENGTH_AUDIO_PACKET_SPK (FREQ_SPK / 500) * NB_CHA_SPK
#define LENGTH_AUDIO_PACKET_MIC (FREQ_MIC / 500) * NB_CHA_MIC

// USBAudio object
USBAudio audio(FREQ_SPK, NB_CHA_SPK, FREQ_MIC, NB_CHA_MIC, 0xab45, 0x0378);
int filled;
int ready = 2;

/* buffer 4 packets to avoid */
int buf[4][LENGTH_AUDIO_PACKET_SPK/sizeof(int)];
void tx_audio(void)
{
/* used some buffer in advance */
ready = (filled+2)&0x3;
audio.writeSync((uint8_t *)buf[ready]);
}


void rx_audio(void)
{
int size=0;
int next = (filled + 1)&0x3;
size = audio.readSync((uint8_t *)buf[next]);
if (size ) filled = next;
if ((size) && (size!=LENGTH_AUDIO_PACKET_MIC)) printf("%d\n",size);
}

int main()
{
filled = 0;
memset(&buf[0][0], 0, sizeof (buf));
audio.attachTx(tx_audio);
audio.attachRx(rx_audio);
/* start the tx with a silent packet */
audio.write((uint8_t *)buf[2]);
while(1);
}
5 changes: 5 additions & 0 deletions tools/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,11 @@
"source_dir": join(TEST_DIR, "usb", "device", "audio"),
"dependencies": [MBED_LIBRARIES, USB_LIBRARIES],
},
{
"id": "USB_8", "description": "AUDIO_CB",
"source_dir": join(TEST_DIR, "usb", "device", "audio_cb"),
"dependencies": [MBED_LIBRARIES, USB_LIBRARIES],
},

# CMSIS DSP
{
Expand Down

0 comments on commit 08509e7

Please sign in to comment.