Simple Example of Linux Drivers
Simple Example of Linux Drivers
Simple Example of Linux Drivers
Rev.01 (20090909)
407
Used to operate the IO port, it is necessary to set the registers they use, we need to call
some of the existing function or macro, for example: s3c2410_gpio_cfgpin.
Why S3C2410? Because the Samsung S3C2440 chips produced the register names
used in the majority and the S3C2410, and resource allocation are the same, all in the current
version of the Linux system, most are using the same function definition and macro definitions.
void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
{
void __iomem *base = S3C24XX_GPIO_BASE(pin);
unsigned long mask;
unsigned long con;
unsigned long flags;
if (pin < S3C2410_GPIO_BANKB) {
mask = 1 << S3C2410_GPIO_OFFSET(pin);
}
else {
mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
}
switch (function) {
case S3C2410_GPIO_LEAVE:
mask = 0;
function = 0;
break;
case S3C2410_GPIO_INPUT:
case S3C2410_GPIO_OUTPUT:
case S3C2410_GPIO_SFN2:
case S3C2410_GPIO_SFN3:
if (pin < S3C2410_GPIO_BANKB) {
function -= 1;
function &= 1;
function <<= S3C2410_GPIO_OFFSET(pin);
}
else {
function &= 3;
function <<= S3C2410_GPIO_OFFSET(pin)*2;
}
Venus Supply Co., Ltd.
196/1, Soi Thedsaban-Nimit-Nau 8, Thedsaban-Nimit-Nau Road,
Ladyao, Chatuchak, Bangkok, 10900, Thailand
Tel. +(66)2954-2408 , Fax. +(66)2953-8443, Website: www.thaieasyelec.com
408
In fact, we do not need to care about drivers as long as they can be used, unless you
are using a CPU system platform is can't supported by Linux, because most common
embedded platforms have very similar definition, you do not need to write their own.
In the following driver list, you can see s3c2410_gpio_cfgpin called the situation. In
addition, you also need to call a number of and device drivers are closely related to basic
functions such as registration device misc_register, fill out the driver function structure
file_operations, as well as Hello module in module_init and module_exit functions as such.
Some functions are not necessary, as you learn more about Linux driver development
and reading more code, you naturally understand. Here is our list of LED driver code written,
Source code:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
<linux/miscdevice.h>
<linux/delay.h>
<asm/irq.h>
<mach/regs-gpio.h>
<mach/hardware.h>
<linux/kernel.h>
<linux/module.h>
<linux/init.h>
<linux/mm.h>
<linux/fs.h>
<linux/types.h>
<linux/delay.h>
<linux/moduleparam.h>
<linux/slab.h>
<linux/errno.h>
<linux/ioctl.h>
<linux/cdev.h>
<linux/string.h>
<linux/list.h>
<linux/pci.h>
<asm/uaccess.h>
<asm/atomic.h>
Venus Supply Co., Ltd.
196/1, Soi Thedsaban-Nimit-Nau 8, Thedsaban-Nimit-Nau Road,
Ladyao, Chatuchak, Bangkok, 10900, Thailand
Tel. +(66)2954-2408 , Fax. +(66)2953-8443, Website: www.thaieasyelec.com
409
410
/opt/FriendlyARM/mini2440/linux-2.6.32.2/drivers/char
mini2440_buttons.c
Misc equipment, device number will be generated
The driver major number
automatically
Device Name
/dev/buttons
Test program source code directory /opt/FriendlyARM/mini2440/examples/buttons
Test program source code name
buttons_test.c
Test program executable file name buttons
Description: The key driver has been compiled into the default kernel, so cannot be loaded
using insmod.
411
412
413
414