carmstm32stm32ldiscovery

Reading file from flash for STM32L476G-DISCO


I am new to embedded systems and have been trying to port over a MP3 conversion program to an ARM-based STM32L476G-DISCO development board. I'm also using the free System Workbench software based on Eclipse. I've been successful to the point that I have compiled the program and flashed it onto the board. It even runs right up to the point that the program asks for file input (.wav).

My question is how do I implement the file handling part? Previously when running the original windows console app I would just send in a command line argument like >C:\file.wav < C:\file.mp3.

I was thinking to start simple and just embed the file but I don't know how to call it in my code. I can program the memory manually via the programming software but again, all I know is the address of where I flashed the data. If I step through my program with the debugger it makes it to "wave_open", but since I'm not using a file I'm not quite sure what I put in place of f_open or f_read. Also I am using the HAL library that is generated by STCubeMX in addition to STDIO.h

Here is a snippet of the code I am using:

/* main.c */ #
include "main.h"#
include "stm32l4xx_hal.h"

/* USER CODE BEGIN Includes */
#
include < stdio.h > #include < stdlib.h > #include < string.h > #include < time.h > #include "layer3.h"#
include "wave.h"
    /* USER CODE END Includes */

/* Private variables ---------------------------------------------------
QSPI_HandleTypeDef hqspi;

SPI_HandleTypeDef hspi2;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------

/* Some global vars. */
char * infname, * outfname;
FILE * infile, * outfile;
int quiet = 0;
int _verbose = 0;
int stereo = STEREO;
int force_mono = 0;
/* USER CODE END PV */
/* Parse command line arguments */
static int parse_command(int argc, char ** argv, shine_config_t * config) {
    int i = 0;
    // if (argc - i != 2) return 0;
    // infname = argv[i++];
    // outfname = argv[i];
    //infname = "pcm1644mE.wav";
    infname = 0x08020000;
    outfname = "pcm1644mE.mp3";
    return 1;
}

int main(void) {
    /* USER CODE BEGIN 1 */
    wave_t wave;
    time_t start_time, end_time;
    int16_t buffer[2 * SHINE_MAX_SAMPLES];
    shine_config_t config;
    shine_t s;
    int written;
    unsigned char * data;

    initialise_monitor_handles();

    uint8_t msg[100];

    time( & start_time);

    /* Set the default MPEG encoding paramters - basically init the struct */
    set_defaults( & config);

    if (!quiet) print_name();

    /* Open the input file and fill the config shine_wave_t header */
    if (!wave_open(infname, & wave, & config, quiet))
        error("Could not open WAVE file");

    infile = wave.file;

    if (force_mono)
        config.wave.channels = 1;
    ....

Solution

  • Figured this out, in Wave.c the original source code looks to open a file handle with the typical "fopen" but since there is not file system in place on the platform, I was able to get around this by using "fmemopen" in place of fopen and just pointing fmemopen to my buffer that has data in it. Hope that helps someone else because it took a long time to figure that one out.