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
Diff: wolfson_3_wav.cpp
- Revision:
- 1:ec4e2020522c
- Parent:
- 0:004cb0595aa8
- Child:
- 2:05b426733282
diff -r 004cb0595aa8 -r ec4e2020522c wolfson_3_wav.cpp --- a/wolfson_3_wav.cpp Mon Oct 01 18:58:35 2018 +0000 +++ b/wolfson_3_wav.cpp Mon Oct 01 19:04:12 2018 +0000 @@ -69,24 +69,9 @@ //printf("value: %d",data_sptr); //i2s.write(buffer, 2);//Send next PWM value to amp } - //i++; - //int hi =i2s.read(buffer[0], bufflen); - //i2s.stop(); - //pc.printf("%d", buffer[0]); + - //pc.printf("Playing Sound!!\n");//0 = ack, 1 = Nack - //if (i>= SINE16LENGTH) {//SINE16LENGTH) { - /*if (i>= wav_format.block_align){ - i = 0; - i2s.stop(); - sampletick.detach(); - - //pc.printf("Sound ended!!"); - myled = 0; - CE - } - -}*/ + void wm8731_setup(int chip_addr, int addr, int cmd){ addr = addr << 1; @@ -288,186 +273,13 @@ printf(" %d subchunk 2 size\n",wav_data.subchunk2_size); - fseek(my_wav,36,SEEK_SET); - fread(&wav_format.sig_bps,2,1,my_wav); - printf("%d bits per sample\n",wav_format.sig_bps);//check how many bits per sample we have - - - i2c.frequency(100000); long j = 0; - /*while(j<1000){ - j=j+1; - wm8731_configTEST(); - }*/ - /*while(j<1000){ - j=j+1; - int addr = WM8731_REG_RESET; - int cmd = _WM8731_RESET; - addr = addr << 1; - addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS ); - i2c.write( addr ); - i2c.write( cmd ); - //wait(1); - i2c.stop(); - //}//am receiving ack = 0 i.e. an anknowledgment!!! when I loop round this section. 14/06/18 - //} - //i2c.stop(); - //while(1){} - //wait(3); - ///try not using the int ack stuff - - //pc.printf("Decimals1: %d", ack1);//0 = ack, 1 = Nack - //int ack12 = - //pc.printf("Decimals12: %d", ack12);//0 = ack, 1 = Nack - //int ack13 = i2c.write( cmd ); - //pc.printf("Decimals13: %d", ack13);//0 = ack, 1 = Nack - //i2c.stop(); - - //2 - - //while(1){ - // } - - - //while(1){ - addr = WM8731_REG_LLINE_IN; - cmd = _WM8731_LEFT_LINEIN; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - addr = WM8731_REG_RLINE_IN; - cmd = _WM8731_RIGHT_LINEIN; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - //4 - addr = WM8731_REG_LHPHONE_OUT; - cmd = _WM8731_LEFT_HP; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS ); - i2c.write( addr );//WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP - i2c.write(cmd); - i2c.stop(); - - //5 - addr = WM8731_REG_RHPHONE_OUT; - cmd = _WM8731_RIGHT_HP; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_RHPHONE_OUT, _WM8731_RIGHT_HP - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - //6 - addr = WM8731_REG_ANALOG_PATH; - cmd = _WM8731_ANALOGAUDIO; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - //7 - addr = WM8731_REG_DIGITAL_PATH; - cmd = _WM8731_DIGITALAUDIO; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - //8 - addr = WM8731_REG_PDOWN_CTRL; - cmd = _WM8731_POWER; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_PDOWN_CTRL, _WM8731_POWER) - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - //9 - addr = WM8731_REG_DIGITAL_IF; - cmd = _WM8731_DAIF; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS ); - i2c.write( addr );//WM8731_REG_DIGITAL_IF, _WM8731_DAIF - i2c.write( cmd ); - i2c.stop(); - - - //10 - addr = WM8731_REG_SAMPLING_CTRL; - cmd = _WM8731_SAMPLING; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - - - //11 - addr = WM8731_REG_ACTIVE_CTRL; - cmd = _WM8731_ACTIVATE; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING - i2c.write( addr ); - i2c.write( cmd );//there is also a deactive version!! - /*(from data sheet) - To prevent any comms problems from arising across te DAI the audio interface (AI) is disabled - (tristate with weak 100k pulldown). ONce the audio interface and the sampling scontrol has been programmed its - activated by setting the ACTIVE bit under software control.*/ - //i2c.stop(); - //} printf("set up the codec\n\r"); wm8731_configTEST(); //i2s audio data transfer code?? @@ -482,28 +294,12 @@ led2 = 1; printf("can we get to this point? 2\n\r"); - /*fseek(my_wav, 40, SEEK_SET); - fread(&wav_data.subchunk2_size,4,1,my_wav); - printf("subchunk2 size: %d",wav_data.subchunk2_size);*/ - int num_slices = wav_data.subchunk2_size/wav_format.block_align; - printf("block align is:%d\n\r",wav_format.block_align); - int a = 44; - //sampletick.attach(&isr,1.0/sample_freq); - myled = 1; i2s.start(); //while (1) { fread(&wav_format,sizeof(wav_format),1,my_wav); printf("wav_format %d\n\r",wav_format); - //sampletick.attach(&isr,1/96000); - /*if (wav_data.subchunk2_size > sizeof(wav_format)){ - fseek(my_wav,wav_data.subchunk2_size-sizeof(wav_format),SEEK_CUR); - printf("chunk_size - sizeof(wav_format) %d",wav_data.subchunk2_size-sizeof(wav_format)); - } - else { - - break; - }*/ + fseek(my_wav,20,SEEK_SET); fread(&wav_format,sizeof(wav_format),1,my_wav); @@ -535,12 +331,7 @@ slice_buf=(char *)malloc(wav_format.block_align); for(int slice = 0; slice<num_slices; slice+=1) { - //printf("slice: %d\n\r",slice); fread(&slice_buf,wav_format.block_align,1,my_wav); - //int filePositionIndicator=ftell(my_wav); - //printf("filePositionIndicator: %d\n\r",filePositionIndicator); - //printf("slice: %d\n\r",slice); - //printf("num_slices: %d\n\r",num_slices); data_sptr=(int *)slice_buf; //printf("data_sptr:%d\n\r",data_sptr); //printf("wav_format.num_channels: %d\n\r",wav_format.num_channels); @@ -577,12 +368,7 @@ } - - - //a = a + wav_format.block_align; - //} -//} - //} + sampletick.detach(); i2s.stop(); @@ -590,183 +376,4 @@ printf("File is now closed."); return 0; } - /*while(1){ - sampletick.attach(&isr,1.0/sample_freq); - led3 = !led3; - wait(1.0); - - //COMMENTS BELOW ARE OUT OF DATE - //The code above is IDENTICAL!!! to code run in TickerHello World. - //However, the 2nd led doesn't even light up and start "flipping" until led 1 has flipped.... - //Looks as if it cannot attach. - //sampletick.attach_us(&isr, 20.833);//try using attach_us instead. - //Not sure why the osther guy just didn't use this in the first place - //sampletick.attach(&isr,1/sample_freq); - //if myled is assigned AFTER the tick is attached, it never switches on if sound does not come on. - } - //sampletick.attach(&isr, 2.0); - } - */ - - - - - - - - - - - - - - - - - - - - - - - - - //old LEGACY code!!! - /*int addr = WM8731_REG_RESET; - int cmd = _WM8731_RESET; - addr = addr << 1; - addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS ); - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - addr = WM8731_REG_LLINE_IN; - cmd = _WM8731_LEFT_LINEIN; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - addr = WM8731_REG_RLINE_IN; - cmd = _WM8731_RIGHT_LINEIN; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - //4 - addr = WM8731_REG_LHPHONE_OUT; - cmd = _WM8731_LEFT_HP; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS ); - i2c.write( addr );//WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP - i2c.write(cmd); - i2c.stop(); - - //5 - addr = WM8731_REG_RHPHONE_OUT; - cmd = _WM8731_RIGHT_HP; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_RHPHONE_OUT, _WM8731_RIGHT_HP - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - //6 - addr = WM8731_REG_ANALOG_PATH; - cmd = _WM8731_ANALOGAUDIO; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - //7 - addr = WM8731_REG_DIGITAL_PATH; - cmd = _WM8731_DIGITALAUDIO; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - //8 - addr = WM8731_REG_PDOWN_CTRL; - cmd = _WM8731_POWER; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_PDOWN_CTRL, _WM8731_POWER) - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - //9 - addr = WM8731_REG_DIGITAL_IF; - cmd = _WM8731_DAIF; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS ); - i2c.write( addr );//WM8731_REG_DIGITAL_IF, _WM8731_DAIF - i2c.write( cmd ); - i2c.stop(); - - - //10 - addr = WM8731_REG_SAMPLING_CTRL; - cmd = _WM8731_SAMPLING; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING - i2c.write( addr ); - i2c.write( cmd ); - i2c.stop(); - - - - - //11 - addr = WM8731_REG_ACTIVE_CTRL; - cmd = _WM8731_ACTIVATE; - addr = addr << 1; - addr = addr|((cmd >> 8 )) & 0x01; - cmd = cmd&0xFF; - i2c.start(); - i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING - i2c.write( addr ); - i2c.write( cmd );//there is also a deactive version!! - i2c.stop();*/ - \ No newline at end of file