Code for measuring the signal with a specified length and sampling rate, and saving it on a SD card.

Dependencies:   EALib I2S mbed

Committer:
msamadani
Date:
Thu Oct 05 17:44:39 2017 +0000
Revision:
2:8c5b6522139f
Parent:
0:c05b00be2229
A version of the code that initializes the SD RAM, activates the codec, measures the signal and saves the signal onto the SD Card.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
msamadani 0:c05b00be2229 1 /**
msamadani 0:c05b00be2229 2 *
msamadani 0:c05b00be2229 3 * @section LICENSE
msamadani 0:c05b00be2229 4 *
msamadani 0:c05b00be2229 5 * Copyright (c) 2012 mbed
msamadani 0:c05b00be2229 6 *
msamadani 0:c05b00be2229 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
msamadani 0:c05b00be2229 8 * of this software and associated documentation files (the "Software"), to deal
msamadani 0:c05b00be2229 9 * in the Software without restriction, including without limitation the rights
msamadani 0:c05b00be2229 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
msamadani 0:c05b00be2229 11 * copies of the Software, and to permit persons to whom the Software is
msamadani 0:c05b00be2229 12 * furnished to do so, subject to the following conditions:
msamadani 0:c05b00be2229 13 *
msamadani 0:c05b00be2229 14 * The above copyright notice and this permission notice shall be included in
msamadani 0:c05b00be2229 15 * all copies or substantial portions of the Software.
msamadani 0:c05b00be2229 16 *
msamadani 0:c05b00be2229 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
msamadani 0:c05b00be2229 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
msamadani 0:c05b00be2229 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
msamadani 0:c05b00be2229 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
msamadani 0:c05b00be2229 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
msamadani 0:c05b00be2229 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
msamadani 0:c05b00be2229 23 * THE SOFTWARE.
msamadani 0:c05b00be2229 24 *
msamadani 0:c05b00be2229 25 * @section DESCRIPTION
msamadani 0:c05b00be2229 26 * A Driver set for the I2C half of the WM8731. Based on the WM8731
msamadani 0:c05b00be2229 27 * code http://mbed.org/users/p07gbar/code/WM8731/
msamadani 0:c05b00be2229 28 *
msamadani 0:c05b00be2229 29 */
msamadani 0:c05b00be2229 30
msamadani 0:c05b00be2229 31 #ifndef WM8731_H
msamadani 0:c05b00be2229 32 #define WM8731_H
msamadani 0:c05b00be2229 33
msamadani 0:c05b00be2229 34 #include "mbed.h"
msamadani 0:c05b00be2229 35
msamadani 0:c05b00be2229 36 #define WM8731_CS_HIGH true
msamadani 0:c05b00be2229 37 #define WM8731_CS_LOW false
msamadani 0:c05b00be2229 38
msamadani 0:c05b00be2229 39 #define WM8731_ON true
msamadani 0:c05b00be2229 40 #define WM8731_OFF false
msamadani 0:c05b00be2229 41
msamadani 0:c05b00be2229 42 #define WM8731_MUTE true
msamadani 0:c05b00be2229 43 #define WM8731_UNMUTE false
msamadani 0:c05b00be2229 44
msamadani 0:c05b00be2229 45 #define WM8731_MASTER true
msamadani 0:c05b00be2229 46 #define WM8731_SLAVE false
msamadani 0:c05b00be2229 47
msamadani 0:c05b00be2229 48 #define WM8731_LINE 0
msamadani 0:c05b00be2229 49 #define WM8731_MIC 1
msamadani 0:c05b00be2229 50 #define WM8731_NO_IN -1
msamadani 0:c05b00be2229 51
msamadani 0:c05b00be2229 52 #define WM8731_DE_EMPH_DISABLED 0
msamadani 0:c05b00be2229 53 #define WM8731_DE_EMPH_32KHZ 1
msamadani 0:c05b00be2229 54 #define WM8731_DE_EMPH_44KHZ 2
msamadani 0:c05b00be2229 55 #define WM8731_DE_EMPH_48KHZ 3
msamadani 0:c05b00be2229 56
msamadani 0:c05b00be2229 57 /** A class to control the I2C part of the WM8731
msamadani 0:c05b00be2229 58 *
msamadani 0:c05b00be2229 59 */
msamadani 0:c05b00be2229 60 class WM8731 {
msamadani 0:c05b00be2229 61 public:
msamadani 0:c05b00be2229 62
msamadani 0:c05b00be2229 63 /** Create an instance of the WM8731 class
msamadani 0:c05b00be2229 64 *
msamadani 0:c05b00be2229 65 * @param i2c_sda The SDA pin of the I2C
msamadani 0:c05b00be2229 66 * @param i2c_scl The SCL pin of the I2C
msamadani 0:c05b00be2229 67 */
msamadani 0:c05b00be2229 68 WM8731(PinName i2c_sda, PinName i2c_scl);
msamadani 0:c05b00be2229 69
msamadani 0:c05b00be2229 70 /** Create an instance of the WM8731 class
msamadani 0:c05b00be2229 71 *
msamadani 0:c05b00be2229 72 * @param i2c_sda The SDA pin of the I2C
msamadani 0:c05b00be2229 73 * @param i2c_scl The SCL pin of the I2C
msamadani 0:c05b00be2229 74 * @param cs_level The level of the CS pin on the WM8731
msamadani 0:c05b00be2229 75 */
msamadani 0:c05b00be2229 76 WM8731(PinName i2c_sda, PinName i2c_scl, bool cs_level);
msamadani 0:c05b00be2229 77
msamadani 0:c05b00be2229 78 /** Control the power of the device
msamadani 0:c05b00be2229 79 *
msamadani 0:c05b00be2229 80 * @param on_off The power state
msamadani 0:c05b00be2229 81 */
msamadani 0:c05b00be2229 82 void power(bool on_off);
msamadani 0:c05b00be2229 83
msamadani 0:c05b00be2229 84 /** Control the input source of the device
msamadani 0:c05b00be2229 85 *
msamadani 0:c05b00be2229 86 * @param input Select the source of the input of the device: WM8731_LINE, WM8731_MIC, WM8731_NO_IN
msamadani 0:c05b00be2229 87 */
msamadani 0:c05b00be2229 88 void input_select(int input);
msamadani 0:c05b00be2229 89
msamadani 0:c05b00be2229 90 /** Set the headphone volume
msamadani 0:c05b00be2229 91 *
msamadani 0:c05b00be2229 92 * @param h_volume The desired headphone volume: 0->1
msamadani 0:c05b00be2229 93 */
msamadani 0:c05b00be2229 94 void headphone_volume(float h_volume);
msamadani 0:c05b00be2229 95
msamadani 0:c05b00be2229 96 /** Set the line in pre-amp volume
msamadani 0:c05b00be2229 97 *
msamadani 0:c05b00be2229 98 * @param li_volume The desired line in volume: 0->1
msamadani 0:c05b00be2229 99 */
msamadani 0:c05b00be2229 100 void linein_volume(float li_volume);
msamadani 0:c05b00be2229 101
msamadani 0:c05b00be2229 102 /** Turn on/off the microphone pre-amp boost
msamadani 0:c05b00be2229 103 *
msamadani 0:c05b00be2229 104 * @param mic_boost Boost on or off
msamadani 0:c05b00be2229 105 */
msamadani 0:c05b00be2229 106 void microphone_boost(bool mic_boost);
msamadani 0:c05b00be2229 107
msamadani 0:c05b00be2229 108 /** Mute the input
msamadani 0:c05b00be2229 109 *
msamadani 0:c05b00be2229 110 * @param mute Mute on/off
msamadani 0:c05b00be2229 111 */
msamadani 0:c05b00be2229 112 void input_mute(bool mute);
msamadani 0:c05b00be2229 113
msamadani 0:c05b00be2229 114 /** Mute the output
msamadani 0:c05b00be2229 115 *
msamadani 0:c05b00be2229 116 * @param mute Mute on/off
msamadani 0:c05b00be2229 117 */
msamadani 0:c05b00be2229 118 void output_mute(bool mute);
msamadani 0:c05b00be2229 119
msamadani 0:c05b00be2229 120 /** Turn on/off the input stage
msamadani 0:c05b00be2229 121 *
msamadani 0:c05b00be2229 122 * @param on_off Input stage on(true)/off(false)
msamadani 0:c05b00be2229 123 */
msamadani 0:c05b00be2229 124 void input_power(bool on_off);
msamadani 0:c05b00be2229 125
msamadani 0:c05b00be2229 126 /** Turn on/off the output stage
msamadani 0:c05b00be2229 127 *
msamadani 0:c05b00be2229 128 * @param on_off Output stage on(true)/off(false)
msamadani 0:c05b00be2229 129 */
msamadani 0:c05b00be2229 130 void output_power(bool on_off);
msamadani 0:c05b00be2229 131
msamadani 0:c05b00be2229 132 /** Select the word size
msamadani 0:c05b00be2229 133 *
msamadani 0:c05b00be2229 134 * @param words 16/20/24/32 bits
msamadani 0:c05b00be2229 135 */
msamadani 0:c05b00be2229 136 void wordsize(int words);
msamadani 0:c05b00be2229 137
msamadani 0:c05b00be2229 138 /** Select interface mode: Master or Slave
msamadani 0:c05b00be2229 139 *
msamadani 0:c05b00be2229 140 * @param master Interface mode: master(true)/slave
msamadani 0:c05b00be2229 141 */
msamadani 0:c05b00be2229 142 void master(bool master);
msamadani 0:c05b00be2229 143
msamadani 0:c05b00be2229 144 /** Select the sample rate
msamadani 0:c05b00be2229 145 *
msamadani 0:c05b00be2229 146 * @param freq Frequency: 96/48/32/8 kHz
msamadani 0:c05b00be2229 147 */
msamadani 0:c05b00be2229 148 void frequency(int freq);
msamadani 0:c05b00be2229 149
msamadani 0:c05b00be2229 150 /** Enable the input highpass filter
msamadani 0:c05b00be2229 151 *
msamadani 0:c05b00be2229 152 * @param enabled Input highpass filter enabled
msamadani 0:c05b00be2229 153 */
msamadani 0:c05b00be2229 154 void input_highpass(bool enabled);
msamadani 0:c05b00be2229 155
msamadani 0:c05b00be2229 156 /** Enable the output soft mute
msamadani 0:c05b00be2229 157 *
msamadani 0:c05b00be2229 158 * @param enabled Output soft mute enabled
msamadani 0:c05b00be2229 159 */
msamadani 0:c05b00be2229 160 void output_softmute(bool enabled);
msamadani 0:c05b00be2229 161
msamadani 0:c05b00be2229 162 /** Turn on and off the I2S
msamadani 0:c05b00be2229 163 *
msamadani 0:c05b00be2229 164 * @param on_off Switch the I2S interface on(true)/off(false)
msamadani 0:c05b00be2229 165 */
msamadani 0:c05b00be2229 166 void interface_switch(bool on_off);
msamadani 0:c05b00be2229 167
msamadani 0:c05b00be2229 168 /** Reset the device and settings
msamadani 0:c05b00be2229 169 *
msamadani 0:c05b00be2229 170 */
msamadani 0:c05b00be2229 171 void reset();
msamadani 0:c05b00be2229 172
msamadani 0:c05b00be2229 173 /** Set the microphone sidetone volume
msamadani 0:c05b00be2229 174 *
msamadani 0:c05b00be2229 175 * @param sidetone_volume The volume of the sidetone: 0->1
msamadani 0:c05b00be2229 176 */
msamadani 0:c05b00be2229 177 void sidetone(float sidetone_vol);
msamadani 0:c05b00be2229 178
msamadani 0:c05b00be2229 179 /** Set the analog bypass
msamadani 0:c05b00be2229 180 *
msamadani 0:c05b00be2229 181 * @param bypass_en Enable the bypass: enabled(true)
msamadani 0:c05b00be2229 182 */
msamadani 0:c05b00be2229 183 void bypass(bool bypass_en);
msamadani 0:c05b00be2229 184
msamadani 0:c05b00be2229 185 /** Set the deemphasis frequency
msamadani 0:c05b00be2229 186 *
msamadani 0:c05b00be2229 187 * @param code The deemphasis code: WM8731_DE_EMPH_DISABLED, WM8731_DE_EMPH_32KHZ, WM8731_DE_EMPH_44KHZ, WM8731_DE_EMPH_48KHZ
msamadani 0:c05b00be2229 188 */
msamadani 0:c05b00be2229 189 void deemphasis(char code);
msamadani 0:c05b00be2229 190
msamadani 0:c05b00be2229 191 /** Enable the input highpass filter
msamadani 0:c05b00be2229 192 *
msamadani 0:c05b00be2229 193 * @param enable Enable the input highpass filter enabled(true)
msamadani 0:c05b00be2229 194 */
msamadani 0:c05b00be2229 195
msamadani 0:c05b00be2229 196 void adc_highpass(bool enable);
msamadani 0:c05b00be2229 197
msamadani 0:c05b00be2229 198 /** Start the device sending/recieving etc
msamadani 0:c05b00be2229 199 */
msamadani 0:c05b00be2229 200 void start();
msamadani 0:c05b00be2229 201
msamadani 0:c05b00be2229 202 /** Stop the device sending/recieving etc
msamadani 0:c05b00be2229 203 */
msamadani 0:c05b00be2229 204 void stop();
msamadani 0:c05b00be2229 205
msamadani 0:c05b00be2229 206 private:
msamadani 0:c05b00be2229 207
msamadani 0:c05b00be2229 208 enum reg_address {
msamadani 0:c05b00be2229 209 line_in_vol_left = 0x00,
msamadani 0:c05b00be2229 210 line_in_vol_right = 0x01,
msamadani 0:c05b00be2229 211 headphone_vol_left = 0x02,
msamadani 0:c05b00be2229 212 headphone_vol_right = 0x03,
msamadani 0:c05b00be2229 213 path_analog = 0x04,
msamadani 0:c05b00be2229 214 path_digital = 0x05,
msamadani 0:c05b00be2229 215 power_control = 0x06,
msamadani 0:c05b00be2229 216 interface_format = 0x07,
msamadani 0:c05b00be2229 217 sample_rate = 0x08,
msamadani 0:c05b00be2229 218 interface_activation = 0x09,
msamadani 0:c05b00be2229 219 reset_reg = 0x0F,
msamadani 0:c05b00be2229 220 all = 0xFF
msamadani 0:c05b00be2229 221 };
msamadani 0:c05b00be2229 222
msamadani 0:c05b00be2229 223 enum tlv320_defaults{
msamadani 0:c05b00be2229 224 df_bypass_ = 0,
msamadani 0:c05b00be2229 225 df_ADC_source = WM8731_LINE,
msamadani 0:c05b00be2229 226 df_mic_mute = WM8731_UNMUTE,
msamadani 0:c05b00be2229 227 df_li_mute_left = 0,
msamadani 0:c05b00be2229 228 df_li_mute_right = 0,
msamadani 0:c05b00be2229 229 df_mic_boost_ = 0,
msamadani 0:c05b00be2229 230 df_out_mute = WM8731_UNMUTE,
msamadani 0:c05b00be2229 231
msamadani 0:c05b00be2229 232 df_de_emph_code = 0x00,
msamadani 0:c05b00be2229 233 df_ADC_highpass_enable = 0,
msamadani 0:c05b00be2229 234
msamadani 0:c05b00be2229 235 df_device_all_pwr = 1,
msamadani 0:c05b00be2229 236 df_device_clk_pwr = 1,
msamadani 0:c05b00be2229 237 df_device_osc_pwr = 1,
msamadani 0:c05b00be2229 238 df_device_out_pwr = 1,
msamadani 0:c05b00be2229 239 df_device_dac_pwr = 1,
msamadani 0:c05b00be2229 240 df_device_adc_pwr = 1,
msamadani 0:c05b00be2229 241 df_device_mic_pwr = 0,
msamadani 0:c05b00be2229 242 df_device_lni_pwr = 1,
msamadani 0:c05b00be2229 243
msamadani 0:c05b00be2229 244 df_device_master = 0,
msamadani 0:c05b00be2229 245 df_device_lrswap = 0,
msamadani 0:c05b00be2229 246 df_device_lrws = 0,
msamadani 0:c05b00be2229 247 df_device_bitlength = 32,
msamadani 0:c05b00be2229 248
msamadani 0:c05b00be2229 249 df_ADC_rate = 32000,
msamadani 0:c05b00be2229 250 df_DAC_rate = 32000,
msamadani 0:c05b00be2229 251
msamadani 0:c05b00be2229 252 df_device_interface_active = 0
msamadani 0:c05b00be2229 253 };
msamadani 0:c05b00be2229 254
msamadani 0:c05b00be2229 255
msamadani 0:c05b00be2229 256 I2C i2c;
msamadani 0:c05b00be2229 257 uint8_t address;
msamadani 0:c05b00be2229 258 void command(reg_address add, uint16_t byte);
msamadani 0:c05b00be2229 259 void form_cmd(reg_address add);
msamadani 0:c05b00be2229 260 void defaulter();
msamadani 0:c05b00be2229 261
msamadani 0:c05b00be2229 262 char gen_samplerate();
msamadani 0:c05b00be2229 263
msamadani 0:c05b00be2229 264 //I2S i2s_tx(I2S_TRANSMIT, p5, p6 , p7);
msamadani 0:c05b00be2229 265 //I2S i2s_rx(I2S_RECIEVE , p8, p29, p30);
msamadani 0:c05b00be2229 266
msamadani 0:c05b00be2229 267 float hp_vol_left, hp_vol_right;
msamadani 0:c05b00be2229 268 float li_vol_left, li_vol_right;
msamadani 0:c05b00be2229 269 float sdt_vol;
msamadani 0:c05b00be2229 270 bool li_mute_left, li_mute_right;
msamadani 0:c05b00be2229 271 bool bypass_;
msamadani 0:c05b00be2229 272 bool ADC_source;
msamadani 0:c05b00be2229 273 bool ADC_source_old;
msamadani 0:c05b00be2229 274 bool mic_mute;
msamadani 0:c05b00be2229 275 bool mic_boost_;
msamadani 0:c05b00be2229 276 bool out_mute;
msamadani 0:c05b00be2229 277 char de_emph_code;
msamadani 0:c05b00be2229 278 bool ADC_highpass_enable;
msamadani 0:c05b00be2229 279
msamadani 0:c05b00be2229 280 bool device_all_pwr;
msamadani 0:c05b00be2229 281 bool device_clk_pwr;
msamadani 0:c05b00be2229 282 bool device_osc_pwr;
msamadani 0:c05b00be2229 283 bool device_out_pwr;
msamadani 0:c05b00be2229 284 bool device_dac_pwr;
msamadani 0:c05b00be2229 285 bool device_adc_pwr;
msamadani 0:c05b00be2229 286 bool device_mic_pwr;
msamadani 0:c05b00be2229 287 bool device_lni_pwr;
msamadani 0:c05b00be2229 288
msamadani 0:c05b00be2229 289 bool device_master;
msamadani 0:c05b00be2229 290 bool device_lrswap;
msamadani 0:c05b00be2229 291 bool device_lrws;
msamadani 0:c05b00be2229 292 char device_bitlength;
msamadani 0:c05b00be2229 293 static const char device_data_form = 0x02;
msamadani 0:c05b00be2229 294
msamadani 0:c05b00be2229 295 int ADC_rate;
msamadani 0:c05b00be2229 296 int DAC_rate;
msamadani 0:c05b00be2229 297 static const bool device_usb_mode = false;
msamadani 0:c05b00be2229 298 static const bool device_clk_in_div = false;
msamadani 0:c05b00be2229 299 static const bool device_clk_out_div = false;
msamadani 0:c05b00be2229 300 bool device_interface_active;
msamadani 0:c05b00be2229 301 };
msamadani 0:c05b00be2229 302
msamadani 0:c05b00be2229 303 #endif