C file functions in the embedded world

I have a problem with the embedded system and one library.

I am currently working with an Ambiq device [3] that uses the Cortex M-4, and I want to use the FANN (Fast Artificial Neural Network) library [2]. I can compile and link everything, but the problem is that the FANN library requires that 2 files be read at startup.

Now I have an embedded system, so I do not have a file system and no operating system. I am quite sure that I can somehow write the contents of this file inside the flash memory, but I really do not know how to associate the first address of the file with the functions of the C file, such as "fopen", which requires a file name as input. I only need the connection between the file name and the physical address of the file, and I am finished if it exits (or a C function that takes something different from the file name).

One thing I have already tried is to hardcode the contents of the file into a C-array using xxd -i [file_name] [1], but I donโ€™t know how to link fopen, which FANN library to use. I also started some parsers, but it seems like it takes a lot of time.

Please let me know if you can offer me anything. Thank you in advance. Regards. Jaskirat

Additional Information: - Software used for compilation: Eclipse with makefile. - Compiler: eabi-none-gcc toolbox - I compile the source code of the FANN library directly using the Ambiq microcontroller application, which should run on the Cortex M4.

References: [1] Read the file in a line at compile time [2] LIBRARY FANN LIBRARY http://leenissen.dk/fann/wp/ [3] AMBIQ MICRO: http://ambiqmicro.com/

+4
source share
2 answers

FANN, , , fscanf sscanf ....

struct fann_train_data *fann_read_train_from_fd(FILE * file, const char *filename)
{
    unsigned int num_input, num_output, num_data, i, j;
    unsigned int line = 1;
    struct fann_train_data *data;

    if(fscanf(file, "%u %u %u\n", &num_data, &num_input, &num_output) != 3)
    {
        fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
        return NULL;
    }
    line++;

    data = fann_create_train(num_data, num_input, num_output);
    if(data == NULL)
    {
        return NULL;
    }

    for(i = 0; i != num_data; i++)
    {
        for(j = 0; j != num_input; j++)
        {
            if(fscanf(file, FANNSCANF " ", &data->input[i][j]) != 1)
            {
                fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
                fann_destroy_train(data);
                return NULL;
            }
        }
        line++;

        for(j = 0; j != num_output; j++)
        {
            if(fscanf(file, FANNSCANF " ", &data->output[i][j]) != 1)
            {
                fann_error(NULL, FANN_E_CANT_READ_TD, filename, line);
                fann_destroy_train(data);
                return NULL;
            }
        }
        line++;
    }
    return data;
}
+2

, FANN, , , .

, , , , , . , newlib, stdio . open(), , :

int open(const char *name, int flags, int mode) {
  return -1;
}

, open , . , , :

static const char file1[] = { '\x00`, `\x01`, ... } ;
static const char file2[] = { '\x00`, `\x01`, ... } ;
static struct
{
    const char* file ;
    int size ;
    int index ;
} files[] = { {file1, sizeof(file1), -1}, {file2, sizeof(file2), -1} } ;

open() - :

int open(const char *name, int flags, int mode) 
{ 
    static const char* file_to_handle[] = { "file1", "file2", 0 } ;  // change names to match the FANN library names.

    int handle = -1 ;
    for( int h = 0; file_to_handle[h] != 0 && handle == -1; h++ )
    {
        if( strcmp( file_to_handle[h], name ) == 0 && files[h].index == -1 )
        {
            handle = h ;
            files[h].index = 0 ;
        }
    }
}

read():

int read(int file, char *ptr, int len) 
{
    int i = -1 ;
    if( files[file].index > 0 )
    {
        i = 0 ;
        while( files[file].index < files[file].size && i < len)
        {
            ptr[i] = files[file].index ;
            i++ ;
            files[file].index++ ;
        }
    }    
    return i ;
}

close():

int close(int file) 
{
    files[file].index = -1 ;
    return -1;
}

lseek(), FANN . files[file].index.

" " . , , , . , . , - .

newlib, , , fopen() .. .

, ROM- , , FANN.

0

Source: https://habr.com/ru/post/1620192/


All Articles