Difference between revisions of "BBB Developing Device Drivers"

From Klaus' wiki
Jump to: navigation, search
Line 1: Line 1:
 
This section is heavily inspired by [http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/ Derek Molloy's] fine writing about device driver development.
 
This section is heavily inspired by [http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/ Derek Molloy's] fine writing about device driver development.
  
I downloaded the 2015-03-01 image and ran it from the SD card. See [BBB New Debian Image] when following these instructions:
+
I downloaded the 2015-03-01 image and ran it from the SD card. See [[BBB New Debian Image]].
 +
 
 +
Then follow these instructions:
  
 
First download the header files that will enable you to develop a device driver.
 
First download the header files that will enable you to develop a device driver.
Line 9: Line 11:
 
]$ apt-cache search linux-headers-`uname -r`
 
]$ apt-cache search linux-headers-`uname -r`
 
</source>
 
</source>
 +
'''NOTE''': Do remember to use the coorrect "reverse"-ping ` which mean that the output of the command enclosed in ` and ` will be replace the `uname -r`. Alternatively use the notation $(uname -r)
  
provided you get a sensible result in the search continue with
+
Provided you get a sensible result in the search continue with
  
 
<source lang=bash>
 
<source lang=bash>
Line 16: Line 19:
 
</source>
 
</source>
  
Now you are ready for developing device drivers directly on the BBB. If you would like to perform cross development the header files shall be installed on your development PC by downloading the files from a repository somewher eon the Internet. See Derek Molloy's blog post for further details.
+
Now you are ready for developing device drivers directly on the BBB. If you would like to perform cross development the header files shall be installed on your development PC by downloading the files from a repository somewhere on the Internet. See Derek Molloy's blog post for further details.
  
I've developed a very simple device driver, a test program and a build script. Download [Example.tar] to your BBB in a suitable directory, e.g. /root/basic-drv/ and unpack it.
+
I've developed a very simple device driver, a test program and a build script. Download [[File:Example.tar]] to your BBB in a suitable directory, e.g. /root/basic-drv/ and unpack it.
  
 
Execute the build.sh script
 
Execute the build.sh script

Revision as of 09:37, 2 May 2015

This section is heavily inspired by Derek Molloy's fine writing about device driver development.

I downloaded the 2015-03-01 image and ran it from the SD card. See BBB New Debian Image.

Then follow these instructions:

First download the header files that will enable you to develop a device driver.

]$ apt-get update
]$ apt-cache search linux-headers-`uname -r`

NOTE: Do remember to use the coorrect "reverse"-ping ` which mean that the output of the command enclosed in ` and ` will be replace the `uname -r`. Alternatively use the notation $(uname -r)

Provided you get a sensible result in the search continue with

]$ apt-get install linux-headers-3.8.13-bone70

Now you are ready for developing device drivers directly on the BBB. If you would like to perform cross development the header files shall be installed on your development PC by downloading the files from a repository somewhere on the Internet. See Derek Molloy's blog post for further details.

I've developed a very simple device driver, a test program and a build script. Download File:Example.tar to your BBB in a suitable directory, e.g. /root/basic-drv/ and unpack it.

Execute the build.sh script

]$ ./build.sh

Expect an output like shown below:

root@beaglebone:~/basic-drv# ./build.sh 
Cleaning everything before testing
rm -rf *.mod.* *.o *.ko .basic* ./.tmp* test-drv Module.markers Module.symvers
Making the kernel module
make -C /lib/modules/3.8.13-bone70/build SUBDIRS=/root/basic-drv modules
make[1]: Entering directory `/usr/src/linux-headers-3.8.13-bone70'
  CC [M]  /root/basic-drv/basic-drv.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/basic-drv/basic-drv.mod.o
  LD [M]  /root/basic-drv/basic-drv.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.8.13-bone70'
Making the test program
gcc -o test-drv test-drv.c
Inserting the module into the kernel
Running the test program. Watch for 0x00
Value : 0x00
Removing the module from the kernel again
Showing you the last ten lines of the messages log
May  2 09:20:22 beaglebone kernel: [ 7034.531259] Writing...
May  2 09:20:22 beaglebone kernel: [ 7034.531273] Reading...
May  2 09:20:22 beaglebone kernel: [ 7034.532301] Device closed
May  2 09:20:22 beaglebone kernel: [ 7034.574910] Module basic-drv ended...
May  2 09:30:50 beaglebone kernel: [ 7662.721965] Module basic-drv started...
May  2 09:30:50 beaglebone kernel: [ 7662.771044] Device open
May  2 09:30:50 beaglebone kernel: [ 7662.771089] Writing...
May  2 09:30:50 beaglebone kernel: [ 7662.771106] Reading...
May  2 09:30:50 beaglebone kernel: [ 7662.772247] Device closed
May  2 09:30:50 beaglebone kernel: [ 7662.815822] Module basic-drv ended...
root@beaglebone:~/basic-drv# 

Dive into the basic-drv.c code and understand what is happening. Also look at the test-drv.c code.