Skip to content

Commit 08509e7

Browse files
authored
Merge pull request #3339 from jamike/USBAudio_CB_RX_TX
USB audio callback rx and tx
2 parents 92fbad7 + 71f570f commit 08509e7

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

features/unsupported/USBDevice/USBAudio/USBAudio.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ bool USBAudio::write(uint8_t * buf) {
113113
return true;
114114
}
115115

116+
void USBAudio::writeSync(uint8_t *buf)
117+
{
118+
USBDevice::writeNB(EP3IN, buf, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
119+
}
120+
121+
uint32_t USBAudio::readSync(uint8_t *buf)
122+
{
123+
uint32_t size = 0;
124+
USBDevice::readEP(EP3OUT, (uint8_t *)buf, &size, PACKET_SIZE_ISO_IN);
125+
return size;
126+
}
116127

117128
float USBAudio::getVolume() {
118129
return (mute) ? 0.0 : volume;
@@ -127,6 +138,10 @@ bool USBAudio::EPISO_OUT_callback() {
127138
available = true;
128139
buf_stream_in = NULL;
129140
}
141+
else {
142+
if (rxDone)
143+
rxDone.call();
144+
}
130145
readStart(EP3OUT, PACKET_SIZE_ISO_IN);
131146
return false;
132147
}
@@ -135,6 +150,8 @@ bool USBAudio::EPISO_OUT_callback() {
135150
bool USBAudio::EPISO_IN_callback() {
136151
interruptIN = true;
137152
writeIN = true;
153+
if (txDone)
154+
txDone.call();
138155
return true;
139156
}
140157

features/unsupported/USBDevice/USBAudio/USBAudio.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ class USBAudio: public USBDevice {
107107
*/
108108
bool readNB(uint8_t * buf);
109109

110+
/**
111+
* read last received packet if some.
112+
* @param buf pointer on a buffer which will be filled if an audio packet is available
113+
*
114+
* @returns the packet length
115+
*/
116+
uint32_t readSync(uint8_t *buf);
117+
110118
/**
111119
* 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.
112120
*
@@ -115,6 +123,12 @@ class USBAudio: public USBDevice {
115123
*/
116124
bool write(uint8_t * buf);
117125

126+
/**
127+
* Write packet in endpoint fifo. assuming tx fifo is empty
128+
* @param buf pointer on the audio packet which will be sent
129+
*/
130+
void writeSync(uint8_t *buf);
131+
118132
/**
119133
* Write and read an audio packet at the same time (on the same frame)
120134
*
@@ -133,6 +147,22 @@ class USBAudio: public USBDevice {
133147
void attach(void(*fptr)(void)) {
134148
updateVol.attach(fptr);
135149
}
150+
/** attach a handler to Tx Done
151+
*
152+
* @param function Function to attach
153+
*
154+
*/
155+
void attachTx(void(*fptr)(void)) {
156+
txDone.attach(fptr);
157+
}
158+
/** attach a handler to Rx Done
159+
*
160+
* @param function Function to attach
161+
*
162+
*/
163+
void attachRx(void(*fptr)(void)) {
164+
rxDone.attach(fptr);
165+
}
136166

137167
/** Attach a nonstatic void/void member function to update the volume
138168
*
@@ -144,6 +174,14 @@ class USBAudio: public USBDevice {
144174
void attach(T *tptr, void(T::*mptr)(void)) {
145175
updateVol.attach(tptr, mptr);
146176
}
177+
template<typename T>
178+
void attachTx(T *tptr, void(T::*mptr)(void)) {
179+
txDone.attach(tptr, mptr);
180+
}
181+
template<typename T>
182+
void attachRx(T *tptr, void(T::*mptr)(void)) {
183+
rxDone.attach(tptr, mptr);
184+
}
147185

148186

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

318+
// callback transmit Done
319+
Callback<void()> txDone;
320+
// callback transmit Done
321+
Callback<void()> rxDone;
322+
280323
// boolean showing that the SOF handler has been called. Useful for readNB.
281324
volatile bool SOF_handler;
282325

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Playback example with the USBAUDIO library
2+
3+
#include "mbed.h"
4+
#include "USBAudio.h"
5+
6+
// frequency: 48 kHz
7+
#define FREQ_SPK 16000
8+
#define FREQ_MIC 16000
9+
10+
// 2channels: stereo
11+
#define NB_CHA_SPK 2
12+
#define NB_CHA_MIC 2
13+
14+
// length computed: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there are two channels, the length will be 48 * 2 * 2
15+
#define LENGTH_AUDIO_PACKET_SPK (FREQ_SPK / 500) * NB_CHA_SPK
16+
#define LENGTH_AUDIO_PACKET_MIC (FREQ_MIC / 500) * NB_CHA_MIC
17+
18+
// USBAudio object
19+
USBAudio audio(FREQ_SPK, NB_CHA_SPK, FREQ_MIC, NB_CHA_MIC, 0xab45, 0x0378);
20+
int filled;
21+
int ready = 2;
22+
23+
/* buffer 4 packets to avoid */
24+
int buf[4][LENGTH_AUDIO_PACKET_SPK/sizeof(int)];
25+
void tx_audio(void)
26+
{
27+
/* used some buffer in advance */
28+
ready = (filled+2)&0x3;
29+
audio.writeSync((uint8_t *)buf[ready]);
30+
}
31+
32+
33+
void rx_audio(void)
34+
{
35+
int size=0;
36+
int next = (filled + 1)&0x3;
37+
size = audio.readSync((uint8_t *)buf[next]);
38+
if (size ) filled = next;
39+
if ((size) && (size!=LENGTH_AUDIO_PACKET_MIC)) printf("%d\n",size);
40+
}
41+
42+
int main()
43+
{
44+
filled = 0;
45+
memset(&buf[0][0], 0, sizeof (buf));
46+
audio.attachTx(tx_audio);
47+
audio.attachRx(rx_audio);
48+
/* start the tx with a silent packet */
49+
audio.write((uint8_t *)buf[2]);
50+
while(1);
51+
}

tools/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,11 @@
10041004
"source_dir": join(TEST_DIR, "usb", "device", "audio"),
10051005
"dependencies": [MBED_LIBRARIES, USB_LIBRARIES],
10061006
},
1007+
{
1008+
"id": "USB_8", "description": "AUDIO_CB",
1009+
"source_dir": join(TEST_DIR, "usb", "device", "audio_cb"),
1010+
"dependencies": [MBED_LIBRARIES, USB_LIBRARIES],
1011+
},
10071012

10081013
# CMSIS DSP
10091014
{

0 commit comments

Comments
 (0)