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;
}
# Copyright (c) 2022 GreenWaves Technologies SAS
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of GreenWaves Technologies SAS nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
cmake_minimum_required(VERSION 3.19)
###############################################################################
# Panel Control
###############################################################################
set(TARGET_NAME "mram_readfs")
set(TARGET_SRCS mram_readfs.c)
set(FILE0_NAME flash_file_0.bin)
set(FILE1_NAME flash_file_1.bin)
set(FILE0 ${CMAKE_CURRENT_SOURCE_DIR}/files/${FILE0_NAME})
set(FILE1 ${CMAKE_CURRENT_SOURCE_DIR}/files/${FILE1_NAME})
###############################################################################
# CMake pre initialization
###############################################################################
include($ENV{GAP_SDK_HOME}/utils/cmake/setup.cmake)
list(APPEND TARGET_PREPROCESSOR -DFILE0=${FILE0_NAME} -DFILE1=${FILE1_NAME})
project(${TARGET_NAME} C ASM)
add_executable(${TARGET_NAME} ${TARGET_SRCS})
target_compile_options(${TARGET_NAME} PUBLIC ${TARGET_PREPROCESSOR})
readfs_add_files(FILES ${FILE0} ${FILE1} FLASH mram)
###############################################################################
# CMake post initialization
###############################################################################
setupos(${TARGET_NAME})