Add nuttx to the system framework, which is 10.1.0

This commit is contained in:
TangYiwen123
2021-06-09 14:33:15 +08:00
parent 06c351e27c
commit 804bd57aa0
5000 changed files with 1488544 additions and 0 deletions
@@ -0,0 +1,141 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config EXAMPLES_MODULE
tristate "Module Example"
default n
depends on MODULE && LIB_BOARDCTL
select BOARDCTL_OS_SYMTAB
---help---
Enable the module example
if EXAMPLES_MODULE
config EXAMPLES_MODULE_BUILTINFS
bool "Built-in File System"
default y
depends on (FS_ROMFS || FS_CROMFS) && BUILD_FLAT
---help---
This example supports a very, non-standard but also very convenient
way of testing with example using CONFIG_EXAMPLES_MODULE_BUILTINFS.
If this option is selected, the test modules will be built,
installed in the module/lib/fsroot, then converted to a built-in
ROMFS file system.
When the test runs, it will automatically create the ROMFS block
device and mount the test modules for testing.
If this option is not selected, then the modules will be left in
the fsroot/ directory. You can then copy them to, say an SD card,
for testing on the target. That file system will need to be mounted
such that CONFIG_EXAMPLES_MODULE_BINDIR refers to the directory
containing the module binaries (unless the file system is auto-
mounted)
NOTE: This option can only be used in the FLAT build mode because
it makes an illegal OS call to ramdisk_register().
choice
prompt "Built-in file system type"
default EXAMPLES_MODULE_ROMFS if FS_ROMFS
default EXAMPLES_MODULE_CROMFS if !FS_ROMFS && FS_CROMFS
depends on EXAMPLES_MODULE_BUILTINFS
config EXAMPLES_MODULE_ROMFS
bool "ROMFS"
depends on FS_ROMFS && BUILD_FLAT
---help---
Automatically generates and mounts an internal ROMFS file
system
config EXAMPLES_MODULE_CROMFS
bool "CROMFS"
depends on FS_CROMFS && BUILD_FLAT
---help---
Automatically generates and mounts an internal CROMFS file
system
endchoice # Built-in file system type
config EXAMPLES_MODULE_DEVMINOR
int "ROMFS Minor Device Number"
default 0
depends on EXAMPLES_MODULE_ROMFS
---help---
The minor device number of the ROMFS block. For example, the N in /dev/ramN.
Used for registering the RAM block driver that will hold the ROMFS file system
containing the MODULE executables to be tested. Default: 0
config EXAMPLES_MODULE_FSMOUNT
bool "Mount external file system"
default y
depends on !EXAMPLES_MODULE_BUILTINFS
---help---
Automatically mount the external file system using the block
device provided by CONFIG_EXAMPLES_MODULE_DEVPATH and the file
system type specified by CONFIG_EXAMPLES_MODULE_FSTYPE.
config EXAMPLES_MODULE_FSREMOVEABLE
bool "Removable file system"
default n
depends on EXAMPLES_MODULE_FSMOUNT
---help---
If the external file system is on removable media such as an
SD card or a USB FLASH driver, then this option may be selected
to build in logic to wait for the media to be installed and
initialized before trying to mount it.
config EXAMPLES_MODULE_FSTYPE
string "External file system type"
default "vfat"
depends on EXAMPLES_MODULE_FSMOUNT
---help---
The type of the external file system as will be used in the mount()
command. Default: "vfat"
config EXAMPLES_MODULE_DEVPATH
string "Block driver device path"
default "/dev/ram0" if EXAMPLES_MODULE_ROMFS
default "/dev/mmcsd0" if EXAMPLES_MODULE_FSMOUNT
depends on EXAMPLES_MODULE_ROMFS || EXAMPLES_MODULE_FSMOUNT
---help---
The path to the ROMFS/External block driver device. This must match
EXAMPLES_MODULE_DEVMINOR for the case of ROMFS. Used for mounting the
file system containing the ELF executables to be tested. Default:
"/dev/ram0" for ROMFS, "/dev/mmcsd0" for the external file system.
config EXAMPLES_MODULE_BINDIR
string "Path to binaries"
default "/mnt/moddata"
depends on !EXAMPLES_MODULE_BUILTINFS && !EXAMPLES_MODULE_FSMOUNT
---help---
The full, absolute path to the location for the binaries can be
located in a pre-mounted external file system.
config EXAMPLES_MODULE_LIBC
bool "Link with LIBC"
default n
depends on EXPERIMENTAL
---help---
Link with the C library (and also math library if it was built).
By default, all undefined symbols must be provided via a symbol
table. But if this option is selected, then each MODULE test program
will link with the C library and will not require symbol table
entries. You probably will NOT want this option, however,
because it will substantially increase the size of code. For
example, a separate copy of printf() would be linked with every
program greatly increasing the total code size. This option is
primarily intended only for testing.
WARNING: Libc will not be built with -mlong-calls and this may
result in runtime linking failures due to out-of-range functions
calls.
config EXAMPLES_MODULE_LIBGCC
bool "Link with LIBGCC"
default n
depends on EXPERIMENTAL
endif
@@ -0,0 +1,38 @@
############################################################################
# apps/examples/module/Make.defs
#
# 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.
#
############################################################################
ifneq ($(CONFIG_EXAMPLES_MODULE),)
CONFIGURED_APPS += $(APPDIR)/examples/module
endif
@@ -0,0 +1,76 @@
############################################################################
# apps/examples/module/Makefile
#
# Copyright (C) 2015-2016 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 $(APPDIR)/Make.defs
# Module example built-in application info
PROGNAME = module
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_MODULE)
# Module Example
ifeq ($(CONFIG_EXAMPLES_MODULE_ROMFS),y)
CSRCS = romfs.c
endif
ifeq ($(CONFIG_EXAMPLES_MODULE_CROMFS),y)
CSRCS = cromfs.c
endif
CSRCS += mod_symtab.c
MAINSRC = module_main.c
VPATH += drivers
DEPPATH += --dep-path drivers
# Build targets
ifeq ($(CONFIG_EXAMPLES_MODULE_ROMFS),y)
drivers/romfs.c: build
endif
ifeq ($(CONFIG_EXAMPLES_MODULE_CROMFS),y)
drivers/cromfs.c: build
endif
drivers/mod_symtab.c: build
.PHONY: build
build:
+$(Q) $(MAKE) -C drivers TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" CROSSDEV=$(CROSSDEV)
clean::
+$(Q) $(MAKE) -C drivers TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" CROSSDEV=$(CROSSDEV) clean
include $(APPDIR)/Application.mk
@@ -0,0 +1,124 @@
############################################################################
# apps/examples/module/drivers/Makefile
#
# Copyright (C) 2015, 2017-2018 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 $(APPDIR)/Make.defs
ALL_SUBDIRS = chardev
BUILD_SUBDIRS = chardev
FSIMG_SUBDIR = fsroot
MODULE_DIR = $(APPDIR)/examples/module
DRIVER_DIR = $(MODULE_DIR)/drivers
FSROOT_DIR = $(DRIVER_DIR)/fsroot
SYMTAB_SRC = $(DRIVER_DIR)/mod_symtab.c
ifneq ($(CONFIG_BUILD_FLAT),y)
PASS1_SYMTAB = $(TOPDIR)/pass1/mod_symtab.c
endif
ifeq ($(CONFIG_EXAMPLES_MODULE_BUILTINFS),y)
ifeq ($(CONFIG_EXAMPLES_MODULE_ROMFS),y)
ROMFS_IMG = $(DRIVER_DIR)/romfs.img
FSIMG_SRC = $(DRIVER_DIR)/romfs.c
else ifeq ($(CONFIG_EXAMPLES_MODULE_CROMFS),y)
NXTOOLDIR = $(TOPDIR)/tools
GENCROMFSSRC = gencromfs.c
GENCROMFSEXE = gencromfs$(HOSTEXEEXT)
FSIMG_SRC = $(DRIVER_DIR)/cromfs.c
endif
endif
define DIR_template
$(1)_$(2):
+$(Q) $(MAKE) -C $(1) $(2) TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" FSROOT_DIR="$(FSROOT_DIR)" CROSSDEV=$(CROSSDEV)
endef
all: $(FSIMG_SRC) $(SYMTAB_SRC) $(PASS1_SYMTAB)
.PHONY: all clean install
$(foreach DIR, $(ALL_SUBDIRS), $(eval $(call DIR_template,$(DIR),clean)))
$(foreach DIR, $(BUILD_SUBDIRS), $(eval $(call DIR_template,$(DIR),install)))
# Install each program in the fsroot directory
install: $(foreach DIR, $(BUILD_SUBDIRS), $(DIR)_install)
ifeq ($(CONFIG_EXAMPLES_MODULE_BUILTINFS),y)
ifeq ($(CONFIG_EXAMPLES_MODULE_ROMFS),y)
# Create the romfs.img file from the populated fsroot directory
$(ROMFS_IMG): install
$(Q) genromfs -f $@.tmp -d $(FSROOT_DIR) -V "MODULETEST"
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
# Create the romfs.c file from the romfs.img file
$(FSIMG_SRC): $(ROMFS_IMG)
$(Q) (cd $(DRIVER_DIR); xxd -i romfs.img | sed -e "s/^unsigned/const unsigned/g" >$@)
else ifeq ($(CONFIG_EXAMPLES_MODULE_CROMFS),y)
# Make sure that the NuttX gencromfs tool has been built
$(NXTOOLDIR)/$(GENCROMFSEXE): $(NXTOOLDIR)/$(GENCROMFSSRC)
$(Q) $(MAKE) -C $(NXTOOLDIR) -f Makefile.host $(GENCROMFSEXE)
# Create the cromfs.c file from the populated cromfs directory
$(FSIMG_SRC): install $(NXTOOLDIR)/$(GENCROMFSEXE)
$(Q) $(NXTOOLDIR)/$(GENCROMFSEXE) $(FSIMG_DIR) $@.tmp
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
endif
endif
# Create the exported symbol table
$(SYMTAB_SRC): install
$(Q) $(APPDIR)$(DELIM)tools$(DELIM)mksymtab.sh $(FSROOT_DIR) g_mod >$@.tmp
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
# Copy the symbol table into the kernel pass1/ build directory
ifneq ($(CONFIG_BUILD_FLAT),y)
$(PASS1_SYMTAB): $(SYMTAB_SRC)
$(Q) install -m 0644 $(SYMTAB_SRC) $(PASS1_SYMTAB)
endif
# Clean each subdirectory
clean: $(foreach DIR, $(ALL_SUBDIRS), $(DIR)_clean)
$(Q) rm -f $(FSIMG_SRC) $(ROMFS_IMG) $(SYMTAB_SRC)
$(Q) rm -rf $(FSROOT_DIR)
@@ -0,0 +1,88 @@
############################################################################
# examples/module/drivers/chardev/Makefile
#
# Copyright (C) 2015-2016 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 $(APPDIR)/Make.defs
CMODULEFLAGS += $(KDEFINE)
ifeq ($(CONFIG_EXAMPLES_MODULE_LIBGCC),y)
LIBGCC = "${shell $(CC) $(ARCHCPUFLAGS) -print-libgcc-file-name 2>/dev/null}"
ifneq ($(LIBGCC),)
LDLIBPATH += -L "${shell dirname $(LIBGCC)}"
LDLIBS += -lgcc
endif
endif
ifeq ($(CONFIG_EXAMPLES_MODULE_LIBC),y)
LDMODULEFLAGS += -Bstatic
LDLIBPATH += -L $(NUTTXLIB)
endif
ifeq ($(CONFIG_EXAMPLES_MODULE_LIBC),y)
ifneq ($(CONFIG_BUILD_FLAT),y)
LDLIBS += -lkc
else
LDLIBS += -lc
endif
endif
BIN = chardev
SRCS = $(BIN).c
OBJS = $(SRCS:.c=$(OBJEXT))
all: $(BIN)
.PHONY: all clean install
$(OBJS): %$(OBJEXT): %.c
@echo "MODULECC: $<"
$(Q) $(MODULECC) -c $(CMODULEFLAGS) $< -o $@
$(BIN): $(OBJS)
@echo "MODULELD: $<"
$(Q) $(MODULELD) $(LDMODULEFLAGS) $(LDLIBPATH) -o $@ $(ARCHCRT0OBJ) $^ $(LDLIBS)
$(FSROOT_DIR)/$(BIN): $(BIN)
$(Q) mkdir -p $(FSROOT_DIR)
$(Q) install $(BIN) $(FSROOT_DIR)/$(BIN)
ifneq ($(CONFIG_DEBUG_SYMBOLS),y)
$(Q) $(MODULESTRIP) $(FSROOT_DIR)/$(BIN)
endif
install: $(FSROOT_DIR)/$(BIN)
clean:
$(call DELFILE, $(BIN))
$(call CLEAN)
@@ -0,0 +1,161 @@
/****************************************************************************
* apps/examples/module/drivers/chardev/chardev.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/types.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <debug.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
#include <nuttx/fs/fs.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
/****************************************************************************
* Private data
****************************************************************************/
static const char g_read_string[] = "Hi there, apps/examples/module test\n";
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static ssize_t chardev_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t chardev_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations chardev_fops =
{
NULL, /* open */
NULL, /* close */
chardev_read, /* read */
chardev_write, /* write */
NULL, /* seek */
NULL, /* ioctl */
NULL /* poll */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, NULL /* unlink */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: chardev_read
****************************************************************************/
static ssize_t chardev_read(FAR struct file *filep, FAR char *buffer,
size_t len)
{
size_t rdlen = strlen(g_read_string);
ssize_t ret = MIN(len, rdlen);
memcpy(buffer, g_read_string, ret);
syslog(LOG_INFO, "chardev_read: Returning %d bytes\n", (int)ret);
lib_dumpbuffer("chardev_read: Returning", (FAR const uint8_t *)buffer, ret);
return ret;
}
/****************************************************************************
* Name: chardev_write
****************************************************************************/
static ssize_t chardev_write(FAR struct file *filep, FAR const char *buffer,
size_t len)
{
syslog(LOG_INFO, "chardev_write: Writing %d bytes\n", (int)len);
lib_dumpbuffer("chardev_write: Writing", (FAR const uint8_t *)buffer, len);
return len;
}
/****************************************************************************
* Name: module_uninitialize
****************************************************************************/
static int module_uninitialize(FAR void *arg)
{
/* TODO: Check if there are any open references to the driver */
syslog(LOG_INFO, "module_uninitialize: arg=%p\n", arg);
return unregister_driver("/dev/chardev");
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: module_initialize
*
* Description:
* Register /dev/chardev
*
****************************************************************************/
int module_initialize(FAR struct mod_info_s *modinfo)
{
syslog(LOG_INFO, "module_initialize:\n");
modinfo->uninitializer = module_uninitialize;
modinfo->arg = NULL;
modinfo->exports = NULL;
modinfo->nexports = 0;
return register_driver("/dev/chardev", &chardev_fops, 0666, NULL);
}
@@ -0,0 +1,351 @@
/****************************************************************************
* examples/module/module_main.c
*
* Copyright (C) 2015, 2017-2018 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 <nuttx/compiler.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/boardctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/module.h>
#include <nuttx/symtab.h>
#if defined(CONFIG_EXAMPLES_MODULE_ROMFS)
# include <nuttx/drivers/ramdisk.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Check configuration. This is not all of the configuration settings that
* are required -- only the more obvious.
*/
#ifndef CONFIG_MODULE
# error "You must select CONFIG_MODULE in your configuration file"
#endif
#if defined(CONFIG_EXAMPLES_MODULE_BUILTINFS)
# if !defined(CONFIG_FS_ROMFS) && !defined(CONFIG_FS_CROMFS)
# error "You must select CONFIG_FS_ROMFS or CONFIG_FS_CROMFS in your configuration file"
# endif
# ifdef CONFIG_DISABLE_MOUNTPOINT
# error "You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT in your configuration file"
# endif
# if defined(CONFIG_EXAMPLES_MODULE_ROMFS)
/* Describe the ROMFS file system */
# define SECTORSIZE 64
# define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
# define MOUNTPT "/mnt/romfs"
# ifndef CONFIG_EXAMPLES_MODULE_DEVMINOR
# define CONFIG_EXAMPLES_MODULE_DEVMINOR 0
# endif
# ifndef CONFIG_EXAMPLES_MODULE_DEVPATH
# define CONFIG_EXAMPLES_MODULE_DEVPATH "/dev/ram0"
# endif
# elif defined(CONFIG_EXAMPLES_MODULE_CROMFS)
/* Describe the CROMFS file system */
# define MOUNTPT "/mnt/cromfs"
# endif
# define BINDIR MOUNTPT
#elif defined(CONFIG_EXAMPLES_MODULE_FSMOUNT)
/* Describe how to auto-mount the external file system */
# define MOUNTPT "/mnt/" CONFIG_EXAMPLES_MODULE_FSTYPE
# define BINDIR MOUNTPT
#else
/* Describe how to use the pre-mounted external file system */
# define BINDIR CONFIG_EXAMPLES_MODULE_BINDIR
#endif /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
/****************************************************************************
* Private data
****************************************************************************/
static const char g_write_string[] = "Hi there installed driver\n";
/****************************************************************************
* Symbols from Auto-Generated Code
****************************************************************************/
#if defined(CONFIG_EXAMPLES_MODULE_ROMFS) || defined(CONFIG_EXAMPLES_MODULE_CROMFS)
extern const unsigned char romfs_img[];
extern const unsigned int romfs_img_len;
#endif
#ifdef CONFIG_BUILD_FLAT
extern const struct symtab_s g_mod_exports[];
extern const int g_mod_nexports;
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: module_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
#ifdef CONFIG_BUILD_FLAT
struct boardioc_symtab_s symdesc;
#endif
#if defined(CONFIG_EXAMPLES_MODULE_FSMOUNT) && \
defined(CONFIG_EXAMPLES_MODULE_FSREMOVEABLE)
struct stat buf;
#endif
FAR void *handle;
char buffer[128];
ssize_t nbytes;
int ret;
int fd;
#ifdef CONFIG_BUILD_FLAT
/* Set the OS symbol table indirectly through the boardctl() */
symdesc.symtab = (FAR struct symtab_s *)g_mod_exports;
symdesc.nsymbols = g_mod_nexports;
ret = boardctl(BOARDIOC_OS_SYMTAB, (uintptr_t)&symdesc);
if (ret < 0)
{
fprintf(stderr, "ERROR: boardctl(BOARDIOC_OS_SYMTAB) failed: %d\n",
ret);
exit(EXIT_FAILURE);
}
#endif
#ifdef CONFIG_EXAMPLES_MODULE_BUILTINFS
#if defined(CONFIG_EXAMPLES_MODULE_ROMFS)
/* Create a ROM disk for the ROMFS filesystem */
printf("main: Registering romdisk at /dev/ram%d\n",
CONFIG_EXAMPLES_MODULE_DEVMINOR);
#if defined(CONFIG_BUILD_FLAT)
/* This example violates the portable POSIX interface by calling the OS
* internal function romdisk_register() (aka ramdisk_register()). We can
* squeak by in with this violation in the FLAT build mode, but not in
* other build modes. In other build modes, the following logic must be
* performed in the OS board initialization logic (where it really belongs
* anyway).
*/
ret = romdisk_register(CONFIG_EXAMPLES_MODULE_DEVMINOR,
(FAR uint8_t *)romfs_img,
NSECTORS(romfs_img_len), SECTORSIZE);
if (ret < 0)
{
/* This will happen naturally if we registered the ROM disk
* previously.
*/
if (ret != -EEXIST)
{
fprintf(stderr, "ERROR: romdisk_register failed: %d\n", ret);
exit(EXIT_FAILURE);
}
printf("main: ROM disk already registered\n");
}
#endif
/* Mount the file system */
printf("main: Mounting ROMFS filesystem at target=%s with source=%s\n",
MOUNTPT, CONFIG_EXAMPLES_MODULE_DEVPATH);
ret = mount(CONFIG_EXAMPLES_MODULE_DEVPATH, MOUNTPT, "romfs",
MS_RDONLY, NULL);
if (ret < 0)
{
fprintf(stderr, "ERROR: mount(%s,%s,romfs) failed: %s\n",
CONFIG_EXAMPLES_MODULE_DEVPATH, MOUNTPT, strerror(errno));
exit(EXIT_FAILURE);
}
#elif defined(CONFIG_EXAMPLES_MODULE_CROMFS)
/* Mount the CROMFS file system */
printf("Mounting CROMFS filesystem at target=%s\n", MOUNTPT);
ret = mount(NULL, MOUNTPT, "cromfs", MS_RDONLY, NULL);
if (ret < 0)
{
errmsg("ERROR: mount(%s, cromfs) failed: %d\n", MOUNTPT, errno);
}
#endif /* CONFIG_EXAMPLES_MODULE_ROMFS */
#else /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
/* An external file system is being used */
#if defined(CONFIG_EXAMPLES_MODULE_FSMOUNT)
#if defined(CONFIG_EXAMPLES_MODULE_FSREMOVEABLE)
/* The file system is removable, wait until the block driver is available */
do
{
ret = stat(CONFIG_EXAMPLES_MODULE_DEVPATH, &buf);
if (ret < 0)
{
int errcode = errno;
if (errcode == ENOENT)
{
printf("%s does not exist. Waiting...\n",
CONFIG_EXAMPLES_MODULE_DEVPATH);
sleep(1);
}
else
{
printf("ERROR: stat(%s) failed: %d Aborting...\n",
CONFIG_EXAMPLES_MODULE_DEVPATH, errcode);
exit(EXIT_FAILURE);
}
}
else if (!S_ISBLK(buf.st_mode))
{
printf("ERROR: stat(%s) exists but is not a block driver: %04x\n",
CONFIG_EXAMPLES_MODULE_DEVPATH, buf.st_mode);
exit(EXIT_FAILURE);
}
}
while (ret < 0);
#endif /* CONFIG_EXAMPLES_MODULE_FSREMOVEABLE */
/* Mount the external file system */
printf("Mounting %s filesystem at target=%s\n",
CONFIG_EXAMPLES_MODULE_FSTYPE, MOUNTPT);
ret = mount(CONFIG_EXAMPLES_MODULE_DEVPATH, MOUNTPT,
CONFIG_EXAMPLES_MODULE_FSTYPE, MS_RDONLY, NULL);
if (ret < 0)
{
printf("ERROR: mount(%s, %s, %s) failed: %d\n",
CONFIG_EXAMPLES_MODULE_DEVPATH, CONFIG_EXAMPLES_MODULE_FSTYPE,
MOUNTPT, errno);
}
#endif /* CONFIG_EXAMPLES_MODULE_FSMOUNT */
#endif /* CONFIG_EXAMPLES_MODULE_BUILTINFS */
/* Install the character driver */
handle = insmod(BINDIR "/chardev", "chardev");
if (handle == NULL)
{
int errcode = errno;
fprintf(stderr, "ERROR: insmod(%s/chardev, chardev) failed: %d\n",
BINDIR, errcode);
exit(EXIT_FAILURE);
}
/* Open the installed character driver */
fd = open("/dev/chardev", O_RDWR);
if (fd < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: Failed to open /dev/chardev: %d\n", errcode);
exit(EXIT_FAILURE);
}
/* Read from the character driver */
nbytes = read(fd, buffer, 128);
if (nbytes < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: Read from /dev/chardev failed: %d\n", errcode);
close(fd);
exit(EXIT_FAILURE);
}
printf("main: Read %d bytes\n", (int)nbytes);
lib_dumpbuffer("main: Bytes read", (FAR const uint8_t *)buffer, nbytes);
/* Write to the character driver */
nbytes = write(fd, g_write_string, strlen(g_write_string));
if (nbytes < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: Write to /dev/chardev failed: %d\n", errcode);
close(fd);
exit(EXIT_FAILURE);
}
printf("main: Wrote %d bytes\n", (int)nbytes);
lib_dumpbuffer("main: Bytes written", (FAR const uint8_t *)g_write_string,
nbytes);
close(fd);
ret = rmmod(handle);
if (ret < 0)
{
fprintf(stderr, "ERROR: rmmod failed: %d\n", ret);
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}