Hello World for SCUX. The SCUX driver can perform asynchronous and synchronous sampling rate conversions using the sampling rate converter.

Dependencies:   R_BSP USBHost mbed

main.cpp

Committer:
1050186
Date:
20 months ago
Revision:
2:1e8469568194
Parent:
1:4596cb683cc3

File content as of revision 2:1e8469568194:

#include "mbed.h"
#include "R_BSP_Scux.h"
#include "USBHostMSD.h"

R_BSP_Scux scux(SCUX_CH_0);

#define WRITE_SAMPLE_NUM (128)
#define READ_SAMPLE_NUM  (2048)

const short sin_data[WRITE_SAMPLE_NUM] = {
 0x0000,0x0000,0x0C8C,0x0C8C,0x18F9,0x18F9,0x2528,0x2528
,0x30FB,0x30FB,0x3C56,0x3C56,0x471C,0x471C,0x5133,0x5133
,0x5A82,0x5A82,0x62F1,0x62F1,0x6A6D,0x6A6D,0x70E2,0x70E2
,0x7641,0x7641,0x7A7C,0x7A7C,0x7D89,0x7D89,0x7F61,0x7F61
,0x7FFF,0x7FFF,0x7F61,0x7F61,0x7D89,0x7D89,0x7A7C,0x7A7C
,0x7641,0x7641,0x70E2,0x70E2,0x6A6D,0x6A6D,0x62F1,0x62F1
,0x5A82,0x5A82,0x5133,0x5133,0x471C,0x471C,0x3C56,0x3C56
,0x30FB,0x30FB,0x2528,0x2528,0x18F9,0x18F9,0x0C8C,0x0C8C
,0x0000,0x0000,0xF374,0xF374,0xE707,0xE707,0xDAD8,0xDAD8
,0xCF05,0xCF05,0xC3AA,0xC3AA,0xB8E4,0xB8E4,0xAECD,0xAECD
,0xA57E,0xA57E,0x9D0F,0x9D0F,0x9593,0x9593,0x8F1E,0x8F1E
,0x89BF,0x89BF,0x8584,0x8584,0x8277,0x8277,0x809F,0x809F
,0x8001,0x8001,0x809F,0x809F,0x8277,0x8277,0x8584,0x8584
,0x89BF,0x89BF,0x8F1E,0x8F1E,0x9593,0x9593,0x9D0F,0x9D0F
,0xA57E,0xA57E,0xAECD,0xAECD,0xB8E4,0xB8E4,0xC3AA,0xC3AA
,0xCF05,0xCF05,0xDAD8,0xDAD8,0xE707,0xE707,0xF374,0xF374
};

#if defined(__ICCARM__)
#pragma data_alignment=4
short write_buff[WRITE_SAMPLE_NUM]@ ".mirrorram";
#pragma data_alignment=4
short read_buff[READ_SAMPLE_NUM]@ ".mirrorram";
#else
short write_buff[WRITE_SAMPLE_NUM] __attribute((section("NC_BSS"),aligned(4)));
short read_buff[READ_SAMPLE_NUM] __attribute((section("NC_BSS"),aligned(4)));
#endif

void scux_setup(void);
void write_task(void const*);
void file_output_to_usb(void);

int main(void) {
    // set up SRC parameters.
    scux_setup();

    printf("Sampling rate conversion Start.\n");
    // start accepting transmit/receive requests.
    scux.TransStart();

    // create a new thread to write to SCUX.
    Thread writeTask(write_task, NULL, osPriorityNormal, 1024 * 4);

    // receive request to the SCUX driver.
    scux.read(read_buff, sizeof(read_buff));
    printf("Sampling rate conversion End.\n");

    // output binary file to USB port 0.
    file_output_to_usb();
}

void scux_setup(void) {
    scux_src_usr_cfg_t src_cfg;

    src_cfg.src_enable           = true;
    src_cfg.word_len             = SCUX_DATA_LEN_16;
    src_cfg.mode_sync            = true;
    src_cfg.input_rate           = SAMPLING_RATE_48000HZ;
    src_cfg.output_rate          = SAMPLING_RATE_96000HZ;
    src_cfg.select_in_data_ch[0] = SELECT_IN_DATA_CH_0;
    src_cfg.select_in_data_ch[1] = SELECT_IN_DATA_CH_1;

    scux.SetSrcCfg(&src_cfg);
}

void scux_flush_callback(int scux_ch) {
    // do nothing
}

void write_task(void const*) {
    memcpy(write_buff, sin_data, sizeof(write_buff));
    // send request to the SCUX driver.
    scux.write(write_buff, sizeof(write_buff));

    // stop the acceptance of transmit/receive requests.
    scux.FlushStop(&scux_flush_callback);
}

void file_output_to_usb(void) {
    FILE * fp = NULL;
    int i;

    USBHostMSD msd("usb");

    // try to connect a MSD device
    for(i = 0; i < 10; i++) {
        if (msd.connect()) {
            break;
        }
        wait(0.5);
    }

    if (msd.connected()) {
        fp = fopen("/usb/scux_input.dat", "rb");
        if (fp == NULL) {
            fp = fopen("/usb/scux_input.dat", "wb");
            if (fp != NULL) {
                fwrite(write_buff, sizeof(short), WRITE_SAMPLE_NUM, fp);
                fclose(fp);
                printf("Output binary file(Input data) to USB.\n");
            } else {
                printf("Failed to output binary file(Input data).\n");
            }
        } else {
            printf("Binary file(Input data) exists.\n");
            fclose(fp);
        }

        fp = fopen("/usb/scux_output.dat", "rb");
        if (fp == NULL) {
            fp = fopen("/usb/scux_output.dat", "wb");
            if (fp != NULL) {
                fwrite(read_buff, sizeof(short), READ_SAMPLE_NUM, fp);
                fclose(fp);
                printf("Output binary file(Output data) to USB.\n");
            } else {
                printf("Failed to output binary file(Output data).\n");
            }
        } else {
            printf("Binary file(Output data) exists.\n");
            fclose(fp);
        }
    } else {
        printf("Failed to connect to the USB device.\n");
    }
}