cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Linux Modules External to PetaLinux

Moderator
Moderator
6 0 1,005

PetaLinux is a great utility that allows designers to easily create Linux images to run on their target platforms. PetaLinux will also create user applications and modules with a template Makefile and BB files so that they can be built and added to a rootfs.

However, for users trying to develop a module, creating, building, and deploying from the command line speeds up the process.

In this blog entry we will discuss how to create a module and then build and deploy it on a ZCU104 board outside of the PetaLinux flow. Once users are content that the module is working, they can then add it to the rootfs.

Building the Linux Image:

petalinux-create -t project -s <path to bsp>.bsp
cd <plnx proj>

PetaLinux uses a TMP directory for the staging files. These are normally removed to save space on your disk.

However, in this case we do not want these files to be removed, so we need to comment out the line below in the build/conf/local.conf.

First, do a petalinux-build, then use Ctrl + c to stop it. This will create the files for you.

local_conf.PNG

Note the location of the TMP directory.

This can be found below:

  • petalinux-config
    • Yocto Settings
      • TMPDIR Location

tmp_dir.PNG

Now build the PetaLinux Project:

petalinux-build
cd images/linux
petalinux-package --boot --fpga system.bit --u-boot

Creating the Module:

I have a very simple module shown below that we can use for demo purposes:

 

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */

static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}

static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye!! \n");
}

module_init(hello_start);
module_exit(hello_end);

 

 Creating the Makefile:

 

export ARCH:=arm64
export CROSS_COMPILE:=aarch64-linux-gnu-

CC=$(CROSS_COMPILE)gcc

obj-m = test_module.o

KVERSION = /tmp/xilinx-zcu104-2020.1-2020.10.23-10.47.08-32S/work/zcu104_zynqmp-xilinx-linux/linux-xlnx/5.4+gitAUTOINC+22b71b4162-r0/linux-zcu104_zynqmp-standard-build/
PWD := $(shell pwd)

all:
make -C $(KVERSION) M=$(PWD) modules
clean:
make -C $(KVERSION) M=$(PWD) clean

 

Note the TMPDIR, which is the location we obtained above.

Building the module:

Then simply call the makefile to build:

build.PNG

Testing on Hardware:

Users can boot Linux, and use SSH to copy the module to the rootfs, or they can copy this onto an SD card.

Here, I used insmod and rmmod to add and then remove the module:

test.PNG