diff --git a/tests/test_midi/src/app_midi_simple.xc b/tests/test_midi/src/app_midi_simple.xc index eecae28b..2dbca5e5 100644 --- a/tests/test_midi/src/app_midi_simple.xc +++ b/tests/test_midi/src/app_midi_simple.xc @@ -36,7 +36,7 @@ on tile[MIDI_TILE] : clock clk_midi = CLKBLK_MIDI; #define TEST_COMMAND_FILE_TX "midi_tx_cmds.txt" #define TEST_COMMAND_FILE_RX "midi_rx_cmds.txt" -#define DEBUG 0 +#define DEBUG 0 // Prints for debugging. Turn off for actual test #if DEBUG #define dprintf(...) printf(__VA_ARGS__) @@ -58,6 +58,7 @@ unsigned mini_in_parse_helper(unsigned midi[3]){ unsigned packed = 0; for(int i = 0; i < 3; i++){ + dprintf("Packing byte %d 0x%x\n", i, midi[i]); {valid, packed} = midi_in_parse(m_state, CABLE_NUM, midi[i]); if(valid){ return packed; @@ -122,10 +123,14 @@ void test(chanend c_midi){ timer tmr; - int t_tx; + int t_tx; // Used for delay between Txs + int tx_end; // Used to wait for packet to have fully left tmr :> t_tx; + tmr :> tx_end; const int max_tx_time = XS1_TIMER_HZ / 31250 * 3 * (8 + 1 + 1); // 30 bits at 31.25 kbps is 0.96ms + const int tx_interval = XS1_TIMER_HZ / 8000; // SoF rate on HS + tx_end += max_tx_time; // One whole packet while(tx_cmd_count < num_to_tx || rx_cmd_count < num_to_rx ){ select{ @@ -137,9 +142,10 @@ void test(chanend c_midi){ unsigned midi_data[3] = {0}; unsigned byte_count = 0; {midi_data[0], midi_data[1], midi_data[2], byte_count} = midi_out_parse(byterev(rx_packet)); - // Note this needs to always print for capff to pick it up + // Note this needs to always print for capfd in pytest to pick it up printf("dut_midi_rx: %u %u %u\n", midi_data[0], midi_data[1], midi_data[2]); rx_cmd_count++; + midi_send_ack(c_midi); } break; @@ -148,15 +154,15 @@ void test(chanend c_midi){ unsigned tx_packet = mini_in_parse_helper(midi); outuint(c_midi, byterev(tx_packet)); dprintf("Sent packet to midi: %u %u %u\n", commands[tx_cmd_count][0], commands[tx_cmd_count][1], commands[tx_cmd_count][2]); - t_tx += max_tx_time; + t_tx += tx_interval; + tx_end += max_tx_time; break; } } dprintf("Tx and Rx count met - exiting after last tx complete.\n"); - tmr when timerafter(t_tx) :> int _; // wait until packet definitely departed + tmr when timerafter(tx_end) :> int _; // wait until packet definitely departed - delay_ticks(max_tx_time / 4); // Allow a few more bit times about to allow TXChecker to do it's thing exit(0); } diff --git a/tests/test_midi_loopback.py b/tests/test_midi_loopback.py new file mode 100644 index 00000000..4a21a753 --- /dev/null +++ b/tests/test_midi_loopback.py @@ -0,0 +1,60 @@ +# Copyright 2014-2024 XMOS LIMITED. +# This Software is subject to the terms of the XMOS Public Licence: Version 1. + +import pytest +import Pyxsim +from Pyxsim import testers +from pathlib import Path +from uart_rx_checker import UARTRxChecker +from midi_test_helpers import midi_expect_rx, create_midi_rx_file, create_midi_tx_file, tempdir, MIDI_RATE +from distutils.dir_util import copy_tree # we're using python 3.7 and dirs_exist_ok=True isn't available until 3.8 :( + +MAX_CYCLES = 15000000 + + +##### +# This test takes the built binary, copies it to a tmp dir and runs the midi loopback test which sends some commands +# the firmware receives them, prints and compares with the expected output +##### +def test_midi_loopback(capfd, build_midi): + # Need tempdir as we use the same config files and this causes issues when using xdist + with tempdir() as tmpdirname: + config = "LOOPBACK" + copy_tree(build_midi, tmpdirname) + xe = str(Path(tmpdirname) / f"{config}/test_midi_{config}.xe") + + midi_commands = [ + [0x90, 60, 81], #note on + [0xc0, 15], #instr select + [0xe0, 0, 96], #pitch bend + [0xff], #MIDI reset + [0x80, 60, 81], #note off + ] + create_midi_rx_file(len(midi_commands)) + create_midi_tx_file(midi_commands) + + expected = midi_expect_rx().expect(midi_commands) + tester = testers.ComparisonTester(expected, ordered = True) + + simthreads = [] + + simargs = ["--max-cycles", str(MAX_CYCLES)] + #This is just for local debug so we can capture the traces if needed. It slows xsim down so not needed + # simargs.extend(["--trace-to", "trace.txt", "--vcd-tracing", "-tile tile[1] -ports -o trace.vcd"]) + + Pyxsim.run_with_pyxsim( + xe, + simthreads=simthreads, + timeout=120, + simargs=simargs, + ) + capture = capfd.readouterr().out + result = tester.run(capture.split("\n")) + + # Print to console + # with capfd.disabled(): + # print("++++", capture, "++++") + # print("----", expected, "----") + + + assert result \ No newline at end of file diff --git a/tests/test_midi_rx.py b/tests/test_midi_rx.py index aa9fc05d..37b7ece1 100644 --- a/tests/test_midi_rx.py +++ b/tests/test_midi_rx.py @@ -24,12 +24,11 @@ def test_rx(capfd, config, build_midi): xe = str(Path(tmpdirname) / f"{config}/test_midi_{config}.xe") midi_commands = [[0x90, 60, 81]] - create_midi_rx_file(1) + create_midi_rx_file(len(midi_commands)) create_midi_tx_file() expected = midi_expect_rx().expect(midi_commands) - tester = testers.ComparisonTester(midi_expect_rx().expect(midi_commands), - ordered = True) + tester = testers.ComparisonTester(expected, ordered = True) rx_port = "tile[1]:XS1_PORT_1F" tx_port = "tile[1]:XS1_PORT_4C" # Needed so that UARTRxChecker (a transmitter) knows when to start @@ -46,20 +45,14 @@ def test_rx(capfd, config, build_midi): ] simargs = ["--max-cycles", str(MAX_CYCLES)] - #This is just for local debug so we can capture the traces if needed. It slows xsim down so not needed + #This is just for local debug so we can capture the traces if needed. It slows xsim down so not good for Jenkins # simargs.extend(["--trace-to", "trace.txt", "--vcd-tracing", "-tile tile[1] -ports -o trace.vcd"]) - - # Print to console - # with capfd.disabled(): - # print("++++", expected, "++++") - # print("****", capture, "****") - # print("****", capture.split("\n"), "****") - # print(xe) - + + # with capfd.disabled(): # use to see xsim and tester output Pyxsim.run_with_pyxsim( xe, simthreads=simthreads, - timeout=1200, + timeout=120, simargs=simargs, ) capture = capfd.readouterr().out diff --git a/tests/test_midi_tx.py b/tests/test_midi_tx.py index 605bfba1..bbf41bbb 100644 --- a/tests/test_midi_tx.py +++ b/tests/test_midi_tx.py @@ -28,8 +28,7 @@ def test_tx(capfd, config, build_midi): create_midi_rx_file() expected = midi_expect_tx().expect(midi_commands) - tester = testers.ComparisonTester( expected, - ordered = True) + tester = testers.ComparisonTester(expected, ordered = True) tx_port = "tile[1]:XS1_PORT_4C" baud = MIDI_RATE @@ -47,20 +46,14 @@ def test_tx(capfd, config, build_midi): #This is just for local debug so we can capture the traces if needed. It slows xsim down so not needed # simargs.extend(["--trace-to", "trace.txt", "--vcd-tracing", "-tile tile[1] -ports -o trace.vcd"]) + # with capfd.disabled(): # use to see xsim and tester output Pyxsim.run_with_pyxsim( xe, simthreads=simthreads, - timeout=1200, + timeout=120, simargs=simargs, ) capture = capfd.readouterr().out result = tester.run(capture.split("\n")) - # Print to console - # with capfd.disabled(): - # print("++++", expected, "++++") - # print("****", capture, "****") - # print("****", capture.split("\n"), "****") - # print(xe) - assert result \ No newline at end of file