Production Test Program (PTP) for the LPC4088 Experiment Base Board

Dependencies:   EALib I2S LM75B SDFileSystem mbed

Revision:
3:7ef908e84ae1
Parent:
2:2f4b7535ceb3
Child:
4:aa34674b6afb
--- a/TestAudio.cpp	Thu Aug 28 09:36:13 2014 +0000
+++ b/TestAudio.cpp	Mon Sep 08 11:34:53 2014 +0000
@@ -21,11 +21,64 @@
 #include "mbed.h"
 #include "TestAudio.h"
 #include "WM8731.h"
+#include "I2S.h"
+#include "sound.h"
 
 /******************************************************************************
  * Defines and typedefs
  *****************************************************************************/
 
+#define SAMPLERATE 32000
+
+/******************************************************************************
+ * Private Functions
+ *****************************************************************************/
+
+void TestAudio::echo(void) {
+    int to_read = _i2sRx.fifo_points();  //Find out how many stereo samples to read
+    
+    //Read 'to_read' number of stereo samples.
+    _i2sRx.read(_rxBuf, to_read);
+    
+    //Loop through all stereo samples
+    for(int i = 0; i < to_read; i+=2) {
+        // Check if wave-file shall be output
+        if (_waveIdx > 0)
+        {
+            _txBuf[i]   = sound_8k[_waveIdx/4] << 5;
+            _txBuf[i+1] = sound_8k[_waveIdx/4] << 5;
+
+            //Increment wave samples pointer and check if all samples have been read, if so, set pointer to zero
+            _waveIdx++;
+            if (_waveIdx/4 > sound_sz)
+            {
+                _waveIdx = 0;
+            }
+        }
+        //Output delayed version of audio input
+        else
+        {
+            //Echo buffer only contains right side samples (since MIC is used as input, which is a mono input)
+            //Output is however stereo, so just copy echo bugger to both right and left side for output samples
+            _txBuf[i]   = _echoBuf[_echoBufPtr];
+            _txBuf[i+1] = _echoBuf[_echoBufPtr];
+
+            //Only fill echo buffer with right side samples, i.e., ignore rxBuf[i+1] content
+            _echoBuf[_echoBufPtr]   = _rxBuf[i];
+        }
+
+        //Increment echo buffer write pointer and check for wrap-around
+        _echoBufPtr++;
+        if (_echoBufPtr >= ECHOLENGTH)
+        {
+            _echoBufPtr = 0;
+        }
+    }
+    
+    //Write samples to output/codec
+    _i2sTx.write(_txBuf, to_read);
+}
+
 /******************************************************************************
  * Public Functions
  *****************************************************************************/
@@ -39,13 +92,58 @@
    - MIC cable, Line OUT, Headphone...
 */
 
+TestAudio::TestAudio() : _codec(P0_27, P0_28), _i2sTx(I2S_TRANSMIT, p11, p12, p13),
+    _i2sRx(I2S_RECEIVE, p14, p33, p34),_aIn(p15),_waveIdx(0),_echoBufPtr(0) {
+}
+
 bool TestAudio::runTest() {
     printf("Testing audio...\n");
-    WM8731 audio(P0_27, P0_28);
-    if (!audio.writeCmd(WM8731::REG_R15_RESET, 0x0000)) {
-        printf("Failed to send command to audio codec\n");
-        return false;
-    }
+
+    // joystick used for audio selection    
+    DigitalIn up(p32);
+    DigitalIn down(p38);
+    
+    _codec.power(true);
+    _codec.frequency(SAMPLERATE);
+    _codec.wordsize(16);  
+    _codec.master(true);
+    _codec.headphone_volume(0.1);
+    _codec.input_select(WM8731_MIC);
+    _codec.microphone_boost(false);
+    _codec.input_mute(false);
+    _codec.output_mute(false);
+    _codec.input_power(true);
+    _codec.output_power(true);
+//    _codec.sidetone(0.99);
+    _codec.linein_volume(0.7);
+    _codec.bypass(true);
+    _codec.start();
+
+    _i2sTx.frequency(SAMPLERATE);
+    _i2sTx.wordsize(16);
+    _i2sTx.stereomono(I2S_STEREO);
+    _i2sTx.masterslave(I2S_SLAVE);
+    _i2sTx.start();
+
+    _i2sRx.frequency(SAMPLERATE);
+    _i2sRx.wordsize(16);
+    _i2sRx.stereomono(I2S_STEREO);
+    _i2sRx.masterslave(I2S_SLAVE);
+    _i2sRx.attach(this, &TestAudio::echo);
+    _echoBufPtr = 0;
+    _waveIdx = 0;
+    _i2sRx.start();
+    
+    do
+    {
+        //check if joystick-down pressed = play wave-file
+        if ((down.read() == 0) && (_waveIdx == 0))
+            _waveIdx = 1;
+
+        _codec.headphone_volume(_aIn);
+        wait(0.05);
+    } while (up.read() != 0);
+    
     return true;
 }