arm9260linux下模块例子.

led.c(这个c不要大写,这曾浪费我好长时间.)源代码

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm-arm/arch-at91/gpio.h>
#include <asm/hardware.h>

#define DEVICE_NAME     "leds" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define LED_MAJOR       231     /* 主设备号 */

/* 应用程序执行ioctl(fd, cmd, arg)时的第2个参数 */
#define IOCTL_LED_ON    0
#define IOCTL_LED_OFF   1

/* 用来指定LED所用的GPIO引脚 */
static unsigned long led_table [] = {
    AT91_PIN_PC8,
    AT91_PIN_PC6,
    AT91_PIN_PC11,
};

/* 用来指定GPIO引脚的功能:输出 */
static unsigned int led_cfg_table [] = {
    1,
    1,
    1,
};

/* 应用程序对设备文件/dev/leds执行open(...)时,
* 就会调用at91sam9260_leds_open函数
*/
static int at91sam9260_leds_open(struct inode *inode, struct file *file)
{
    int i;
    
    for (i = 0; i <3; i++) {
        // 输出功能,输出高电平
        at91_set_gpio_output(led_table[i], led_cfg_table[i]);
    }
    return 0;
}

/* 应用程序对设备文件/dev/leds执行ioclt(...)时,
* 就会调用at91sam9260_leds_ioctl函数
*/
static int at91sam9260_leds_ioctl(
    struct inode *inode, 
    struct file *file, 
    unsigned int cmd, 
    unsigned long arg)
{
    if (arg > 3) {
        return -EINVAL;
    }
    
    switch(cmd) {
    case IOCTL_LED_ON:
        // 设置指定引脚的输出电平为0
        at91_set_gpio_output(led_table[arg], 0);
        return 0;

    case IOCTL_LED_OFF:
        // 设置指定引脚的输出电平为1
        at91_set_gpio_output(led_table[arg], 1);
        return 0;

    default:
        return -EINVAL;
    }
}

/* 这个结构是字符设备驱动程序的核心
* 当应用程序操作设备文件时所调用的open、read、write等函数,
* 最终会调用这个结构中指定的对应函数
*/
static struct file_operations at91sam9260_leds_fops = {
    .owner =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   at91sam9260_leds_open,     
    .ioctl =   at91sam9260_leds_ioctl,
};

/*
* 执行“insmod leds.ko”命令时就会调用这个函数
*/
static int __init at91sam9260_leds_init(void)
{
    int ret;

    /* 注册字符设备驱动程序
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为LED_MAJOR的设备文件时,就会调用at91sam9260_leds_fops中的相关成员函数
     * LED_MAJOR可以设为0,表示由内核自动分配主设备号
     */
    ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &at91sam9260_leds_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }
    
    printk(DEVICE_NAME " initialized\n");
    return 0;
}

/*
* 执行”rmmod leds.ko”命令时就会调用这个函数 
*/
static void __exit at91sam9260_leds_exit(void)
{
    /* 卸载驱动程序 */
    unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(at91sam9260_leds_init);
module_exit(at91sam9260_leds_exit);

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("STUDY");             // 驱动程序的作者
MODULE_DESCRIPTION("at91sam9260/at91sam9260 LED Driver");   // 一些描述信息
MODULE_LICENSE("GPL");                              // 遵循的协议

以下为Makefile:

CROSS=arm-linux-
AR =ar
ARCH=arm
CC=arm-linux-gcc
obj-m += led.o
KDIR:=/szwinet/kernel/linux-2.6.24/
PWD=$(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.o *.ko *.bak *.mod.c

make前面要空一个"Tab"

KDIR 为内核的路径,$(shell pwd)这个表示源代码所在的地方

linux下执行:make

生成led.ko

以下为测试代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define IOCTL_LED_ON    0
#define IOCTL_LED_OFF   1

void usage(char *exename)
{
    printf("Usage:\n");
    printf("    %s <led_no> <on/off>\n", exename);
    printf("    led_no = 1, 2, 3\r\n");
}

int main(int argc, char **argv)
{
    unsigned int led_no;
    int fd = -1;
    
    if (argc != 3)
        goto err;
        
    fd = open("/dev/leds", 0); // 打开设备
    if (fd < 0) {
        printf("Can't open /dev/leds\n");
        return -1;
    }

/*
  unsigned long strtoul( const char *nptr, char endptr, int base ) 
  把输入的字符串转换成数字。
  参数一 字符串的起始地址。
  参数二 返回字符串有效数字的结尾地址。如 123456ff789 则返回数字6的地址。
  参数三 转化基数。
*/
    led_no = strtoul(argv[1], 0, 0) - 1;    // 操作哪个LED?
    if (led_no > 3)
        goto err;
    
    if (!strcmp(argv[2], "on")) {
        ioctl(fd, IOCTL_LED_ON, led_no);    // 点亮它
    } else if (!strcmp(argv[2], "off")) {
        ioctl(fd, IOCTL_LED_OFF, led_no);   // 熄灭它
    } else {
        goto err;
    }
    
    close(fd);
    return 0;
    
err:
    if (fd > 0) 
        close(fd);
    usage(argv[0]);
    return -1;
}
测试代码的Makefile

CFLAGS = -O -Wall
CC      = arm-linux-gcc
LD      = arm-linux-ld
all:kd 
kd: led_test.c 
$(CC) $(CFLAGS) -o kd led_test.c

clean:
rm -f *.o *~ kd

把led.ko传到或是nfs到目标板.然后在这个led.ko同文件目录下mknod /dev/leds c 231 0 然后在生成的kd目标执行文件执行kd 1 on即点亮一灯,kd 2 off即灭一灯.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值