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:
- 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
su
make scripts
-
make clean
make
this will compile the module -
./create_device
-
insmod counter_driver.ko
-
compile the test program: cc testcounter.c
-
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
Harry
The Makefile looks like that, create a file named: Makefile
obj-m += counter_driver.o
KDIR = /usr/src/kernel
PWD := $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
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
cpufreq-info
testcounter.c (1.47 KB)
counter_driver.c (12.3 KB)