examples/media: Add a simple test of access to media by block and MTD drivers

This commit is contained in:
Gregory Nutt 2015-11-09 11:45:44 -06:00
parent 57050b0f82
commit 9ee96d1430
8 changed files with 358 additions and 0 deletions

View File

@ -1443,3 +1443,6 @@
tmpfs (2015-10-10).
* apps/examples/zerocross: Add a Zero Cross application example.
From Alan Carvalho de Assis (2015-10-13).
* apps/examples/media: Add a simple test for access of media via
a block driver or MTD driver (2015-11-09).

View File

@ -30,6 +30,7 @@ source "$APPSDIR/examples/igmp/Kconfig"
source "$APPSDIR/examples/i2schar/Kconfig"
source "$APPSDIR/examples/lcdrw/Kconfig"
source "$APPSDIR/examples/ltdc/Kconfig"
source "$APPSDIR/examples/media/Kconfig"
source "$APPSDIR/examples/mm/Kconfig"
source "$APPSDIR/examples/modbus/Kconfig"
source "$APPSDIR/examples/mount/Kconfig"

View File

@ -709,6 +709,30 @@ examples/ltdc
* CONFIG_EXAMPLES_LTDC
examples/media
^^^^^^^^^^^^^^
The media test simply writes values onto the media hidden behind a
character driver and verifies that the media can be successfully written
and read. This low level test is useful in the early phases of the
bringup of a new block or mtd driver because it avoids the complexity of
a file system.
This test uses a character driver and cannot directly access block or mtd
drivers. This test is suitable for use EEPROM character drivers (see
nuttx/drivers/eeprom), or with block drivers wrapped as character drivers
(see nuttx/drivers/bch)
int ret = bchdev_register(<path-to-block-dirver>,
<path-to-character-driver>, false);
MTD drivers need an additional wrapper layer, the FTL wrapper must first
be used to convert the MTD driver to a block device:
int ret = ftl_initialize(<N>, mtd);
ret = bchdev_register(/dev/mtdblock<N>, <path-to-character-driver>,
false);
examples/mm
^^^^^^^^^^^

11
examples/media/.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/Make.dep
/.depend
/.built
/*.asm
/*.obj
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src

70
examples/media/Kconfig Normal file
View File

@ -0,0 +1,70 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config EXAMPLES_MEDIA
bool "Media test"
default n
---help---
Enable the media test. This test simply writes values onto the
media hidden behind a character driver and verifies that the media
can be successfully written and read. This low level test is
useful in the early phases of the bringup of a new block or mtd
driver because it avoids the complexity of a file system.
This test uses a character driver and cannot directly access block
or mtd drivers. This test is suitable for use EEPROM character
drivers (see nuttx/drivers/eeprom), or with block drivers wrapped
as character drivers (see nuttx/drivers/bch)
int ret = bchdev_register(<path-to-block-dirver>,
<path-to-character-driver>, false);
MTD drivers need an additional wrapper layer, the FTL wrapper must
first be used to convert the MTD driver to a block device:
int ret = ftl_initialize(<N>, mtd);
ret = bchdev_register(/dev/mtdblock<N>, <path-to-character-driver>,
false);
if EXAMPLES_MEDIA
config EXAMPLES_MEDIA_DEVPATH
string "Character driver path"
default "/dev/mtd0"
---help---
This is the full path to the the character driver that is used to
access the media. This test is suitable for use EEPROM character
drivers (see nuttx/drivers/eeprom), or with block drivers wrapped as
character drivers (see nuttx/drivers/bch)
int ret = bchdev_register(<path-to-block-dirver>,
<path-to-character-driver>, false);
MTD drivers need an additional wrapper layer, the FTL wrapper must
first be used to convert the MTD driver to a block device:
int ret = ftl_initialize(<N>, mtd);
ret = bchdev_register(/dev/mtdblock<N>, <path-to-character-driver>,
false);
config EXAMPLES_MEDIA_BLOCKSIZE
int "Block size"
default 512
---help---
This test will attempt to determine the block size of the underlying
block or MTD driver using ioctl calls. This default value will only
be used in the event that the test logic is unable to determine the
underlying block size of the media. This value should match the
block/MTD device's (erase) block size.
config EXAMPLES_MEDIA_PROGNAME
string "Program name"
default "media"
depends on BUILD_KERNEL
---help---
This is the name of the program that will be use when the NSH ELF
program is installed.
endif

39
examples/media/Make.defs Normal file
View File

@ -0,0 +1,39 @@
############################################################################
# apps/examples/media/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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 NuttX 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.
#
############################################################################
ifeq ($(CONFIG_EXAMPLES_MEDIA),y)
CONFIGURED_APPS += examples/media
endif

53
examples/media/Makefile Normal file
View File

@ -0,0 +1,53 @@
############################################################################
# apps/examples/media/Makefile
#
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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 NuttX 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.
#
############################################################################
-include $(TOPDIR)/Make.defs
# Media test application info
APPNAME = media
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Media test application files
ASRCS =
CSRCS =
MAINSRC = media_main.c
CONFIG_EXAMPLES_MEDIA_PROGNAME ?= media$(EXEEXT)
PROGNAME = $(CONFIG_EXAMPLES_MEDIA_PROGNAME)
include $(APPDIR)/Application.mk

157
examples/media/media_main.c Normal file
View File

@ -0,0 +1,157 @@
/****************************************************************************
* examples/media/hello_main.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 NuttX 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include <nuttx/mtd/mtd.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private types
****************************************************************************/
struct media_info_s
{
off_t blocksize;
off_t nblocks;
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* get_blocksize
****************************************************************************/
static void get_blocksize(int fd, FAR struct media_info_s *info)
{
struct mtd_geometry_s mtdgeo;
int ret;
/* If the underlying driver is an MTD driver, then we should able to get
* the erase block size from an MTD ioctl call.
*/
ret = ioctl(fd, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&mtdgeo));
if (ret >= 0)
{
printf("MTD Geometry:\n");
printf(" blocksize: %u\n", (unsigned int)mtdgeo.blocksize);
printf(" erasesize: %lu\n", (unsigned long)mtdgeo.erasesize);
printf(" neraseblocks: %lu\n", (unsigned long)mtdgeo.neraseblocks);
info->blocksize = mtdgeo.erasesize;
info->nblocks = mtdgeo.neraseblocks;
}
/* Otherwise, use the configured default. We have no idea of the size
* of the media in this case.
*/
else
{
info->blocksize = CONFIG_EXAMPLES_MEDIA_BLOCKSIZE;
info->nblocks = 0;
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* media_main
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int media_main(int argc, char *argv[])
#endif
{
FAR uint8_t *buffer;
struct media_info_s info;
int fd;
/* Open the character driver that wraps the media */
fd = open(CONFIG_EXAMPLES_MEDIA_DEVPATH, O_RDWR);
if (fd < 0)
{
fprintf(stderr, "ERROR: failed to open %s: %d\n",
CONFIG_EXAMPLES_MEDIA_DEVPATH, errno);
return 1;
}
/* Get the block size to use */
get_blocksize(fd, &info);
printf("Using:\n");
printf(" blocksize: %lu\n", (unsigned long)info.blocksize);
printf(" nblocks: %lu\n", (unsigned long)info.nblocks);
/* Allocate an I/O buffer of the correct block size */
buffer = (FAR uint8_t *)malloc((size_t)info.blocksize);
if (buffer == NULL)
{
fprintf(stderr, "ERROR: failed to allocate I/O buffer of size %ul\n",
(unsigned long)info.blocksize);
close(fd);
return 1;
}
/* Clean-up and exit */
free(buffer);
close(fd);
return 0;
}