From 392aed44a06d27887c3404261942913157cd8f38 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 15 Sep 2015 15:02:04 +0100 Subject: [PATCH 01/21] Initial changes for PDM outline --- module_usb_audio/audio.xc | 10 ++++++++++ module_usb_audio/main.xc | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 30e705ee..2d1f36db 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -433,6 +433,10 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, #if(defined(SPDIF_RX) || defined(ADAT_RX)) chanend c_dig_rx, #endif +#ifdef PDM_PCM_IN + streaming chanend c_pdm_pcm, +#endif + chanend ?c_adc) { @@ -924,6 +928,9 @@ chanend ?c_config, chanend ?c #if XUD_TILE != 0 , server interface i_dfu dfuInterface #endif +#ifdef PDM_PCM_IN +, streaming chanend c_pdm_in +#endif ) { #if defined (SPDIF_TX) && (SPDIF_TX_TILE == AUDIO_IO_TILE) @@ -1190,6 +1197,9 @@ chanend ?c_config, chanend ?c divide, curSamFreq, #if defined (ADAT_RX) || defined (SPDIF_RX) c_dig_rx, +#endif +#ifdef PDM_PCM_IN + c_pdm_in, #endif c); diff --git a/module_usb_audio/main.xc b/module_usb_audio/main.xc index a73b8150..a6bba8d0 100755 --- a/module_usb_audio/main.xc +++ b/module_usb_audio/main.xc @@ -43,6 +43,8 @@ #include "clocking.h" +void genclock(); + [[distributable]] void DFUHandler(server interface i_dfu i, chanend ?c_user_cmd); @@ -262,6 +264,14 @@ void xscope_user_init() } #endif +#ifdef PDM_PCM_IN +void dummy_pdm_code(streaming chanend c) +{ + while(1) + c <: 0xff00ff00; +} +#endif + /* Core USB Audio functions - must be called on the Tile connected to the USB Phy */ void usb_audio_core(chanend c_mix_out #ifdef MIDI @@ -416,6 +426,10 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, #define c_dig_rx null #endif +#ifdef PDM_PCM_IN + streaming chan c_pdm_pcm +#endif + par { #ifdef MIXER @@ -442,7 +456,10 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, #endif c_aud_cfg, c_adc #if XUD_TILE != 0 - ,dfuInterface + , dfuInterface +#endif +#ifdef PDM_PCM_IN + , c_pdm_pcm #endif ); } @@ -455,6 +472,11 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, } #endif + +#ifdef PDM_PCM_IN + dummy_pdm_code(c_pdm_pcm); +#endif + //: } } From be0bf0d9833dfed02895e89a0f29c26d67c8fe8e Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 22 Sep 2015 11:15:05 +0100 Subject: [PATCH 02/21] Trial add of PDM code --- module_usb_audio/audio.h | 3 + module_usb_audio/audio.xc | 10 ++++ module_usb_audio/main.xc | 36 +++++------ module_usb_audio/pcm_pdm_mic.xc | 103 ++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 17 deletions(-) create mode 100644 module_usb_audio/pcm_pdm_mic.xc diff --git a/module_usb_audio/audio.h b/module_usb_audio/audio.h index 271def28..662a9af3 100644 --- a/module_usb_audio/audio.h +++ b/module_usb_audio/audio.h @@ -26,6 +26,9 @@ void audio(chanend c_in, #if (XUD_TILE != 0) , server interface i_dfu dfuInterface #endif +#ifdef PDM_PCM_IN +, streaming chanend c_pdm_in +#endif ); void SpdifTxWrapper(chanend c_spdif_tx); diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 2d1f36db..5b6c23a8 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -696,6 +696,15 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, unsigned sample = samplesOut[SPDIF_TX_INDEX + 1]; outuint(c_spd_out, sample); /* Forward sample to S/PDIF Tx thread */ #endif + +#ifdef PDM_PCM_IN + c_pdm_in <: 1; + c_pdm_in :> samplesIn_0[0]; + c_pdm_in :> samplesIn_0[1]; +#endif + + + } @@ -1171,6 +1180,7 @@ chanend ?c_config, chanend ?c outuint(c_spdif_out, mClk); #endif + #ifdef ADAT_TX // Configure ADAT parameters ... // diff --git a/module_usb_audio/main.xc b/module_usb_audio/main.xc index b4c08e8a..9eb586b2 100755 --- a/module_usb_audio/main.xc +++ b/module_usb_audio/main.xc @@ -43,6 +43,10 @@ #include "clocking.h" +#ifdef PDM_PCM_IN +void pcm_pdm_mic(streaming chanend c_pcm_out); +#endif + void genclock(); [[distributable]] @@ -267,14 +271,6 @@ void xscope_user_init() } #endif -#ifdef PDM_PCM_IN -void dummy_pdm_code(streaming chanend c) -{ - while(1) - c <: 0xff00ff00; -} -#endif - /* Core USB Audio functions - must be called on the Tile connected to the USB Phy */ void usb_audio_core(chanend c_mix_out #ifdef MIDI @@ -417,6 +413,9 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, #if (XUD_TILE != 0) , server interface i_dfu dfuInterface #endif +#ifdef PDM_PCM_IN + , streaming chanend c_pdm_pcm +#endif ) { #ifdef MIXER @@ -429,10 +428,6 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, #define c_dig_rx null #endif -#ifdef PDM_PCM_IN - streaming chan c_pdm_pcm -#endif - par { #ifdef MIXER @@ -476,10 +471,6 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, } #endif -#ifdef PDM_PCM_IN - dummy_pdm_code(c_pdm_pcm); -#endif - //: } } @@ -491,7 +482,6 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, #ifndef USER_MAIN_CORES #define USER_MAIN_CORES #endif -//:: /* Main for USB Audio Applications */ int main() @@ -553,6 +543,9 @@ int main() #define dfuInterface null #endif +#ifdef PDM_PCM_IN + streaming chan c_pdm_pcm; +#endif USER_MAIN_DECLARATIONS @@ -582,6 +575,7 @@ int main() , c_mix_ctl #endif , c_clk_int, c_clk_ctl, dfuInterface + ); } @@ -596,6 +590,9 @@ int main() #if XUD_TILE != 0 , dfuInterface #endif +#ifdef PDM_PCM_IN + , c_pdm_pcm +#endif ); @@ -657,6 +654,11 @@ int main() } } #endif + +#ifdef PDM_PCM_IN + // TODO tile + on stdcore[0]: pcm_pdm_mic(c_pdm_pcm); +#endif USER_MAIN_CORES } diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc new file mode 100644 index 00000000..427df39c --- /dev/null +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include "beam.h" +#include +#include +#include + +#include "static_constants.h" +#if 1 +on tile[0]: in port p_pdm_clk = XS1_PORT_1E; +on tile[0]: in buffered port:8 p_pdm_mics = XS1_PORT_8B; +in port p_mclk = on tile[0]: XS1_PORT_1F; +clock mclk = on tile[0]: XS1_CLKBLK_1; +clock pdmclk = on tile[0]: XS1_CLKBLK_3; + +in port p_buttons = on tile[0]: XS1_PORT_4C; + +typedef struct { + unsigned ch_a; + unsigned ch_b; +} double_packed_audio; + +typedef struct { + double_packed_audio data[4][1< unsigned req; + c_pcm_out <: audio[buffer].data[0][0].ch_a; + c_pcm_out <: audio[buffer].data[0][0].ch_b; + printintln(audio[buffer].data[0][0].ch_b); + } + } +} + + +void pcm_pdm_mic(streaming chanend c_pcm_out) +{ + streaming chan c_multi_channel_pdm, c_sync, c_4x_pdm_mic_0, c_4x_pdm_mic_1; + streaming chan c_ds_output_0, c_ds_output_1; + streaming chan c_buffer_mic0, c_buffer_mic1; + unsigned long long shared_memory[2] = {0}; + + configure_clock_src(mclk, p_mclk); + configure_clock_src_divide(pdmclk, p_mclk, 2); + configure_port_clock_output(p_pdm_clk, pdmclk); + configure_in_port(p_pdm_mics, pdmclk); + start_clock(mclk); + start_clock(pdmclk); + + unsafe + { + unsigned long long * unsafe p_shared_memory = shared_memory; + par + { + + //Input stage + pdm_first_stage(p_pdm_mics, p_shared_memory, + PDM_BUFFER_LENGTH_LOG2, c_sync, + c_4x_pdm_mic_0, c_4x_pdm_mic_1); + + pdm_to_pcm_4x(c_4x_pdm_mic_0, c_ds_output_0); + pdm_to_pcm_4x(c_4x_pdm_mic_1, c_ds_output_1); + + example(c_ds_output_0, c_ds_output_1, c_pcm_out); + + } + } +} + +#endif From a230754991b74ba62a8b5e09e2feef50a4081b53 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 22 Sep 2015 11:16:49 +0100 Subject: [PATCH 03/21] Fixed PDM build issue - bad channel name --- module_usb_audio/audio.xc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 5b6c23a8..44f37a67 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -698,9 +698,9 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, #endif #ifdef PDM_PCM_IN - c_pdm_in <: 1; - c_pdm_in :> samplesIn_0[0]; - c_pdm_in :> samplesIn_0[1]; + c_pdm_pcm <: 1; + c_pdm_pcm :> samplesIn_0[0]; + c_pdm_pcm :> samplesIn_0[1]; #endif From 5adfd29c17f0427ec46031f1630e94c8c2fa95b2 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 22 Sep 2015 11:17:21 +0100 Subject: [PATCH 04/21] Removed print --- module_usb_audio/pcm_pdm_mic.xc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index 427df39c..8575618c 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -60,7 +60,7 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s c_pcm_out :> unsigned req; c_pcm_out <: audio[buffer].data[0][0].ch_a; c_pcm_out <: audio[buffer].data[0][0].ch_b; - printintln(audio[buffer].data[0][0].ch_b); + //printintln(audio[buffer].data[0][0].ch_b); } } } From 21bc1a47082da4d0662ca6a1d11c24bf3e7322e4 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 22 Sep 2015 12:15:53 +0100 Subject: [PATCH 05/21] Change mic --- module_usb_audio/pcm_pdm_mic.xc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index 8575618c..0945b945 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -58,7 +58,7 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s //xscope_int(0, audio[buffer].data[0][0].ch_a); //xscope_int(1, audio[buffer].data[0][0].ch_b); c_pcm_out :> unsigned req; - c_pcm_out <: audio[buffer].data[0][0].ch_a; + c_pcm_out <: audio[buffer].data[1][0].ch_a; c_pcm_out <: audio[buffer].data[0][0].ch_b; //printintln(audio[buffer].data[0][0].ch_b); } From 35834c4fb8a137051e4cec5dc1ada278e712bd1d Mon Sep 17 00:00:00 2001 From: Andrew Stanford-Jason Date: Tue, 22 Sep 2015 12:21:33 +0100 Subject: [PATCH 06/21] audio scaling for pdm --- module_usb_audio/.cproject | 1327 +++++++++++++++++++++---------- module_usb_audio/.project | 160 ++-- module_usb_audio/pcm_pdm_mic.xc | 25 +- 3 files changed, 1006 insertions(+), 506 deletions(-) diff --git a/module_usb_audio/.cproject b/module_usb_audio/.cproject index 5b0ab598..5cbe48b9 100644 --- a/module_usb_audio/.cproject +++ b/module_usb_audio/.cproject @@ -1,323 +1,72 @@ - - - - - - - - - - - - - - - - - - xmake - -f .makefile - all - true - true - true - - - xmake - -f .makefile - clean - true - true - true - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/module_usb_audio/.project b/module_usb_audio/.project index cb42a172..80c9d6e8 100644 --- a/module_usb_audio/.project +++ b/module_usb_audio/.project @@ -1,76 +1,90 @@ - module_usb_audio - - - - - - org.eclipse.cdt.managedbuilder.core.genmakebuilder - clean,full,incremental, - - - ?children? - ?name?=outputEntries\|?children?=?name?=entry\\\\|\\|\|| - - - ?name? - - - - org.eclipse.cdt.make.core.append_environment - true - - - org.eclipse.cdt.make.core.buildArguments - CONFIG=Debug - - - org.eclipse.cdt.make.core.buildCommand - xmake - - - org.eclipse.cdt.make.core.cleanBuildTarget - clean - - - org.eclipse.cdt.make.core.contents - org.eclipse.cdt.make.core.activeConfigSettings - - - org.eclipse.cdt.make.core.enableAutoBuild - false - - - org.eclipse.cdt.make.core.enableCleanBuild - true - - - org.eclipse.cdt.make.core.enableFullBuild - true - - - org.eclipse.cdt.make.core.stopOnError - true - - - org.eclipse.cdt.make.core.useDefaultBuildCmd - false - - - - - org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder - full,incremental, - - - - - - org.eclipse.cdt.core.cnature - org.eclipse.cdt.managedbuilder.core.managedBuildNature - org.eclipse.cdt.managedbuilder.core.ScannerConfigNature - com.xmos.cdt.core.XdeProjectNature - + module_usb_audio + + + + + + com.xmos.cdt.core.LegacyProjectCheckerBuilder + + + + + com.xmos.cdt.core.ProjectInfoSyncBuilder + + + + + com.xmos.cdt.core.ModulePathBuilder + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?children? + ?name?=outputEntries\|?children?=?name?=entry\\\\|\\|\|| + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.buildArguments + CONFIG=Debug + + + org.eclipse.cdt.make.core.buildCommand + xmake + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + false + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + com.xmos.cdt.core.XdeProjectNature + - diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index 8575618c..fae744ea 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -27,6 +27,13 @@ typedef struct { } synchronised_audio; +static int dc_offset_removal(int sample, int &prex_x, int &prev_y){ + int r = prev_y- (prev_y>>5) + (sample - prex_x); + prex_x = sample; + prev_y = r; + return r; +} + void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, streaming chanend c_pcm_out) { @@ -35,6 +42,8 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s synchronised_audio audio[2]; //double buffered memset(audio, sizeof(synchronised_audio), 2); + int prev_x[7] = {0}; + int prev_y[7] = {0}; unsafe { @@ -52,15 +61,17 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s buffer = 1 - buffer; - //The data has already been bit reversed in the downsampler - // audio[buffer] is good to go - //xscope_int(0, audio[buffer].data[0][0].ch_a); - //xscope_int(1, audio[buffer].data[0][0].ch_b); + + int a = dc_offset_removal( audio[buffer].data[1][0].ch_a, prev_x[0], prev_y[0]); + int b = dc_offset_removal( audio[buffer].data[1][0].ch_b, prev_x[1], prev_y[1]); + + xscope_int(0,a); + xscope_int(1,b); + c_pcm_out :> unsigned req; - c_pcm_out <: audio[buffer].data[0][0].ch_a; - c_pcm_out <: audio[buffer].data[0][0].ch_b; - //printintln(audio[buffer].data[0][0].ch_b); + c_pcm_out <: (a>>14); + c_pcm_out <: (b>>14); } } } From 71f55b183b599a1bbdaf67cbf7f8182f36b7ef8c Mon Sep 17 00:00:00 2001 From: Andrew Stanford-Jason Date: Tue, 22 Sep 2015 13:41:25 +0100 Subject: [PATCH 07/21] added agc --- module_usb_audio/pcm_pdm_mic.xc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index fae744ea..6890d6bb 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -6,7 +6,7 @@ #include #include #include - +#include #include "static_constants.h" #if 1 on tile[0]: in port p_pdm_clk = XS1_PORT_1E; @@ -45,6 +45,8 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s int prev_x[7] = {0}; int prev_y[7] = {0}; + int max = 0; + unsafe { c_ds_output_0 <: (synchronised_audio * unsafe)audio[0].data[0]; @@ -66,12 +68,21 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s int a = dc_offset_removal( audio[buffer].data[1][0].ch_a, prev_x[0], prev_y[0]); int b = dc_offset_removal( audio[buffer].data[1][0].ch_b, prev_x[1], prev_y[1]); - xscope_int(0,a); - xscope_int(1,b); + //xscope_int(0,a); + //xscope_int(1,b); + + if((-a) > max) max = (-a); + if(a > max) max = a; + if((-b) > max) max = (-b); + if(b > max) max = b; + int output_a = a<<(clz(max)-1); + int output_b = b<<(clz(max)-1); + + max = max - (max>>17); c_pcm_out :> unsigned req; - c_pcm_out <: (a>>14); - c_pcm_out <: (b>>14); + c_pcm_out <: output_a; + c_pcm_out <: output_b; } } } From 62d2d1d5be8e157dd85343b3c62fe61cb51e76ba Mon Sep 17 00:00:00 2001 From: Andrew Stanford-Jason Date: Tue, 22 Sep 2015 15:30:31 +0100 Subject: [PATCH 08/21] sort of demo code --- module_usb_audio/pcm_pdm_mic.xc | 208 ++++++++++++++++++++++++++++---- 1 file changed, 185 insertions(+), 23 deletions(-) diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index 6890d6bb..c95a760b 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -8,6 +8,8 @@ #include #include #include "static_constants.h" +#include "debug_print.h" + #if 1 on tile[0]: in port p_pdm_clk = XS1_PORT_1E; on tile[0]: in buffered port:8 p_pdm_mics = XS1_PORT_8B; @@ -15,7 +17,129 @@ in port p_mclk = on tile[0]: XS1_PORT_1F; clock mclk = on tile[0]: XS1_CLKBLK_1; clock pdmclk = on tile[0]: XS1_CLKBLK_3; -in port p_buttons = on tile[0]: XS1_PORT_4C; + +// LEDs +out port p_led0to7 = on tile[0]: XS1_PORT_8C; +out port p_led8 = on tile[0]: XS1_PORT_1K; +out port p_led9 = on tile[0]: XS1_PORT_1L; +out port p_led10to12 = on tile[0]: XS1_PORT_8D; +out port p_leds_oen = on tile[0]: XS1_PORT_1P; +// Buttons +in port p_buttons = on tile[0]: XS1_PORT_4A; + +enum buttons +{ + BUTTON_A=1<<0, + BUTTON_B=1<<1, + BUTTON_C=1<<2, + BUTTON_D=1<<3 +}; + +#define BUTTON_PRESSED(but_mask, old_val, new_val) (((old_val) & (but_mask)) == (but_mask) && ((new_val) & (but_mask)) == 0) +#define BUTTON_DEBOUNCE_DELAY (20000000) +#define LED_ON 0xFFFF +void buttons_and_leds(chanend c) +{ + int button_val; + int buttons_active = 1; + unsigned buttons_timeout; + unsigned time; + unsigned glow_time; + timer button_tmr; + timer leds_tmr; + timer glow_tmr; + const int pwm_cycle = 100000; // The period in 100Mhz timer ticks of the pwm + const int pwm_res = 256; // The resolution of the pwm + const int pwm_delay = pwm_cycle / pwm_res; // The period between updates to the port output + int count = 0; // The count that tracks where we are in the pwm cycle + + int period = 1 * 1000 * 1000 * 100 * 15; // period from off to on = 1s; + unsigned res = 300; // increment the brightness in this + // number of steps + int delay = period / res; // how long to wait between updates + // int delay = 1 * 1000 * 1000 * 100; + int dir = 1; + int on_led = 0; + + p_leds_oen <: 1; + p_leds_oen <: 0; + // This array stores the pwm levels for the leds + int level[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + for (int i=0; i < 13; i++) { + level[i] = level[i] / (0xFFFF / pwm_res); + } + + p_buttons :> button_val; + leds_tmr :> time; + glow_tmr :> glow_time; + + int only_one_mic = 1; + + p_led0to7 <:~0; + p_led8 <:~0; + p_led9 <:~0; + //p_led10to12 <:~0; +// p_leds_oen <:~0; + + + p_led10to12 <: ~((only_one_mic&0x1)<<2); + level[0] = 0xff; + while (1) { + select + { + + case buttons_active => p_buttons when pinsneq(button_val) :> unsigned new_button_val: + + if BUTTON_PRESSED(BUTTON_A, button_val, new_button_val) { + debug_printf("Button A\n"); + only_one_mic = 1-only_one_mic; + only_one_mic? printstrln("one"):printstrln("all"); + p_led10to12 <: ~((only_one_mic&0x1)<<2); + c <: 0; + buttons_active = 0; + } + if BUTTON_PRESSED(BUTTON_B, button_val, new_button_val) { + debug_printf("Button B\n"); + only_one_mic = 1-only_one_mic; + only_one_mic? printstrln("one"):printstrln("all"); + p_led10to12 <: ~((only_one_mic&0x1)<<2); + c <: 0; + buttons_active = 0; + } + if BUTTON_PRESSED(BUTTON_C, button_val, new_button_val) { + debug_printf("Button C\n"); + only_one_mic = 1-only_one_mic; + only_one_mic? printstrln("one"):printstrln("all"); + p_led10to12 <: ~((only_one_mic&0x1)<<2); + c <: 0; + buttons_active = 0; + } + if BUTTON_PRESSED(BUTTON_D, button_val, new_button_val) { + debug_printf("Button D\n"); + only_one_mic = 1-only_one_mic; + only_one_mic? printstrln("one"):printstrln("all"); + p_led10to12 <: ~((only_one_mic&0x1)<<2); + c <: 0; + buttons_active = 0; + } + if (!buttons_active) + { + button_tmr :> buttons_timeout; + buttons_timeout += BUTTON_DEBOUNCE_DELAY; + } + button_val = new_button_val; + break; + case !buttons_active => button_tmr when timerafter(buttons_timeout) :> void: + buttons_active = 1; + p_buttons :> button_val; + break; + + + } + } +} + typedef struct { unsigned ch_a; @@ -34,7 +158,7 @@ static int dc_offset_removal(int sample, int &prex_x, int &prev_y){ return r; } -void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, streaming chanend c_pcm_out) +void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, streaming chanend c_pcm_out, chanend cc) { @@ -52,6 +176,7 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s c_ds_output_0 <: (synchronised_audio * unsafe)audio[0].data[0]; c_ds_output_1 <: (synchronised_audio * unsafe)audio[0].data[2]; + int only_one_mic=1; while(1) { @@ -65,24 +190,55 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s // audio[buffer] is good to go - int a = dc_offset_removal( audio[buffer].data[1][0].ch_a, prev_x[0], prev_y[0]); - int b = dc_offset_removal( audio[buffer].data[1][0].ch_b, prev_x[1], prev_y[1]); + int a = dc_offset_removal( audio[buffer].data[0][0].ch_b, prev_x[0], prev_y[0]); + int b = dc_offset_removal( audio[buffer].data[1][0].ch_a, prev_x[1], prev_y[1]); + int c = dc_offset_removal( audio[buffer].data[1][0].ch_b, prev_x[2], prev_y[2]); + int d = dc_offset_removal( audio[buffer].data[2][0].ch_a, prev_x[3], prev_y[3]); + int e = dc_offset_removal( audio[buffer].data[2][0].ch_b, prev_x[4], prev_y[4]); + int f = dc_offset_removal( audio[buffer].data[3][0].ch_a, prev_x[5], prev_y[5]); + int g = dc_offset_removal( audio[buffer].data[3][0].ch_b, prev_x[6], prev_y[6]); - //xscope_int(0,a); - //xscope_int(1,b); + // printf("%x %x %x %x %x %x %x\n", a, b, c, d, e, f, g); + + unsigned v = a*a; + + + select { + case cc:> int:{ + only_one_mic = 1-only_one_mic; + break; + } + default:break; + } if((-a) > max) max = (-a); if(a > max) max = a; - if((-b) > max) max = (-b); - if(b > max) max = b; - int output_a = a<<(clz(max)-1); - int output_b = b<<(clz(max)-1); + int output; + if(only_one_mic){ + output = a<<(clz(max)-1); + } else { + if((-a) > max) max = (-a); + if(a > max) max = a; + if((-b) > max) max = (-b); + if(b > max) max = b; + if((-c) > max) max = (-c); + if(c > max) max = c; + if((-d) > max) max = (-d); + if(d > max) max = d; + if((-e) > max) max = (-e); + if(e > max) max = e; + if((-f) > max) max = (-f); + if(f > max) max = f; + output = a+b+c+d+e+f+g+g; + output >>=3; + output = output<<(clz(max)-1); + } max = max - (max>>17); c_pcm_out :> unsigned req; - c_pcm_out <: output_a; - c_pcm_out <: output_b; + c_pcm_out <: output; + c_pcm_out <: output; } } } @@ -102,23 +258,29 @@ void pcm_pdm_mic(streaming chanend c_pcm_out) start_clock(mclk); start_clock(pdmclk); - unsafe - { - unsigned long long * unsafe p_shared_memory = shared_memory; - par + chan c; + par { + buttons_and_leds(c); + + unsafe { + unsigned long long * unsafe p_shared_memory = shared_memory; + par + { - //Input stage - pdm_first_stage(p_pdm_mics, p_shared_memory, - PDM_BUFFER_LENGTH_LOG2, c_sync, - c_4x_pdm_mic_0, c_4x_pdm_mic_1); + //Input stage + pdm_first_stage(p_pdm_mics, p_shared_memory, + PDM_BUFFER_LENGTH_LOG2, c_sync, + c_4x_pdm_mic_0, c_4x_pdm_mic_1); - pdm_to_pcm_4x(c_4x_pdm_mic_0, c_ds_output_0); - pdm_to_pcm_4x(c_4x_pdm_mic_1, c_ds_output_1); + pdm_to_pcm_4x(c_4x_pdm_mic_0, c_ds_output_0); + pdm_to_pcm_4x(c_4x_pdm_mic_1, c_ds_output_1); - example(c_ds_output_0, c_ds_output_1, c_pcm_out); + example(c_ds_output_0, c_ds_output_1, c_pcm_out, c); + } } + } } From e9b19cedaf83749259c890cd08bb8a088f27229f Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 22 Sep 2015 16:47:24 +0100 Subject: [PATCH 09/21] Quickly hacked in a input -> output loopback. --- module_usb_audio/audio.xc | 1 + 1 file changed, 1 insertion(+) diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 44f37a67..abeb7237 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -262,6 +262,7 @@ static inline unsigned DoSampleTransfer(chanend c_out, int readBuffNo, unsigned { int tmp = inuint(c_out); samplesOut[i] = tmp; + samplesOut[i] = samplesIn_0[i]; } #else inuint(c_out); From 38094b90c4b61fc312c006c99dd45158a4714cf8 Mon Sep 17 00:00:00 2001 From: Andrew Stanford-Jason Date: Wed, 23 Sep 2015 09:43:16 +0100 Subject: [PATCH 10/21] removed the agc and replaced it with a fixed gain --- module_usb_audio/pcm_pdm_mic.xc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index c95a760b..a56fdb8a 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -211,11 +211,13 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s default:break; } +#define GAIN 0 + if((-a) > max) max = (-a); if(a > max) max = a; int output; if(only_one_mic){ - output = a<<(clz(max)-1); + output = a< max) max = (-a); if(a > max) max = a; @@ -231,7 +233,7 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s if(f > max) max = f; output = a+b+c+d+e+f+g+g; output >>=3; - output = output<<(clz(max)-1); + output = output<>17); From 5306fda2ab04cc44f3a5a8dc1a8d80c05efe61e4 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Wed, 23 Sep 2015 10:38:24 +0100 Subject: [PATCH 11/21] Gain fiddle and fixed LEDs --- module_usb_audio/pcm_pdm_mic.xc | 35 ++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index a56fdb8a..f2539ed3 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -35,6 +35,24 @@ enum buttons BUTTON_D=1<<3 }; +void lightLeds(int only_one_mic) +{ + if(only_one_mic) + { + p_led10to12 <: 0x3; + p_led0to7 <: 0xff; + p_led8 <: 1; + p_led9 <: 1; + } + else + { + p_led0to7 <: 0; + p_led10to12 <: 0x4; + p_led8 <: 0; + p_led9 <: 0; + } +} + #define BUTTON_PRESSED(but_mask, old_val, new_val) (((old_val) & (but_mask)) == (but_mask) && ((new_val) & (but_mask)) == 0) #define BUTTON_DEBOUNCE_DELAY (20000000) #define LED_ON 0xFFFF @@ -83,7 +101,9 @@ void buttons_and_leds(chanend c) // p_leds_oen <:~0; - p_led10to12 <: ~((only_one_mic&0x1)<<2); + lightLeds(only_one_mic); + only_one_mic? printstrln("one"):printstrln("all"); + level[0] = 0xff; while (1) { select @@ -95,7 +115,9 @@ void buttons_and_leds(chanend c) debug_printf("Button A\n"); only_one_mic = 1-only_one_mic; only_one_mic? printstrln("one"):printstrln("all"); - p_led10to12 <: ~((only_one_mic&0x1)<<2); + + lightLeds(only_one_mic); + c <: 0; buttons_active = 0; } @@ -103,7 +125,7 @@ void buttons_and_leds(chanend c) debug_printf("Button B\n"); only_one_mic = 1-only_one_mic; only_one_mic? printstrln("one"):printstrln("all"); - p_led10to12 <: ~((only_one_mic&0x1)<<2); + lightLeds(only_one_mic); c <: 0; buttons_active = 0; } @@ -111,7 +133,6 @@ void buttons_and_leds(chanend c) debug_printf("Button C\n"); only_one_mic = 1-only_one_mic; only_one_mic? printstrln("one"):printstrln("all"); - p_led10to12 <: ~((only_one_mic&0x1)<<2); c <: 0; buttons_active = 0; } @@ -119,7 +140,7 @@ void buttons_and_leds(chanend c) debug_printf("Button D\n"); only_one_mic = 1-only_one_mic; only_one_mic? printstrln("one"):printstrln("all"); - p_led10to12 <: ~((only_one_mic&0x1)<<2); + lightLeds(only_one_mic); c <: 0; buttons_active = 0; } @@ -211,7 +232,7 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s default:break; } -#define GAIN 0 +#define GAIN 7 if((-a) > max) max = (-a); if(a > max) max = a; @@ -233,7 +254,7 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s if(f > max) max = f; output = a+b+c+d+e+f+g+g; output >>=3; - output = output<>17); From e114e32503085ba4094fe576c91deb869a351765 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Wed, 23 Sep 2015 16:05:03 +0100 Subject: [PATCH 12/21] 8 mic channels now recorded. Fixed bad mic selection and reduced gain. --- module_usb_audio/audio.xc | 11 ++++--- module_usb_audio/pcm_pdm_mic.xc | 51 +++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index abeb7237..4f470862 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -262,7 +262,7 @@ static inline unsigned DoSampleTransfer(chanend c_out, int readBuffNo, unsigned { int tmp = inuint(c_out); samplesOut[i] = tmp; - samplesOut[i] = samplesIn_0[i]; + samplesOut[i] = samplesIn_0[0]; } #else inuint(c_out); @@ -702,10 +702,13 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, c_pdm_pcm <: 1; c_pdm_pcm :> samplesIn_0[0]; c_pdm_pcm :> samplesIn_0[1]; + c_pdm_pcm :> samplesIn_0[2]; + c_pdm_pcm :> samplesIn_0[3]; + c_pdm_pcm :> samplesIn_0[4]; + c_pdm_pcm :> samplesIn_0[5]; + c_pdm_pcm :> samplesIn_0[6]; + c_pdm_pcm :> samplesIn_0[7]; #endif - - - } diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pcm_pdm_mic.xc index f2539ed3..546a1cd7 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pcm_pdm_mic.xc @@ -211,19 +211,19 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s // audio[buffer] is good to go - int a = dc_offset_removal( audio[buffer].data[0][0].ch_b, prev_x[0], prev_y[0]); - int b = dc_offset_removal( audio[buffer].data[1][0].ch_a, prev_x[1], prev_y[1]); - int c = dc_offset_removal( audio[buffer].data[1][0].ch_b, prev_x[2], prev_y[2]); - int d = dc_offset_removal( audio[buffer].data[2][0].ch_a, prev_x[3], prev_y[3]); - int e = dc_offset_removal( audio[buffer].data[2][0].ch_b, prev_x[4], prev_y[4]); - int f = dc_offset_removal( audio[buffer].data[3][0].ch_a, prev_x[5], prev_y[5]); - int g = dc_offset_removal( audio[buffer].data[3][0].ch_b, prev_x[6], prev_y[6]); + int a = dc_offset_removal( audio[buffer].data[0][0].ch_a, prev_x[0], prev_y[0]); + int b = dc_offset_removal( audio[buffer].data[0][0].ch_b, prev_x[0], prev_y[0]); + int c = dc_offset_removal( audio[buffer].data[1][0].ch_a, prev_x[1], prev_y[1]); + int d = dc_offset_removal( audio[buffer].data[1][0].ch_b, prev_x[2], prev_y[2]); + int e = dc_offset_removal( audio[buffer].data[2][0].ch_a, prev_x[3], prev_y[3]); + int f = dc_offset_removal( audio[buffer].data[2][0].ch_b, prev_x[4], prev_y[4]); + int g = dc_offset_removal( audio[buffer].data[3][0].ch_a, prev_x[5], prev_y[5]); + int h = dc_offset_removal( audio[buffer].data[3][0].ch_b, prev_x[6], prev_y[6]);//Expect dead - // printf("%x %x %x %x %x %x %x\n", a, b, c, d, e, f, g); + //printf("%x %x %x %x %x %x %x %x\n", a, b, c, d, e, f, g, h); unsigned v = a*a; - select { case cc:> int:{ only_one_mic = 1-only_one_mic; @@ -232,13 +232,24 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s default:break; } -#define GAIN 7 +#define GAIN 5 if((-a) > max) max = (-a); if(a > max) max = a; int output; if(only_one_mic){ output = a< unsigned req; + c_pcm_out <: output; + c_pcm_out <: output; + c_pcm_out <: output; + c_pcm_out <: output; + c_pcm_out <: output; + c_pcm_out <: output; + c_pcm_out <: output; + c_pcm_out <: output; + + } else { if((-a) > max) max = (-a); if(a > max) max = a; @@ -252,16 +263,26 @@ void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, s if(e > max) max = e; if((-f) > max) max = (-f); if(f > max) max = f; + if((-g) > max) max = (-g); + if(g > max) max = g; output = a+b+c+d+e+f+g+g; output >>=3; - output = output<<(GAIN+1); + output = output<<(GAIN); + c_pcm_out :> unsigned req; + c_pcm_out <: output; + c_pcm_out <: a << GAIN; + c_pcm_out <: b << GAIN; + c_pcm_out <: c << GAIN; + c_pcm_out <: d << GAIN; + c_pcm_out <: e << GAIN; + c_pcm_out <: f << GAIN; + c_pcm_out <: g << GAIN; + } max = max - (max>>17); - c_pcm_out :> unsigned req; - c_pcm_out <: output; - c_pcm_out <: output; + } } } @@ -297,7 +318,7 @@ void pcm_pdm_mic(streaming chanend c_pcm_out) c_4x_pdm_mic_0, c_4x_pdm_mic_1); pdm_to_pcm_4x(c_4x_pdm_mic_0, c_ds_output_0); - pdm_to_pcm_4x(c_4x_pdm_mic_1, c_ds_output_1); + pdm_to_pcm_4x(c_4x_pdm_mic_1, c_ds_output_1); example(c_ds_output_0, c_ds_output_1, c_pcm_out, c); From b14a0de740dae6795c59366868e83736ffef1803 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 6 Oct 2015 16:06:45 +0100 Subject: [PATCH 13/21] Initial work to move codebase to use lib_array_mic --- module_usb_audio/audio.h | 2 +- module_usb_audio/audio.xc | 26 +- module_usb_audio/devicedefines.h | 18 +- module_usb_audio/main.xc | 6 +- .../{ => pdm_mics}/pcm_pdm_mic.xc | 244 ++++++++++++++---- 5 files changed, 230 insertions(+), 66 deletions(-) rename module_usb_audio/{ => pdm_mics}/pcm_pdm_mic.xc (57%) diff --git a/module_usb_audio/audio.h b/module_usb_audio/audio.h index 662a9af3..d8c56ac1 100644 --- a/module_usb_audio/audio.h +++ b/module_usb_audio/audio.h @@ -27,7 +27,7 @@ void audio(chanend c_in, , server interface i_dfu dfuInterface #endif #ifdef PDM_PCM_IN -, streaming chanend c_pdm_in + , chanend c_pdm_in #endif ); diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 4f470862..31f43714 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -224,7 +224,7 @@ static inline void TransferAdatTxSamples(chanend c_adat_out, const unsigned samp #pragma unsafe arrays -static inline unsigned DoSampleTransfer(chanend c_out, int readBuffNo, unsigned underflowWord) +static inline unsigned DoSampleTransfer(chanend c_out, const int readBuffNo, const unsigned underflowWord) { outuint(c_out, underflowWord); @@ -262,7 +262,6 @@ static inline unsigned DoSampleTransfer(chanend c_out, int readBuffNo, unsigned { int tmp = inuint(c_out); samplesOut[i] = tmp; - samplesOut[i] = samplesIn_0[0]; } #else inuint(c_out); @@ -435,7 +434,7 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, chanend c_dig_rx, #endif #ifdef PDM_PCM_IN - streaming chanend c_pdm_pcm, + chanend c_pdm_pcm, #endif chanend ?c_adc) @@ -689,7 +688,7 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, #endif #if defined(SPDIF_RX) || defined(ADAT_RX) - /* Request digital data (with prefill) */ + /* Request digital data (with prefill) */ outuint(c_dig_rx, 0); #endif #if defined(SPDIF_TX) && (NUM_USB_CHAN_OUT > 0) @@ -699,15 +698,14 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, #endif #ifdef PDM_PCM_IN - c_pdm_pcm <: 1; - c_pdm_pcm :> samplesIn_0[0]; - c_pdm_pcm :> samplesIn_0[1]; - c_pdm_pcm :> samplesIn_0[2]; - c_pdm_pcm :> samplesIn_0[3]; - c_pdm_pcm :> samplesIn_0[4]; - c_pdm_pcm :> samplesIn_0[5]; - c_pdm_pcm :> samplesIn_0[6]; - c_pdm_pcm :> samplesIn_0[7]; + /* Request samples from PDM->PCM comverter */ + //c_pdm_pcm <: 1; + +#pragma loop unroll + for(int i = 0; i < 2 /*NUM_PDM_MICS*/; i++) + { + c_pdm_pcm :> samplesIn_0[i]; + } #endif } @@ -942,7 +940,7 @@ chanend ?c_config, chanend ?c , server interface i_dfu dfuInterface #endif #ifdef PDM_PCM_IN -, streaming chanend c_pdm_in +, chanend c_pdm_in #endif ) { diff --git a/module_usb_audio/devicedefines.h b/module_usb_audio/devicedefines.h index cdfaa48d..ad33f4cd 100644 --- a/module_usb_audio/devicedefines.h +++ b/module_usb_audio/devicedefines.h @@ -45,6 +45,13 @@ #define SPDIF_TX_TILE AUDIO_IO_TILE #endif +/** + * @brief Location (tile) of PDM Rx. Default: AUDIO_IO_TILE + */ +#ifndef PDM_TILE +#define PDM_TILE AUDIO_IO_TILE +#endif + /** * @brief Number of input channels (device to host). Default: NONE (Must be defined by app) */ @@ -201,11 +208,18 @@ /* Feature defines */ +/** + * @brief Number of PDM microphones in the design. Default: None + */ +#ifndef NUM_PDM_MICS +#define NUM_PDM_MICS (0) +#endif + /** * @brief Enable MIDI functionality including buffering, descriptors etc. Default: DISABLED */ #ifndef MIDI -#define MIDI (0) +#define MIDI (0) #endif #if defined(MIDI) && (MIDI == 0) @@ -1088,6 +1102,8 @@ #endif #endif + + /* Endpoint addresses enums */ enum USBEndpointNumber_In { diff --git a/module_usb_audio/main.xc b/module_usb_audio/main.xc index 9eb586b2..c14c6c61 100755 --- a/module_usb_audio/main.xc +++ b/module_usb_audio/main.xc @@ -44,7 +44,7 @@ #include "clocking.h" #ifdef PDM_PCM_IN -void pcm_pdm_mic(streaming chanend c_pcm_out); +void pcm_pdm_mic(chanend c_pcm_out); #endif void genclock(); @@ -414,7 +414,7 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, , server interface i_dfu dfuInterface #endif #ifdef PDM_PCM_IN - , streaming chanend c_pdm_pcm + , chanend c_pdm_pcm #endif ) { @@ -544,7 +544,7 @@ int main() #endif #ifdef PDM_PCM_IN - streaming chan c_pdm_pcm; + chan c_pdm_pcm; #endif USER_MAIN_DECLARATIONS diff --git a/module_usb_audio/pcm_pdm_mic.xc b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc similarity index 57% rename from module_usb_audio/pcm_pdm_mic.xc rename to module_usb_audio/pdm_mics/pcm_pdm_mic.xc index 546a1cd7..89a90e89 100644 --- a/module_usb_audio/pcm_pdm_mic.xc +++ b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc @@ -1,29 +1,35 @@ -#include +/* This file includes an integration of lib_array_mic into USB Audio */ + #include #include #include -#include "beam.h" #include #include #include #include -#include "static_constants.h" -#include "debug_print.h" +//#include "debug_print.h" +#include "devicedefines.h" +#include "mic_array.h" +#include "mic_array_board_support.h" #if 1 -on tile[0]: in port p_pdm_clk = XS1_PORT_1E; -on tile[0]: in buffered port:8 p_pdm_mics = XS1_PORT_8B; -in port p_mclk = on tile[0]: XS1_PORT_1F; -clock mclk = on tile[0]: XS1_CLKBLK_1; -clock pdmclk = on tile[0]: XS1_CLKBLK_3; +in port p_pdm_clk = PORT_PDM_CLK; +in port p_pdm_mics = PORT_PDM_DATA; + +in port p_mclk = PORT_PDM_MCLK; +clock mclk = on tile[PDM_TILE]: XS1_CLKBLK_1; +clock pdmclk = on tile[PDM_TILE]: XS1_CLKBLK_3; + + +on tile[0]:p_leds leds = DEFAULT_INIT; // LEDs -out port p_led0to7 = on tile[0]: XS1_PORT_8C; -out port p_led8 = on tile[0]: XS1_PORT_1K; -out port p_led9 = on tile[0]: XS1_PORT_1L; -out port p_led10to12 = on tile[0]: XS1_PORT_8D; -out port p_leds_oen = on tile[0]: XS1_PORT_1P; +//out port p_led0to7 = on tile[0]: XS1_PORT_8C; +//out port p_led8 = on tile[0]: XS1_PORT_1K; +//out port p_led9 = on tile[0]: XS1_PORT_1L; +//out port p_led10to12 = on tile[0]: XS1_PORT_8D; +//out port p_leds_oen = on tile[0]: XS1_PORT_1P; // Buttons in port p_buttons = on tile[0]: XS1_PORT_4A; @@ -35,6 +41,7 @@ enum buttons BUTTON_D=1<<3 }; +#if 0 void lightLeds(int only_one_mic) { if(only_one_mic) @@ -52,7 +59,9 @@ void lightLeds(int only_one_mic) p_led9 <: 0; } } +#endif +#if 0 #define BUTTON_PRESSED(but_mask, old_val, new_val) (((old_val) & (but_mask)) == (but_mask) && ((new_val) & (but_mask)) == 0) #define BUTTON_DEBOUNCE_DELAY (20000000) #define LED_ON 0xFFFF @@ -160,16 +169,7 @@ void buttons_and_leds(chanend c) } } } - - -typedef struct { - unsigned ch_a; - unsigned ch_b; -} double_packed_audio; - -typedef struct { - double_packed_audio data[4][1< 0){ + delay--; + printf("n: %d\n", delay); + } + break; + } + case 3:{ + dir++; + if(dir == 6) + dir = 0; + break; + } + } + set_dir(lb, dir); + } + break; + } + default:break; + } +#if 1 + int output = - 2*delay_buffer[(delay_head-delay)%MAX_DELAY][0]; + switch(dir){ + case 0: + output = delay_buffer[delay_head][1] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][4]; + break; + case 1: + output = delay_buffer[delay_head][2] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][5]; + break; + case 2: + output = delay_buffer[delay_head][3] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][6]; + break; + case 3: + output = delay_buffer[delay_head][4] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][1]; + break; + case 4: + output = delay_buffer[delay_head][5] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][2]; + break; + case 5: + output = delay_buffer[delay_head][6] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][3]; + break; + } + c_audio <: output<<2; + c_audio <: output<<2; +#else + int output = 0; + for(unsigned i=1;i<6;i++){ + output += audio[buffer].data[i][0]; + } + c_audio <: output; + c_audio <: output; +#endif + delay_head++; + delay_head%=MAX_DELAY; + } + } +} + + + +void pcm_pdm_mic(chanend c_pcm_out) { streaming chan c_multi_channel_pdm, c_sync, c_4x_pdm_mic_0, c_4x_pdm_mic_1; streaming chan c_ds_output_0, c_ds_output_1; streaming chan c_buffer_mic0, c_buffer_mic1; - unsigned long long shared_memory[2] = {0}; - + + interface led_button_if lb; + configure_clock_src(mclk, p_mclk); configure_clock_src_divide(pdmclk, p_mclk, 2); configure_port_clock_output(p_pdm_clk, pdmclk); @@ -302,27 +462,17 @@ void pcm_pdm_mic(streaming chanend c_pcm_out) start_clock(mclk); start_clock(pdmclk); - chan c; - par { - buttons_and_leds(c); - - unsafe + decimator_config dc = {0, 1, 0, 0}; + + unsafe + { + par { - unsigned long long * unsafe p_shared_memory = shared_memory; - par - { - - //Input stage - pdm_first_stage(p_pdm_mics, p_shared_memory, - PDM_BUFFER_LENGTH_LOG2, c_sync, - c_4x_pdm_mic_0, c_4x_pdm_mic_1); - - pdm_to_pcm_4x(c_4x_pdm_mic_0, c_ds_output_0); - pdm_to_pcm_4x(c_4x_pdm_mic_1, c_ds_output_1); - - example(c_ds_output_0, c_ds_output_1, c_pcm_out, c); - - } + button_and_led_server(lb, leds, p_buttons); + pdm_rx(p_pdm_mics, c_4x_pdm_mic_0, c_4x_pdm_mic_1); + decimate_to_pcm_4ch_48KHz(c_4x_pdm_mic_0, c_ds_output_0, dc); + decimate_to_pcm_4ch_48KHz(c_4x_pdm_mic_1, c_ds_output_1, dc); + lores_DAS_fixed(c_ds_output_0, c_ds_output_1, lb, c_pcm_out); } } From 09437dc5882113586038ff2f700fd3379725c75f Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Mon, 12 Oct 2015 11:52:20 +0100 Subject: [PATCH 14/21] Fixed num mics and loopback. --- module_usb_audio/audio.xc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 31f43714..0b07c330 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -262,6 +262,7 @@ static inline unsigned DoSampleTransfer(chanend c_out, const int readBuffNo, con { int tmp = inuint(c_out); samplesOut[i] = tmp; + samplesOut[i] = samplesIn_0[0]; } #else inuint(c_out); @@ -702,7 +703,7 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, //c_pdm_pcm <: 1; #pragma loop unroll - for(int i = 0; i < 2 /*NUM_PDM_MICS*/; i++) + for(int i = 0; i < 8 /*NUM_PDM_MICS*/; i++) { c_pdm_pcm :> samplesIn_0[i]; } From a587c48f76020ea0b06c36b4987fd75322ef8f10 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Mon, 12 Oct 2015 11:52:45 +0100 Subject: [PATCH 15/21] Added build config for non-beam and beam-forming demo --- module_usb_audio/pdm_mics/pcm_pdm_mic.xc | 454 ++++++++--------------- 1 file changed, 155 insertions(+), 299 deletions(-) diff --git a/module_usb_audio/pdm_mics/pcm_pdm_mic.xc b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc index 89a90e89..ddec9cd9 100644 --- a/module_usb_audio/pdm_mics/pcm_pdm_mic.xc +++ b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc @@ -7,11 +7,13 @@ #include #include #include -//#include "debug_print.h" +#include #include "devicedefines.h" #include "mic_array.h" #include "mic_array_board_support.h" +#define FORM_BEAM 1 + #if 1 in port p_pdm_clk = PORT_PDM_CLK; in port p_pdm_mics = PORT_PDM_DATA; @@ -23,13 +25,6 @@ clock pdmclk = on tile[PDM_TILE]: XS1_CLKBLK_3; on tile[0]:p_leds leds = DEFAULT_INIT; - -// LEDs -//out port p_led0to7 = on tile[0]: XS1_PORT_8C; -//out port p_led8 = on tile[0]: XS1_PORT_1K; -//out port p_led9 = on tile[0]: XS1_PORT_1L; -//out port p_led10to12 = on tile[0]: XS1_PORT_8D; -//out port p_leds_oen = on tile[0]: XS1_PORT_1P; // Buttons in port p_buttons = on tile[0]: XS1_PORT_4A; @@ -41,258 +36,19 @@ enum buttons BUTTON_D=1<<3 }; -#if 0 -void lightLeds(int only_one_mic) -{ - if(only_one_mic) - { - p_led10to12 <: 0x3; - p_led0to7 <: 0xff; - p_led8 <: 1; - p_led9 <: 1; - } - else - { - p_led0to7 <: 0; - p_led10to12 <: 0x4; - p_led8 <: 0; - p_led9 <: 0; - } -} -#endif -#if 0 -#define BUTTON_PRESSED(but_mask, old_val, new_val) (((old_val) & (but_mask)) == (but_mask) && ((new_val) & (but_mask)) == 0) -#define BUTTON_DEBOUNCE_DELAY (20000000) -#define LED_ON 0xFFFF -void buttons_and_leds(chanend c) -{ - int button_val; - int buttons_active = 1; - unsigned buttons_timeout; - unsigned time; - unsigned glow_time; - timer button_tmr; - timer leds_tmr; - timer glow_tmr; - const int pwm_cycle = 100000; // The period in 100Mhz timer ticks of the pwm - const int pwm_res = 256; // The resolution of the pwm - const int pwm_delay = pwm_cycle / pwm_res; // The period between updates to the port output - int count = 0; // The count that tracks where we are in the pwm cycle - - int period = 1 * 1000 * 1000 * 100 * 15; // period from off to on = 1s; - unsigned res = 300; // increment the brightness in this - // number of steps - int delay = period / res; // how long to wait between updates - // int delay = 1 * 1000 * 1000 * 100; - int dir = 1; - int on_led = 0; - - p_leds_oen <: 1; - p_leds_oen <: 0; - // This array stores the pwm levels for the leds - int level[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - for (int i=0; i < 13; i++) { - level[i] = level[i] / (0xFFFF / pwm_res); - } - - p_buttons :> button_val; - leds_tmr :> time; - glow_tmr :> glow_time; - - int only_one_mic = 1; - - p_led0to7 <:~0; - p_led8 <:~0; - p_led9 <:~0; - //p_led10to12 <:~0; -// p_leds_oen <:~0; +static const one_meter_thirty_degrees[6] = {0, 3, 8, 11, 8, 3}; - lightLeds(only_one_mic); - only_one_mic? printstrln("one"):printstrln("all"); - - level[0] = 0xff; - while (1) { - select - { - case buttons_active => p_buttons when pinsneq(button_val) :> unsigned new_button_val: - - if BUTTON_PRESSED(BUTTON_A, button_val, new_button_val) { - debug_printf("Button A\n"); - only_one_mic = 1-only_one_mic; - only_one_mic? printstrln("one"):printstrln("all"); - - lightLeds(only_one_mic); - - c <: 0; - buttons_active = 0; - } - if BUTTON_PRESSED(BUTTON_B, button_val, new_button_val) { - debug_printf("Button B\n"); - only_one_mic = 1-only_one_mic; - only_one_mic? printstrln("one"):printstrln("all"); - lightLeds(only_one_mic); - c <: 0; - buttons_active = 0; - } - if BUTTON_PRESSED(BUTTON_C, button_val, new_button_val) { - debug_printf("Button C\n"); - only_one_mic = 1-only_one_mic; - only_one_mic? printstrln("one"):printstrln("all"); - c <: 0; - buttons_active = 0; - } - if BUTTON_PRESSED(BUTTON_D, button_val, new_button_val) { - debug_printf("Button D\n"); - only_one_mic = 1-only_one_mic; - only_one_mic? printstrln("one"):printstrln("all"); - lightLeds(only_one_mic); - c <: 0; - buttons_active = 0; - } - if (!buttons_active) - { - button_tmr :> buttons_timeout; - buttons_timeout += BUTTON_DEBOUNCE_DELAY; - } - button_val = new_button_val; - break; - case !buttons_active => button_tmr when timerafter(buttons_timeout) :> void: - buttons_active = 1; - p_buttons :> button_val; - break; - - - } - } -} -#endif - - -static int dc_offset_removal(int sample, int &prex_x, int &prev_y){ - int r = prev_y- (prev_y>>5) + (sample - prex_x); - prex_x = sample; - prev_y = r; - return r; -} - -#if 0 -void example(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, streaming chanend c_pcm_out, chanend cc) -{ - - - unsigned buffer = 1; //buffer index - synchronised_audio audio[2]; //double buffered - memset(audio, sizeof(synchronised_audio), 2); - - int prev_x[7] = {0}; - int prev_y[7] = {0}; - - int max = 0; - - unsafe - { - c_ds_output_0 <: (synchronised_audio * unsafe)audio[0].data[0]; - c_ds_output_1 <: (synchronised_audio * unsafe)audio[0].data[2]; - - int only_one_mic=1; - while(1) - { - - schkct(c_ds_output_0, 8); - schkct(c_ds_output_1, 8); - - c_ds_output_0 <: (synchronised_audio * unsafe)audio[buffer].data[0]; - c_ds_output_1 <: (synchronised_audio * unsafe)audio[buffer].data[2]; - - buffer = 1 - buffer; - - // audio[buffer] is good to go - - int a = dc_offset_removal( audio[buffer].data[0][0].ch_a, prev_x[0], prev_y[0]); - int b = dc_offset_removal( audio[buffer].data[0][0].ch_b, prev_x[0], prev_y[0]); - int c = dc_offset_removal( audio[buffer].data[1][0].ch_a, prev_x[1], prev_y[1]); - int d = dc_offset_removal( audio[buffer].data[1][0].ch_b, prev_x[2], prev_y[2]); - int e = dc_offset_removal( audio[buffer].data[2][0].ch_a, prev_x[3], prev_y[3]); - int f = dc_offset_removal( audio[buffer].data[2][0].ch_b, prev_x[4], prev_y[4]); - int g = dc_offset_removal( audio[buffer].data[3][0].ch_a, prev_x[5], prev_y[5]); - int h = dc_offset_removal( audio[buffer].data[3][0].ch_b, prev_x[6], prev_y[6]);//Expect dead - - //printf("%x %x %x %x %x %x %x %x\n", a, b, c, d, e, f, g, h); - - unsigned v = a*a; - - select { - case cc:> int:{ - only_one_mic = 1-only_one_mic; - break; - } - default:break; - } - -#define GAIN 5 - - if((-a) > max) max = (-a); - if(a > max) max = a; - int output; - if(only_one_mic){ - output = a< unsigned req; - c_pcm_out <: output; - c_pcm_out <: output; - c_pcm_out <: output; - c_pcm_out <: output; - c_pcm_out <: output; - c_pcm_out <: output; - c_pcm_out <: output; - c_pcm_out <: output; - - - } else { - if((-a) > max) max = (-a); - if(a > max) max = a; - if((-b) > max) max = (-b); - if(b > max) max = b; - if((-c) > max) max = (-c); - if(c > max) max = c; - if((-d) > max) max = (-d); - if(d > max) max = d; - if((-e) > max) max = (-e); - if(e > max) max = e; - if((-f) > max) max = (-f); - if(f > max) max = f; - if((-g) > max) max = (-g); - if(g > max) max = g; - output = a+b+c+d+e+f+g+g; - output >>=3; - output = output<<(GAIN); - c_pcm_out :> unsigned req; - c_pcm_out <: output; - c_pcm_out <: a << GAIN; - c_pcm_out <: b << GAIN; - c_pcm_out <: c << GAIN; - c_pcm_out <: d << GAIN; - c_pcm_out <: e << GAIN; - c_pcm_out <: f << GAIN; - c_pcm_out <: g << GAIN; - - } - - max = max - (max>>17); - - - } - } -} -#endif - -static void set_dir(client interface led_button_if lb, unsigned dir){ +static void set_dir(client interface led_button_if lb, unsigned dir, unsigned delay[]){ for(unsigned i=0;i<13;i++) lb.set_led_brightness(i, 0); + delay[0] = 5; + for(unsigned i=0;i<6;i++) + delay[i+1] = one_meter_thirty_degrees[(i - dir + 3 +6)%6]; + switch(dir){ case 0:{ lb.set_led_brightness(0, 255); @@ -328,22 +84,40 @@ static void set_dir(client interface led_button_if lb, unsigned dir){ } - void lores_DAS_fixed(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, - client interface led_button_if lb, chanend c_audio){ + client interface led_button_if lb, chanend c_audio) +{ unsigned buffer = 1; //buffer index frame_audio audio[2]; //double buffered memset(audio, sizeof(frame_audio), 0); + int omni = 0; + #define MAX_DELAY 128 - unsigned delay = 6; + unsigned gain = 128; +#ifdef FORM_BEAM + unsigned delay[7] = {0, 0, 0, 0, 0, 0, 0}; int delay_buffer[MAX_DELAY][7]; memset(delay_buffer, sizeof(int)*8*8, 0); unsigned delay_head = 0; unsigned dir = 0; - set_dir(lb, dir); +#endif + +#ifdef FORM_BEAM + set_dir(lb, dir, delay); +#else + int summed = 0; + + /* Light center LED, kill other LEDs */ + for(unsigned i=0;i<13;i++) + lb.set_led_brightness(i, 0); + + lb.set_led_brightness(12, 255); + +#endif + unsafe{ c_ds_output_0 <: (frame_audio * unsafe)audio[0].data[0]; @@ -359,11 +133,15 @@ void lores_DAS_fixed(streaming chanend c_ds_output_0, streaming chanend c_ds_out buffer = 1 - buffer; +#ifdef FORM_BEAM + //copy the current sample to the delay buffer for(unsigned i=0;i<7;i++) delay_buffer[delay_head][i] = audio[buffer].data[i][0]; //light the LED for the current direction +#endif + int t; @@ -375,78 +153,156 @@ void lores_DAS_fixed(streaming chanend c_ds_output_0, streaming chanend c_ds_out if(pressed == BUTTON_PRESSED){ switch(button){ case 0:{ - dir--; - if(dir == -1) - dir = 5; + +#ifdef FORM_BEAM + printf("beamed\n"); + if(omni) + { + omni = 0; + lb.set_led_brightness(12, 0); + set_dir(lb, dir, delay); + } + else + { + dir--; + if(dir == -1) + dir = 5; + set_dir(lb, dir, delay); + printf("dir %d\n", dir+1); + for(unsigned i=0;i<7;i++) + printf("delay[%d] = %d\n", i, delay[i]); + printf("\n"); + } +#else + summed = !summed; + + if(summed) + { + for(unsigned i=0; i < 13; i++) + lb.set_led_brightness(i, 255); + } + else + { + for(unsigned i=0;i<13;i++) + lb.set_led_brightness(i, 0); + + lb.set_led_brightness(12, 255); + + } + +#endif break; } case 1:{ - if(delay +1 < MAX_DELAY){ - delay++; - printf("n: %d\n", delay); - } + gain = ((gain<<3) + gain)>>3; + printf("gain: %d\n", gain); break; } case 2:{ - if(delay > 0){ - delay--; - printf("n: %d\n", delay); - } + gain = ((gain<<3) - gain)>>3; + printf("gain: %d\n", gain); break; } case 3:{ +#ifdef FORM_BEAM + lb.set_led_brightness(12, 255); + + for(unsigned i=0;i<12;i++) + lb.set_led_brightness(i, 0); +#if 0 dir++; if(dir == 6) dir = 0; + set_dir(lb, dir, delay); + printf("dir %d\n", dir+1); + for(unsigned i=0;i<7;i++) + printf("delay[%d] = %d\n", i, delay[i]); + printf("\n"); + #endif + printf("omni\n"); + omni = 1; +#endif break; } } - set_dir(lb, dir); } break; } default:break; } -#if 1 - int output = - 2*delay_buffer[(delay_head-delay)%MAX_DELAY][0]; - switch(dir){ - case 0: - output = delay_buffer[delay_head][1] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][4]; - break; - case 1: - output = delay_buffer[delay_head][2] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][5]; - break; - case 2: - output = delay_buffer[delay_head][3] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][6]; - break; - case 3: - output = delay_buffer[delay_head][4] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][1]; - break; - case 4: - output = delay_buffer[delay_head][5] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][2]; - break; - case 5: - output = delay_buffer[delay_head][6] + delay_buffer[(delay_head-2*delay)%MAX_DELAY][3]; - break; - } - c_audio <: output<<2; - c_audio <: output<<2; -#else int output = 0; - for(unsigned i=1;i<6;i++){ - output += audio[buffer].data[i][0]; + +#ifdef FORM_BEAM + if(!omni) + { + /* Do the sum of the delayed mics */ + for(unsigned i=0;i<7;i++) + output += delay_buffer[(delay_head - delay[i])%MAX_DELAY][i]; + + output = ((uint64_t)output*gain)>>8; + + c_audio <: output; + + /* Send out the individual mics */ + for(unsigned i=0;i<7;i++) + { + /* Apply gain and output samples */ + output = audio[buffer].data[i][0]; + output = ((uint64_t)output*gain)>>8; + c_audio <: output; + } + } + else + { + /* Send out Mic[0] 8 times */ + output = audio[buffer].data[0][0]; + output <<=2; + + output = ((uint64_t)output*gain)>>8; + + for(unsigned i=0;i<8;i++) + c_audio <: output; + } +#else + if(summed) + { + /* Output summed */ + for(unsigned i=0;i<7;i++) + output += audio[buffer].data[i][0]; + + output = ((uint64_t)output*gain)>>8; + c_audio <: output; + + /* Apply gain to all mics and send */ + for(unsigned i=0;i<7;i++) + { + output = audio[buffer].data[i][0]; + output = ((uint64_t)output*gain)>>8; + c_audio <: output; + } + } + else + { + /* Send mic 0 out 8 times */ + for(unsigned i=0;i<8;i++) + { + /* Apply gain and output samples */ + output = audio[buffer].data[0][0]; + output = ((uint64_t)output*gain)>>8; + c_audio <: output<<2; + } } - c_audio <: output; - c_audio <: output; #endif + +#ifdef FORM_BEAM delay_head++; delay_head%=MAX_DELAY; +#endif } } } - void pcm_pdm_mic(chanend c_pcm_out) { streaming chan c_multi_channel_pdm, c_sync, c_4x_pdm_mic_0, c_4x_pdm_mic_1; From 2580dcc7213e36b09036ad1144235dcecbf08708 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Tue, 13 Oct 2015 17:32:12 +0100 Subject: [PATCH 16/21] Updated pdm code to use latest lib_array_mic code --- module_usb_audio/pdm_mics/pcm_pdm_mic.xc | 43 +++++++++++++----------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/module_usb_audio/pdm_mics/pcm_pdm_mic.xc b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc index ddec9cd9..18a10225 100644 --- a/module_usb_audio/pdm_mics/pcm_pdm_mic.xc +++ b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc @@ -9,18 +9,20 @@ #include #include #include "devicedefines.h" + +#include "fir_decimator.h" #include "mic_array.h" #include "mic_array_board_support.h" #define FORM_BEAM 1 #if 1 -in port p_pdm_clk = PORT_PDM_CLK; -in port p_pdm_mics = PORT_PDM_DATA; +in port p_pdm_clk = PORT_PDM_CLK; +in buffered port:32 p_pdm_mics = PORT_PDM_DATA; -in port p_mclk = PORT_PDM_MCLK; -clock mclk = on tile[PDM_TILE]: XS1_CLKBLK_1; -clock pdmclk = on tile[PDM_TILE]: XS1_CLKBLK_3; +in port p_mclk = PORT_PDM_MCLK; +clock mclk = on tile[PDM_TILE]: XS1_CLKBLK_1; +clock pdmclk = on tile[PDM_TILE]: XS1_CLKBLK_3; on tile[0]:p_leds leds = DEFAULT_INIT; @@ -96,16 +98,13 @@ void lores_DAS_fixed(streaming chanend c_ds_output_0, streaming chanend c_ds_out #define MAX_DELAY 128 - unsigned gain = 128; + unsigned gain = 4096; #ifdef FORM_BEAM unsigned delay[7] = {0, 0, 0, 0, 0, 0, 0}; int delay_buffer[MAX_DELAY][7]; memset(delay_buffer, sizeof(int)*8*8, 0); unsigned delay_head = 0; unsigned dir = 0; -#endif - -#ifdef FORM_BEAM set_dir(lb, dir, delay); #else int summed = 0; @@ -117,9 +116,8 @@ void lores_DAS_fixed(streaming chanend c_ds_output_0, streaming chanend c_ds_out lb.set_led_brightness(12, 255); #endif - - - unsafe{ + unsafe + { c_ds_output_0 <: (frame_audio * unsafe)audio[0].data[0]; c_ds_output_1 <: (frame_audio * unsafe)audio[0].data[4]; @@ -302,6 +300,11 @@ void lores_DAS_fixed(streaming chanend c_ds_output_0, streaming chanend c_ds_out } } +//TODO make these not global +int data_0[8*COEFS_PER_PHASE] = {0}; +int data_1[8*COEFS_PER_PHASE] = {0}; + + void pcm_pdm_mic(chanend c_pcm_out) { @@ -318,17 +321,19 @@ void pcm_pdm_mic(chanend c_pcm_out) start_clock(mclk); start_clock(pdmclk); - decimator_config dc = {0, 1, 0, 0}; - unsafe { + const int * unsafe p[1] = {fir_1_coefs[0]}; + decimator_config dc0 = {0, 1, 0, 0, 1, p, data_0, 0, {0,0, 0, 0}}; + decimator_config dc1 = {0, 1, 0, 0, 1, p, data_1, 0, {0,0, 0, 0}}; + par { - button_and_led_server(lb, leds, p_buttons); - pdm_rx(p_pdm_mics, c_4x_pdm_mic_0, c_4x_pdm_mic_1); - decimate_to_pcm_4ch_48KHz(c_4x_pdm_mic_0, c_ds_output_0, dc); - decimate_to_pcm_4ch_48KHz(c_4x_pdm_mic_1, c_ds_output_1, dc); - lores_DAS_fixed(c_ds_output_0, c_ds_output_1, lb, c_pcm_out); + button_and_led_server(lb, leds, p_buttons); + pdm_rx(p_pdm_mics, c_4x_pdm_mic_0, c_4x_pdm_mic_1); + decimate_to_pcm_4ch(c_4x_pdm_mic_0, c_ds_output_0, dc0); + decimate_to_pcm_4ch(c_4x_pdm_mic_1, c_ds_output_1, dc1); + lores_DAS_fixed(c_ds_output_0, c_ds_output_1, lb, c_pcm_out); } } From 11c43920ee9648f2eeed1a759e08110a850fd9e7 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Thu, 29 Oct 2015 20:45:07 +0000 Subject: [PATCH 17/21] Removed PDM_PCM_IN define (replaced with check for NUM_PDM_MICS > 0). Also use of PDM_TILE on pcm_pdm_mic() task call. --- module_usb_audio/audio.h | 2 +- module_usb_audio/audio.xc | 6 +++--- module_usb_audio/main.xc | 17 ++++++++--------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/module_usb_audio/audio.h b/module_usb_audio/audio.h index d8c56ac1..304656e8 100644 --- a/module_usb_audio/audio.h +++ b/module_usb_audio/audio.h @@ -26,7 +26,7 @@ void audio(chanend c_in, #if (XUD_TILE != 0) , server interface i_dfu dfuInterface #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) , chanend c_pdm_in #endif ); diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 99e9f97d..36ae61df 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -434,7 +434,7 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, #if(defined(SPDIF_RX) || defined(ADAT_RX)) chanend c_dig_rx, #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) chanend c_pdm_pcm, #endif @@ -940,7 +940,7 @@ chanend ?c_config, chanend ?c #if XUD_TILE != 0 , server interface i_dfu dfuInterface #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) , chanend c_pdm_in #endif ) @@ -1211,7 +1211,7 @@ chanend ?c_config, chanend ?c #if defined (ADAT_RX) || defined (SPDIF_RX) c_dig_rx, #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) c_pdm_in, #endif c); diff --git a/module_usb_audio/main.xc b/module_usb_audio/main.xc index c14c6c61..93cef778 100755 --- a/module_usb_audio/main.xc +++ b/module_usb_audio/main.xc @@ -43,8 +43,8 @@ #include "clocking.h" -#ifdef PDM_PCM_IN -void pcm_pdm_mic(chanend c_pcm_out); +#if (NUM_PDM_MICS > 0) +#include "pcm_pdm_mic.h" #endif void genclock(); @@ -413,7 +413,7 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, #if (XUD_TILE != 0) , server interface i_dfu dfuInterface #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) , chanend c_pdm_pcm #endif ) @@ -456,7 +456,7 @@ void usb_audio_io(chanend c_aud_in, chanend ?c_adc, #if XUD_TILE != 0 , dfuInterface #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) , c_pdm_pcm #endif ); @@ -543,7 +543,7 @@ int main() #define dfuInterface null #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) chan c_pdm_pcm; #endif @@ -590,7 +590,7 @@ int main() #if XUD_TILE != 0 , dfuInterface #endif -#ifdef PDM_PCM_IN +#if (NUM_PDM_MICS > 0) , c_pdm_pcm #endif @@ -655,9 +655,8 @@ int main() } #endif -#ifdef PDM_PCM_IN - // TODO tile - on stdcore[0]: pcm_pdm_mic(c_pdm_pcm); +#if (NUM_PDM_MICS > 0) + on stdcore[PDM_TILE]: pcm_pdm_mic(c_pdm_pcm); #endif USER_MAIN_CORES } From ca657877e9563154ee802e530218c4b53a6eb48c Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Fri, 30 Oct 2015 11:50:59 +0000 Subject: [PATCH 18/21] Tidy up mic integration into deliver() --- module_usb_audio/audio.xc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 36ae61df..60828ffb 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -698,12 +698,10 @@ unsigned static deliver(chanend c_out, chanend ?c_spd_out, outuint(c_spd_out, sample); /* Forward sample to S/PDIF Tx thread */ #endif -#ifdef PDM_PCM_IN - /* Request samples from PDM->PCM comverter */ - //c_pdm_pcm <: 1; - +#if (NUM_PDM_MICS > 0) + /* Get samples from PDM->PCM comverter */ #pragma loop unroll - for(int i = 0; i < 8 /*NUM_PDM_MICS*/; i++) + for(int i = 0; i < NUM_PDM_MICS; i++) { c_pdm_pcm :> samplesIn_0[i]; } From 91ac3c60bd8be80dfaaae6ea435b04e0992eabf4 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Fri, 30 Oct 2015 11:51:17 +0000 Subject: [PATCH 19/21] Remove hard-coded loopback from deliver() --- module_usb_audio/audio.xc | 1 - 1 file changed, 1 deletion(-) diff --git a/module_usb_audio/audio.xc b/module_usb_audio/audio.xc index 60828ffb..5ec45615 100755 --- a/module_usb_audio/audio.xc +++ b/module_usb_audio/audio.xc @@ -262,7 +262,6 @@ static inline unsigned DoSampleTransfer(chanend c_out, const int readBuffNo, con { int tmp = inuint(c_out); samplesOut[i] = tmp; - samplesOut[i] = samplesIn_0[0]; } #else inuint(c_out); From 37752f979c779637b5b14c75a1d12953e6e3c94c Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Fri, 30 Oct 2015 11:51:32 +0000 Subject: [PATCH 20/21] Added pcm_pdm_mic.h --- module_usb_audio/pdm_mics/pcm_pdm_mic.h | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 module_usb_audio/pdm_mics/pcm_pdm_mic.h diff --git a/module_usb_audio/pdm_mics/pcm_pdm_mic.h b/module_usb_audio/pdm_mics/pcm_pdm_mic.h new file mode 100644 index 00000000..dccd9ff1 --- /dev/null +++ b/module_usb_audio/pdm_mics/pcm_pdm_mic.h @@ -0,0 +1,2 @@ + +void pcm_pdm_mic(chanend c_pcm_out); From 9e7274d3d349e5b5f8614ef493ff873f0602ca66 Mon Sep 17 00:00:00 2001 From: Ross Owen Date: Fri, 30 Oct 2015 11:52:23 +0000 Subject: [PATCH 21/21] Tidy up mic integration task --- module_usb_audio/pdm_mics/pcm_pdm_mic.xc | 71 +++++++++++------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/module_usb_audio/pdm_mics/pcm_pdm_mic.xc b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc index 57e8b213..3f959b47 100644 --- a/module_usb_audio/pdm_mics/pcm_pdm_mic.xc +++ b/module_usb_audio/pdm_mics/pcm_pdm_mic.xc @@ -1,4 +1,9 @@ -/* This file includes an integration of lib_array_mic into USB Audio */ + +#include "devicedefines.h" + +#if (NUM_PDM_MICS > 0) + +/* This file includes an example integration of lib_array_mic into USB Audio */ #include #include @@ -8,58 +13,43 @@ #include #include #include -#include "devicedefines.h" #include "fir_decimator.h" #include "mic_array.h" -#include "mic_array_board_support.h" -#define FORM_BEAM 1 - -#if 1 +/* Hardware resources */ in port p_pdm_clk = PORT_PDM_CLK; in buffered port:32 p_pdm_mics = PORT_PDM_DATA; +in port p_mclk = PORT_PDM_MCLK; +clock pdmclk = on tile[PDM_TILE]: XS1_CLKBLK_3; -in port p_mclk = PORT_PDM_MCLK; -clock mclk = on tile[PDM_TILE]: XS1_CLKBLK_1; -clock pdmclk = on tile[PDM_TILE]: XS1_CLKBLK_3; - -void user_pdm_process(frame_audio *audio) -{ - static unsigned gain = 8*4096*8; - - for(unsigned i=0;i<7;i++) - { - unsigned output = audio->data[i][0]; - audio->data[i][0] = ((uint64_t)output*gain)>>8; - } -} - +/* User hooks */ +unsafe void user_pdm_process(frame_audio * unsafe audio, int output[]); +void user_pdm_init(); void pdm_process(streaming chanend c_ds_output_0, streaming chanend c_ds_output_1, chanend c_audio) { - - unsigned buffer = 1; //buffer index - frame_audio audio[2]; //double buffered + unsigned buffer = 1; // Buffer index + frame_audio audio[2]; // Double buffered memset(audio, sizeof(frame_audio), 0); + int output[NUM_PDM_MICS]; + + user_pdm_init(); decimator_init_audio_frame(c_ds_output_0, c_ds_output_1, buffer, audio); - unsafe + while(1) { - while(1) + frame_audio * unsafe current = decimator_get_next_audio_frame(c_ds_output_0, c_ds_output_1, buffer, audio); + + unsafe { - frame_audio * current = decimator_get_next_audio_frame(c_ds_output_0, c_ds_output_1, buffer, audio); - - user_pdm_process(current); - - /* Send out the individual mics */ - for(unsigned i=0;i<7;i++) + user_pdm_process(current, output); + + for(int i = 0; i < NUM_PDM_MICS; i++) { - unsigned output = current->data[i][0]; - c_audio <: output; + c_audio <: output[i]; } - c_audio <: 0; } } } @@ -67,7 +57,13 @@ void pdm_process(streaming chanend c_ds_output_0, streaming chanend c_ds_output_ #define DF 1 #define OUTPUT_SAMPLE_RATE (48000/DF) -#define MASTER_CLOCK_FREQUENCY 24576000 + +#if MAX_FREQ != 48000 +#error NOT CURRENTLY SUPPORTED +#endif +#if MIN_FREQ != 48000 +#error NOT CURRENTLY SUPPORTED +#endif //TODO make these not global int data_0[4*COEFS_PER_PHASE*DF] = {0}; @@ -79,11 +75,9 @@ void pcm_pdm_mic(chanend c_pcm_out) streaming chan c_ds_output_0, c_ds_output_1; streaming chan c_buffer_mic0, c_buffer_mic1; - configure_clock_src(mclk, p_mclk); configure_clock_src_divide(pdmclk, p_mclk, 2); configure_port_clock_output(p_pdm_clk, pdmclk); configure_in_port(p_pdm_mics, pdmclk); - start_clock(mclk); start_clock(pdmclk); unsafe @@ -98,7 +92,6 @@ void pcm_pdm_mic(chanend c_pcm_out) decimate_to_pcm_4ch(c_4x_pdm_mic_1, c_ds_output_1, dc1); pdm_process(c_ds_output_0, c_ds_output_1, c_pcm_out); } - } }