diff --git a/ChangeLog.txt b/ChangeLog.txt index 386ed441f..57efe32ff 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -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). + diff --git a/examples/Kconfig b/examples/Kconfig index 04a2a39bc..f7fa2c8fa 100644 --- a/examples/Kconfig +++ b/examples/Kconfig @@ -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" diff --git a/examples/README.txt b/examples/README.txt index 562000fc0..67a3ad872 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -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(, + , 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(, mtd); + ret = bchdev_register(/dev/mtdblock, , + false); + examples/mm ^^^^^^^^^^^ diff --git a/examples/media/.gitignore b/examples/media/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/examples/media/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/media/Kconfig b/examples/media/Kconfig new file mode 100644 index 000000000..6450cb1d8 --- /dev/null +++ b/examples/media/Kconfig @@ -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(, + , 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(, mtd); + ret = bchdev_register(/dev/mtdblock, , + 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(, + , 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(, mtd); + ret = bchdev_register(/dev/mtdblock, , + 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 diff --git a/examples/media/Make.defs b/examples/media/Make.defs new file mode 100644 index 000000000..53111aed5 --- /dev/null +++ b/examples/media/Make.defs @@ -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 +# +# 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 diff --git a/examples/media/Makefile b/examples/media/Makefile new file mode 100644 index 000000000..098494629 --- /dev/null +++ b/examples/media/Makefile @@ -0,0 +1,53 @@ +############################################################################ +# apps/examples/media/Makefile +# +# Copyright (C) 2015 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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 diff --git a/examples/media/media_main.c b/examples/media/media_main.c new file mode 100644 index 000000000..a91c3e9eb --- /dev/null +++ b/examples/media/media_main.c @@ -0,0 +1,157 @@ +/**************************************************************************** + * examples/media/hello_main.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#include +#include +#include +#include + +#include +#include + +/**************************************************************************** + * 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; +}