Enhanced app_midi_simple to support file based commands and readiness for Rx testing

This commit is contained in:
Ed
2024-04-16 13:37:29 +01:00
parent 098c39b659
commit a6969a8610
4 changed files with 111 additions and 60 deletions

View File

@@ -32,8 +32,15 @@ on tile[MIDI_TILE] : buffered in port:1 p_midi_rx = XS1_PORT_1F;
#define CLKBLK_MIDI XS1_CLKBLK_2
on tile[MIDI_TILE] : clock clk_midi = CLKBLK_MIDI;
#define MAX_TEST_COMMANDS 10
#define TEST_COMMAND_FILE "midi_tx_cmds.txt"
#define MAX_TEST_COMMANDS 100
#define TEST_COMMAND_FILE_TX "midi_tx_cmds.txt"
#define TEST_COMMAND_FILE_RX "midi_rx_cmds.txt"
#ifndef DEBUG
#define dprintf(...)
#else
#define dprintf(...) printf(__VA_ARGS__)
#endif
/* See hwsupport.xc */
void ctrlPort();
@@ -42,8 +49,6 @@ void ctrlPort();
unsigned mini_in_parse_helper(unsigned midi[3]){
// printf("Composing data: 0x%x 0x%x 0x%x\n", midi[0], midi[1], midi[2]);
struct midi_in_parse_state m_state;
reset_midi_state(m_state);
@@ -59,69 +64,97 @@ unsigned mini_in_parse_helper(unsigned midi[3]){
return 0;
}
unsigned parse_cmd_line(uint8_t commands[MAX_TEST_COMMANDS][3])
{unsigned, unsigned} read_config_file(uint8_t commands[MAX_TEST_COMMANDS][3])
{
FILE * movable fptr_tx = fopen(TEST_COMMAND_FILE,"rt");
unsigned tx_line_count = 0;
FILE * movable fptr_tx = fopen(TEST_COMMAND_FILE_TX,"rt");
if (fptr_tx == NULL) {
printf("ERROR: TX command file %s not found or unable to open.\n", TEST_COMMAND_FILE);
fclose(move(fptr_tx));
return 0;
}
unsigned line = 0;
unsigned a,b,c;
while (fscanf(fptr_tx, "%u %u %u\n", &a, &b, &c) == 3) {
commands[line][0] = a;
commands[line][1] = b;
commands[line][2] = c;
// printf("Line %u params: 0x%x 0x%x 0x%x\n", line, commands[line][0], commands[line][1], commands[line][2]);
line++;
if(line > MAX_TEST_COMMANDS){
printf("ERROR: Too many lines in TX command file\n");
fclose(move(fptr_tx));
return MAX_TEST_COMMANDS;
dprintf("WARNING: TX command file %s not found or unable to open.\n", TEST_COMMAND_FILE_TX);
} else {
unsigned a,b,c;
while (fscanf(fptr_tx, "%u %u %u\n", &a, &b, &c) == 3) {
commands[tx_line_count][0] = a;
commands[tx_line_count][1] = b;
commands[tx_line_count][2] = c;
//printf("Line %u params: 0x%x 0x%x 0x%x\n", tx_line_count, commands[tx_line_count][0], commands[tx_line_count][1], commands[tx_line_count][2]);
tx_line_count++;
if(tx_line_count > MAX_TEST_COMMANDS){
printf("ERROR: Too many lines in TX command file\n");
tx_line_count = MAX_TEST_COMMANDS;
}
}
}
fclose(move(fptr_tx));
return line;
unsigned rx_cmd_count = 0;
FILE * movable fptr_rx = fopen(TEST_COMMAND_FILE_RX,"rt");
if (fptr_rx == NULL) {
dprintf("WARNING: RX command file %s not found or unable to open.\n", TEST_COMMAND_FILE_RX);
} else {
if(fscanf(fptr_rx, "%u\n", &rx_cmd_count) != 1){
printf("ERROR: Not enough or too many items in RX command file line\n");
}
}
fclose(move(fptr_rx));
return {tx_line_count, rx_cmd_count};
}
void test(chanend c_midi){
uint8_t commands[MAX_TEST_COMMANDS][3] = {{0}};
unsigned num_tx = parse_cmd_line(commands);
unsigned num_to_tx = 0;
unsigned num_to_rx = 0;
{num_to_tx, num_to_rx} = read_config_file(commands);
dprintf("Sending %u MIDI command line(s) and receiving %u MIDI command(s)\n", num_to_tx, num_to_rx);
// For MIDI Rx
int is_ack;
unsigned datum;
unsigned rx_packet;
unsigned line = 0;
while(1){
// Counters for Rx and Tx
unsigned tx_cmd_count = 0;
unsigned rx_cmd_count = 0;
timer tmr;
int t_tx;
tmr :> t_tx;
const int max_tx_time = XS1_TIMER_HZ / 31250 * 3 * (8 + 1 + 1); // 30 bits at 31.25 kbps is 0.96ms
while(tx_cmd_count < num_to_tx || rx_cmd_count < num_to_rx ){
select{
case midi_get_ack_or_data(c_midi, is_ack, datum):
// printf("ACK: %d Datum: 0x%x\n", is_ack, datum);
line++;
if(line == num_tx){
delay_microseconds(200); // Allow frame to complete
exit(0);
case midi_get_ack_or_data(c_midi, is_ack, rx_packet):
if(is_ack){
dprintf("ACK from Tx\n");
tx_cmd_count++;
} else {
unsigned midi_data[3] = {0};
unsigned byte_count = 0;
{midi_data[0], midi_data[1], midi_data[2], byte_count} = midi_out_parse(rx_packet);
dprintf("dut_midi_rx: %u %u %u\n", midi_data[0], midi_data[1], midi_data[2]);
rx_cmd_count++;
}
break;
default:
if(num_tx){
unsigned midi[] = {commands[line][0], commands[line][1], commands[line][2]};
unsigned tx_packet = mini_in_parse_helper(midi);
outuint(c_midi, byterev(tx_packet));
// printf("SEND TO MIDI: 0x%x\n", tx_packet);
delay_milliseconds(2); // 30 bits at 31.25 kbps is 0.96ms
} else {
exit(0);
}
case tx_cmd_count < num_to_tx => tmr when timerafter(t_tx) :> int _:
unsigned midi[] = {commands[tx_cmd_count][0], commands[tx_cmd_count][1], commands[tx_cmd_count][2]};
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;
break;
}
}
dprintf("Tx and Rx count met - exiting after last tx complete.\n");
tmr when timerafter(t_tx) :> 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);
}