I had written a blog on how to integrate a custom driver into Linux kernel tree previously here. Integrating a custom driver in nuttx kernel is pretty similar. But writing this down would probably be a good idea for any future reference. So below are the steps to follow to integrate a custom driver in nuttx.

Copy the drivers source code into driver folder
Lets say we are including the xyz driver source code into nuttx. Let’s say the name of the folder which contains all the source code for the driver is called xyz. Copy this folder into the drivers folder of the nuttx source tree.

cp -r ~/xyz /nuttx/drivers/.

Write your own makefile for compiling the drivers

You need to write your own makefile unless you are lucky to have the Makefile coming as a part of the driver source tarball. I wrote for my purposes a simple Makefile as below:

+ifeq ($(CONFIG_WIFI),y)
+#ifeq ($(CONFIG_WIFI_XYZ),y)
+CSRCS += xyz.c
+CSRCS += xyz_main.c
+DEPPATH += --dep-path xyz
+VPATH += :xyz
+CFLAGS += $(shell $(INCDIR) $(INCDIROPT) "$(CC)" $(SDKDIR)$(DELIM)drivers$(DELIM)xyz)

Please note the Makefile will vary depending on the build system you have in place. So going into the details of the Makefile would be quite pointless here. Important point is you either need to use the drivers’ exiting Makefile (if there is one available/exists and you can fit it in your build system) or need to write your own.

Include the custom driver’s Makefile into the top level Makefile
Now that you have a Makefile for your custom drivers, you need to tell the nuttx build system that there is a new Makefile available for its attention. So modify the Makefile in the nuttx/drivers folder as below:

include xyz$(DELIM)Make.defs

Write Kconfig for your custom driver
You need a Kconfig file if you particularly want to enable and disable your driver using Kernel/nuttx’s menuconfig. Here is a sample Kconfig file I wrote for my custom driver:

menuconfig WIFI
	bool "WIFI drivers"
		WIFI device drivers.


	bool "XYZ Wifi support"
	default y
		Enable driver for the XYZ Wifi

In the above Kconfig I chose the name WIFI randomly, you are free to choose anything as a name for your drivers. Whatever you choose will appear in the menuconfig. The the CONFIG_WIFI_XYZ config name is again random, or perhaps following the convention already in the available drivers. I think you need to prefix the macro with CONFIG_ and then can add any string as a config name. As you can see the config option by default is enabled but depends on the WIFI which is what is top level config option for enabling and disabling the driver support from menuconfig. Rest I guess is self-explanatory.

Source the custom Kconfig in the top level Kconfig
Now you need to tell the Kconfig system that you exist. To do that add the below in the drivers/Kconfig file:

source "$SDKDIR/drivers/esp8266/Kconfig"

Now if you do the below in the top level folder where you .config file sits, you should see something like the below:

Screenshot from 2019-10-22 13-58-29

Navigate to Drivers

Screenshot from 2019-10-23 16-44-02

Press Enter and you should see your custom driver listed:

Screenshot from 2019-10-23 16-44-48

Press the space bar to select it.

Compile the driver as below:

make clean && make buildkernel && make

Put some junk characters in your driver source code and see if the compilation fails. If it does then that would mean the build systems is picking up your custom driver. Then remove the junks and try compiling.  Now if rest of the building should be very specific to what exactly you are building and hence out of the scope of this post. For any question you are welcome to comment below.

Leave a Reply