Skip to content

Commit 5c20760

Browse files
committed
Initial commit of the mbed libraries and tools
1 parent 083a956 commit 5c20760

File tree

932 files changed

+270822
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

932 files changed

+270822
-0
lines changed

libraries/USBDevice/USBAudio/USBAudio.cpp

Lines changed: 618 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
/* Copyright (c) 2010-2011 mbed.org, MIT License
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4+
* and associated documentation files (the "Software"), to deal in the Software without
5+
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
6+
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7+
* Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or
10+
* substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
#ifndef USBAudio_H
20+
#define USBAudio_H
21+
22+
/* These headers are included for child class. */
23+
#include "USBEndpoints.h"
24+
#include "USBDescriptor.h"
25+
#include "USBDevice_Types.h"
26+
27+
#include "USBDevice.h"
28+
29+
30+
/**
31+
* USBAudio example
32+
*
33+
* @code
34+
* #include "mbed.h"
35+
* #include "USBAudio.h"
36+
*
37+
* Serial pc(USBTX, USBRX);
38+
*
39+
* // frequency: 48 kHz
40+
* #define FREQ 48000
41+
*
42+
* // 1 channel: mono
43+
* #define NB_CHA 1
44+
*
45+
* // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
46+
* #define AUDIO_LENGTH_PACKET 48 * 2 * 1
47+
*
48+
* // USBAudio
49+
* USBAudio audio(FREQ, NB_CHA);
50+
*
51+
* int main() {
52+
* int16_t buf[AUDIO_LENGTH_PACKET/2];
53+
*
54+
* while (1) {
55+
* // read an audio packet
56+
* audio.read((uint8_t *)buf);
57+
*
58+
*
59+
* // print packet received
60+
* pc.printf("recv: ");
61+
* for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
62+
* pc.printf("%d ", buf[i]);
63+
* }
64+
* pc.printf("\r\n");
65+
* }
66+
* }
67+
* @endcode
68+
*/
69+
class USBAudio: public USBDevice {
70+
public:
71+
72+
/**
73+
* Constructor
74+
*
75+
* @param frequency_in frequency in Hz (default: 48000)
76+
* @param channel_nb_in channel number (1 or 2) (default: 1)
77+
* @param frequency_out frequency in Hz (default: 8000)
78+
* @param channel_nb_out_in channel number (1 or 2) (default: 1)
79+
* @param vendor_id Your vendor_id
80+
* @param product_id Your product_id
81+
* @param product_release Your preoduct_release
82+
*/
83+
USBAudio(uint32_t frequency_in = 48000, uint8_t channel_nb_in = 1, uint32_t frequency_out = 8000, uint8_t channel_nb_out = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
84+
85+
/**
86+
* Get current volume between 0.0 and 1.0
87+
*
88+
* @returns volume
89+
*/
90+
float getVolume();
91+
92+
/**
93+
* Read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Blocking
94+
*
95+
* @param buf pointer on a buffer which will be filled with an audio packet
96+
*
97+
* @returns true if successfull
98+
*/
99+
bool read(uint8_t * buf);
100+
101+
/**
102+
* Try to read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Non Blocking
103+
*
104+
* @param buf pointer on a buffer which will be filled if an audio packet is available
105+
*
106+
* @returns true if successfull
107+
*/
108+
bool readNB(uint8_t * buf);
109+
110+
/**
111+
* 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.
112+
*
113+
* @param buf pointer on the audio packet which will be sent
114+
* @returns true if successful
115+
*/
116+
bool write(uint8_t * buf);
117+
118+
/**
119+
* Write and read an audio packet at the same time (on the same frame)
120+
*
121+
* @param buf_read pointer on a buffer which will be filled with an audio packet
122+
* @param buf_write pointer on the audio packet which will be sent
123+
* @returns true if successful
124+
*/
125+
bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
126+
127+
128+
/** attach a handler to update the volume
129+
*
130+
* @param function Function to attach
131+
*
132+
*/
133+
void attach(void(*fptr)(void)) {
134+
updateVol.attach(fptr);
135+
}
136+
137+
/** Attach a nonstatic void/void member function to update the volume
138+
*
139+
* @param tptr Object pointer
140+
* @param mptr Member function pointer
141+
*
142+
*/
143+
template<typename T>
144+
void attach(T *tptr, void(T::*mptr)(void)) {
145+
updateVol.attach(tptr, mptr);
146+
}
147+
148+
149+
protected:
150+
151+
/*
152+
* Called by USBDevice layer. Set configuration of the device.
153+
* For instance, you can add all endpoints that you need on this function.
154+
*
155+
* @param configuration Number of the configuration
156+
* @returns true if class handles this request
157+
*/
158+
virtual bool USBCallback_setConfiguration(uint8_t configuration);
159+
160+
/*
161+
* Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
162+
* This is used to handle extensions to standard requests
163+
* and class specific requests
164+
*
165+
* @returns true if class handles this request
166+
*/
167+
virtual bool USBCallback_request();
168+
169+
/*
170+
* Get string product descriptor
171+
*
172+
* @returns pointer to the string product descriptor
173+
*/
174+
virtual uint8_t * stringIproductDesc();
175+
176+
/*
177+
* Get string interface descriptor
178+
*
179+
* @returns pointer to the string interface descriptor
180+
*/
181+
virtual uint8_t * stringIinterfaceDesc();
182+
183+
/*
184+
* Get configuration descriptor
185+
*
186+
* @returns pointer to the configuration descriptor
187+
*/
188+
virtual uint8_t * configurationDesc();
189+
190+
/*
191+
* Called by USBDevice layer. Set interface/alternate of the device.
192+
*
193+
* @param interface Number of the interface to be configured
194+
* @param alternate Number of the alternate to be configured
195+
* @returns true if class handles this request
196+
*/
197+
virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
198+
199+
/*
200+
* Called by USBDevice on Endpoint0 request completion
201+
* if the 'notify' flag has been set to true. Warning: Called in ISR context
202+
*
203+
* In this case it is used to indicate that a HID report has
204+
* been received from the host on endpoint 0
205+
*
206+
* @param buf buffer received on endpoint 0
207+
* @param length length of this buffer
208+
*/
209+
virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
210+
211+
/*
212+
* Callback called on each Start of Frame event
213+
*/
214+
virtual void SOF(int frameNumber);
215+
216+
/*
217+
* Callback called when a packet is received
218+
*/
219+
virtual bool EP3_OUT_callback();
220+
221+
/*
222+
* Callback called when a packet has been sent
223+
*/
224+
virtual bool EP3_IN_callback();
225+
226+
private:
227+
228+
// stream available ?
229+
volatile bool available;
230+
231+
// interrupt OUT has been received
232+
volatile bool interruptOUT;
233+
234+
// interrupt IN has been received
235+
volatile bool interruptIN;
236+
237+
// audio packet has been written
238+
volatile bool writeIN;
239+
240+
// FREQ
241+
uint32_t FREQ_OUT;
242+
uint32_t FREQ_IN;
243+
244+
// size of the maximum packet for the isochronous endpoint
245+
uint32_t PACKET_SIZE_ISO_IN;
246+
uint32_t PACKET_SIZE_ISO_OUT;
247+
248+
// mono, stereo,...
249+
uint8_t channel_nb_in;
250+
uint8_t channel_nb_out;
251+
252+
// channel config: master, left, right
253+
uint8_t channel_config_in;
254+
uint8_t channel_config_out;
255+
256+
// mute state
257+
uint8_t mute;
258+
259+
// Volume Current Value
260+
uint16_t volCur;
261+
262+
// Volume Minimum Value
263+
uint16_t volMin;
264+
265+
// Volume Maximum Value
266+
uint16_t volMax;
267+
268+
// Volume Resolution
269+
uint16_t volRes;
270+
271+
// Buffer containing one audio packet (to be read)
272+
volatile uint8_t * buf_stream_in;
273+
274+
// Buffer containing one audio packet (to be written)
275+
volatile uint8_t * buf_stream_out;
276+
277+
// callback to update volume
278+
FunctionPointer updateVol;
279+
280+
// boolean showing that the SOF handler has been called. Useful for readNB.
281+
volatile bool SOF_handler;
282+
283+
volatile float volume;
284+
285+
};
286+
287+
#endif
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright (c) 2010-2011 mbed.org, MIT License
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4+
* and associated documentation files (the "Software"), to deal in the Software without
5+
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
6+
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7+
* Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or
10+
* substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
#ifndef USBAUDIO_TYPES_H
20+
#define USBAUDIO_TYPES_H
21+
22+
23+
#define DEFAULT_CONFIGURATION (1)
24+
25+
// Audio Request Codes
26+
#define REQUEST_SET_CUR 0x01
27+
#define REQUEST_GET_CUR 0x81
28+
#define REQUEST_SET_MIN 0x02
29+
#define REQUEST_GET_MIN 0x82
30+
#define REQUEST_SET_MAX 0x03
31+
#define REQUEST_GET_MAX 0x83
32+
#define REQUEST_SET_RES 0x04
33+
#define REQUEST_GET_RES 0x84
34+
35+
#define MUTE_CONTROL 0x01
36+
#define VOLUME_CONTROL 0x02
37+
38+
39+
// Audio Descriptor Sizes
40+
#define CONTROL_INTERFACE_DESCRIPTOR_LENGTH 0x09
41+
#define STREAMING_INTERFACE_DESCRIPTOR_LENGTH 0x07
42+
#define INPUT_TERMINAL_DESCRIPTOR_LENGTH 0x0C
43+
#define OUTPUT_TERMINAL_DESCRIPTOR_LENGTH 0x09
44+
#define FEATURE_UNIT_DESCRIPTOR_LENGTH 0x09
45+
#define STREAMING_ENDPOINT_DESCRIPTOR_LENGTH 0x07
46+
47+
// Audio Format Type Descriptor Sizes
48+
#define FORMAT_TYPE_I_DESCRIPTOR_LENGTH 0x0b
49+
50+
#define AUDIO_CLASS 0x01
51+
#define SUBCLASS_AUDIOCONTROL 0x01
52+
#define SUBCLASS_AUDIOSTREAMING 0x02
53+
54+
// Audio Descriptor Types
55+
#define INTERFACE_DESCRIPTOR_TYPE 0x24
56+
#define ENDPOINT_DESCRIPTOR_TYPE 0x25
57+
58+
// Audio Control Interface Descriptor Subtypes
59+
#define CONTROL_HEADER 0x01
60+
#define CONTROL_INPUT_TERMINAL 0x02
61+
#define CONTROL_OUTPUT_TERMINAL 0x03
62+
#define CONTROL_FEATURE_UNIT 0x06
63+
64+
// USB Terminal Types
65+
#define TERMINAL_USB_STREAMING 0x0101
66+
67+
// Predefined Audio Channel Configuration Bits
68+
// Mono
69+
#define CHANNEL_M 0x0000
70+
#define CHANNEL_L 0x0001 /* Left Front */
71+
#define CHANNEL_R 0x0002 /* Right Front */
72+
73+
// Feature Unit Control Bits
74+
#define CONTROL_MUTE 0x0001
75+
#define CONTROL_VOLUME 0x0002
76+
77+
// Input Terminal Types
78+
#define TERMINAL_MICROPHONE 0x0201
79+
80+
// Output Terminal Types
81+
#define TERMINAL_SPEAKER 0x0301
82+
#define TERMINAL_HEADPHONES 0x0302
83+
84+
// Audio Streaming Interface Descriptor Subtypes
85+
#define STREAMING_GENERAL 0x01
86+
#define STREAMING_FORMAT_TYPE 0x02
87+
88+
// Audio Data Format Type I Codes
89+
#define FORMAT_PCM 0x0001
90+
91+
// Audio Format Types
92+
#define FORMAT_TYPE_I 0x01
93+
94+
// Audio Endpoint Descriptor Subtypes
95+
#define ENDPOINT_GENERAL 0x01
96+
97+
#endif

0 commit comments

Comments
 (0)