A small HowTo (and reminder for myself) on how to use Eclipse (Xilinx SDK) to develop, cross-compile and upload Linux kernel modules for Zynq (ARM-based) embedded board using Xilinx SDK and Xilinx Embedded Linux aka Petalinux. But most steps are universal, you just have to setup cross-compiler and possibly some missing packages, like sshpass and scp.
This HowTo based on:
HowTo use the CDT to navigate Linux kernel source
Configuring Eclipse for Linux Kernel module development
- Linux kernel sources is a huge collection of files and Eclipse indexer needs lots of memory. So, start Xilinx SDK with extra memory for indexer:
xsdk -vmargs -Xms1024m -Xmx2048m -XX:+UseParallelGC
- Add new C Project and configure it:
- Give it a name AXIDMATest.
- Check Use default location.
- Set Project Type to Makefile Project > Empty Project.
- Select Xilinx ARM GNU/Linux Toolchain for Toolchain.
- Click Finish.
- In C/C++ General properties for AXIDMAProject:
- Check Enable project specific settings.
- Select Indexer on left menu:
- Check Enable project specific settings.
- Uncheck Index source files not included in the build and check Index all header variants.
- Select Preprocessor Include Paths, Macros etc. on left menu:
- Select GNU C in Languages
- Select CDT User Settings Entries for Settings Entries.
- Click Add... button on right.
- Select Preprocessor Macros File in top-left drop-down menu.
- Select File System Path in top-right left drop-down menu.
- Set File to /home/d9/Projects/ZedBoardPetalinux/build/linux/kernel/xlnx-4.0/include/generated/autoconf.h.
- Now click on Providers tab and check CDT GCC Built-in Compiler Settings.
- Uncheck Use global provider shared between projects and add -nostdinc to Command to get compiler specs:.
- Now select Paths and Symbols on left menu:
- Select Includes tab.
- Select GNU C on left languages list.
- Click Add... button on right.
- Check Add to all configurations and click File system... button.
- Select /opt/Petalinux/petalinux-v2015.4-final/components/linux-kernel/xlnx-4.0/arch/arm/include and click OK.
- Repeat prev. steps and add /opt/Petalinux/petalinux-v2015.4-final/components/linux-kernel/xlnx-4.0/include.
- Now we have to set some symbols, so select #Symbols tab.
- Select GNU C on left languages list.
- Click Add... button on right.
- Type name __KERNEL__ and value 1, check Add to all configurations and click OK.
- You may need other symbols too, like DEBUG and CONFIG_OF.
- Now we must exclude staff we don't need, so select Source Location tab.
- Click on your project and click Link Folder... button on right.
- Check Link to folder in the file system and click Browse... button.
- Browse to /opt/Petalinux/petalinux-v2015.4-final/components/linux-kernel/xlnx-4.0 and click OK.
- Next expand project, select Filter and click on Edit Filter...button.
Here we can add folders which we want to exclude from the indexing. All paths are relative to project folder. What to exclude is up to you, but the minimum will be all architectures except ARM. - Apply changes and click OK to close all dialogs.
- We can exclude kernel source folders from indexer other way too:
- Expand AXIDMATest in Project Explorer tab.
- Expand xlnx-4.0, select source folders you think you don't need to be indexed and right click on them.
- Select Resource Configuration and click on Exclude from Build....
- Select project build configurations in which you want to exclude selected sources. In this case you must have the only Default, so select it and click Ok.
- Verify that previously selected folders and sources now greyed out and icons "crossed".
- If you want to enable them back - do same steps and uncheck related "build configuration" in Resource configuration.
- Add sources to project:
- Add new Sources Folder and name it sources.
- Copy into it axidmatest.c from "/opt/Petalinux/petalinux-v2015.4-final/components/linux-kernel/xlnx-4.0/drivers/dma/xilinx"
- Open axidmatest.c and make sure Indexer did it job (you don't have indexer markers complaining about syntax errors).
- Now we need a Makefile:
- Add new File to the project folder and name it Makefile.
- Code below represents Makefile in my case. You have to modify at least path to your configured kernel folder:
ARCH:=arm CROSS_COMPILE:=arm-xilinx-linux-gnueabi- KDIR_DI := ~/Projects/ZedBoardPetalinux/build/linux/kernel/xlnx-4.0/ PWD := $(shell pwd) obj-m += sources/axidmatest.o all: make -C $(KDIR_DI) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) SUBDIRS=$(PWD/sources) modules clean: make -C $(KDIR_DI) M=$(PWD) clean
- Finally we are ready to compile our project. Run Build Project, expand sources folder and and verify that it now includes axidmatest.ko.
- To upload module and insmod it to remote system I write a small script, which can be used from Eclipse:
- In a AXIDMAProject create folder utils.
- In utils folder create file upload_rmmod_insmod.sh.
- Put something like code below. You obviously must install sshpass and maybe some other missing utils and modify names, passwords, addresses and etc.
#!/bin/bash REMOTE_IP="172.21.0.10" FILENAME="axidmatest" sshpass -p 'xxxx' scp $FILENAME.ko root@$REMOTE_IP:~ sshpass -p 'xxxx' ssh root@$REMOTE_IP "/sbin/rmmod $FILENAME" sshpass -p 'xxxx' ssh root@$REMOTE_IP "/sbin/insmod $FILENAME.ko"
- Set Execute permissions for this script in console or by right clicking on it and selecting Properties and checking permission boxes.
- In Run -> External Tools -> External Tools Configurations... add New launch configuration for Program.
For some reason it is filtered by default in XSDK 2015.4. So, uncheck Filter Configuration Types in menu on top. - In Main tab:
- Type name: AXIDMATest
- For Location: select ${workspace_loc:/AXIDMATest/utils/upload_insmod.sh}
- For Working Directory: select ${workspace_loc:/AXIDMATest/sources}
- In Build tab:
- Check Build before launch box.
- Select Specific projects radio-button.
- Click Projects... button to select projects to build.
- Check AXIDMATest box and click OK.
- In Common tab:
- Check Allocate console (necessary for input) box.
- Check Launch in background box.
- Click Apply and Close button.
- Now we can just run this "External Tool" after making changes to source and it will compile project, upload module to remote system, overwrite existin file, rmmod previous and insmod new version of the module. Output related to script execution will be captured in Eclipse console and module output will be posted on remote system stdout.
- axidmatest requires an entrance in DTS in order to work and without it insmod and rmmod are silent. So, to make it print something to stdout add pr_info() to the module __init() and __exit():
static int __init axidma_init(void) { pr_info("axidmatest.ko axidma_init() called.\n"); return platform_driver_register(&xilinx_axidmatest_driver); } late_initcall(axidma_init); static void __exit axidma_exit(void) { pr_info("axidmatest.ko axidma_exit() called.\n"); platform_driver_unregister(&xilinx_axidmatest_driver); } module_exit(axidma_exit) MODULE_AUTHOR("Xilinx, Inc."); MODULE_DESCRIPTION("Xilinx AXI DMA Test Client"); MODULE_LICENSE("GPL v2");