Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of I2S by
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 #include "MK64F12.h" 00034 00035 #ifndef I2S_H 00036 #define I2S_H 00037 00038 #define I2S_TRANSMIT 0 00039 #define I2S_RECEIVE 1 00040 00041 #define I2S_MASTER 0 00042 #define I2S_SLAVE 1 00043 00044 #define I2S_STEREO 0 00045 #define I2S_MONO 1 00046 00047 #define I2S_MUTED 1 00048 #define I2S_UNMUTED 0 00049 00050 #define I2S_4WIRE 1 00051 #define I2S_3WIRE 0 00052 00053 /** A class to play give access to the I2S library 00054 */ 00055 00056 class I2S 00057 { 00058 00059 public: 00060 00061 /** Create a I2S instance 00062 * 00063 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE) 00064 * @param SerialData The serial data pin 00065 * @param WordSelect The word select pin 00066 * @param BitClk The clock pin 00067 */ 00068 I2S(bool rxtx, PinName SerialData, PinName WordSelect, PinName BitClk); 00069 00070 /** Create a I2S instance: Only with the serial data line set. Won't really do much. 00071 * 00072 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE) 00073 * @param SerialData The serial data pin 00074 */ 00075 // I2S(bool rxtx, PinName SerialData); 00076 00077 00078 /** Create a I2S instance: Only with serial data line and word select. 00079 * 00080 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE) 00081 * @param SerialData The serial data pin 00082 * @param WordSelect The word select pin 00083 */ 00084 // I2S(bool rxtx, PinName SerialData, PinName WordSelect); 00085 00086 00087 00088 /** Create a I2S instance: Only with serial data line. Four wire mode means this is functional 00089 * 00090 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE) 00091 * @param SerialData The serial data pin 00092 * @param fourwiremode True means the peripheral is in 4-wire mode. It borroWordSelect WS and CLK from the other half 00093 */ 00094 // I2S(bool rxtx, PinName SerialData, bool fourwiremode); 00095 00096 /** Create a I2S instance: Only with serial data line and word select line. Four wire mode means this is functional 00097 * 00098 * @param rxtx Set the I2S instance to be transmit or recieve (I2S_TRANSMIT/I2S_RECEIVE) 00099 * @param SerialData The serial data pin 00100 * @param WordSelect The word select pin 00101 * @param fourwiremode True means the peripheral is in 4-wire mode. It borroWordSelect WS and CLK from the other half 00102 */ 00103 // I2S(bool rxtx, PinName SerialData, PinName WordSelect, bool fourwiremode); 00104 00105 /** Destroy the I2S instance 00106 */ 00107 ~I2S(); 00108 00109 /** Write to the FIFO 00110 * 00111 * @param buf[] The buffer of values to write: are bit stuffed in fours 00112 * @param len The number of chars to write 00113 */ 00114 void write(char buf[], int len); 00115 00116 /** Write to the FIFO 00117 * 00118 * @param buf[] The buffer of values to write: are bit stuffed automatically 00119 * @param len The number of chars to write 00120 */ 00121 void write(int buf[], int len); 00122 00123 /** Read the FIFOs contents 00124 * 00125 * @return The buffers value. 00126 */ 00127 int read(); 00128 00129 /** Read from the FIFO 00130 * 00131 * @param buf[] The buffer of values to read: raw bit shifted 00132 * @param len The number of chars to read 00133 */ 00134 void read(char buf[], int len); 00135 00136 /** Read from the FIFO 00137 * 00138 * @param buf[] The buffer of values to read: sorted to just values 00139 * @param len The number of chars to read 00140 */ 00141 void read(int buf[], int len); 00142 00143 /** Get the maximum number of points of data the FIFO could store 00144 * 00145 * @return The number of points 00146 */ 00147 int max_fifo_points(); 00148 /** Switch the peripheral on and off 00149 * 00150 * @param pwr Power status 00151 */ 00152 void power(bool pwr); 00153 00154 /** Switch the peripheral between master and slave 00155 * 00156 * @param mastermode The peripherals master/slave status (I2S_MASTER/I2S_SLAVE) 00157 */ 00158 void masterslave(bool mastermode); 00159 00160 /** Switch the peripheral between different wordsizes 00161 * 00162 * @param words The number of bits per word: 8,16,32 00163 */ 00164 void wordsize(int words); 00165 00166 /** Define the MasterClk frequency 00167 * 00168 * @param freq The frequency desired for the MasterClk 00169 */ 00170 void mclk_freq(int freq); 00171 00172 /** Define the sample rate 00173 * 00174 * @param freq The desired sample rate frequency 00175 */ 00176 void frequency(int freq); 00177 00178 /** Set the level that the fifo interrupts at 00179 * 00180 * @param level A number between 0 and 7 at which the fifo interrupts 00181 */ 00182 void set_interrupt_fifo_level(int level); 00183 00184 /** Get the current FIFO level 00185 * 00186 * @return A number between 0 and 7 the FIFO is currently at 00187 */ 00188 int fifo_level(); 00189 00190 /** Get the current number of samples in the FIFO 00191 * 00192 * @return A number showing how many samples are in the FIFO 00193 */ 00194 int fifo_points(); 00195 00196 /** Set whether the peripheral is in stereo or mono mode: in mono the perifpheral sends out the same data twice 00197 * 00198 * @param stereomode Whether the peripheral is in stereo or mono: I2S_STEREO/I2S_MONO 00199 */ 00200 void stereomono(bool stereomode); 00201 00202 /** Mute the peripheral 00203 * 00204 */ 00205 void mute(); 00206 00207 /** Set the mute status of the peripheral 00208 * 00209 * @param mute_en Set whether the mute is enabled 00210 */ 00211 void mute(bool mute_en); 00212 00213 /** Stop the peripheral 00214 * 00215 */ 00216 void stop(); 00217 00218 /** Start the peripheral 00219 * 00220 */ 00221 void start(); 00222 00223 /** Check the Clock and Pin setup went according to plan 00224 * 00225 * @return Setup okay? 00226 */ 00227 bool setup_ok(); 00228 00229 /** Attach a function to be called when the FIFO triggers 00230 * 00231 * @param fptr A pointer to the function to be called 00232 */ 00233 void attach(void (*fptr)(void)) 00234 { 00235 if (_rxtx == I2S_TRANSMIT) 00236 { 00237 I2STXISR.attach(fptr); 00238 txisr = true; 00239 } 00240 else 00241 { 00242 I2SRXISR.attach(fptr); 00243 rxisr = true; 00244 } 00245 } 00246 00247 /** Attach a member function to be called when the FIFO triggers 00248 * 00249 * @param tptr A pointer to the instance of the class 00250 * @param mptr A pointer to the member function 00251 */ 00252 template<typename T> 00253 void attach(T *tptr, void (T::*mptr)(void)) 00254 { 00255 if (_rxtx == I2S_TRANSMIT) 00256 { 00257 I2STXISR.attach(tptr, mptr); 00258 txisr = true; 00259 } 00260 else 00261 { 00262 I2SRXISR.attach(tptr, mptr); 00263 rxisr = true; 00264 } 00265 } 00266 00267 private: 00268 00269 void _set_clock_112896(void); 00270 void _set_clock_122800(void); 00271 void _i2s_init(void); 00272 void _i2s_set_rate(int smprate); 00273 00274 void mclk_enable(bool mclk_en); 00275 00276 void write_registers(); 00277 00278 void pin_setup(); 00279 00280 void fraction_estimator(float in, int * num, int * den); 00281 00282 float mod(float in); 00283 00284 void defaulter(); 00285 00286 PinName _SerialData, _WordSelect, _BitClk, _MasterClk; 00287 bool WordSelect_d, BitClk_d, MasterClk_d; 00288 bool _rxtx; 00289 bool pwr; 00290 bool master; 00291 int wordwidth; 00292 char wordwidth_code; 00293 bool mclk_en; 00294 int mclk_frequency; 00295 int freq; 00296 bool stereo; 00297 bool muted; 00298 bool stopped; 00299 int interrupt_fifo_level; 00300 int pin_setup_err; 00301 int reg_write_err; 00302 bool deallocating; 00303 int old_freq; 00304 00305 bool fourwire; 00306 00307 int old_pre_num; 00308 int old_pre_den; 00309 int old_bitrate_div; 00310 static void _i2sisr(void); 00311 00312 static FunctionPointer I2STXISR; 00313 static FunctionPointer I2SRXISR; 00314 00315 static bool txisr; 00316 static bool rxisr; 00317 00318 void write(int bufr[], int bufl[], int len); 00319 void read(int bufr[], int bufl[], int len); 00320 }; 00321 00322 #endif
Generated on Sun Jul 17 2022 00:39:12 by
1.7.2
