This tutorial will walk you through the process of writing and compiling an application with NuttX. The tutorial will focus on the method and hence I "borrowed" some materials from sdharrison. At the end of the tutorial, you will notice that our project is pretty similar to other apps in the apps/examples
folder.
1. Prepare necessary files
mkdir -p apps/examples/my_app
cd apps/examples/my_app
touch Kconfig Make.defs counter.c Makefile
nano *
Just in case you are wondering:
touch
is a command to create files, so we created a series of files with names that followed
nano *
is to edit all the files in the current directory
Here comes the content of those files
primes.c
#include <nuttx/config.h>
#include <stdio.h>
#include <time.h>
static double TimeSpecToSeconds(struct timespec* ts){
return (double)ts -> tv_sec + (double)ts->tv_sec / 1000000000.0;
}
#ifdef CONFIG_BUILD_KERNEL
int main (int argc, FAR char *argv[])
#else
int counter_main(int argc, char *argv[])
#endif
{
struct timespec start;
struct timespec end;
double elapsed_secs;
int N = 1000;
int i = 1;
printf("Hello world! I am good at counting! Here I go... \n");
clock_gettime(CLOCK_MONOTONIC, &start);
while (i <= N) {
printf("%d ", i);
i = i + 1;
}
clock_gettime(CLOCK_MONOTONIC, &end);
elapsed_secs = TimeSpecToSeconds(&end) - TimeSpecToSeconds(&start);
printf("\n See... I counted &d numbers in %.3f seconds", N, elapsed_secs);
return 0;
}
Kconfig
config EXAMPLES_MY_APP
bool "MY FIRST APP - COUNTING"
default n
---help---
Enable the \"Counter\" Example
if EXAMPLES_MY_APP
endif
Make.defs
ifeq ($(CONFIG_EXAMPLES_MY_APP), y)
CONFIGURED_APPS += examples/my_app
endif
Makefile
-include $(TOPDIR)/Make.defs
APPNAME = counter
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
ASRCS =
CSRCS =
MAINSRC = counter.c
CONFIG_MY_APP_PROGNAME ?= counter$(EXEEXT)
PROGNAME = $(CONFIG_MY_APP_PROGNAME)
include $(APPDIR)/Application.mk
apps/examples/Kconfig Insert the following line (apps arranged alphabetically)
source "$APPSDIR/examples/my_app/Kconfig"
File editing is complete. Now go back into the nuttx directory and prepare for compliation
2. Compilation
cd nuttx/
make apps_distclean
make menuconfig
Application Configuration --->
Examples --->
- Select
[*] MY FIRST APP - COUNTING
- Exit
- Select
- Exit
RTOS Features -->
Tasks and Scheduling --->
Application entry point
- Type:
counter_main
- Exit
- Type:
- Exit
Clocks and Timers --->
- Select
[*] Support CLOCK_MONOTONIC
- Exit
- Select
- Exit
- Exit & Save
make
If everything compiles without error, then you should have three output files
- nuttx (Arm Elf 32 Bit Executable)
- nuttx.bin (binary data file)
- nuttx.hex (Ascii file)
The 'nm' command recognizes the nuttx Arm Elf file and will list the symbols. Use as follows:
nm nuttx | grep primes_main
you should see something like this: 080047a5 T primes_main
3. Reference
http://sdharrison.blogspot.sg/?view=classic