A library which allows the playing of Wav files using the TLV320

Dependents:   RSALB_hbridge_helloworld RSALB_lobster WavPlayer_test AudioCODEC_HelloWorld

Committer:
p07gbar
Date:
Fri Sep 21 14:24:00 2012 +0000
Revision:
3:a7380cfc1987
Parent:
1:3eb96771bbee
Minor fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p07gbar 1:3eb96771bbee 1 /**
p07gbar 1:3eb96771bbee 2 * @author Giles Barton-Owen
p07gbar 1:3eb96771bbee 3 *
p07gbar 1:3eb96771bbee 4 * @section LICENSE
p07gbar 1:3eb96771bbee 5 *
p07gbar 1:3eb96771bbee 6 * Copyright (c) 2012 mbed
p07gbar 1:3eb96771bbee 7 *
p07gbar 1:3eb96771bbee 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
p07gbar 1:3eb96771bbee 9 * of this software and associated documentation files (the "Software"), to deal
p07gbar 1:3eb96771bbee 10 * in the Software without restriction, including without limitation the rights
p07gbar 1:3eb96771bbee 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
p07gbar 1:3eb96771bbee 12 * copies of the Software, and to permit persons to whom the Software is
p07gbar 1:3eb96771bbee 13 * furnished to do so, subject to the following conditions:
p07gbar 1:3eb96771bbee 14 *
p07gbar 1:3eb96771bbee 15 * The above copyright notice and this permission notice shall be included in
p07gbar 1:3eb96771bbee 16 * all copies or substantial portions of the Software.
p07gbar 1:3eb96771bbee 17 *
p07gbar 1:3eb96771bbee 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
p07gbar 1:3eb96771bbee 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
p07gbar 1:3eb96771bbee 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
p07gbar 1:3eb96771bbee 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
p07gbar 1:3eb96771bbee 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
p07gbar 1:3eb96771bbee 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
p07gbar 1:3eb96771bbee 24 * THE SOFTWARE.
p07gbar 1:3eb96771bbee 25 *
p07gbar 1:3eb96771bbee 26 * @section DESCRIPTION
p07gbar 1:3eb96771bbee 27 * A I2S library for the LPC1768's built-in I2S peripheral
p07gbar 1:3eb96771bbee 28 *
p07gbar 1:3eb96771bbee 29 */
p07gbar 1:3eb96771bbee 30
p07gbar 1:3eb96771bbee 31 #include "mbed.h"
p07gbar 1:3eb96771bbee 32 #include "math.h"
p07gbar 1:3eb96771bbee 33
p07gbar 1:3eb96771bbee 34 #ifndef I2S_H
p07gbar 1:3eb96771bbee 35 #define I2S_H
p07gbar 1:3eb96771bbee 36
p07gbar 1:3eb96771bbee 37 #define I2S_TRANSMIT 0
p07gbar 1:3eb96771bbee 38 #define I2S_RECEIVE 1
p07gbar 1:3eb96771bbee 39
p07gbar 1:3eb96771bbee 40 #define I2S_MASTER 0
p07gbar 1:3eb96771bbee 41 #define I2S_SLAVE 1
p07gbar 1:3eb96771bbee 42
p07gbar 1:3eb96771bbee 43 #define I2S_STEREO 0
p07gbar 1:3eb96771bbee 44 #define I2S_MONO 1
p07gbar 1:3eb96771bbee 45
p07gbar 1:3eb96771bbee 46 #define I2S_MUTED 1
p07gbar 1:3eb96771bbee 47 #define I2S_UNMUTED 0
p07gbar 1:3eb96771bbee 48
p07gbar 1:3eb96771bbee 49 #define I2S_4WIRE 1
p07gbar 1:3eb96771bbee 50 #define I2S_3WIRE 0
p07gbar 1:3eb96771bbee 51
p07gbar 1:3eb96771bbee 52 /** A class to play give access to the I2S library
p07gbar 1:3eb96771bbee 53 */
p07gbar 1:3eb96771bbee 54
p07gbar 1:3eb96771bbee 55 class I2S
p07gbar 1:3eb96771bbee 56 {
p07gbar 1:3eb96771bbee 57
p07gbar 1:3eb96771bbee 58 public:
p07gbar 1:3eb96771bbee 59
p07gbar 1:3eb96771bbee 60 /** Create a I2S instance
p07gbar 1:3eb96771bbee 61 *
p07gbar 1:3eb96771bbee 62 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
p07gbar 1:3eb96771bbee 63 * @param sd The serial data pin
p07gbar 1:3eb96771bbee 64 * @param ws The word select pin
p07gbar 1:3eb96771bbee 65 * @param clk The clock pin
p07gbar 1:3eb96771bbee 66 */
p07gbar 1:3eb96771bbee 67 I2S(bool rxtx, PinName sd, PinName ws, PinName clk);
p07gbar 1:3eb96771bbee 68
p07gbar 1:3eb96771bbee 69 /** Create a I2S instance: Only with the serial data line set. Won't really do much.
p07gbar 1:3eb96771bbee 70 *
p07gbar 1:3eb96771bbee 71 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
p07gbar 1:3eb96771bbee 72 * @param sd The serial data pin
p07gbar 1:3eb96771bbee 73 */
p07gbar 1:3eb96771bbee 74 I2S(bool rxtx, PinName sd);
p07gbar 1:3eb96771bbee 75
p07gbar 1:3eb96771bbee 76
p07gbar 1:3eb96771bbee 77 /** Create a I2S instance: Only with serial data line and word select.
p07gbar 1:3eb96771bbee 78 *
p07gbar 1:3eb96771bbee 79 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
p07gbar 1:3eb96771bbee 80 * @param sd The serial data pin
p07gbar 1:3eb96771bbee 81 * @param ws The word select pin
p07gbar 1:3eb96771bbee 82 */
p07gbar 1:3eb96771bbee 83 I2S(bool rxtx, PinName sd, PinName ws);
p07gbar 1:3eb96771bbee 84
p07gbar 1:3eb96771bbee 85
p07gbar 1:3eb96771bbee 86
p07gbar 1:3eb96771bbee 87 /** Create a I2S instance: Only with serial data line. Four wire mode means this is functional
p07gbar 1:3eb96771bbee 88 *
p07gbar 1:3eb96771bbee 89 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
p07gbar 1:3eb96771bbee 90 * @param sd The serial data pin
p07gbar 1:3eb96771bbee 91 * @param fourwiremode True means the peripheral is in 4-wire mode. It borrows WS and CLK from the other half
p07gbar 1:3eb96771bbee 92 */
p07gbar 1:3eb96771bbee 93 I2S(bool rxtx, PinName sd, bool fourwiremode);
p07gbar 1:3eb96771bbee 94
p07gbar 1:3eb96771bbee 95 /** Create a I2S instance: Only with serial data line and word select line. Four wire mode means this is functional
p07gbar 1:3eb96771bbee 96 *
p07gbar 1:3eb96771bbee 97 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
p07gbar 1:3eb96771bbee 98 * @param sd The serial data pin
p07gbar 1:3eb96771bbee 99 * @param ws The word select pin
p07gbar 1:3eb96771bbee 100 * @param fourwiremode True means the peripheral is in 4-wire mode. It borrows WS and CLK from the other half
p07gbar 1:3eb96771bbee 101 */
p07gbar 1:3eb96771bbee 102 I2S(bool rxtx, PinName sd, PinName ws, bool fourwiremode);
p07gbar 1:3eb96771bbee 103
p07gbar 1:3eb96771bbee 104 /** Destroy the I2S instance
p07gbar 1:3eb96771bbee 105 */
p07gbar 1:3eb96771bbee 106 ~I2S();
p07gbar 1:3eb96771bbee 107
p07gbar 1:3eb96771bbee 108 /** Write to the FIFO
p07gbar 1:3eb96771bbee 109 *
p07gbar 1:3eb96771bbee 110 * @param buf[] The buffer of values to write: are bit stuffed in fours
p07gbar 1:3eb96771bbee 111 * @param len The number of chars to write
p07gbar 1:3eb96771bbee 112 */
p07gbar 1:3eb96771bbee 113 void write(char buf[], int len);
p07gbar 1:3eb96771bbee 114
p07gbar 1:3eb96771bbee 115 /** Write to the FIFO
p07gbar 1:3eb96771bbee 116 *
p07gbar 1:3eb96771bbee 117 * @param buf[] The buffer of values to write: are bit stuffed automatically
p07gbar 1:3eb96771bbee 118 * @param len The number of chars to write
p07gbar 1:3eb96771bbee 119 */
p07gbar 1:3eb96771bbee 120 void write(int buf[], int len);
p07gbar 1:3eb96771bbee 121
p07gbar 1:3eb96771bbee 122 /** Read the FIFOs contents
p07gbar 1:3eb96771bbee 123 *
p07gbar 1:3eb96771bbee 124 * @return The buffers value.
p07gbar 1:3eb96771bbee 125 */
p07gbar 1:3eb96771bbee 126 int read();
p07gbar 1:3eb96771bbee 127
p07gbar 1:3eb96771bbee 128 /** Read from the FIFO
p07gbar 1:3eb96771bbee 129 *
p07gbar 1:3eb96771bbee 130 * @param buf[] The buffer of values to read: raw bit shifted
p07gbar 1:3eb96771bbee 131 * @param len The number of chars to read
p07gbar 1:3eb96771bbee 132 */
p07gbar 1:3eb96771bbee 133 void read(char buf[], int len);
p07gbar 1:3eb96771bbee 134
p07gbar 1:3eb96771bbee 135 /** Read from the FIFO
p07gbar 1:3eb96771bbee 136 *
p07gbar 1:3eb96771bbee 137 * @param buf[] The buffer of values to read: sorted to just values
p07gbar 1:3eb96771bbee 138 * @param len The number of chars to read
p07gbar 1:3eb96771bbee 139 */
p07gbar 1:3eb96771bbee 140 void read(int buf[], int len);
p07gbar 1:3eb96771bbee 141
p07gbar 1:3eb96771bbee 142 /** Get the maximum number of points of data the FIFO could store
p07gbar 1:3eb96771bbee 143 *
p07gbar 1:3eb96771bbee 144 * @return The number of points
p07gbar 1:3eb96771bbee 145 */
p07gbar 1:3eb96771bbee 146 int max_fifo_points();
p07gbar 1:3eb96771bbee 147 /** Switch the peripheral on and off
p07gbar 1:3eb96771bbee 148 *
p07gbar 1:3eb96771bbee 149 * @param pwr Power status
p07gbar 1:3eb96771bbee 150 */
p07gbar 1:3eb96771bbee 151 void power(bool pwr);
p07gbar 1:3eb96771bbee 152
p07gbar 1:3eb96771bbee 153 /** Switch the peripheral between master and slave
p07gbar 1:3eb96771bbee 154 *
p07gbar 1:3eb96771bbee 155 * @param mastermode The peripherals master/slave status (I2S_MASTER/I2S_SLAVE)
p07gbar 1:3eb96771bbee 156 */
p07gbar 1:3eb96771bbee 157 void masterslave(bool mastermode);
p07gbar 1:3eb96771bbee 158
p07gbar 1:3eb96771bbee 159 /** Switch the peripheral between different wordsizes
p07gbar 1:3eb96771bbee 160 *
p07gbar 1:3eb96771bbee 161 * @param words The number of bits per word: 8,16,32
p07gbar 1:3eb96771bbee 162 */
p07gbar 1:3eb96771bbee 163 void wordsize(int words);
p07gbar 1:3eb96771bbee 164
p07gbar 1:3eb96771bbee 165 /** Define the mclk frequency
p07gbar 1:3eb96771bbee 166 *
p07gbar 1:3eb96771bbee 167 * @param freq The frequency desired for the mclk
p07gbar 1:3eb96771bbee 168 */
p07gbar 1:3eb96771bbee 169 void mclk_freq(int freq);
p07gbar 1:3eb96771bbee 170
p07gbar 1:3eb96771bbee 171 /** Define the sample rate
p07gbar 1:3eb96771bbee 172 *
p07gbar 1:3eb96771bbee 173 * @param freq The desired sample rate frequency
p07gbar 1:3eb96771bbee 174 */
p07gbar 1:3eb96771bbee 175 void frequency(int freq);
p07gbar 1:3eb96771bbee 176
p07gbar 1:3eb96771bbee 177 /** Set the level that the fifo interrupts at
p07gbar 1:3eb96771bbee 178 *
p07gbar 1:3eb96771bbee 179 * @param level A number between 0 and 7 at which the fifo interrupts
p07gbar 1:3eb96771bbee 180 */
p07gbar 1:3eb96771bbee 181 void set_interrupt_fifo_level(int level);
p07gbar 1:3eb96771bbee 182
p07gbar 1:3eb96771bbee 183 /** Get the current FIFO level
p07gbar 1:3eb96771bbee 184 *
p07gbar 1:3eb96771bbee 185 * @return A number between 0 and 7 the FIFO is currently at
p07gbar 1:3eb96771bbee 186 */
p07gbar 1:3eb96771bbee 187 int fifo_level();
p07gbar 1:3eb96771bbee 188
p07gbar 1:3eb96771bbee 189 /** Get the current number of samples in the FIFO
p07gbar 1:3eb96771bbee 190 *
p07gbar 1:3eb96771bbee 191 * @return A number showing how many samples are in the FIFO
p07gbar 1:3eb96771bbee 192 */
p07gbar 1:3eb96771bbee 193 int fifo_points();
p07gbar 1:3eb96771bbee 194
p07gbar 1:3eb96771bbee 195 /** Set whether the peripheral is in stereo or mono mode: in mono the perifpheral sends out the same data twice
p07gbar 1:3eb96771bbee 196 *
p07gbar 1:3eb96771bbee 197 * @param stereomode Whether the peripheral is in stereo or mono: I2S_STEREO/I2S_MONO
p07gbar 1:3eb96771bbee 198 */
p07gbar 1:3eb96771bbee 199 void stereomono(bool stereomode);
p07gbar 1:3eb96771bbee 200
p07gbar 1:3eb96771bbee 201 /** Mute the peripheral
p07gbar 1:3eb96771bbee 202 *
p07gbar 1:3eb96771bbee 203 */
p07gbar 1:3eb96771bbee 204 void mute();
p07gbar 1:3eb96771bbee 205
p07gbar 1:3eb96771bbee 206 /** Set the mute status of the peripheral
p07gbar 1:3eb96771bbee 207 *
p07gbar 1:3eb96771bbee 208 * @param mute_en Set whether the mute is enabled
p07gbar 1:3eb96771bbee 209 */
p07gbar 1:3eb96771bbee 210 void mute(bool mute_en);
p07gbar 1:3eb96771bbee 211
p07gbar 1:3eb96771bbee 212 /** Stop the peripheral
p07gbar 1:3eb96771bbee 213 *
p07gbar 1:3eb96771bbee 214 */
p07gbar 1:3eb96771bbee 215 void stop();
p07gbar 1:3eb96771bbee 216
p07gbar 1:3eb96771bbee 217 /** Start the peripheral
p07gbar 1:3eb96771bbee 218 *
p07gbar 1:3eb96771bbee 219 */
p07gbar 1:3eb96771bbee 220 void start();
p07gbar 1:3eb96771bbee 221
p07gbar 1:3eb96771bbee 222 /** Check the Clock and Pin setup went according to plan
p07gbar 1:3eb96771bbee 223 *
p07gbar 1:3eb96771bbee 224 * @return Setup okay?
p07gbar 1:3eb96771bbee 225 */
p07gbar 1:3eb96771bbee 226 bool setup_ok();
p07gbar 1:3eb96771bbee 227
p07gbar 1:3eb96771bbee 228 /** Attach a function to be called when the FIFO triggers
p07gbar 1:3eb96771bbee 229 *
p07gbar 1:3eb96771bbee 230 * @param fptr A pointer to the function to be called
p07gbar 1:3eb96771bbee 231 */
p07gbar 1:3eb96771bbee 232 void attach(void (*fptr)(void))
p07gbar 1:3eb96771bbee 233 {
p07gbar 1:3eb96771bbee 234 if (_rxtx == I2S_TRANSMIT)
p07gbar 1:3eb96771bbee 235 {
p07gbar 1:3eb96771bbee 236 I2STXISR.attach(fptr);
p07gbar 1:3eb96771bbee 237 txisr = true;
p07gbar 1:3eb96771bbee 238 }
p07gbar 1:3eb96771bbee 239 else
p07gbar 1:3eb96771bbee 240 {
p07gbar 1:3eb96771bbee 241 I2SRXISR.attach(fptr);
p07gbar 1:3eb96771bbee 242 rxisr = true;
p07gbar 1:3eb96771bbee 243 }
p07gbar 1:3eb96771bbee 244 }
p07gbar 1:3eb96771bbee 245
p07gbar 1:3eb96771bbee 246 /** Attach a member function to be called when the FIFO triggers
p07gbar 1:3eb96771bbee 247 *
p07gbar 1:3eb96771bbee 248 * @param tptr A pointer to the instance of the class
p07gbar 1:3eb96771bbee 249 * @param mptr A pointer to the member function
p07gbar 1:3eb96771bbee 250 */
p07gbar 1:3eb96771bbee 251 template<typename T>
p07gbar 1:3eb96771bbee 252 void attach(T *tptr, void (T::*mptr)(void))
p07gbar 1:3eb96771bbee 253 {
p07gbar 1:3eb96771bbee 254 if (_rxtx == I2S_TRANSMIT)
p07gbar 1:3eb96771bbee 255 {
p07gbar 1:3eb96771bbee 256 I2STXISR.attach(tptr, mptr);
p07gbar 1:3eb96771bbee 257 txisr = true;
p07gbar 1:3eb96771bbee 258 }
p07gbar 1:3eb96771bbee 259 else
p07gbar 1:3eb96771bbee 260 {
p07gbar 1:3eb96771bbee 261 I2SRXISR.attach(tptr, mptr);
p07gbar 1:3eb96771bbee 262 rxisr = true;
p07gbar 1:3eb96771bbee 263 }
p07gbar 1:3eb96771bbee 264 }
p07gbar 1:3eb96771bbee 265
p07gbar 1:3eb96771bbee 266 private:
p07gbar 1:3eb96771bbee 267
p07gbar 1:3eb96771bbee 268 void mclk_enable(bool mclk_en);
p07gbar 1:3eb96771bbee 269
p07gbar 1:3eb96771bbee 270 void write_registers();
p07gbar 1:3eb96771bbee 271
p07gbar 1:3eb96771bbee 272 void pin_setup();
p07gbar 1:3eb96771bbee 273
p07gbar 1:3eb96771bbee 274 void fraction_estimator(float in, int * num, int * den);
p07gbar 1:3eb96771bbee 275
p07gbar 1:3eb96771bbee 276 float mod(float in);
p07gbar 1:3eb96771bbee 277
p07gbar 1:3eb96771bbee 278 void defaulter();
p07gbar 1:3eb96771bbee 279
p07gbar 1:3eb96771bbee 280 PinName _sd, _ws, _clk, _mclk;
p07gbar 1:3eb96771bbee 281 bool ws_d, clk_d, mclk_d;
p07gbar 1:3eb96771bbee 282 bool _rxtx;
p07gbar 1:3eb96771bbee 283 bool pwr;
p07gbar 1:3eb96771bbee 284 bool master;
p07gbar 1:3eb96771bbee 285 int wordwidth;
p07gbar 1:3eb96771bbee 286 char wordwidth_code;
p07gbar 1:3eb96771bbee 287 bool mclk_en;
p07gbar 1:3eb96771bbee 288 int mclk_frequency;
p07gbar 1:3eb96771bbee 289 int freq;
p07gbar 1:3eb96771bbee 290 bool stereo;
p07gbar 1:3eb96771bbee 291 bool muted;
p07gbar 1:3eb96771bbee 292 bool stopped;
p07gbar 1:3eb96771bbee 293 int interrupt_fifo_level;
p07gbar 1:3eb96771bbee 294 int pin_setup_err;
p07gbar 1:3eb96771bbee 295 int reg_write_err;
p07gbar 1:3eb96771bbee 296 bool deallocating;
p07gbar 1:3eb96771bbee 297 int old_freq;
p07gbar 1:3eb96771bbee 298
p07gbar 1:3eb96771bbee 299 bool fourwire;
p07gbar 1:3eb96771bbee 300
p07gbar 1:3eb96771bbee 301 int old_pre_num;
p07gbar 1:3eb96771bbee 302 int old_pre_den;
p07gbar 1:3eb96771bbee 303 int old_bitrate_div;
p07gbar 1:3eb96771bbee 304 static void _i2sisr(void);
p07gbar 1:3eb96771bbee 305
p07gbar 1:3eb96771bbee 306 static FunctionPointer I2STXISR;
p07gbar 1:3eb96771bbee 307 static FunctionPointer I2SRXISR;
p07gbar 1:3eb96771bbee 308
p07gbar 1:3eb96771bbee 309 static bool txisr;
p07gbar 1:3eb96771bbee 310 static bool rxisr;
p07gbar 1:3eb96771bbee 311
p07gbar 1:3eb96771bbee 312 void write(int bufr[], int bufl[], int len);
p07gbar 1:3eb96771bbee 313 void read(int bufr[], int bufl[], int len);
p07gbar 1:3eb96771bbee 314 };
p07gbar 1:3eb96771bbee 315
p07gbar 1:3eb96771bbee 316 #endif