背景
接之前wiki【Nsh中运行第一个程序】https://blog.csdn.net/nobigdeal00/article/details/144728771
OS还是比较庞大,且上面搭载了Nsh(Nuttx Shell),需要接入串口才能正常工作,一般调试的时候用,非调试的时候,可以取消以节省空间
目标
裁剪掉Nuttx shell,并在OS启动后,运行指定程序,周期打印当前OS上运行的任务
配置Nuttx Demo
进入到Nuttx项目目录,运行
make distclean
清除掉之前项目配置残留后,运行
adminpc@adminpc:~/nuttx$ ./tools/configure.sh -l sim:ostest
Copy files
Select CONFIG_HOST_LINUX=y
Refreshing...
CP: arch/dummy/Kconfig to /home/adminpc/nuttx/arch/dummy/dummy_kconfig
CP: boards/dummy/Kconfig to /home/adminpc/nuttx/boards/dummy/dummy_kconfig
LN: platform/board to /home/adminpc/nuttx-apps/platform/dummy
LN: include/arch to arch/sim/include
LN: include/arch/board to /home/adminpc/nuttx/boards/sim/sim/sim/include
LN: drivers/platform to /home/adminpc/nuttx/drivers/dummy
LN: include/arch/chip to /home/adminpc/nuttx/arch/sim/include/sim
LN: arch/sim/src/chip to /home/adminpc/nuttx/arch/sim/src/sim
LN: arch/sim/src/board to /home/adminpc/nuttx/boards/sim/sim/sim/src
mkkconfig in /home/adminpc/nuttx-apps/audioutils
mkkconfig in /home/adminpc/nuttx-apps/benchmarks
mkkconfig in /home/adminpc/nuttx-apps/boot
mkkconfig in /home/adminpc/nuttx-apps/canutils
mkkconfig in /home/adminpc/nuttx-apps/crypto
mkkconfig in /home/adminpc/nuttx-apps/database
mkkconfig in /home/adminpc/nuttx-apps/examples/mcuboot
mkkconfig in /home/adminpc/nuttx-apps/examples/module
mkkconfig in /home/adminpc/nuttx-apps/examples/sotest
mkkconfig in /home/adminpc/nuttx-apps/examples
mkkconfig in /home/adminpc/nuttx-apps/fsutils
mkkconfig in /home/adminpc/nuttx-apps/games
mkkconfig in /home/adminpc/nuttx-apps/graphics
mkkconfig in /home/adminpc/nuttx-apps/industry
mkkconfig in /home/adminpc/nuttx-apps/inertial
mkkconfig in /home/adminpc/nuttx-apps/interpreters/luamodules
mkkconfig in /home/adminpc/nuttx-apps/interpreters
mkkconfig in /home/adminpc/nuttx-apps/logging
mkkconfig in /home/adminpc/nuttx-apps/lte
mkkconfig in /home/adminpc/nuttx-apps/math
mkkconfig in /home/adminpc/nuttx-apps/mlearning
mkkconfig in /home/adminpc/nuttx-apps/netutils
mkkconfig in /home/adminpc/nuttx-apps/sdr
mkkconfig in /home/adminpc/nuttx-apps/system
mkkconfig in /home/adminpc/nuttx-apps/testing
mkkconfig in /home/adminpc/nuttx-apps/videoutils
mkkconfig in /home/adminpc/nuttx-apps/wireless/bluetooth
mkkconfig in /home/adminpc/nuttx-apps/wireless/ieee802154
mkkconfig in /home/adminpc/nuttx-apps/wireless
mkkconfig in /home/adminpc/nuttx-apps
#
# configuration written to .config
#
配置一个模拟器的测试demo,该demo默认不带Nsh,接着运行
make menuconfig
打开配置界面,选择Application Configuration
选择Testing
找到OS test example,按n取消选定,我们这里是借鸡下蛋,不用ostest
回到配置主界面,选择File Systems,把里面的配置项都清空,不用文件系统
选择Save保存配置后,退出
编写app
进入nuttx-apps目录,新建个目录myapp,目录下有文件如下
/nuttx-apps/myapp/Kconfig
/nuttx-apps/myapp/Make.defs
/nuttx-apps/myapp/Makefile
/nuttx-apps/myapp/myapp_main.c
- Kconfig代码如下,这里MYAPP为配置项(在.config文件中名称),该配置项名称为"My Application"(在make menuconfig时界面显示名称),类型为bool,默认不打开
config MYAPP
bool "My Application"
default n
help
This is a description of your application
- Make.defs代码如下,这里对.config文件中的配置项CONFIG_MYAPP做判断,如果存在该配置项,则将该文件夹下的文件参与构建
ifneq ($(CONFIG_MYAPP),)
CONFIGURED_APPS += $(APPDIR)/myapp
endif
- Makefile代码如下,程序输出件的名字为myapp,调度优先级默认
include $(APPDIR)/Make.defs
PROGNAME = myapp
PRIORITY = SCHED_PRIORITY_DEFAULT
CSRCS = myapp_main.c
include $(APPDIR)/Application.mk
- myapp_main.c代码如下,这里将每隔5s打印一次OS中正在运行的task
#include <nuttx/config.h>
#include <stdio.h>
#include <nuttx/sched.h>
void print_tasks(void)
{
struct sched_param param;
int pid;
// 遍历所有可能的 PID(假设最大 PID 为 256)
for (pid = 0; pid < 256; pid++) {
// 获取任务控制块
struct tcb_s *tcb = nxsched_get_tcb(pid);
if (tcb != NULL) {
// 获取任务名称和优先级
if (sched_getparam(pid, ¶m) == OK) {
printf("Task: %s (PID=%d, Priority=%d)\n",
tcb->name, pid, param.sched_priority);
}
}
}
}
int myapp_main(int argc, char *argv[])
{
while (1) {
print_tasks();
sleep(5); // 每隔5秒打印一次任务列表
}
return 0;
}
配置Nuttx myapp
再次回到nuttx目录下,运行
make menuconfig
打开配置界面后,选择RTOS Features
选择Tasks and Scheduling
将Application entry point和Application entry name改成我们刚才的程序入口myapp_main
回到配置主界面,选择Application Configuration
选择我们刚才新建的My Application
保存配置,退出
构建
回到nuttx目录下,运行make clean;make执行构建
adminpc@adminpc:~/nuttx$ make clean; make
LD: nuttx
Pac SIM with dynamic libs..
'/lib/x86_64-linux-gnu/libm.so.6' -> 'sim-pac/libs/libm.so.6'
'/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
'/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
SIM elf with dynamic libs archive in nuttx.tgz
查看nuttx目录下的可执行文件nuttx,1709624字节,1.63M,比之前wiki【Nsh中运行第一个程序】https://blog.csdn.net/nobigdeal00/article/details/144728771小了一半多空间,之前3.57M
adminpc@adminpc:~/nuttx$ ll | grep nuttx
lrwxrwxrwx 1 adminpc adminpc 65 Dec 27 23:01 Make.defs -> /home/adminpc/nuttx/tools/../boards/sim/sim/sim/scripts/Make.defs
-rwxrwxr-x 1 adminpc adminpc 1709624 Dec 27 23:33 nuttx*
-rw-rw-r-- 1 adminpc adminpc 27 Dec 27 23:33 nuttx.manifest
-rw-rw-r-- 1 adminpc adminpc 129277 Dec 27 23:33 nuttx.map
-rw-rw-r-- 1 adminpc adminpc 2082049 Dec 27 23:33 nuttx.tgz
内核大小333184字节,325.375K,为之前内核大小的四分之一,之前内核大小为1.19M
最终效果
运行程序,可周期打印出OS中的任务信息
adminpc@adminpc:~/nuttx$ ./nuttx
Task: Idle_Task (PID=0, Priority=100)
Task: loop_task (PID=1, Priority=224)
Task: hpwork (PID=2, Priority=224)
Task: myapp_main (PID=3, Priority=100)
Task: Idle_Task (PID=0, Priority=100)
Task: loop_task (PID=1, Priority=224)
Task: hpwork (PID=2, Priority=224)
Task: myapp_main (PID=3, Priority=100)