mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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