Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

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