1 /* Copyright (c) 2010-2011 mbed.org, MIT License
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:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
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.
22 /* These headers are included for child class. */
23 #include "USBEndpoints.h"
24 #include "USBDescriptor.h"
25 #include "USBDevice_Types.h"
27 #include "USBDevice.h"
35 * #include "USBAudio.h"
37 * Serial pc(USBTX, USBRX);
39 * // frequency: 48 kHz
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
49 * USBAudio audio(FREQ, NB_CHA);
52 * int16_t buf[AUDIO_LENGTH_PACKET/2];
55 * // read an audio packet
56 * audio.read((uint8_t *)buf);
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]);
69 class USBAudio
: public USBDevice
{
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
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);
86 * Get current volume between 0.0 and 1.0
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
95 * @param buf pointer on a buffer which will be filled with an audio packet
97 * @returns true if successfull
99 bool read(uint8_t * buf
);
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
104 * @param buf pointer on a buffer which will be filled if an audio packet is available
106 * @returns true if successfull
108 bool readNB(uint8_t * buf
);
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.
113 * @param buf pointer on the audio packet which will be sent
114 * @returns true if successful
116 bool write(uint8_t * buf
);
119 * Write and read an audio packet at the same time (on the same frame)
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
125 bool readWrite(uint8_t * buf_read
, uint8_t * buf_write
);
128 /** attach a handler to update the volume
130 * @param function Function to attach
133 void attach(void(*fptr
)(void)) {
134 updateVol
.attach(fptr
);
137 /** Attach a nonstatic void/void member function to update the volume
139 * @param tptr Object pointer
140 * @param mptr Member function pointer
144 void attach(T
*tptr
, void(T::*mptr
)(void)) {
145 updateVol
.attach(tptr
, mptr
);
152 * Called by USBDevice layer. Set configuration of the device.
153 * For instance, you can add all endpoints that you need on this function.
155 * @param configuration Number of the configuration
156 * @returns true if class handles this request
158 virtual bool USBCallback_setConfiguration(uint8_t configuration
);
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
165 * @returns true if class handles this request
167 virtual bool USBCallback_request();
170 * Get string product descriptor
172 * @returns pointer to the string product descriptor
174 virtual uint8_t * stringIproductDesc();
177 * Get string interface descriptor
179 * @returns pointer to the string interface descriptor
181 virtual uint8_t * stringIinterfaceDesc();
184 * Get configuration descriptor
186 * @returns pointer to the configuration descriptor
188 virtual uint8_t * configurationDesc();
191 * Called by USBDevice layer. Set interface/alternate of the device.
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
197 virtual bool USBCallback_setInterface(uint16_t interface
, uint8_t alternate
);
200 * Called by USBDevice on Endpoint0 request completion
201 * if the 'notify' flag has been set to true. Warning: Called in ISR context
203 * In this case it is used to indicate that a HID report has
204 * been received from the host on endpoint 0
206 * @param buf buffer received on endpoint 0
207 * @param length length of this buffer
209 virtual void USBCallback_requestCompleted(uint8_t * buf
, uint32_t length
);
212 * Callback called on each Start of Frame event
214 virtual void SOF(int frameNumber
);
217 * Callback called when a packet is received
219 virtual bool EPISO_OUT_callback();
222 * Callback called when a packet has been sent
224 virtual bool EPISO_IN_callback();
228 // stream available ?
229 volatile bool available
;
231 // interrupt OUT has been received
232 volatile bool interruptOUT
;
234 // interrupt IN has been received
235 volatile bool interruptIN
;
237 // audio packet has been written
238 volatile bool writeIN
;
244 // size of the maximum packet for the isochronous endpoint
245 uint32_t PACKET_SIZE_ISO_IN
;
246 uint32_t PACKET_SIZE_ISO_OUT
;
249 uint8_t channel_nb_in
;
250 uint8_t channel_nb_out
;
252 // channel config: master, left, right
253 uint8_t channel_config_in
;
254 uint8_t channel_config_out
;
259 // Volume Current Value
262 // Volume Minimum Value
265 // Volume Maximum Value
271 // Buffer containing one audio packet (to be read)
272 volatile uint8_t * buf_stream_in
;
274 // Buffer containing one audio packet (to be written)
275 volatile uint8_t * buf_stream_out
;
277 // callback to update volume
278 FunctionPointer updateVol
;
280 // boolean showing that the SOF handler has been called. Useful for readNB.
281 volatile bool SOF_handler
;
283 volatile float volume
;