A well tested I2S library where the send and recieve parts are seperate instances.

Dependents:   TLV320_Write_test flash_audio_playerI2S Wolfson_3_wav

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2S.h Source File

I2S.h

00001 /**
00002 * @author Giles Barton-Owen
00003 *
00004 * @section LICENSE
00005 *
00006 * Copyright (c) 2012 mbed
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a copy
00009 * of this software and associated documentation files (the "Software"), to deal
00010 * in the Software without restriction, including without limitation the rights
00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 * copies of the Software, and to permit persons to whom the Software is
00013 * furnished to do so, subject to the following conditions:
00014 *
00015 * The above copyright notice and this permission notice shall be included in
00016 * all copies or substantial portions of the Software.
00017 *
00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 * THE SOFTWARE.
00025 *
00026 * @section DESCRIPTION
00027 *    A I2S library for the LPC1768's built-in I2S peripheral
00028 *
00029 */ 
00030 
00031 #include "mbed.h"
00032 #include "math.h"
00033 
00034 #ifndef I2S_H
00035 #define I2S_H
00036 
00037 #define I2S_TRANSMIT     0
00038 #define I2S_RECEIVE     1
00039 
00040 #define I2S_MASTER     0
00041 #define I2S_SLAVE     1
00042 
00043 #define I2S_STEREO     0
00044 #define I2S_MONO     1
00045 
00046 #define I2S_MUTED     1
00047 #define I2S_UNMUTED 0
00048 
00049 #define I2S_4WIRE     1
00050 #define I2S_3WIRE     0
00051 
00052 /** A class to play give access to the I2S library
00053  */
00054 
00055 class I2S
00056 {
00057 
00058 public:
00059 
00060     /** Create a I2S instance
00061      * 
00062      * @param rxtx     Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
00063      * @param sd    The serial data pin
00064      * @param ws    The word select pin
00065      * @param clk    The clock pin
00066      */
00067     I2S(bool rxtx, PinName sd, PinName ws, PinName clk);
00068 
00069     /** Create a I2S instance: Only with the serial data line set. Won't really do much.
00070      * 
00071      * @param rxtx     Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
00072      * @param sd    The serial data pin
00073      */
00074     I2S(bool rxtx, PinName sd);
00075 
00076     
00077     /** Create a I2S instance: Only with serial data line and word select. 
00078      * 
00079      * @param rxtx     Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
00080      * @param sd    The serial data pin
00081      * @param ws    The word select pin
00082      */
00083     I2S(bool rxtx, PinName sd, PinName ws);
00084     
00085     
00086     
00087     /** Create a I2S instance: Only with serial data line. Four wire mode means this is functional
00088      * 
00089      * @param rxtx     Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
00090      * @param sd    The serial data pin
00091      * @param fourwiremode True means the peripheral is in 4-wire mode. It borrows WS and CLK from the other half
00092      */
00093     I2S(bool rxtx, PinName sd, bool fourwiremode);
00094     
00095     /** Create a I2S instance: Only with serial data line and word select line. Four wire mode means this is functional
00096      * 
00097      * @param rxtx     Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE)
00098      * @param sd    The serial data pin
00099      * @param ws    The word select pin
00100      * @param fourwiremode True means the peripheral is in 4-wire mode. It borrows WS and CLK from the other half
00101      */
00102     I2S(bool rxtx, PinName sd, PinName ws, bool fourwiremode);
00103 
00104     /** Destroy the I2S instance
00105      */
00106     ~I2S();
00107 
00108     /** Write to the FIFO
00109      * 
00110      * @param buf[] The buffer of values to write: are bit stuffed in fours
00111      * @param len    The number of chars to write
00112      */
00113     void write(char buf[], int len);
00114 
00115     /** Write to the FIFO
00116      * 
00117      * @param buf[] The buffer of values to write: are bit stuffed automatically
00118      * @param len    The number of chars to write
00119      */
00120     void write(int buf[], int len);
00121 
00122     /** Read the FIFOs contents
00123      * 
00124      * @return The buffers value.
00125      */
00126     int read();
00127 
00128     /** Read from the FIFO
00129      * 
00130      * @param buf[] The buffer of values to read: raw bit shifted
00131      * @param len    The number of chars to read
00132      */
00133     void read(char buf[], int len);
00134 
00135     /** Read from the FIFO
00136      * 
00137      * @param buf[] The buffer of values to read: sorted to just values
00138      * @param len    The number of chars to read
00139      */
00140     void read(int buf[], int len);
00141     
00142     /** Get the maximum number of points of data the FIFO could store
00143      *
00144      * @return The number of points
00145      */
00146     int max_fifo_points();
00147     /** Switch the peripheral on and off
00148      *
00149      * @param pwr Power status
00150      */
00151     void power(bool pwr);
00152 
00153     /** Switch the peripheral between master and slave
00154      *
00155      * @param mastermode The peripherals master/slave status (I2S_MASTER/I2S_SLAVE)
00156      */
00157     void masterslave(bool mastermode);
00158 
00159     /** Switch the peripheral between different wordsizes
00160      *
00161      * @param words The number of bits per word: 8,16,32
00162      */
00163     void wordsize(int words);
00164 
00165     /** Define the mclk frequency
00166      *
00167      * @param freq The frequency desired for the mclk
00168      */
00169     void mclk_freq(int freq);
00170     
00171     /** Define the sample rate
00172      *
00173      * @param freq The desired sample rate frequency
00174      */
00175     void frequency(int freq);
00176 
00177     /** Set the level that the fifo interrupts at
00178      *
00179      * @param level A number between 0 and 7 at which the fifo interrupts
00180      */
00181     void set_interrupt_fifo_level(int level);
00182 
00183     /** Get the current FIFO level
00184      *
00185      * @return A number between 0 and 7 the FIFO is currently at
00186      */
00187     int fifo_level();
00188 
00189     /** Get the current number of samples in the FIFO
00190      *
00191     * @return A number showing how many samples are in the FIFO
00192      */
00193     int fifo_points();
00194 
00195     /** Set whether the peripheral is in stereo or mono mode: in mono the perifpheral sends out the same data twice
00196      *
00197      * @param stereomode Whether the peripheral is in stereo or mono: I2S_STEREO/I2S_MONO
00198      */
00199     void stereomono(bool stereomode);
00200 
00201     /** Mute the peripheral
00202      *
00203      */
00204     void mute();
00205 
00206     /** Set the mute status of the peripheral
00207      *
00208      * @param mute_en Set whether the mute is enabled
00209      */
00210     void mute(bool mute_en);
00211 
00212     /** Stop the peripheral
00213      *
00214      */
00215     void stop();
00216 
00217     /** Start the peripheral
00218      *
00219      */
00220     void start();
00221 
00222     /** Check the Clock and Pin setup went according to plan
00223      *
00224      * @return Setup okay?
00225      */
00226     bool setup_ok();
00227 
00228     /** Attach a function to be called when the FIFO triggers
00229      *
00230      * @param fptr A pointer to the function to be called
00231      */
00232     void attach(void (*fptr)(void))
00233     {
00234         if (_rxtx == I2S_TRANSMIT)
00235         {
00236             I2STXISR.attach(fptr);
00237             txisr = true;
00238         }
00239         else
00240         {
00241             I2SRXISR.attach(fptr);
00242             rxisr = true;
00243         }
00244     }
00245 
00246     /** Attach a member function to be called when the FIFO triggers
00247      *
00248      * @param tptr A pointer to the instance of the class
00249      * @param mptr A pointer to the member function
00250      */
00251     template<typename T>
00252     void attach(T *tptr, void (T::*mptr)(void))
00253     {
00254         if (_rxtx == I2S_TRANSMIT)
00255         {
00256             I2STXISR.attach(tptr, mptr);
00257             txisr = true;
00258         }
00259         else
00260         {
00261             I2SRXISR.attach(tptr, mptr);
00262             rxisr = true;
00263         }
00264     }
00265 
00266 private:
00267 
00268     void mclk_enable(bool mclk_en);
00269 
00270     void write_registers();
00271 
00272     void pin_setup();
00273 
00274     void fraction_estimator(float in, int * num, int * den);
00275 
00276     float mod(float in);
00277 
00278     void defaulter();
00279 
00280     PinName _sd, _ws, _clk, _mclk;
00281     bool ws_d, clk_d, mclk_d;
00282     bool _rxtx;
00283     bool pwr;
00284     bool master;
00285     int wordwidth;
00286     char wordwidth_code;
00287     bool mclk_en;
00288     int mclk_frequency;
00289     int freq;
00290     bool stereo;
00291     bool muted;
00292     bool stopped;
00293     int interrupt_fifo_level;
00294     int pin_setup_err;
00295     int reg_write_err;
00296     bool deallocating;
00297     int old_freq;
00298     
00299     bool fourwire;
00300 
00301     int old_pre_num;
00302     int old_pre_den;
00303     int old_bitrate_div;
00304     static void _i2sisr(void);
00305 
00306     static FunctionPointer I2STXISR;
00307     static FunctionPointer I2SRXISR;
00308 
00309     static bool txisr;
00310     static bool rxisr;
00311     
00312     void write(int bufr[], int bufl[], int len);
00313     void read(int bufr[], int bufl[], int len);
00314 };
00315 
00316 #endif