HC-SR04 kernel driver

Post Reply
crashoverride
Posts: 4470
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1
Has thanked: 0
Been thanked: 52 times
Contact:

HC-SR04 kernel driver

Unread post by crashoverride » Tue Jan 26, 2016 1:43 pm

I modified a HC-SR04 ultrasonic sensor kernel driver for use with Odroid-C1/C1+. It uses GPIO interrupts rather than polling to provide measurements. Instead of creating a character device and fighting with udev, it uses the sysfs filesystem.

Example usage:

Code: Select all

# cat /sys/class/hcsr04/value
The result returned is the duration of the echo pulse in microseconds (us). A python script to convert to centimeters and inches is included (distance.py).

https://github.com/OtherCrashOverride/hc-sr04

greg3
Posts: 1
Joined: Sat May 14, 2016 7:11 pm
languages_spoken: english
ODROIDs: C1, C2
Has thanked: 0
Been thanked: 0
Contact:

Re: HC-SR04 kernel driver

Unread post by greg3 » Sat May 14, 2016 7:13 pm

Thanks for sharing your work. I was wondering, what would be involved to make this driver work on the C2?

crashoverride
Posts: 4470
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1
Has thanked: 0
Been thanked: 52 times
Contact:

Re: HC-SR04 kernel driver

Unread post by crashoverride » Sat May 14, 2016 9:23 pm

I haven't used GPIO on the C2 yet so can't say for sure. Likely it will just involve changing GPIO numbers since the IRQ architecture should be the same. When the S905 datasheet is released, it should shed more light on the subject. The HardKernel staff may also be able to provide additional information.

User avatar
odroid
Site Admin
Posts: 31377
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 52 times
Been thanked: 184 times
Contact:

Re: HC-SR04 kernel driver

Unread post by odroid » Mon May 16, 2016 11:01 am

GPIO interrupt example was already released around a month ago. :)
http://odroid.com/dokuwiki/doku.php?id= ... rdware_irq

crashoverride
Posts: 4470
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1
Has thanked: 0
Been thanked: 52 times
Contact:

Re: HC-SR04 kernel driver

Unread post by crashoverride » Mon May 16, 2016 12:34 pm

Well, then it is indeed a big change. The C2 kernel appears to use the kernel GPIO API and no longer needs the Amlogic GPIO API. This would make the driver closer to the original it was based on.

xkus
Posts: 4
Joined: Sat Dec 03, 2016 7:11 pm
languages_spoken: english, german
Has thanked: 0
Been thanked: 0
Contact:

Re: HC-SR04 kernel driver

Unread post by xkus » Sat Dec 03, 2016 7:18 pm

Hi,

I have an odroid C1 and wan't to install the driver for the Ultrasonic-Sensor.
But I dont't know how to compile the hcsr04.c.
If I type:

Code: Select all

make -f Makefile 
the will be an error:
make -C /lib/modules/3.10.104-177/build SUBDIRS=/home/odroid/Desktop/hc-sr04-master modules
make[1]: Entering directory '/usr/src/linux-headers-3.10.104-177'
CC [M] /home/odroid/Desktop/hc-sr04-master/hcsr04.o
In file included from /usr/src/linux-headers-3.10.104-177/arch/arm/include/asm/page.h:163:0,
from include/linux/mmzone.h:20,
from include/linux/gfp.h:4,
from include/linux/kmod.h:22,
from include/linux/module.h:13,
from /home/odroid/Desktop/hc-sr04-master/hcsr04.c:3:


/usr/src/linux-headers-3.10.104-177/arch/arm/include/asm/memory.h:22:25: fatal error: mach/memory.h: No such file or directory
compilation terminated.


scripts/Makefile.build:314: recipe for target '/home/odroid/Desktop/hc-sr04-master/hcsr04.o' failed
make[2]: *** [/home/odroid/Desktop/hc-sr04-master/hcsr04.o] Error 1
Makefile:1233: recipe for target '_module_/home/odroid/Desktop/hc-sr04-master' failed
make[1]: *** [_module_/home/odroid/Desktop/hc-sr04-master] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-3.10.104-177'
Makefile:6: recipe for target 'default' failed
make: *** [default] Error 2
It would be nice if someone can help me ;)

User avatar
meveric
Posts: 10276
Joined: Mon Feb 25, 2013 2:41 pm
languages_spoken: german, english
ODROIDs: X2, U2, U3, XU-Lite, XU3, XU3-Lite, C1, XU4, C2, C1+, XU4Q, HC1, N1, Go, H2 (N4100), N2
Has thanked: 13 times
Been thanked: 102 times
Contact:

Re: HC-SR04 kernel driver

Unread post by meveric » Sat Dec 03, 2016 8:38 pm

you're missing the correct Headers. Either you install Kernel/Headers that are actually working, or you have to download the github repo for the kernel sources from HardKernel and link the /lib/modules/3.10.104-177/build folder against the downloaded Kernel sources.
You might even have to build the Kernel first (although not installing it) to have the correct headers.
Donate to support my work on the ODROID GameStation Turbo Image for U2/U3 XU3/XU4 X2 X C1 as well as many other releases.
Check out the Games and Emulators section to find some of my work or check the files in my repository to find the software i build for ODROIDs.
If you want to add my repository to your image read my HOWTO integrate my repo into your image.

xkus
Posts: 4
Joined: Sat Dec 03, 2016 7:11 pm
languages_spoken: english, german
Has thanked: 0
Been thanked: 0
Contact:

Re: HC-SR04 kernel driver

Unread post by xkus » Sat Dec 03, 2016 10:39 pm

I already installed the header and Kernel over apt-get to
/usr/src/linux-headers-3.10.104-177
/usr/src/linux-source-4.4.0
I also linked it from /lib/modules/3.10.104-177/build to /usr/src/linux-headers-3.10.104-177.

How can I build the Kernel to get the headers?

User avatar
meveric
Posts: 10276
Joined: Mon Feb 25, 2013 2:41 pm
languages_spoken: german, english
ODROIDs: X2, U2, U3, XU-Lite, XU3, XU3-Lite, C1, XU4, C2, C1+, XU4Q, HC1, N1, Go, H2 (N4100), N2
Has thanked: 13 times
Been thanked: 102 times
Contact:

Re: HC-SR04 kernel driver

Unread post by meveric » Sat Dec 03, 2016 10:49 pm

xkus wrote:I already installed the header and Kernel over apt-get to
you installed A header package, but not a correctly working one.
xkus wrote:/usr/src/linux-headers-3.10.104-177
That's the Kernel Header package from HardKernel which is not working
xkus wrote:/usr/src/linux-source-4.4.0
This is ubuntu 16.04 Kernel sources for Kernel 4.4.0 which is also not what you need for ODROIDs
xkus wrote:I also linked it from /lib/modules/3.10.104-177/build to /usr/src/linux-headers-3.10.104-177.
since the Kernel Headers from HardKernel are broken this won't work
xkus wrote:How can I build the Kernel to get the headers?
There's a guide for this in the ODROID Wiki: http://odroid.com/dokuwiki/doku.php?id= ... rnel#linux
it's just a few commands. Just follow "Linux" and "Compile" Afterwards you need to link /lib/modules/3.10.104-177/build to the Kernel source folder.
Donate to support my work on the ODROID GameStation Turbo Image for U2/U3 XU3/XU4 X2 X C1 as well as many other releases.
Check out the Games and Emulators section to find some of my work or check the files in my repository to find the software i build for ODROIDs.
If you want to add my repository to your image read my HOWTO integrate my repo into your image.

xkus
Posts: 4
Joined: Sat Dec 03, 2016 7:11 pm
languages_spoken: english, german
Has thanked: 0
Been thanked: 0
Contact:

Re: HC-SR04 kernel driver

Unread post by xkus » Sun Dec 04, 2016 9:08 pm

Thank you a lot for the help,
with the sources from https://github.com/hardkernel/linux.git I compiled it successful.

but now if I want to add the driver to the Kernel, there is an other error Message.
odroid@odroid:~/Desktop/hc-sr04-master$ sudo insmod hcsr04.ko trigger_gpio=104 echo_gpio=102
insmod: ERROR: could not insert module hcsr04.ko: Invalid module format
odroid@odroid:~/Desktop/hc-sr04-master$ uname -a
Linux odroid 3.10.104-177 #1 SMP PREEMPT Fri Nov 25 03:55:51 UTC 2016 armv7l armv7l armv7l GNU/Linux

odroid@odroid:~/Desktop/hc-sr04-master$ file hcsr04.ko
hcsr04.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=56902ff5db1658f9acad52418390c7cdef70c543, not stripped
Do I use the right headers or do I use insmod in a wrong way?

User avatar
odroid
Site Admin
Posts: 31377
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 52 times
Been thanked: 184 times
Contact:

Re: HC-SR04 kernel driver

Unread post by odroid » Mon Dec 05, 2016 8:38 am

Did you build the kernel and module on the board?
http://odroid.com/dokuwiki/doku.php?id= ... nux_kernel

xkus
Posts: 4
Joined: Sat Dec 03, 2016 7:11 pm
languages_spoken: english, german
Has thanked: 0
Been thanked: 0
Contact:

Re: HC-SR04 kernel driver

Unread post by xkus » Mon Dec 05, 2016 9:07 pm

On my odroid this image is running: http://de.eu.odroid.in/ubuntu_16.04lts/ ... 727.img.xz
To compile the hcsr04.c, I dowloaded the source files from https://github.com/hardkernel/linux.git (branch odroidc-3.10.y) and compiled it on the Board.
Afterwards I compiled the hcsr04.c with no errors.

zantiago
Posts: 7
Joined: Tue Aug 26, 2014 5:20 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: HC-SR04 kernel driver

Unread post by zantiago » Wed Dec 21, 2016 3:19 am

Hi, I am trying to set up the HC-SR04 on my C1+ but something is not working. I can compile the driver from https://github.com/OtherCrashOverride/hc-sr04 but the file (/sys/class/hcsr04/value) is never created.

While building it I get:
make -C /lib/modules/3.10.96-149/build SUBDIRS=/home/odroid/workspace/hc-sr04 modules
make[1]: Entering directory `/usr/src/linux-headers-3.10.96-149'
CC [M] /home/odroid/workspace/hc-sr04/hcsr04.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/odroid/workspace/hc-sr04/hcsr04.mod.o
LD [M] /home/odroid/workspace/hc-sr04/hcsr04.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.10.96-149'

Any suggestions are greatly appreciated,

Thanks

crashoverride
Posts: 4470
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1
Has thanked: 0
Been thanked: 52 times
Contact:

Re: HC-SR04 kernel driver

Unread post by crashoverride » Wed Dec 21, 2016 10:16 am

After compiling, you will need to load the driver as stated in the README.

Code: Select all

sudo insmod hcsr04.ko [trigger_gpio=xxx] [echo_gpio=yyy]
If it fails to load, look in the system log (dmesg) output for the reason.

zantiago
Posts: 7
Joined: Tue Aug 26, 2014 5:20 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: HC-SR04 kernel driver

Unread post by zantiago » Fri Dec 23, 2016 5:15 am

insmod: ERROR: could not insert module hcsr04.ko: Invalid module format

From DMESG:
[12554.407888@0] hcsr04: version magic '3.10.96 SMP preempt mod_unload ARMv7 ' should be '3.10.96-149 SMP preempt mod_unload ARMv7 '

Thanks

crashoverride
Posts: 4470
Joined: Tue Dec 30, 2014 8:42 pm
languages_spoken: english
ODROIDs: C1
Has thanked: 0
Been thanked: 52 times
Contact:

Re: HC-SR04 kernel driver

Unread post by crashoverride » Fri Dec 23, 2016 7:51 pm

zantiago wrote:[12554.407888@0] hcsr04: version magic '3.10.96 SMP preempt mod_unload ARMv7 ' should be '3.10.96-149 SMP preempt mod_unload ARMv7 '
That error message means the kernel headers installed do not match the running kernel. It will affect any kernel module you attempt to build and is not specific to the HC-SR04 driver.

Post Reply

Return to “Ubuntu”

Who is online

Users browsing this forum: No registered users and 1 guest