does not work yet. Sound starts but then stops after a few seconds, whole thing hangs. Published so as I can import through mbed CLI.
Dependencies: mbed sinelookup SDFileSystem_Copy_of_mbed_version I2S
wolfson_3_wav.cpp@7:fb47ff047ac7, 2018-10-28 (annotated)
- Committer:
- roryhand
- Date:
- Sun Oct 28 21:31:58 2018 +0000
- Revision:
- 7:fb47ff047ac7
- Parent:
- 6:fd6ae534d120
- Child:
- 8:40f37287589a
we are now playing sound throughg codec with intwrrupt and block processing. however, it is either stopping file early, OR irunning it way too fast.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roryhand | 0:004cb0595aa8 | 1 | |
roryhand | 0:004cb0595aa8 | 2 | // 24/03/2018 update - I appear to be able to address the device and write something, as I am getting an ACK returned from the i2c write() function. |
roryhand | 0:004cb0595aa8 | 3 | //however if i use the write function with 4 arguments (as opposed to just 1 argument) then it doesnt work |
roryhand | 0:004cb0595aa8 | 4 | //only works with the 1 argument version!!! |
roryhand | 0:004cb0595aa8 | 5 | |
roryhand | 0:004cb0595aa8 | 6 | |
roryhand | 0:004cb0595aa8 | 7 | //THIS VERSION WORKED, CHANGED SOME THINGS, THEN CHANGED THEM BACK. NOW IT NO LONGER WORKS!!!! |
roryhand | 0:004cb0595aa8 | 8 | #include "mbed.h" |
roryhand | 0:004cb0595aa8 | 9 | #include "sinelookup.h" |
roryhand | 0:004cb0595aa8 | 10 | #include "I2S.h" |
roryhand | 0:004cb0595aa8 | 11 | #define sample_freq 48000 |
roryhand | 0:004cb0595aa8 | 12 | DigitalOut myled(LED1); |
roryhand | 0:004cb0595aa8 | 13 | DigitalOut led2(LED2); |
roryhand | 0:004cb0595aa8 | 14 | DigitalOut led3(LED3); |
roryhand | 0:004cb0595aa8 | 15 | Ticker sampletick; |
roryhand | 0:004cb0595aa8 | 16 | Timer t; |
roryhand | 6:fd6ae534d120 | 17 | Timer t2; |
roryhand | 0:004cb0595aa8 | 18 | #include "SDFileSystem.h" |
roryhand | 0:004cb0595aa8 | 19 | #include "WOLFSON_config.h" |
roryhand | 0:004cb0595aa8 | 20 | Serial pc(USBTX, USBRX); // tx, rx //FOR DEBUGGING PROGRAM USING GNU SCREEN |
roryhand | 0:004cb0595aa8 | 21 | DigitalOut cs(p8); |
roryhand | 0:004cb0595aa8 | 22 | I2S i2s(I2S_TRANSMIT,p5,p6,p7); |
roryhand | 0:004cb0595aa8 | 23 | I2C i2c(p9, p10); |
roryhand | 0:004cb0595aa8 | 24 | |
roryhand | 0:004cb0595aa8 | 25 | SDFileSystem sd(p11, p12, p13, p8, "sd"); // the new pinout that i am using |
roryhand | 0:004cb0595aa8 | 26 | |
roryhand | 0:004cb0595aa8 | 27 | typedef struct uFMT_STRUCT{ |
roryhand | 0:004cb0595aa8 | 28 | short comp_code; |
roryhand | 0:004cb0595aa8 | 29 | short num_channels; |
roryhand | 0:004cb0595aa8 | 30 | unsigned sample_rate; |
roryhand | 0:004cb0595aa8 | 31 | unsigned avg_Bps; |
roryhand | 0:004cb0595aa8 | 32 | short block_align; |
roryhand | 0:004cb0595aa8 | 33 | short sig_bps; |
roryhand | 0:004cb0595aa8 | 34 | } FMT_STRUCT; |
roryhand | 0:004cb0595aa8 | 35 | |
roryhand | 0:004cb0595aa8 | 36 | typedef struct uDATA_STRUCT { |
roryhand | 0:004cb0595aa8 | 37 | unsigned subchunk2_ID; |
roryhand | 0:004cb0595aa8 | 38 | unsigned subchunk2_size; |
roryhand | 0:004cb0595aa8 | 39 | char * data_buf; |
roryhand | 0:004cb0595aa8 | 40 | }DATA_STRUCT; |
roryhand | 0:004cb0595aa8 | 41 | short hello; |
roryhand | 0:004cb0595aa8 | 42 | int i = 0; |
roryhand | 0:004cb0595aa8 | 43 | int h = 0; |
roryhand | 7:fb47ff047ac7 | 44 | short bufflen = 1; |
roryhand | 0:004cb0595aa8 | 45 | int buffer[1]; |
roryhand | 0:004cb0595aa8 | 46 | int AudioFormat, NumChannels, SampleRate, BitsPerSample ; |
roryhand | 0:004cb0595aa8 | 47 | char *slice_buf; |
roryhand | 5:5b120a2d9249 | 48 | short *data_sptr; |
roryhand | 3:6fba0a044d85 | 49 | unsigned char *data_bptr; |
roryhand | 3:6fba0a044d85 | 50 | int *data_wptr; |
roryhand | 3:6fba0a044d85 | 51 | unsigned channel; |
roryhand | 0:004cb0595aa8 | 52 | int newvar; |
roryhand | 3:6fba0a044d85 | 53 | long slice, num_slices; |
roryhand | 3:6fba0a044d85 | 54 | int verbosity = 0; |
roryhand | 7:fb47ff047ac7 | 55 | int interrupt_condition = 1; |
roryhand | 6:fd6ae534d120 | 56 | int sampling_freq = 48000; |
roryhand | 7:fb47ff047ac7 | 57 | short Buffer1[8]; |
roryhand | 7:fb47ff047ac7 | 58 | short Buffer2[8]; |
roryhand | 7:fb47ff047ac7 | 59 | short place_hold1 = 0; |
roryhand | 7:fb47ff047ac7 | 60 | short place_hold2 = 0; |
roryhand | 7:fb47ff047ac7 | 61 | int flag1 = 1; |
roryhand | 7:fb47ff047ac7 | 62 | int flag2 = 0; |
roryhand | 7:fb47ff047ac7 | 63 | int flag3 = 1; |
roryhand | 7:fb47ff047ac7 | 64 | int flag4 = 0; |
roryhand | 7:fb47ff047ac7 | 65 | short value[1]; |
roryhand | 0:004cb0595aa8 | 66 | //long long slice_value; |
roryhand | 5:5b120a2d9249 | 67 | int slice_value[1]; |
roryhand | 0:004cb0595aa8 | 68 | FILE *my_wav; |
roryhand | 0:004cb0595aa8 | 69 | FMT_STRUCT wav_format; |
roryhand | 0:004cb0595aa8 | 70 | DATA_STRUCT wav_data; |
roryhand | 0:004cb0595aa8 | 71 | |
roryhand | 0:004cb0595aa8 | 72 | Ticker flipper; |
roryhand | 0:004cb0595aa8 | 73 | //test |
roryhand | 0:004cb0595aa8 | 74 | void flip() { |
roryhand | 0:004cb0595aa8 | 75 | led2 = !led2; |
roryhand | 0:004cb0595aa8 | 76 | } |
roryhand | 0:004cb0595aa8 | 77 | |
roryhand | 0:004cb0595aa8 | 78 | |
roryhand | 0:004cb0595aa8 | 79 | void isr() |
roryhand | 0:004cb0595aa8 | 80 | { |
roryhand | 7:fb47ff047ac7 | 81 | //buffer[0] = (*data_sptr)>>1; |
roryhand | 7:fb47ff047ac7 | 82 | //myled = 1; |
roryhand | 7:fb47ff047ac7 | 83 | //int bufflen = sizeof(buffer); |
roryhand | 5:5b120a2d9249 | 84 | if(verbosity){ |
roryhand | 4:1a9482a07ee2 | 85 | printf("buffer data: %d \n\r",buffer); |
roryhand | 7:fb47ff047ac7 | 86 | printf("data_sptr data: %d",data_sptr); |
roryhand | 7:fb47ff047ac7 | 87 | short value = Buffer1[0]; |
roryhand | 7:fb47ff047ac7 | 88 | printf("value: %d",value); |
roryhand | 5:5b120a2d9249 | 89 | } |
roryhand | 7:fb47ff047ac7 | 90 | //t.start(); |
roryhand | 6:fd6ae534d120 | 91 | |
roryhand | 7:fb47ff047ac7 | 92 | //t.stop(); |
roryhand | 7:fb47ff047ac7 | 93 | //printf("The time taken was %f seconds\n", t.read()); |
roryhand | 7:fb47ff047ac7 | 94 | //t.reset(); |
roryhand | 7:fb47ff047ac7 | 95 | //myled = 0; |
roryhand | 7:fb47ff047ac7 | 96 | if(flag1 == 0) |
roryhand | 7:fb47ff047ac7 | 97 | { |
roryhand | 7:fb47ff047ac7 | 98 | //access double buffer pt1 |
roryhand | 7:fb47ff047ac7 | 99 | value[0] = Buffer1[place_hold1]; |
roryhand | 7:fb47ff047ac7 | 100 | i2s.write(value,1);//Send next PWM value to amp |
roryhand | 7:fb47ff047ac7 | 101 | place_hold1 = place_hold1 + 1; |
roryhand | 7:fb47ff047ac7 | 102 | if(place_hold1 == 8) |
roryhand | 7:fb47ff047ac7 | 103 | { |
roryhand | 7:fb47ff047ac7 | 104 | place_hold1 = 0; |
roryhand | 7:fb47ff047ac7 | 105 | place_hold2 = 0; |
roryhand | 7:fb47ff047ac7 | 106 | flag1 = 1; |
roryhand | 7:fb47ff047ac7 | 107 | flag2 = 0; |
roryhand | 7:fb47ff047ac7 | 108 | flag3 = 1; |
roryhand | 7:fb47ff047ac7 | 109 | } |
roryhand | 7:fb47ff047ac7 | 110 | if(verbosity == 1) |
roryhand | 7:fb47ff047ac7 | 111 | { |
roryhand | 7:fb47ff047ac7 | 112 | printf("value Buffer1: %d\n\r",value[0]); |
roryhand | 7:fb47ff047ac7 | 113 | } |
roryhand | 7:fb47ff047ac7 | 114 | } |
roryhand | 7:fb47ff047ac7 | 115 | else if(flag2 == 0) |
roryhand | 7:fb47ff047ac7 | 116 | { |
roryhand | 7:fb47ff047ac7 | 117 | //access double buffer pt2 |
roryhand | 7:fb47ff047ac7 | 118 | value[0] = Buffer1[place_hold2]; |
roryhand | 7:fb47ff047ac7 | 119 | i2s.write(value,1);//Send next PWM value to amp |
roryhand | 7:fb47ff047ac7 | 120 | place_hold2 = place_hold2 + 1; |
roryhand | 7:fb47ff047ac7 | 121 | if(place_hold2 == 8) |
roryhand | 7:fb47ff047ac7 | 122 | { |
roryhand | 7:fb47ff047ac7 | 123 | place_hold1 = 0; |
roryhand | 7:fb47ff047ac7 | 124 | place_hold2 = 0; |
roryhand | 7:fb47ff047ac7 | 125 | flag1 = 0; |
roryhand | 7:fb47ff047ac7 | 126 | flag2 = 1; |
roryhand | 7:fb47ff047ac7 | 127 | flag3 = 1; |
roryhand | 7:fb47ff047ac7 | 128 | } |
roryhand | 7:fb47ff047ac7 | 129 | if(verbosity == 1) |
roryhand | 7:fb47ff047ac7 | 130 | { |
roryhand | 7:fb47ff047ac7 | 131 | printf("value Buffer2: %d\n\r",value[0]); |
roryhand | 7:fb47ff047ac7 | 132 | } |
roryhand | 0:004cb0595aa8 | 133 | } |
roryhand | 7:fb47ff047ac7 | 134 | |
roryhand | 7:fb47ff047ac7 | 135 | } |
roryhand | 0:004cb0595aa8 | 136 | |
roryhand | 1:ec4e2020522c | 137 | |
roryhand | 0:004cb0595aa8 | 138 | |
roryhand | 0:004cb0595aa8 | 139 | void wm8731_setup(int chip_addr, int addr, int cmd){ |
roryhand | 0:004cb0595aa8 | 140 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 141 | addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations |
roryhand | 0:004cb0595aa8 | 142 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 143 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 144 | i2c.write( chip_addr ); |
roryhand | 0:004cb0595aa8 | 145 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 146 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 147 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 148 | } |
roryhand | 0:004cb0595aa8 | 149 | |
roryhand | 0:004cb0595aa8 | 150 | |
roryhand | 0:004cb0595aa8 | 151 | void wm8731_config(void){ |
roryhand | 0:004cb0595aa8 | 152 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_RESET, _WM8731_RESET );//1 |
roryhand | 0:004cb0595aa8 | 153 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_LLINE_IN, _WM8731_LEFT_LINEIN );//2 |
roryhand | 0:004cb0595aa8 | 154 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_RLINE_IN, _WM8731_RIGHT_LINEIN );//3 |
roryhand | 0:004cb0595aa8 | 155 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP );//4 |
roryhand | 0:004cb0595aa8 | 156 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_RHPHONE_OUT, _WM8731_RIGHT_HP );//5 |
roryhand | 0:004cb0595aa8 | 157 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO );//6 |
roryhand | 0:004cb0595aa8 | 158 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO );//7 |
roryhand | 0:004cb0595aa8 | 159 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_PDOWN_CTRL, _WM8731_POWER );//8 |
roryhand | 0:004cb0595aa8 | 160 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_DIGITAL_IF, _WM8731_DAIF );//9 |
roryhand | 0:004cb0595aa8 | 161 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING );//10 |
roryhand | 0:004cb0595aa8 | 162 | } |
roryhand | 0:004cb0595aa8 | 163 | |
roryhand | 0:004cb0595aa8 | 164 | void wm8731_configTEST(void){ |
roryhand | 0:004cb0595aa8 | 165 | int addr = WM8731_REG_RESET; |
roryhand | 0:004cb0595aa8 | 166 | int cmd = _WM8731_RESET; |
roryhand | 0:004cb0595aa8 | 167 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 168 | addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations |
roryhand | 0:004cb0595aa8 | 169 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 170 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 171 | i2c.write( WM8731_ADDRESS ); |
roryhand | 0:004cb0595aa8 | 172 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 173 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 174 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 175 | |
roryhand | 0:004cb0595aa8 | 176 | addr = WM8731_REG_LLINE_IN; |
roryhand | 0:004cb0595aa8 | 177 | cmd = _WM8731_LEFT_LINEIN; |
roryhand | 0:004cb0595aa8 | 178 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 179 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 180 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 181 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 182 | i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program |
roryhand | 0:004cb0595aa8 | 183 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 184 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 185 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 186 | |
roryhand | 0:004cb0595aa8 | 187 | |
roryhand | 0:004cb0595aa8 | 188 | addr = WM8731_REG_RLINE_IN; |
roryhand | 0:004cb0595aa8 | 189 | cmd = _WM8731_RIGHT_LINEIN; |
roryhand | 0:004cb0595aa8 | 190 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 191 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 192 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 193 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 194 | i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program |
roryhand | 0:004cb0595aa8 | 195 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 196 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 197 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 198 | |
roryhand | 0:004cb0595aa8 | 199 | |
roryhand | 0:004cb0595aa8 | 200 | //4 |
roryhand | 0:004cb0595aa8 | 201 | addr = WM8731_REG_LHPHONE_OUT; |
roryhand | 0:004cb0595aa8 | 202 | cmd = _WM8731_LEFT_HP; |
roryhand | 0:004cb0595aa8 | 203 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 204 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 205 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 206 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 207 | i2c.write( WM8731_ADDRESS ); |
roryhand | 0:004cb0595aa8 | 208 | i2c.write( addr );//WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP |
roryhand | 0:004cb0595aa8 | 209 | i2c.write(cmd); |
roryhand | 0:004cb0595aa8 | 210 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 211 | |
roryhand | 0:004cb0595aa8 | 212 | //5 |
roryhand | 0:004cb0595aa8 | 213 | addr = WM8731_REG_RHPHONE_OUT; |
roryhand | 0:004cb0595aa8 | 214 | cmd = _WM8731_RIGHT_HP; |
roryhand | 0:004cb0595aa8 | 215 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 216 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 217 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 218 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 219 | i2c.write( WM8731_ADDRESS );//WM8731_REG_RHPHONE_OUT, _WM8731_RIGHT_HP |
roryhand | 0:004cb0595aa8 | 220 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 221 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 222 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 223 | |
roryhand | 0:004cb0595aa8 | 224 | |
roryhand | 0:004cb0595aa8 | 225 | //6 |
roryhand | 0:004cb0595aa8 | 226 | addr = WM8731_REG_ANALOG_PATH; |
roryhand | 0:004cb0595aa8 | 227 | cmd = _WM8731_ANALOGAUDIO; |
roryhand | 0:004cb0595aa8 | 228 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 229 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 230 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 231 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 232 | i2c.write( WM8731_ADDRESS );//WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO |
roryhand | 0:004cb0595aa8 | 233 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 234 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 235 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 236 | |
roryhand | 0:004cb0595aa8 | 237 | //7 |
roryhand | 0:004cb0595aa8 | 238 | addr = WM8731_REG_DIGITAL_PATH; |
roryhand | 0:004cb0595aa8 | 239 | cmd = _WM8731_DIGITALAUDIO; |
roryhand | 0:004cb0595aa8 | 240 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 241 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 242 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 243 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 244 | i2c.write( WM8731_ADDRESS );//WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO |
roryhand | 0:004cb0595aa8 | 245 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 246 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 247 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 248 | |
roryhand | 0:004cb0595aa8 | 249 | //8 |
roryhand | 0:004cb0595aa8 | 250 | addr = WM8731_REG_PDOWN_CTRL; |
roryhand | 0:004cb0595aa8 | 251 | cmd = _WM8731_POWER; |
roryhand | 0:004cb0595aa8 | 252 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 253 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 254 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 255 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 256 | i2c.write( WM8731_ADDRESS );//WM8731_REG_PDOWN_CTRL, _WM8731_POWER) |
roryhand | 0:004cb0595aa8 | 257 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 258 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 259 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 260 | |
roryhand | 0:004cb0595aa8 | 261 | |
roryhand | 0:004cb0595aa8 | 262 | //9 |
roryhand | 0:004cb0595aa8 | 263 | addr = WM8731_REG_DIGITAL_IF; |
roryhand | 0:004cb0595aa8 | 264 | cmd = _WM8731_DAIF; |
roryhand | 0:004cb0595aa8 | 265 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 266 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 267 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 268 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 269 | i2c.write( WM8731_ADDRESS ); |
roryhand | 0:004cb0595aa8 | 270 | i2c.write( addr );//WM8731_REG_DIGITAL_IF, _WM8731_DAIF |
roryhand | 0:004cb0595aa8 | 271 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 272 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 273 | |
roryhand | 0:004cb0595aa8 | 274 | |
roryhand | 0:004cb0595aa8 | 275 | //10 |
roryhand | 0:004cb0595aa8 | 276 | addr = WM8731_REG_SAMPLING_CTRL; |
roryhand | 0:004cb0595aa8 | 277 | cmd = _WM8731_SAMPLING; |
roryhand | 0:004cb0595aa8 | 278 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 279 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 280 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 281 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 282 | i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING |
roryhand | 0:004cb0595aa8 | 283 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 284 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 285 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 286 | |
roryhand | 0:004cb0595aa8 | 287 | |
roryhand | 0:004cb0595aa8 | 288 | |
roryhand | 0:004cb0595aa8 | 289 | |
roryhand | 0:004cb0595aa8 | 290 | //11 |
roryhand | 0:004cb0595aa8 | 291 | addr = WM8731_REG_ACTIVE_CTRL; |
roryhand | 0:004cb0595aa8 | 292 | cmd = _WM8731_ACTIVATE; |
roryhand | 0:004cb0595aa8 | 293 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 294 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 295 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 296 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 297 | i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING |
roryhand | 0:004cb0595aa8 | 298 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 299 | i2c.write( cmd );//there is also a deactive version!! |
roryhand | 0:004cb0595aa8 | 300 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 301 | |
roryhand | 0:004cb0595aa8 | 302 | |
roryhand | 0:004cb0595aa8 | 303 | |
roryhand | 0:004cb0595aa8 | 304 | } |
roryhand | 0:004cb0595aa8 | 305 | |
roryhand | 0:004cb0595aa8 | 306 | int main() { |
roryhand | 0:004cb0595aa8 | 307 | FMT_STRUCT wav_format; |
roryhand | 0:004cb0595aa8 | 308 | char data[2]; |
roryhand | 0:004cb0595aa8 | 309 | pc.printf("Beginning of program\n"); |
roryhand | 0:004cb0595aa8 | 310 | data[0] = 0x0F; |
roryhand | 0:004cb0595aa8 | 311 | data[1] = 0x00; |
roryhand | 0:004cb0595aa8 | 312 | FILE *my_wav; |
roryhand | 0:004cb0595aa8 | 313 | //my_wav = fopen("/sd/mydir/gp40.wav", "rb");//i think that it is "w" for write, "r" for read. |
roryhand | 7:fb47ff047ac7 | 314 | my_wav = fopen("/sd/mydir/gp40_48K.wav", "rb");//i think that it is "w" for write, "r" for read. |
roryhand | 5:5b120a2d9249 | 315 | //my_wav = fopen("/sd/mydir/gp40_48K_quieter.wav", "rb");//i think that it is "w" for write, "r" for read. |
roryhand | 7:fb47ff047ac7 | 316 | //my_wav = fopen("/sd/mydir/gp40_8K.wav","rb"); |
roryhand | 7:fb47ff047ac7 | 317 | //my_wav = fopen("/sd/mydir/gp40_8K_2018.wav","rb"); |
roryhand | 5:5b120a2d9249 | 318 | //my_wav = fopen("/sd/mydir/emd_645.wav", "rb"); |
roryhand | 0:004cb0595aa8 | 319 | fseek(my_wav, 20, SEEK_SET); // set pointer to byte 20 |
roryhand | 0:004cb0595aa8 | 320 | fread(&AudioFormat, 2, 1, my_wav); // check file is PCM |
roryhand | 0:004cb0595aa8 | 321 | if (AudioFormat==0x01) { |
roryhand | 0:004cb0595aa8 | 322 | printf("Wav file is PCM data\n\r"); |
roryhand | 0:004cb0595aa8 | 323 | } |
roryhand | 0:004cb0595aa8 | 324 | else { |
roryhand | 0:004cb0595aa8 | 325 | printf("Wav file is not PCM data\n\r"); |
roryhand | 0:004cb0595aa8 | 326 | } |
roryhand | 0:004cb0595aa8 | 327 | |
roryhand | 0:004cb0595aa8 | 328 | fseek (my_wav,20,SEEK_SET); |
roryhand | 0:004cb0595aa8 | 329 | fread(&wav_format,sizeof(wav_format),1,my_wav); |
roryhand | 0:004cb0595aa8 | 330 | printf("%d block align\n",wav_format.block_align); |
roryhand | 0:004cb0595aa8 | 331 | printf(" %d channels\n",wav_format.num_channels); |
roryhand | 0:004cb0595aa8 | 332 | |
roryhand | 0:004cb0595aa8 | 333 | |
roryhand | 0:004cb0595aa8 | 334 | fseek(my_wav, 36, SEEK_SET); |
roryhand | 0:004cb0595aa8 | 335 | fread(&wav_data.subchunk2_ID,4,1,my_wav); |
roryhand | 0:004cb0595aa8 | 336 | fseek(my_wav, 40, SEEK_SET); |
roryhand | 0:004cb0595aa8 | 337 | fread(&wav_data.subchunk2_size,4,1,my_wav); |
roryhand | 0:004cb0595aa8 | 338 | printf("DATA chunck\n"); |
roryhand | 0:004cb0595aa8 | 339 | printf(" %d subchunk 2 ID\n",wav_data.subchunk2_ID); |
roryhand | 0:004cb0595aa8 | 340 | printf(" %d subchunk 2 size\n",wav_data.subchunk2_size); |
roryhand | 0:004cb0595aa8 | 341 | |
roryhand | 0:004cb0595aa8 | 342 | |
roryhand | 0:004cb0595aa8 | 343 | |
roryhand | 0:004cb0595aa8 | 344 | |
roryhand | 0:004cb0595aa8 | 345 | |
roryhand | 0:004cb0595aa8 | 346 | |
roryhand | 0:004cb0595aa8 | 347 | i2c.frequency(100000); |
roryhand | 0:004cb0595aa8 | 348 | long j = 0; |
roryhand | 0:004cb0595aa8 | 349 | |
roryhand | 0:004cb0595aa8 | 350 | printf("set up the codec\n\r"); |
roryhand | 3:6fba0a044d85 | 351 | while(j <1000){ |
roryhand | 0:004cb0595aa8 | 352 | wm8731_configTEST(); |
roryhand | 3:6fba0a044d85 | 353 | j = j + 1; |
roryhand | 3:6fba0a044d85 | 354 | } |
roryhand | 0:004cb0595aa8 | 355 | //i2s audio data transfer code?? |
roryhand | 0:004cb0595aa8 | 356 | i2s.stereomono(I2S_STEREO); |
roryhand | 0:004cb0595aa8 | 357 | i2s.masterslave(I2S_MASTER);//MASTER definitely works! |
roryhand | 0:004cb0595aa8 | 358 | i2s.frequency(sample_freq); |
roryhand | 0:004cb0595aa8 | 359 | int yes = i2s.setup_ok(); |
roryhand | 0:004cb0595aa8 | 360 | pc.printf("Setup went ok?: %d\n", yes);//0 = ack, 1 = Nack*/ |
roryhand | 0:004cb0595aa8 | 361 | printf("can we get to this point? 1\n\r"); |
roryhand | 0:004cb0595aa8 | 362 | led3 = 1; |
roryhand | 0:004cb0595aa8 | 363 | //myled = 1; |
roryhand | 0:004cb0595aa8 | 364 | led2 = 1; |
roryhand | 0:004cb0595aa8 | 365 | printf("can we get to this point? 2\n\r"); |
roryhand | 0:004cb0595aa8 | 366 | |
roryhand | 0:004cb0595aa8 | 367 | myled = 1; |
roryhand | 0:004cb0595aa8 | 368 | i2s.start(); |
roryhand | 0:004cb0595aa8 | 369 | //while (1) { |
roryhand | 0:004cb0595aa8 | 370 | fread(&wav_format,sizeof(wav_format),1,my_wav); |
roryhand | 0:004cb0595aa8 | 371 | printf("wav_format %d\n\r",wav_format); |
roryhand | 1:ec4e2020522c | 372 | |
roryhand | 0:004cb0595aa8 | 373 | |
roryhand | 0:004cb0595aa8 | 374 | fseek(my_wav,20,SEEK_SET); |
roryhand | 0:004cb0595aa8 | 375 | fread(&wav_format,sizeof(wav_format),1,my_wav); |
roryhand | 0:004cb0595aa8 | 376 | fseek(my_wav,36,SEEK_SET); |
roryhand | 0:004cb0595aa8 | 377 | fread(&wav_data,sizeof(wav_data),1,my_wav); |
roryhand | 2:05b426733282 | 378 | int num_slices = wav_data.subchunk2_size/wav_format.block_align; |
roryhand | 0:004cb0595aa8 | 379 | printf("wav_data.subchunk2_size: %d\n\r",wav_data.subchunk2_size); |
roryhand | 0:004cb0595aa8 | 380 | printf("wav_format.block_align: %d\n\r",wav_format.block_align); |
roryhand | 0:004cb0595aa8 | 381 | printf("num_slices: %d\n\r",num_slices); |
roryhand | 0:004cb0595aa8 | 382 | printf("num_slices*wav_format.block_align %d\n\r",num_slices*wav_format.block_align); |
roryhand | 0:004cb0595aa8 | 383 | printf("wav_format.num_channels*wav_format.sig_bps/8: %d\n\r",wav_format.num_channels*wav_format.sig_bps/8); |
roryhand | 3:6fba0a044d85 | 384 | printf("chunk_size - sizeof(wav_format) %d\n\r",wav_data.subchunk2_size-sizeof(wav_format)); |
roryhand | 0:004cb0595aa8 | 385 | printf("sizeof(wav_format): %d\n\r",sizeof(wav_format)); |
roryhand | 3:6fba0a044d85 | 386 | printf("do we egt to this point:\n\r"); |
roryhand | 6:fd6ae534d120 | 387 | if (interrupt_condition == 1) |
roryhand | 6:fd6ae534d120 | 388 | { |
roryhand | 7:fb47ff047ac7 | 389 | sampletick.attach(&isr,1.0/16000); //1/16000 |
roryhand | 7:fb47ff047ac7 | 390 | } |
roryhand | 3:6fba0a044d85 | 391 | slice_buf=(char *)malloc(wav_format.block_align); |
roryhand | 7:fb47ff047ac7 | 392 | while(1){ |
roryhand | 7:fb47ff047ac7 | 393 | slice = 0; |
roryhand | 7:fb47ff047ac7 | 394 | while(flag3 == 1) |
roryhand | 7:fb47ff047ac7 | 395 | { |
roryhand | 7:fb47ff047ac7 | 396 | //for (slice=0;slice<num_slices;slice+=1) |
roryhand | 7:fb47ff047ac7 | 397 | while(slice<num_slices){ |
roryhand | 7:fb47ff047ac7 | 398 | //t2.start(); |
roryhand | 3:6fba0a044d85 | 399 | fread(slice_buf,wav_format.block_align,1,my_wav);//THIS IS WHERE HE READS IN THE DATA, TO SLICE_BUF. BUT USES WAV_FORMAT.BLOCKALIGN AS HIS SIZE GUIDE |
roryhand | 2:05b426733282 | 400 | //I do not understand why he is not "seeking" through the file first though... |
roryhand | 3:6fba0a044d85 | 401 | if (feof(my_wav)){ |
roryhand | 2:05b426733282 | 402 | printf("Oops -- not enough slices in the wave file\n"); |
roryhand | 2:05b426733282 | 403 | exit(1); |
roryhand | 2:05b426733282 | 404 | } |
roryhand | 5:5b120a2d9249 | 405 | data_sptr=(short *)slice_buf; // 16 bit samples |
roryhand | 2:05b426733282 | 406 | data_bptr=(unsigned char *)slice_buf; // 8 bit samples |
roryhand | 2:05b426733282 | 407 | data_wptr=(int *)slice_buf; // 32 bit samples |
roryhand | 5:5b120a2d9249 | 408 | slice_value[1]=0; |
roryhand | 2:05b426733282 | 409 | for (channel=0;channel<wav_format.num_channels;channel++) { |
roryhand | 2:05b426733282 | 410 | switch (wav_format.sig_bps) { |
roryhand | 2:05b426733282 | 411 | case 16: |
roryhand | 7:fb47ff047ac7 | 412 | //int mybuffer[1]; |
roryhand | 7:fb47ff047ac7 | 413 | //mybuffer[0] = data_sptr[channel];// - 32768; |
roryhand | 7:fb47ff047ac7 | 414 | if(flag1 == 1) |
roryhand | 7:fb47ff047ac7 | 415 | { |
roryhand | 7:fb47ff047ac7 | 416 | Buffer1[place_hold1] = data_sptr[channel]; |
roryhand | 7:fb47ff047ac7 | 417 | place_hold1 = place_hold1 + 1; |
roryhand | 7:fb47ff047ac7 | 418 | if(place_hold1 == 8) |
roryhand | 7:fb47ff047ac7 | 419 | { |
roryhand | 7:fb47ff047ac7 | 420 | flag4 = 1; |
roryhand | 7:fb47ff047ac7 | 421 | break; |
roryhand | 7:fb47ff047ac7 | 422 | } |
roryhand | 7:fb47ff047ac7 | 423 | |
roryhand | 7:fb47ff047ac7 | 424 | } |
roryhand | 7:fb47ff047ac7 | 425 | else if(flag2 == 0) |
roryhand | 7:fb47ff047ac7 | 426 | { |
roryhand | 7:fb47ff047ac7 | 427 | Buffer2[place_hold2] = data_sptr[channel]; |
roryhand | 7:fb47ff047ac7 | 428 | place_hold2 = place_hold2 + 1; |
roryhand | 7:fb47ff047ac7 | 429 | if(place_hold2 == 8) |
roryhand | 7:fb47ff047ac7 | 430 | { |
roryhand | 7:fb47ff047ac7 | 431 | flag4 = 1; |
roryhand | 7:fb47ff047ac7 | 432 | break; |
roryhand | 7:fb47ff047ac7 | 433 | } |
roryhand | 7:fb47ff047ac7 | 434 | |
roryhand | 7:fb47ff047ac7 | 435 | } |
roryhand | 7:fb47ff047ac7 | 436 | |
roryhand | 6:fd6ae534d120 | 437 | if(interrupt_condition==1) |
roryhand | 6:fd6ae534d120 | 438 | { |
roryhand | 6:fd6ae534d120 | 439 | |
roryhand | 6:fd6ae534d120 | 440 | |
roryhand | 6:fd6ae534d120 | 441 | } |
roryhand | 6:fd6ae534d120 | 442 | else |
roryhand | 6:fd6ae534d120 | 443 | { |
roryhand | 6:fd6ae534d120 | 444 | if(verbosity == 1) |
roryhand | 6:fd6ae534d120 | 445 | { |
roryhand | 6:fd6ae534d120 | 446 | t.start(); |
roryhand | 6:fd6ae534d120 | 447 | i2s.write(data_sptr,2); |
roryhand | 6:fd6ae534d120 | 448 | t.stop(); |
roryhand | 6:fd6ae534d120 | 449 | printf("The time taken was %f seconds\n", t.read()); |
roryhand | 6:fd6ae534d120 | 450 | t.reset(); |
roryhand | 6:fd6ae534d120 | 451 | } |
roryhand | 6:fd6ae534d120 | 452 | |
roryhand | 6:fd6ae534d120 | 453 | else |
roryhand | 6:fd6ae534d120 | 454 | { |
roryhand | 6:fd6ae534d120 | 455 | |
roryhand | 6:fd6ae534d120 | 456 | i2s.write(data_sptr,2); |
roryhand | 6:fd6ae534d120 | 457 | |
roryhand | 6:fd6ae534d120 | 458 | } |
roryhand | 6:fd6ae534d120 | 459 | |
roryhand | 6:fd6ae534d120 | 460 | if(sampling_freq == 48000) |
roryhand | 6:fd6ae534d120 | 461 | { |
roryhand | 6:fd6ae534d120 | 462 | wait_us(0.1); |
roryhand | 6:fd6ae534d120 | 463 | } |
roryhand | 6:fd6ae534d120 | 464 | else if(sampling_freq == 8000) |
roryhand | 6:fd6ae534d120 | 465 | { |
roryhand | 6:fd6ae534d120 | 466 | wait_us(2); |
roryhand | 6:fd6ae534d120 | 467 | } |
roryhand | 6:fd6ae534d120 | 468 | } |
roryhand | 6:fd6ae534d120 | 469 | |
roryhand | 6:fd6ae534d120 | 470 | |
roryhand | 6:fd6ae534d120 | 471 | |
roryhand | 6:fd6ae534d120 | 472 | |
roryhand | 6:fd6ae534d120 | 473 | |
roryhand | 2:05b426733282 | 474 | if (verbosity) |
roryhand | 5:5b120a2d9249 | 475 | printf("16 bit channel %d data=%d\n\r ",channel,data_sptr[channel]); |
roryhand | 5:5b120a2d9249 | 476 | slice_value[1]+=data_sptr[channel]; |
roryhand | 2:05b426733282 | 477 | break; |
roryhand | 2:05b426733282 | 478 | case 32: |
roryhand | 2:05b426733282 | 479 | if (verbosity) |
roryhand | 2:05b426733282 | 480 | printf("32 bit channel %d data=%d ",channel,data_wptr[channel]); |
roryhand | 5:5b120a2d9249 | 481 | slice_value[1]+=data_wptr[channel]; |
roryhand | 2:05b426733282 | 482 | break; |
roryhand | 2:05b426733282 | 483 | case 8: |
roryhand | 2:05b426733282 | 484 | if (verbosity) |
roryhand | 2:05b426733282 | 485 | printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]); |
roryhand | 5:5b120a2d9249 | 486 | slice_value[1]+=data_bptr[channel]; |
roryhand | 2:05b426733282 | 487 | break; |
roryhand | 2:05b426733282 | 488 | } |
roryhand | 2:05b426733282 | 489 | } |
roryhand | 7:fb47ff047ac7 | 490 | if(flag4 == 1) |
roryhand | 7:fb47ff047ac7 | 491 | { |
roryhand | 7:fb47ff047ac7 | 492 | flag4 = 0; |
roryhand | 7:fb47ff047ac7 | 493 | break; |
roryhand | 7:fb47ff047ac7 | 494 | } |
roryhand | 5:5b120a2d9249 | 495 | slice_value[1]/=wav_format.num_channels; |
roryhand | 2:05b426733282 | 496 | |
roryhand | 2:05b426733282 | 497 | // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value |
roryhand | 2:05b426733282 | 498 | // with DC offset so it can be written to the DAC. |
roryhand | 2:05b426733282 | 499 | switch (wav_format.sig_bps) { |
roryhand | 5:5b120a2d9249 | 500 | case 8: slice_value[1]<<=8; |
roryhand | 2:05b426733282 | 501 | break; |
roryhand | 5:5b120a2d9249 | 502 | case 16: slice_value[1]+=32768; |
roryhand | 2:05b426733282 | 503 | break; |
roryhand | 5:5b120a2d9249 | 504 | case 32: slice_value[1]>>=16; |
roryhand | 5:5b120a2d9249 | 505 | slice_value[1]+=32768; |
roryhand | 2:05b426733282 | 506 | break; |
roryhand | 2:05b426733282 | 507 | } |
roryhand | 5:5b120a2d9249 | 508 | if (verbosity) |
roryhand | 5:5b120a2d9249 | 509 | { |
roryhand | 5:5b120a2d9249 | 510 | printf("data_sptr data: %d \n\r",data_sptr); |
roryhand | 5:5b120a2d9249 | 511 | printf("slice_value data: %d \n\r",slice_value); |
roryhand | 5:5b120a2d9249 | 512 | } |
roryhand | 7:fb47ff047ac7 | 513 | slice = slice + 1; |
roryhand | 5:5b120a2d9249 | 514 | //i2s.write(data_sptr,2); |
roryhand | 7:fb47ff047ac7 | 515 | //t2.stop(); |
roryhand | 7:fb47ff047ac7 | 516 | //printf("The time taken was %f seconds\n", t2.read()); |
roryhand | 7:fb47ff047ac7 | 517 | //t2.reset(); |
roryhand | 3:6fba0a044d85 | 518 | } |
roryhand | 7:fb47ff047ac7 | 519 | printf("we must have filled the buffer"); |
roryhand | 7:fb47ff047ac7 | 520 | } |
roryhand | 7:fb47ff047ac7 | 521 | } |
roryhand | 7:fb47ff047ac7 | 522 | |
roryhand | 7:fb47ff047ac7 | 523 | |
roryhand | 3:6fba0a044d85 | 524 | /* dac_data=(short unsigned)slice_value; |
roryhand | 2:05b426733282 | 525 | if (verbosity) |
roryhand | 2:05b426733282 | 526 | printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data); |
roryhand | 2:05b426733282 | 527 | DAC_fifo[DAC_wptr]=dac_data; |
roryhand | 2:05b426733282 | 528 | DAC_wptr=(DAC_wptr+1) & 0xff; |
roryhand | 2:05b426733282 | 529 | while (DAC_wptr==DAC_rptr) { |
roryhand | 2:05b426733282 | 530 | } |
roryhand | 3:6fba0a044d85 | 531 | }*/ |
roryhand | 3:6fba0a044d85 | 532 | |
roryhand | 2:05b426733282 | 533 | |
roryhand | 6:fd6ae534d120 | 534 | if(interrupt_condition == 1) |
roryhand | 6:fd6ae534d120 | 535 | { |
roryhand | 6:fd6ae534d120 | 536 | |
roryhand | 6:fd6ae534d120 | 537 | sampletick.detach(); |
roryhand | 6:fd6ae534d120 | 538 | } |
roryhand | 6:fd6ae534d120 | 539 | |
roryhand | 0:004cb0595aa8 | 540 | i2s.stop(); |
roryhand | 0:004cb0595aa8 | 541 | |
roryhand | 0:004cb0595aa8 | 542 | fclose(my_wav); |
roryhand | 0:004cb0595aa8 | 543 | printf("File is now closed."); |
roryhand | 0:004cb0595aa8 | 544 | return 0; |
roryhand | 0:004cb0595aa8 | 545 | } |
roryhand | 0:004cb0595aa8 | 546 |