Skip to content

Commit

Permalink
Merge pull request #549 from xmos/fix/fileio
Browse files Browse the repository at this point in the history
address potential xscope_fileio timing issues
  • Loading branch information
keithm-xmos authored Nov 8, 2022
2 parents de977f9 + a58fd9f commit c6679cf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/freertos/xscope_fileio/src/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ your application. */
#define configUSE_TICKLESS_IDLE 0
#define configCPU_CLOCK_HZ 100000000

#define configNUM_CORES 5
#define configNUM_CORES 7
#define configTICK_RATE_HZ 1000
#define configMAX_PRIORITIES 32
#define configRUN_MULTIPLE_PRIORITIES 1
Expand Down
6 changes: 3 additions & 3 deletions examples/freertos/xscope_fileio/src/app_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "platform/driver_instances.h"

/* App configuration */
#define appconfINPUT_FILENAME "in.wav"
#define appconfOUTPUT_FILENAME "out.wav"
#define appconfINPUT_FILENAME "in.wav\0"
#define appconfOUTPUT_FILENAME "out.wav\0"
#define appconfMAX_CHANNELS 1
#define appconfFRAME_ADVANCE 240
#define appconfFRAME_ELEMENT_SIZE sizeof(int32_t)
Expand All @@ -22,7 +22,7 @@

/* Task Priorities */
#define appconfSTARTUP_TASK_PRIORITY (configMAX_PRIORITIES - 2)
#define appconfXSCOPE_IO_TASK_PRIORITY (configMAX_PRIORITIES - 3)
#define appconfXSCOPE_IO_TASK_PRIORITY (configMAX_PRIORITIES - 1)
#define appconfDATA_PIPELINE_TASK_PRIORITY (configMAX_PRIORITIES - 1)

#endif /* APP_CONF_H_ */
64 changes: 38 additions & 26 deletions examples/freertos/xscope_fileio/src/fileio/xscope_fileio_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "wav_utils.h"

static TaskHandle_t fileio_task_handle;
static QueueHandle_t fileio_queue;

static xscope_file_t infile;
static xscope_file_t outfile;

Expand All @@ -45,9 +47,7 @@ void init_xscope_host_data_user_cb(chanend_t c_host) {

size_t xscope_fileio_tx_to_host(uint8_t *buf, size_t len_bytes) {
size_t ret = 0;
// rtos_printf("Write outfile\n");

xscope_fwrite(&outfile, buf, len_bytes);
xQueueSend(fileio_queue, buf, portMAX_DELAY);

return ret;
}
Expand Down Expand Up @@ -88,26 +88,31 @@ void xscope_fileio(void *arg) {
unsigned input_header_size;
unsigned frame_count;
unsigned block_count;
uint8_t in_buf[appconfDATA_FRAME_SIZE_BYTES];
uint8_t out_buf[appconfDATA_FRAME_SIZE_BYTES];
size_t bytes_read = 0;

/* Wait until xscope_fileio is initialized */
while(xscope_fileio_is_initialized() == 0) {
vTaskDelay(pdMS_TO_TICKS(1));
}

fileio_queue = xQueueCreate(1, appconfDATA_FRAME_SIZE_BYTES);

rtos_printf("Open test files\n");
infile = xscope_open_file(appconfINPUT_FILENAME, "rb");
outfile = xscope_open_file(appconfOUTPUT_FILENAME, "wb");
uint32_t mask = rtos_interrupt_mask_all(); // Disable preemption around xscope_freads
state = rtos_osal_critical_enter();
{
infile = xscope_open_file(appconfINPUT_FILENAME, "rb");
outfile = xscope_open_file(appconfOUTPUT_FILENAME, "wb");
// Validate input wav file
if(get_wav_header_details(&infile, &input_header_struct, &input_header_size) != 0){
rtos_printf("Error: error in get_wav_header_details()\n");
_Exit(1);
}
xscope_fseek(&infile, input_header_size, SEEK_SET);
}
rtos_interrupt_mask_set(mask); // Re-enable preemption
rtos_osal_critical_exit(state);

xscope_fseek(&infile, input_header_size, SEEK_SET);
// Ensure 32bit wav file
if(input_header_struct.bit_depth != 32)
{
Expand All @@ -126,33 +131,39 @@ void xscope_fileio(void *arg) {

// Create output wav file
wav_form_header(&output_header_struct,
input_header_struct.audio_format,
appconfMAX_CHANNELS,
input_header_struct.sample_rate,
input_header_struct.bit_depth,
block_count*appconfFRAME_ADVANCE);
input_header_struct.audio_format,
appconfMAX_CHANNELS,
input_header_struct.sample_rate,
input_header_struct.bit_depth,
block_count*appconfFRAME_ADVANCE);

xscope_fwrite(&outfile, (uint8_t*)(&output_header_struct), WAV_HEADER_BYTES);

uint8_t buf[appconfDATA_FRAME_SIZE_BYTES];
size_t bytes_read = 0;
// ensure the write above has time to complete before performing any reads
vTaskDelay(pdMS_TO_TICKS(1000));

// Iterate over frame blocks and send the data to the first pipeline stage on tile[1]
for(unsigned b=0; b<block_count; b++) {
memset(buf, 0x00, sizeof(buf));
memset(in_buf, 0x00, appconfDATA_FRAME_SIZE_BYTES);
long input_location = wav_get_frame_start(&input_header_struct, b * appconfFRAME_ADVANCE, input_header_size);

xscope_fseek(&infile, input_location, SEEK_SET);
uint32_t mask = rtos_interrupt_mask_all(); // Disable preemption around xscope_freads
state = rtos_osal_critical_enter();
{
bytes_read = xscope_fread(&infile, buf, sizeof(buf));
xscope_fseek(&infile, input_location, SEEK_SET);
bytes_read = xscope_fread(&infile, in_buf, appconfDATA_FRAME_SIZE_BYTES);
}
rtos_interrupt_mask_set(mask); // Re-enable preemption
rtos_osal_critical_exit(state);

memset(in_buf + bytes_read, 0x00, appconfDATA_FRAME_SIZE_BYTES - bytes_read);

memset(buf + bytes_read, 0x00, sizeof(buf) - bytes_read);
rtos_intertile_tx(intertile_ctx,
appconfEXAMPLE_DATA_PORT,
buf,
sizeof(buf));
appconfEXAMPLE_DATA_PORT,
in_buf,
appconfDATA_FRAME_SIZE_BYTES);

// read from queue here and write to file
xQueueReceive(fileio_queue, out_buf, portMAX_DELAY);
xscope_fwrite(&outfile, out_buf, appconfDATA_FRAME_SIZE_BYTES);
}

#if (appconfAPP_NOTIFY_FILEIO_DONE == 1)
Expand Down Expand Up @@ -182,8 +193,9 @@ void xscope_fileio_tasks_create(unsigned priority, void* app_data) {
priority,
&fileio_task_handle);

// Define the core affinity mask such that this task can not run on core 0
UBaseType_t uxCoreAffinityMask = 0xFFFFFFFE;
// Define the core affinity mask such that this task can only run on a specific core
UBaseType_t uxCoreAffinityMask = 0x10;


/* Set the core affinity mask for the task. */
vTaskCoreAffinitySet( fileio_task_handle, uxCoreAffinityMask );
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos/xscope_fileio/src/wav/wav_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int wav_form_header(wav_header *header,
}

unsigned wav_get_num_bytes_per_frame(const wav_header *s){
int bytes_per_sample = s->bit_depth/CHAR_BIT;
int bytes_per_sample = s->bit_depth/8;
return (unsigned)(bytes_per_sample * s->num_channels);
}

Expand Down
1 change: 0 additions & 1 deletion examples/freertos/xscope_fileio/xscope_fileio.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ set(APP_COMPILE_DEFINITIONS
)

set(APP_LINK_OPTIONS
-lquadspi
-report
${CMAKE_CURRENT_LIST_DIR}/src/config.xscope
${CMAKE_CURRENT_LIST_DIR}/XCORE-AI-EXPLORER.xn
Expand Down

0 comments on commit c6679cf

Please sign in to comment.