host:
[root@localhost linux-2.6.14]# which arm-linux-gcc
/usr/local/arm/3.4.1/bin/arm-linux-gcc
[root@localhost linux-2.6.14]# make modules
[root@localhost linux-2.6.14]# cp drivers/char/myleds.ko
/var/ftp
[root@localhost vshare]#
/home/lessonwill/buildroot-2012.08/output//host/usr/bin/arm-linux-gcc
ledtest.c
[root@localhost vshare]# cp a.out /var/ftp
target:
/ # rmmod myleds.ko
/ # wget ftp://192.168.1.108/myleds.ko
Connecting to 192.168.1.108[192.168.1.108]:21
myleds.ko
100% |*****************************|
4018
00:00 ETA
/ # insmod myleds.ko
gpio_va=0xc2852000
leds initialized
mknod /dev/leds c 231 0
/ # wget ftp://192.168.1.108/a.out
Connecting to 192.168.1.108[192.168.1.108]:21
a.out
100% |*****************************|
5617
00:00 ETA
/ # chmod 777 a.out
/ # ./a.out
Usage:
./a.out
eg.
./a.out /dev/leds 68
/ # ./a.out /dev/leds 89
/ #
驱动程序:myleds.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define
DEVICE_NAME
"leds"
#define
LED_MAJOR
231
static unsigned long gpio_va;
void LedOut(short ch, short flag)
{
char
chmod1[]={0x48,0xeb,0x52,
0x62,0xe1,0x64,0x44,0xea,0x40,0x60,0xC0,0x45,0x5c,0x43,0x54,0xd4};
unsigned
short value;
volatile
short *p = (volatile short *)(gpio_va+0x100);
if (flag)
{
value = chmod1[ch/16];
value <<= 8;
value |= chmod1[ch];
} else
{
if (ch>99) ch = ch % 100;
value = chmod1[ch/10];
value <<= 8;
value |= chmod1[ch];
}
*p =
value;
}
static int s3c24xx_leds_open(struct inode *inode, struct file
*file)
{
return
0;
}
static int s3c24xx_leds_read(struct file *filp, char __user
*buff,
size_t count, loff_t *offp)
{
return
1;
}
static ssize_t s3c24xx_leds_write(struct file *file, const char
__user *buf, size_t count, loff_t * ppos)
{
char
val;
copy_from_user(&val, buf, 1);
LedOut(val,0);
return
1;
}
static struct file_operations s3c24xx_leds_fops = {
.owner =
THIS_MODULE,
.open
=
s3c24xx_leds_open,
.read
=
s3c24xx_leds_read,
.write
=
s3c24xx_leds_write
};
static int __init s3c24xx_leds_init(void)
{
int
ret;
int minor = 0;
int i;
gpio_va =
ioremap(0x30000000, 0x1000);
if (!gpio_va) {
return -EIO;
}
printk("gpio_va=0x%x\n",gpio_va);
for(i=0;i<20;i++)
{
LedOut(i,0);
mdelay(100);
}
ret =
register_chrdev(LED_MAJOR, DEVICE_NAME,
&s3c24xx_leds_fops);
if (ret
< 0) {
printk(DEVICE_NAME " can't register major
number\n");
return ret;
}
printk(DEVICE_NAME "
initialized\n");
return
0;
}
static void __exit s3c24xx_leds_exit(void)
{
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
iounmap(gpio_va);
}
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED
Driver");
MODULE_LICENSE("GPL");
ledtest.c
#include
#include
#include
#include
#include
void print_usage(char *file)
{
printf("Usage:\n");
printf("%s
\n",file);
printf("eg. \n");
printf("%s /dev/leds 68\n",
file);
}
int main(int argc, char **argv)
{
int
fd;
char*
filename;
char
val;
if (argc
!= 3)
{
print_usage(argv[0]);
return 0;
}
filename
= argv[1];
fd =
open(filename, O_RDWR);
if (fd
< 0)
{
printf("error, can't open %s\n",
filename);
return 0;
}
val =
atoi(argv[2]);
write(fd,&val,1);
return
0;
}