Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions tests/test_mic_input/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The TARGET variable determines what target system the application is
# compiled for. It either refers to an XN file in the source directories
# or a valid argument for the --target option when compiling
TARGET = SMART-MIC-BASE-1V0


# The APP_NAME variable determines the name of the final .xe file. It should
# not include the .xe postfix. If left blank the name will default to
Expand All @@ -18,15 +18,25 @@ USED_MODULES = lib_mic_array lib_mic_array_board_support
# xcc for the final link (mapping) stage.
COMMON_FLAGS = -O2 -g -report

XCC_FLAGS_8_MICS_DDR = $(COMMON_FLAGS) -DCOUNT=8 -DDDR=1
XCC_FLAGS_7_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=7 -DDDR=0
#XCC_FLAGS_8_MICS_DDR = $(COMMON_FLAGS) -DCOUNT=8 -DDDR=1
# XCC_FLAGS_2_MICS_DDR = $(COMMON_FLAGS) -DCOUNT=2 -DDDR=1
#XCC_FLAGS_7_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=7 -DDDR=0
#XCC_FLAGS_6_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=6 -DDDR=0
#XCC_FLAGS_5_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=5 -DDDR=0
XCC_FLAGS_4_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=4 -DDDR=0
#XCC_FLAGS_4_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=4 -DDDR=0
#XCC_FLAGS_3_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=3 -DDDR=0
#XCC_FLAGS_2_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=2 -DDDR=0
#XCC_FLAGS_1_MICS_SDR = $(COMMON_FLAGS) -DCOUNT=1 -DDDR=0

XCC_FLAGS_2_MICS_DDR_XS3 = $(COMMON_FLAGS) -DCOUNT=2 -DDDR=1 -DXCORE_AI=1

ifeq ($(findstring XS3, $(CONFIG)), XS3)
TARGET = XCORE-AI-EXPLORER
else
TARGET = SMART-MIC-BASE-1V0
endif


# The XCORE_ARM_PROJECT variable, if set to 1, configures this
# project to create both xCORE and ARM binaries.
XCORE_ARM_PROJECT = 0
Expand Down
4 changes: 4 additions & 0 deletions tests/test_mic_input/src/mic_array_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
#define MIC_ARRAY_MAX_FRAME_SIZE_LOG2 7
#define MIC_ARRAY_NUM_MICS 8

#define MIC_ARRAY_CH0 PIN0
#define MIC_ARRAY_CH1 PIN4
#define MIC_ARRAY_CH2 PIN1
#define MIC_ARRAY_CH3 PIN3

#endif /* MIC_ARRAY_CONF_H_ */
157 changes: 135 additions & 22 deletions tests/test_mic_input/src/test_mic_input.xc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "stdio.h"
#include <stdlib.h>
#include <math.h>
#include <print.h>

#include "mic_array.h"
#include "mic_array_board_support.h"
Expand All @@ -26,15 +27,35 @@
#define FFT_CHANNELS ((COUNT+1)/2)
#define ENABLE_PRECISION_MAXIMISATION 1

on tile[0]: out port p_pdm_clk = XS1_PORT_1E;
#if DDR
on tile[0]: in buffered port:32 p_pdm_mics = XS1_PORT_4D;
on tile[0]: clock pdmclk6 = XS1_CLKBLK_3;
#ifdef XCORE_AI
#define PDM_TILE 1
#else
#define PDM_TILE 0
#endif

// Nominal setting is ref div = 25, fb_div = 1024, op_div = 2
// PCF Freq 0.96GHz
#define PLL_NOM 0xC003FF18 // This is 3.072MHz * 20

#ifdef XCORE_AI
on tile[1]: in buffered port:32 p_pdm_mics = PORT_PDM_DATA;
on tile[1]: in buffered port:32 p_pdm_mics_4b = XS1_PORT_4E;
on tile[1]: out port p_pdm_clk = PORT_PDM_CLK;
on tile[1]: clock pdmclk6 = XS1_CLKBLK_3;
on tile[1]: clock pdmclk = XS1_CLKBLK_2;
on tile[1]: in port p_mclk_in = PORT_MCLK_IN;
#else
on tile[0]: in buffered port:32 p_pdm_mics = XS1_PORT_8B;
on tile[0]: out port p_pdm_clk = XS1_PORT_1L;
#if DDR
on tile[0]: in buffered port:32 p_pdm_mics = XS1_PORT_4E;
on tile[0]: clock pdmclk6 = XS1_CLKBLK_3;
#else
on tile[0]: in buffered port:32 p_pdm_mics = XS1_PORT_8B;
#endif
on tile[0]: in port p_mclk_in = XS1_PORT_1K;
on tile[0]: clock pdmclk = XS1_CLKBLK_2;
#endif
on tile[0]: in port p_mclk = XS1_PORT_1F;
on tile[0]: clock pdmclk = XS1_CLKBLK_2;


int data[8][THIRD_STAGE_COEFS_PER_STAGE*DECIMATION_FACTOR];

Expand All @@ -46,6 +67,31 @@ int your_favourite_window_function(unsigned i, unsigned window_length){
void dsp_bfp_shl2( dsp_complex_t pts[], const uint32_t N,
const int32_t shift_re, const int32_t shift_im );

//Prints an ASCII art spectrum plot of the two mics.
//Linear X axis and Log Y axis
void print_spec(int64_t subband_rms_power[COUNT][FRAME_LENGTH/2]){
int dbs[COUNT][FRAME_LENGTH/2] = {{0}};
for (unsigned band=1;band < FRAME_LENGTH/2;band++){
for(unsigned ch=0;ch<2;ch++){
int64_t b = subband_rms_power[ch][band];
double p = sqrt((double)b);
int db = 10 * log10(p) - 25;
dbs[ch][band] = db;
// printf("db %d: %d\n", band, db);
}
}
for(int dbp = 50; dbp >= 0; dbp--){
for(unsigned ch=0;ch<2;ch++){
for (unsigned band=1;band < FRAME_LENGTH/2;band++){
printf("%s", dbs[ch][band] >= dbp ? "M" : "." );
}
printf(" ");
}
printf("\n");
}
}


void test(streaming chanend c_ds_output[DECIMATOR_COUNT]) {
unsafe{
unsigned buffer;
Expand Down Expand Up @@ -140,6 +186,8 @@ void test(streaming chanend c_ds_output[DECIMATOR_COUNT]) {
printf("\n");
#endif
}
print_spec(subband_rms_power);

if(total_power < 10000.0){
for(unsigned i=0;i<COUNT;i++){
printf("Microphone %d absent\n", i);
Expand Down Expand Up @@ -278,26 +326,85 @@ void test(streaming chanend c_ds_output[DECIMATOR_COUNT]) {
}
}

port p_rst_shared = on tile[1]: XS1_PORT_4F; // Bit 0: DAC_RST_N, Bit 1: ETH_RST_N
port p_i2c = on tile[1]: XS1_PORT_4E; // Bit 0: SCLK, Bit 1: SDA
on tile[PDM_TILE] : clock mclk_internal = XS1_CLKBLK_5;

void set_node_pll_reg(tileref tile_ref, unsigned reg_val){
write_sswitch_reg(get_tile_id(tile_ref), XS1_SSWITCH_PLL_CTL_NUM, reg_val);
}

void run_clock(void) {
configure_clock_xcore(mclk_internal, 10); // 24.576 MHz
configure_port_clock_output(p_mclk_in, mclk_internal);
start_clock(mclk_internal);
}




void set_pll(void) {
set_node_pll_reg(tile[0], PLL_NOM);
run_clock();
}

//Note unusual casting of channel to a port. i.e. we output directly onto channel rather than port
unsafe{
void call_mic_array_pdm_rx(chanend c_mic_dual_pdm, streaming chanend c_4x_pdm_mic_0, streaming chanend c_4x_pdm_mic_1){
unsafe{
buffered port:32 * unsafe p_ptr = ( buffered port:32 * unsafe ) &c_mic_dual_pdm;
//printf("%p\n", *p_ptr);
mic_array_pdm_rx(*p_ptr, c_4x_pdm_mic_0, c_4x_pdm_mic_1);
}

}
}

//This receives PDM on the 1b port and re-packs it so that
//it looks like what would have been rx'ed on a 4b port
//We then punt it over a channel directly to the input to mic_array
void port_shim_1b_4b(in buffered port:32 p_pdm, chanend c_out){
while(1){
unsigned val;
timer t;
unsigned t0, t1;

//GET PORT DATA
asm volatile("in %0, res[%1]" : "=r"(val) : "r"(p_pdm)); //Use ASM so we avoid SETC instruction
// printbinln(val);
t :> t0;

#pragma loop unroll
#pragma unsafe arrays
for(int w = 0; w < 4; w++){
unsigned o = 0;

#pragma loop unroll
#pragma unsafe arrays
for(int i = 0; i < 32; i+=4){
if(val & 0x1){
o |= 0x1 << i;
}
val >>= 1;
}
outuint(c_out, o);
// printbinln(o);

}
t :> t1;
// printf("%d\n", t1-t0);
}
}


int main() {
chan c_sync;
i2c_master_if i_i2c[1];

par {
on tile[1]: i2c_master_single_port(i_i2c, 1, p_i2c, 100, 0, 1, 0);
on tile[1]: {
p_rst_shared <: 0x00;
mabs_init_pll(i_i2c[0], SMART_MIC_BASE);
delay_seconds(5);
c_sync <: 1;
}

on tile[0]:{
c_sync :> int;
stop_clock(pdmclk);
on tile[PDM_TILE]:{

stop_clock(pdmclk);
set_pll();
#if DDR
mic_array_setup_ddr(pdmclk, pdmclk6, p_mclk, p_pdm_clk, p_pdm_mics, 8);
mic_array_setup_ddr(pdmclk, pdmclk6, p_mclk_in, p_pdm_clk, p_pdm_mics, 8);
#else
/* configure_clock_src_divide(pdmclk, p_mclk, 4);
configure_port_clock_output(p_pdm_clk, pdmclk);
Expand All @@ -308,9 +415,15 @@ int main() {

streaming chan c_4x_pdm_mic[DECIMATOR_COUNT];
streaming chan c_ds_output[DECIMATOR_COUNT];
chan c_shim;

par {
#ifdef XCORE_AI
port_shim_1b_4b(p_pdm_mics, c_shim);
call_mic_array_pdm_rx(c_shim, c_4x_pdm_mic[0], c_4x_pdm_mic[1]);
#else
mic_array_pdm_rx(p_pdm_mics, c_4x_pdm_mic[0], c_4x_pdm_mic[1]);
#endif
mic_array_decimate_to_pcm_4ch(c_4x_pdm_mic[0], c_ds_output[0], MIC_ARRAY_NO_INTERNAL_CHANS);
mic_array_decimate_to_pcm_4ch(c_4x_pdm_mic[1], c_ds_output[1], MIC_ARRAY_NO_INTERNAL_CHANS);
test(c_ds_output);
Expand Down