Skip to content

Commit

Permalink
Move model to filesystem
Browse files Browse the repository at this point in the history
Also adds supporting scripts
  • Loading branch information
xmos-jmccarthy committed Jul 19, 2021
1 parent 05f8aea commit 489ffe2
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 20 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ set(APP_SOURCES

"src/gpio_test/gpio_test.c"

"$ENV{WW_PATH}/models/common/WR_250k.en-US.alexa.cpp"
# "$ENV{WW_PATH}/models/common/WR_250k.en-US.alexa.cpp"
)

set_source_files_properties(src/usb/adaptive_rate_adjust.c PROPERTIES COMPILE_FLAGS "-O3")
Expand All @@ -121,7 +121,7 @@ add_compile_definitions(
CFG_TUSB_DEBUG_PRINTF=rtos_printf
CFG_TUSB_DEBUG=0

PRL_MODEL_ALIGN=\_\_attribute\_\_\(\(aligned\(8\)\)\)\_\_attribute\_\_\(\(section\(\".ExtMem_data\"\)\)\)
# PRL_MODEL_ALIGN=\_\_attribute\_\_\(\(aligned\(8\)\)\)\_\_attribute\_\_\(\(section\(\".ExtMem_data\"\)\)\)
)

add_executable(${TARGET_NAME})
Expand Down
39 changes: 39 additions & 0 deletions filesystem_support/create_fs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Get unix name for determining OS
UNAME=$(uname)

# Create an empty 1 MiB file
dd if=/dev/zero of=fat.fs bs=1024 count=1024

if [ "$UNAME" == "Linux" ] ; then
MKFS_VFAT_PATH=/sbin
sudo umount -q fat_mnt
elif [ "$UNAME" == "Darwin" ] ; then
MKFS_VFAT_PATH=/usr/local/sbin
hdiutil detach fat_mnt
fi

# Create an empty FAT filesystem in it
$MKFS_VFAT_PATH/mkfs.vfat -v -F12 -s1 -S4096 -n xcore_fs fat.fs

mkdir -p fat_mnt

# Mount the filesystem
if [ "$UNAME" == "Linux" ] ; then
sudo mount -o loop fat.fs fat_mnt
elif [ "$UNAME" == "Darwin" ] ; then
hdiutil attach -imagekey diskimage-class=CRawDiskImage -mountpoint fat_mnt fat.fs
fi

# Copy files into filesystem
sudo mkdir fat_mnt/ww
sudo cp "$WW_PATH/models/common/WR_250k.en-US.alexa.bin" fat_mnt/ww/model.bin

# Unmount the filesystem
if [ "$UNAME" == "Linux" ] ; then
sudo umount fat_mnt
elif [ "$UNAME" == "Darwin" ] ; then
hdiutil detach fat_mnt
fi

# Cleanup
sudo rm -rf fat_mnt
2 changes: 2 additions & 0 deletions filesystem_support/flash_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
./create_fs.sh
xflash --quad-spi-clock 50MHz --factory ../bin/sw_xvf3652.xe --boot-partition-size 0x100000 --data ./fat.fs
2 changes: 1 addition & 1 deletion src/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ your application. */
#define configSUPPORT_STATIC_ALLOCATION 0
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#if ON_TILE(0)
#define configTOTAL_HEAP_SIZE 120*1024
#define configTOTAL_HEAP_SIZE 256*1024
#endif
#if ON_TILE(1)
#define configTOTAL_HEAP_SIZE 128*1024
Expand Down
7 changes: 0 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@

#include "gpio_test/gpio_test.h"

#define WW_TILE_NO 0
#define FS_TILE_NO 0

#if (WW_TILE_NO != FS_TILE_NO)
#error WW and FS must be on the same tile
#endif

volatile int mic_from_usb = 0;
volatile int aec_ref_source = appconfAEC_REF_DEFAULT;

Expand Down
6 changes: 6 additions & 0 deletions src/platform/driver_instances.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#define I2C_CTRL_TILE_NO 1
#define SPI_OUTPUT_TILE_NO 0
#define AUDIO_HW_TILE_NO 1
#define WW_TILE_NO 0
#define FS_TILE_NO 0

#if (WW_TILE_NO != FS_TILE_NO)
#error WW and FS must be on the same tile
#endif

extern rtos_intertile_t *intertile_ctx;
extern rtos_gpio_t *gpio_ctx_t0;
Expand Down
71 changes: 61 additions & 10 deletions src/ww_model_runner/ww_model_runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
#if appconfWW_ENABLED

#include "pryon_lite.h"
#include "fs_support.h"

// sizes below are based on verion v2.10.6
// #define MODEL_RUNNER_STACK_SIZE (3 * 1024)
// #define MODEL_RUNNER_CODE_SIZE (43 * 1024)
// #define MODEL_RUNNER_DECODE_BUFFER_SIZE (30 * 1024)
// #define MODEL_RUNNER_MODEL_DATA_SIZE (234 * 1024)
// #define WW_MODEL_RUNTICKS 1250000
#define MODEL_RUNNER_STACK_SIZE 2500//(650+1450) // in WORDS
#define DECODER_BUF_SIZE (35000) // (29936) for v2.10.6
#define WW_MAX_SIZE_BYTES (300000) // 250k model size + 20% headroom

#define MODEL_RUNNER_STACK_SIZE (650) // in WORDS
#define DECODER_BUF_SIZE (30000) // (29936) for v2.10.6
/* Due to the xcore qspi flash driver being written in software
* we cannot read directly to DDR, as the nondeterministic
* transaction time will cause the qspi driver to fail to meet timing */
#define WW_FLASH_TO_EXTMEM_CHUNK_SIZE_BYTES (4096)

extern char prlBinaryModelData[];
extern size_t prlBinaryModelLen;
#define WW_MODEL_FILEPATH "/flash/ww/model.bin"

__attribute__((section(".ExtMem_data"))) __attribute__((aligned(8)))
char prlBinaryModelData[WW_MAX_SIZE_BYTES] = {0};

static PryonLiteDecoderHandle sDecoder = NULL;

Expand All @@ -51,10 +53,59 @@ static void vadCallback(PryonLiteDecoderHandle handle,
rtos_printf("VAD state: %s\n", vadEvent->vadState ? "active" : "inactive");
}

size_t ww_load_model_from_fs_to_extmem(void)
{
size_t retval = 0;
uint8_t transfer_buf[WW_FLASH_TO_EXTMEM_CHUNK_SIZE_BYTES];
FIL ww_model_file;
uint32_t ww_model_file_size = -1;
uint32_t bytes_read = 0;
FRESULT result;

result = f_open(&ww_model_file, WW_MODEL_FILEPATH, FA_READ);
if (result == FR_OK)
{
rtos_printf("Found model %s\n", WW_MODEL_FILEPATH);
ww_model_file_size = f_size(&ww_model_file);

if (ww_model_file_size > 0)
{
uint8_t *extmem_ptr = (uint8_t*)prlBinaryModelData;
uint32_t more = WW_FLASH_TO_EXTMEM_CHUNK_SIZE_BYTES;
uint32_t total_read = 0;
do {
rtos_printf("Read more %u 0x%x\n", more, transfer_buf[0]);
result = f_read(&ww_model_file,
transfer_buf,
more,
(unsigned int*)&bytes_read);
if ((result == FR_OK) && (bytes_read == more))
{
memcpy(extmem_ptr, transfer_buf, bytes_read);
total_read += bytes_read;
extmem_ptr += bytes_read;
more = (ww_model_file_size - total_read) > WW_FLASH_TO_EXTMEM_CHUNK_SIZE_BYTES
? WW_FLASH_TO_EXTMEM_CHUNK_SIZE_BYTES
: (ww_model_file_size - total_read);
}
} while (more > 0);
}
}
if (ww_model_file_size != -1)
{
f_close(&ww_model_file);
retval = ww_model_file_size;
}

return retval;
}

static void model_runner_manager(void *args)
{
StreamBufferHandle_t input_queue = (StreamBufferHandle_t)args;
size_t prlBinaryModelLen = 0;

prlBinaryModelLen = ww_load_model_from_fs_to_extmem();

rtos_printf("model size is %d bytes\n", prlBinaryModelLen);
/* load model */
Expand Down

0 comments on commit 489ffe2

Please sign in to comment.