]> git.feebdaed.xyz Git - linuxyz.git/commitdiff
gpio isr tested
authorseantywork <seantywork@gmail.com>
Wed, 28 May 2025 13:09:47 +0000 (14:09 +0100)
committerseantywork <seantywork@gmail.com>
Wed, 28 May 2025 13:09:47 +0000 (14:09 +0100)
ksock-gpio/ins.conf [new file with mode: 0644]
ksock-gpio/ins.sh [new file with mode: 0755]
ksock-gpio/ksock_gpio.c

diff --git a/ksock-gpio/ins.conf b/ksock-gpio/ins.conf
new file mode 100644 (file)
index 0000000..e4b27ce
--- /dev/null
@@ -0,0 +1,2 @@
+CTLOUT=GPIO17
+CTLIN=GPIO27
\ No newline at end of file
diff --git a/ksock-gpio/ins.sh b/ksock-gpio/ins.sh
new file mode 100755 (executable)
index 0000000..f291406
--- /dev/null
@@ -0,0 +1,31 @@
+#!/bin/bash 
+
+source ./ins.conf
+
+CTLOUT_LINE=$(cat /sys/kernel/debug/gpio | grep $CTLOUT) 
+CTLIN_LINE=$(cat /sys/kernel/debug/gpio | grep $CTLIN)
+
+
+if [[ "$CTLOUT_LINE" == "" ]]
+then 
+    echo "couldn't find $CTLOUT"
+    exit 1
+fi
+
+if [[ "$CTLIN_LINE" == "" ]]
+then 
+    echo "couldn't find $CTLIN"
+    exit 1
+fi
+
+CTLOUT_LINE=$(echo $CTLOUT_LINE | cut -d "-" -f 2)
+CTLOUT_LINE=$(echo $CTLOUT_LINE | cut -d " " -f 1)
+
+CTLIN_LINE=$(echo $CTLIN_LINE | cut -d "-" -f 2)
+CTLIN_LINE=$(echo $CTLIN_LINE | cut -d " " -f 1)
+
+echo "CTL OUT: $CTLOUT = $CTLOUT_LINE"
+echo "CTL IN : $CTLIN = $CTLIN_LINE"
+
+insmod ksock_gpio.ko gpio_ctl_o=$CTLOUT_LINE gpio_ctl_i=$CTLIN_LINE
+
index ecbcf574ec0be645f3e525cb62eeace3e761abbd..f0f358298dea86570e1c550fcae1881e36a04a7e 100644 (file)
 #include <linux/string.h>
 #include <linux/gpio.h>
 #include <linux/interrupt.h>
+#include <linux/workqueue.h>
+#include <linux/sched.h>
+#include <linux/time.h>
+#include <linux/delay.h>
+
+static DECLARE_WAIT_QUEUE_HEAD(this_wq);
+
+static int condition = 0;
+
+static struct work_struct job;
+
+static int gpio_ctl_o;
+static int gpio_ctl_i;
+
+module_param(gpio_ctl_o, int, 0664);
+module_param(gpio_ctl_i, int, 0664);
+
+static unsigned int gpio_ctl_i_irq;
+
+static void job_handler(struct work_struct* work){
+
+
+    printk(KERN_INFO "waitqueue handler: %s\n", __FUNCTION__);
+
+    msleep(5000);
+
+       gpio_set_value(gpio_ctl_o, IRQF_TRIGGER_RISING);
+
+    printk(KERN_INFO "up after 5000ms\n");
+
+    condition = 1;
+
+    wake_up_interruptible(&this_wq);
+
+
+}
+
+static irqreturn_t gpio_irq_handler(int irq, void *dev_id) {
+       printk("sock gpio: interrupt was triggered and ISR was called\n");
+       return IRQ_HANDLED;
+}
 
 static int __init ksock_gpio_init(void) {
 
+       if(gpio_request(gpio_ctl_o, "gpio-ctl-o")) {
+               printk("sock gpio: can't allocate gpio_ctl_o: %d\n", gpio_ctl_o);
+               return -1;
+       }
+
+
+       if(gpio_request(gpio_ctl_i, "gpio-ctl-i")) {
+               printk("sock gpio: can't allocate gpio_ctl_i: %d\n", gpio_ctl_i);
+               gpio_free(gpio_ctl_o);
+               return -1;
+       }
+
+       if(gpio_direction_output(gpio_ctl_o, IRQF_TRIGGER_NONE)) {
+               printk("sock gpio: can't set gpio_ctl_o to output\n");
+               gpio_free(gpio_ctl_o);
+               gpio_free(gpio_ctl_i);
+               return -1;
+       }
+
+       if(gpio_direction_input(gpio_ctl_i)) {
+               printk("sock gpio: can't set gpio_ctl_i to input\n");
+               gpio_free(gpio_ctl_o);
+               gpio_free(gpio_ctl_i);
+               return -1;
+       }
+
+       gpio_ctl_i_irq = gpio_to_irq(gpio_ctl_i);
+
+       if(request_irq(gpio_ctl_i_irq, gpio_irq_handler, IRQF_TRIGGER_RISING, "gpio_ctl_i_irq", NULL) != 0) {
+               printk("sock gpio: can't request interrupt\n");
+               gpio_free(gpio_ctl_o);
+               gpio_free(gpio_ctl_i);
+               return -1;
+       }
+
+
+       printk("sock gpio: gpio_ctl_i to IRQ %d\n", gpio_ctl_i_irq);
+
+       printk("sock gpio: module is initialized into the kernel\n");
+
+    INIT_WORK(&job, job_handler);
+
+    schedule_work(&job);
+
+    printk(KERN_INFO "putting to sleep: %s\n", __FUNCTION__);
+
+    wait_event_interruptible(this_wq, condition != 0);
+
+    printk(KERN_INFO "woken up\n");
+
+
        return 0;
+
 }
 
 static void __exit ksock_gpio_exit(void) {
 
+       gpio_free(gpio_ctl_o);
+       gpio_free(gpio_ctl_i);
+       free_irq(gpio_ctl_i_irq, NULL);
+       printk("sock gpio: module is removed from the kernel\n");
 }
 
 module_init(ksock_gpio_init);