MRAM readfs

Brief

This example demonstrates how to use GAP9 MRAM device with a readfs filesystem.

Requirements

This example has been developped on GAP9EVK v1.3 board.

Description

This test writes the two files from file directory in MRAM memory. It initializes a readfs filesystem to open the two files from the MRAM. Then, it tests if the content of each file is the expected one.

Kconfig options

This application requires the following options :

Option name

Meaning

CONFIG_PLATFORM_BOARD

The application is run on board

CONFIG_BOARD_GAP9MOD_V1_0_B

Select gap9mod v1.0b board

CONFIG_BOARD_GAP9EVK_V1_3

Select gap9evk v1.3 board

CONFIG_DRIVER_TYPE_FLASH

Enable flash drivers category

CONFIG_DRIVER_MRAM

Enable MRAM driver

CONFIG_DRIVER_READFS

Enable readfs driver

How to run

From an up to date and compiled SDK:

  • Configure CMake

cmake -B build
  • Build and run the code

cmake --build build -t run

Code

/*
 * Copyright (C) 2022 GreenWaves Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use fxl6408 file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "pmsis.h"
#include "bsp/bsp.h"

// Format macro (->file name) into a string
#define QUOTE(name) #name
#define STR(macro)  QUOTE(macro)

#define BUFF_SIZE   (64)
static PI_L2 char buff[2][BUFF_SIZE];

/**
 * @brief   Read two files and compare their content to an expected value
 *          file[0] : Content is (index % 128) & 0x7f
 *          file[1] : Content is (index % 128) | 0x80
 * 
 * @param file Files to read
 * @return pi_err_t Error management
 *                  @arg PI_OK Files read successfully
 *                  @arg PI_FAIL Read values are not expected
 */
pi_err_t read_files(pi_fs_file_t* file[2])
{
    pi_err_t err = PI_OK;

    int32_t  size_0      = file[0]->size;
    int32_t  size_1      = file[1]->size;
    int32_t  read_size_0 = 0;
    int32_t  read_size_1 = 0;
    uint32_t index_0     = 0;
    uint32_t index_1     = 0;

    do
    {
        if(size_0 > BUFF_SIZE)
        {
            read_size_0 = BUFF_SIZE;
        }
        else
        {
            read_size_0 = size_0;
        }
        if(size_1 > BUFF_SIZE)
        {
            read_size_1 = BUFF_SIZE;
        }
        else
        {
            read_size_1 = size_1;
        }
        pi_fs_read(file[0], buff[0], read_size_0);
        pi_fs_read(file[1], buff[1], read_size_1);

        size_0 -= read_size_0;
        size_1 -= read_size_1;

        for (int32_t i = 0; i < read_size_0; i++)
        {
            uint8_t expected = (index_0 % 128) & 0x7f;
            if (expected != buff[0][i])
            {
                PI_LOG_ERR("mram_readfs","Error while reading file 0 at index %d. Expected: 0x%x, read: 0x%x\n", i, expected, buff[0][i]);
                err = PI_FAIL;
                break;
            }
            index_0++;
        }

        for (int32_t i = 0; i < read_size_1; i++)
        {
            uint8_t expected = (index_1 % 128) | 0x80;
            if (expected != buff[1][i])
            {
                PI_LOG_ERR("mram_readfs","Error while reading file 1 at index %d. Expected: 0x%x, read: 0x%x\n", i, expected, buff[1][i]);
                err = PI_FAIL;
                break;
            }
            index_1++;
        }

        if(err)
        {
            break;
        }

    } while (size_0 && size_1);

    return err;
}

int main(void)
{
    pi_err_t err = PI_OK;
    pi_device_t  readfs_dev;
    struct pi_readfs_conf readfs_conf;
    pi_fs_file_t* file[2];

    pi_freq_set(PI_FREQ_DOMAIN_FC, 240000000);

    pi_readfs_conf_init(&readfs_conf);
    // see default layout
    readfs_conf.fs.partition_name = "readfs_mram";
    pi_open_from_conf(&readfs_dev, &readfs_conf);

    if(PI_OK != pi_fs_mount(&readfs_dev))
    {
        err = PI_FAIL;
        PI_LOG_ERR("mram_readfs","Failed to mount filesystem\n");
        goto end_of_example;
    }
    file[0] = pi_fs_open(&readfs_dev, STR(FILE0), 0);
    file[1] = pi_fs_open(&readfs_dev, STR(FILE1), 0);

    if((NULL == file[0]) || (NULL == file[1]))
    {
        err = PI_FAIL;
        PI_LOG_ERR("mram_readfs","Failed to open files\n");
        goto end_of_example;
    }

    if(PI_OK != read_files(file))
    {
        err = PI_FAIL;
        PI_LOG_ERR("mram_readfs","Failed to read files\n");
        goto end_of_example;
    }
    pi_fs_close(file[0]);
    pi_fs_close(file[1]);
    pi_fs_unmount(&readfs_dev);


end_of_example:
    if(err)
    {
        printf("Failure");
    }
    else
    {
        printf("Success");
    }
    return err;
}