Sample Kernel Driver for Frequency Measurement

This kernel modul may be useful for kernel module beginners (like myself) to show usage of these kernel functions:

  • reserving/muxing/read/write of GPIO ports (no, I did not use the device tree).
  • GPIO interrupts
  • kernel timers and interrupts
  • using a character device for communication with the user program (a block device would be better in that case, I will do that later).

This kernel driver can be compiled under Angstrom Linux directly on the BBB.You can connect a square wave frequency to GPIO P 9 / Pin 15 and this kernel module will measure the frequency.
The range is 0 … 100 kHz.

The result can be read from device “fcounter”.

create_device … create /dev/fcounter and switch off cpu-governor (important, if it is not switched off, then this modul will only work up to 5kHz ! or the system will hang up due to an I2C timeout)

testcounter.c … user space program to test the module
counter_driver.c … this kernel module

to get it running:

  1. be sure that the system is updated and the kernel headers and kernel-dev are the same version as the running kernel.
    maybe you have to install:
    opkg install kernel-headers

opkg install kernel-dev

cd /usr/src/kernel


make scripts

  1. make clean
    this will compile the module

  2. ./create_device

  3. insmod counter_driver.ko

  4. compile the test program: cc testcounter.c

  5. and show the measurement results: ./a.out

Now you can connect a square wave signal to P9 Pin15 and read the frequency on the screen.
Be sure to limit the signal to 3,3 volts or the input will be damaged !

The reason for this modul was to make an rpm-measurement for engines.

good luck

The Makefile looks like that, create a file named: Makefile

obj-m += counter_driver.o
KDIR = /usr/src/kernel
PWD := $(shell pwd)

make -C $(KDIR) M=$(PWD) modules
make -C $(KDIR) M=$(PWD) clean

to create the device use this script, create a file named: create_device

mknod /dev/fcounter c 60 0

set the CPU governor to a fixed frequency of 1GHz

cpufreq-set -c 0 -g performance
cpufreq-set -d 1GHz
cpufreq-set -u 1GHz

testcounter.c (1.47 KB)

counter_driver.c (12.3 KB)


This kernel modul may be useful for kernel module beginners (like myself) to show usage of these kernel functions:

May I suggest a few formal things - without going into technical details?

*) try to write it in the Linux kernel programming style

indent -nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -nprs -npsl -sai -saf -saw -ncs -nsc -sob -nfca -cp33 -ss -ts8 -il1 counter_driver.c

might be a good starting point

run and adjust the kernel code until checkpatch is happy, you can find this script under the linux kernel tree

linux/scripts/ --file counter_driver.c

*) why don’t you put this as a little project under a public git repo?




thanks a lot for your response, I will follow your suggestions
and try .