22071驱动day10

三个按键实现按键中断,key1-》led1 key2->led2 key3->led3

按键按一下灯亮,再按一下灯灭

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>

/*  myirq{
    interrupt-parent=<&gpiof>;
    interrupts=<7 0>,<8 0>,<9 0>;      
};
*/

//定义指向获取的设备树节点信息空间
struct device_node *node1;
struct device_node *node2;
struct gpio_desc * gpiono1;
struct gpio_desc * gpiono2;
struct gpio_desc * gpiono3;
int ret;
unsigned int irqno1;//用于接收软中断
unsigned int irqno2;
unsigned int irqno3;
unsigned int count=0;//用于计数,中断发生一次+1

//定义工作队列底半部对象
struct work_struct work1;
struct work_struct work2;
struct work_struct work3;

//工作队列底半部处理函数
void work_func1(struct work_struct *work)
{
    gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));
}

void work_func2(struct work_struct *work)
{
    gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
}

void work_func3(struct work_struct *work)
{
    gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));
}
//中断处理函数顶半部
irqreturn_t irq_handler1(int irqno,void *dev)
{
    count++;
    printk("key interrupt......%d..\n",count);
    //开启中断底半部
    schedule_work(&work1);
    return IRQ_HANDLED;
}
irqreturn_t irq_handler2(int irqno,void *dev)
{
    count++;
    printk("key interrupt......%d..\n",count);
    //开启中断底半部
    schedule_work(&work2);
    return IRQ_HANDLED;
}
irqreturn_t irq_handler3(int irqno,void *dev)
{
    count++;
    printk("key interrupt......%d..\n",count);
    //开启中断底半部
    schedule_work(&work3);
    return IRQ_HANDLED;
}

static int __init mycdev_init(void)
{  
    //对象初始化
    INIT_WORK(&work1,work_func1); 
    INIT_WORK(&work2,work_func2); 
    INIT_WORK(&work3,work_func3); 
    //通过名字获取设备树节点信息
    node1=of_find_node_by_name(NULL,"myirq");
    if(node1==NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析设备树节点\n");

    //通过名字获取设备树节点信息
    node2=of_find_node_by_name(NULL,"myleds");
    if(node2==NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析设备树节点\n");

    //获取申请gpio编号
    gpiono1=gpiod_get_from_of_node(node2,"myled1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono1))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono1);
    }
     gpiono2=gpiod_get_from_of_node(node2,"myled2",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono2))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono2);
    }
     gpiono3=gpiod_get_from_of_node(node2,"myled3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono3))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono3);
    }

    printk("获取gpio编号成功\n");
    //获取软中断号
    irqno1=irq_of_parse_and_map(node1,1);
    if(irqno1==0)
    {
        printk("获取软中断号失败\n");
        return EINVAL;
    }
    irqno2=irq_of_parse_and_map(node1,0);
    if(irqno2==0)
    {
        printk("获取软中断号失败\n");
        return EINVAL;
    }
    irqno3=irq_of_parse_and_map(node1,2);
    if(irqno3==0)
    {
        printk("获取软中断号失败\n");
        return EINVAL;
    }

    printk("获取软中断号成功\n");

    //将中断注册进内核
    ret=request_irq(irqno1,irq_handler1,IRQF_TRIGGER_FALLING,"key1_inte",NULL);
    if(ret)
    {
        printk("注册中断失败\n");
        return ret;
    }
    request_irq(irqno2,irq_handler2,IRQF_TRIGGER_FALLING,"key2_inte",NULL);
    request_irq(irqno3,irq_handler3,IRQF_TRIGGER_FALLING,"key3_inte",NULL);
    printk("注册中断成功\n"); 
    return 0;

}
static void __exit mycdev_exit(void)
{
    //中断注销
    free_irq(irqno1,NULL);
    free_irq(irqno2,NULL);
    free_irq(irqno3,NULL);

    gpiod_set_value(gpiono1,0);
    gpiod_set_value(gpiono2,0);
    gpiod_set_value(gpiono3,0);
    //释放
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是基于STM32E10系列单片机的DS3231驱动代码示例。 首先需要了解DS3231芯片的通信方式,DS3231采用I2C通信方式,需要连接SDA和SCL两个引脚。其中,SDA为数据线,SCL为时钟线。 以下是DS3231驱动代码示例: ```c #include "main.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "ds3231.h" #include "i2c.h" #define DS3231_ADDRESS 0xD0 // DS3231芯片地址 uint8_t DS3231_Write(uint8_t reg_address, uint8_t data) { uint8_t buf[2]; buf[0] = reg_address; buf[1] = data; return HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, buf, 2, 1000); } uint8_t DS3231_Read(uint8_t reg_address) { uint8_t data; HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, &reg_address, 1, 1000); HAL_I2C_Master_Receive(&hi2c1, DS3231_ADDRESS, &data, 1, 1000); return data; } void DS3231_Init(void) { // 设置时钟输出 DS3231_Write(DS3231_CONTROL_REG, DS3231_CONTROL_REG_SQWAVE_1HZ); // 清除报警标志 DS3231_Write(DS3231_STATUS_REG, 0x00); } void DS3231_SetTime(uint8_t hour, uint8_t minute, uint8_t second) { // 设置小时 uint8_t hour_bcd = (((hour / 10) << 4) | (hour % 10)); DS3231_Write(DS3231_HOUR_REG, hour_bcd); // 设置分钟 uint8_t minute_bcd = (((minute / 10) << 4) | (minute % 10)); DS3231_Write(DS3231_MINUTE_REG, minute_bcd); // 设置秒钟 uint8_t second_bcd = (((second / 10) << 4) | (second % 10)); DS3231_Write(DS3231_SECOND_REG, second_bcd); } void DS3231_GetTime(uint8_t *hour, uint8_t *minute, uint8_t *second) { // 获取小时 uint8_t hour_bcd = DS3231_Read(DS3231_HOUR_REG); *hour = ((hour_bcd >> 4) * 10 + (hour_bcd & 0x0F)); // 获取分钟 uint8_t minute_bcd = DS3231_Read(DS3231_MINUTE_REG); *minute = ((minute_bcd >> 4) * 10 + (minute_bcd & 0x0F)); // 获取秒钟 uint8_t second_bcd = DS3231_Read(DS3231_SECOND_REG); *second = ((second_bcd >> 4) * 10 + (second_bcd & 0x0F)); } void DS3231_SetDate(uint8_t year, uint8_t month, uint8_t day) { // 设置年份 uint8_t year_bcd = (((year / 10) << 4) | (year % 10)); DS3231_Write(DS3231_YEAR_REG, year_bcd); // 设置月份 uint8_t month_bcd = (((month / 10) << 4) | (month % 10)); DS3231_Write(DS3231_MONTH_REG, month_bcd); // 设置日期 uint8_t day_bcd = (((day / 10) << 4) | (day % 10)); DS3231_Write(DS3231_DAY_REG, day_bcd); } void DS3231_GetDate(uint8_t *year, uint8_t *month, uint8_t *day) { // 获取年份 uint8_t year_bcd = DS3231_Read(DS3231_YEAR_REG); *year = ((year_bcd >> 4) * 10 + (year_bcd & 0x0F)); // 获取月份 uint8_t month_bcd = DS3231_Read(DS3231_MONTH_REG); *month = ((month_bcd >> 4) * 10 + (month_bcd & 0x0F)); // 获取日期 uint8_t day_bcd = DS3231_Read(DS3231_DAY_REG); *day = ((day_bcd >> 4) * 10 + (day_bcd & 0x0F)); } ``` 上述代码中,`DS3231_Write`和`DS3231_Read`函数用于向DS3231芯片写入数据和读取数据。`DS3231_Init`函数用于初始化DS3231芯片。`DS3231_SetTime`和`DS3231_GetTime`函数用于设置和获取时间,其中时间以小时、分钟和秒钟表示。`DS3231_SetDate`和`DS3231_GetDate`函数用于设置和获取日期,其中日期以年、月和日表示。 希望这个示例代码可以帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值