{"id":4706,"date":"2021-07-21T04:03:01","date_gmt":"2021-07-20T19:03:01","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=4706"},"modified":"2021-07-21T04:03:38","modified_gmt":"2021-07-20T19:03:38","slug":"timer-%ec%8b%a4%ec%8a%b5","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/timer-%ec%8b%a4%ec%8a%b5\/","title":{"rendered":"timer \uc2e4\uc2b5"},"content":{"rendered":"\n<p>kernel timer\ub97c \uc2e4\uc2b5\ud588\ub2e4. callback \ud568\uc218 argrument\ub85c timer\ub97c \uac00\uc9c0\uace0 \uc788\ub294 \uad6c\uc870\uccb4 \ub370\uc774\ud130\ub97c \uc804\ub2ec\ud560 \uc218 \uc788\ub2e4. work queue\uc640 \uac19\uc740 \ubc29\uc2dd\uc774\ub2e4. \ucee4\ub110 \ud2b9\uc815 \ubc84\uc804\ubd80\ud130 \uc774\ub7f0 \uc2dd\uc73c\ub85c \ubcc0\uacbd\ub418\uc5c8\ub294 \ub4ef \ud558\ub2e4. \ud0c0\uc774\uba38\uac00 \ub9cc\ub8cc\ub418\uba74 \ub2e4\uc2dc \ub4f1\ub85d\ud558\ub3c4\ub85d \ud588\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;linux\/module.h>\n#include &lt;linux\/kernel.h>\n#include &lt;linux\/init.h>\n#include &lt;linux\/irq.h>\n#include &lt;linux\/interrupt.h>\n#include &lt;linux\/gpio.h>     \/\/GPIO\n\n\n#include &lt;linux\/cdev.h>\n#include &lt;linux\/device.h>\n#include &lt;linux\/uaccess.h>\n#include &lt;linux\/fcntl.h>\n\n#include &lt;linux\/timer.h>\n#include &lt;linux\/jiffies.h>\n\n#define GPIO_10_OUT (10)\n#define DEVICE_DATA_MAX 256\n\nunsigned int GPIO_irqNumber;\n\n\/*\\uc0ac\\uc6a9\\uc790 \\ub370\\uc774\\ud130 \\ubd80\\ubd84*\/\nstatic struct my_device_data{\n\tstruct cdev cdev;\n\tint index;\n\tchar my_string[DEVICE_DATA_MAX];\n\tstruct timer_list simple_timer;\n} my_data1;\n\n\nvoid timer_action_fn(struct timer_list *t);\n\nvoid timer_action_fn(struct timer_list *t)\n{\n\tstruct my_device_data *ptr1;\n\tpr_info(\"%ld: timer function was activated\\n\", jiffies);\n\tptr1=from_timer(ptr1, t, simple_timer);\n\tptr1->index++;\n\tpr_info(\"index is %d\\n\", ptr1->index);\n\tmod_timer(&amp;my_data1.simple_timer, jiffies+msecs_to_jiffies(1000));\n\n\n}\n\n\nstatic int init_timer(void){\n\ttimer_setup(&amp;my_data1.simple_timer, timer_action_fn, 0);\n\tpr_info(\"%ld: timer was setup\\n\", jiffies);\n\tmod_timer(&amp;my_data1.simple_timer, jiffies+msecs_to_jiffies(1000));\n\treturn 0;\n\n}\n\nstatic irqreturn_t gpio_irq_handler(int irq, void *dev_id)\n{\n\t\/*Scheduling work queue*\/\n\treturn IRQ_HANDLED;\n\n}\n\n\n\/\/device driver \\uc791\\uc131 \\ubd80\\ubd84.\n\/*************\\ub4dc\\ub77c\\uc774\\ubc84 \\ud568\\uc218 ******************\/\nstatic int mydriver_open(struct inode *inode, struct file *file);\nstatic int mydriver_release(struct inode *inode, struct file *file);\nstatic ssize_t mydriver_read(struct file *flip,\n\t\tchar *buf, size_t len, loff_t *off);\nstatic ssize_t mydriver_write(struct file *flip,\n\t\tconst char *buf, size_t len, loff_t *off);\n\/********************************************\/\n\n\n\n\/\/file operation structure\n\nstatic struct file_operations fops = \n{\n\t.owner = THIS_MODULE,\n\t.read = mydriver_read,\n\t.write = mydriver_write,\n\t.open = mydriver_open,\n\t.release = mydriver_release,\n};\n\nstatic int mydriver_open(struct inode *inode, struct file *file)\n{\n\tpr_info(\"Deviced file was opend.\\n\");\n\treturn 0;\n\n}\n\nstatic int mydriver_release(struct inode *inode, struct file *file)\n{\n\tpr_info(\"Deviced file was closed.\\n\");\n\treturn 0;\n}\n\n\nstatic int mydriver_read(struct file *file,\n\t\tchar *buf, size_t len, loff_t *off)\n{\n\tpr_info(\"read\\n\");\n\treturn 0;\n}\n\n\nstatic int mydriver_write(struct file *flip,\n\t\tconst char *buf, size_t len, loff_t *off)\n{\n\treturn 0;\n\n}\n\ndev_t dev = 0;\nstatic struct cdev my_cdev;\nstatic struct class *dev_class;\nstatic int __init init_hw(void)\n{\n\t\/\/\\ub514\\ubc14\\uc774\\uc2a4 \\ub4f1\\ub85d\n\tif(( alloc_chrdev_region(&amp;dev, 0, 1, \"test_device\") &lt; 0))\n\t{\n\t\tpr_err(\"[!]character device was not allocated\\n\");\n\t\tgoto r_unreg;\n\n\t}\n\tpr_info(\"[=]%d-%d, was allocated\\n\", MAJOR(dev), MINOR(dev));\n\n\n\t\/\/\\ucd08\\uae30\\ud654\n\tcdev_init(&amp;my_cdev, &amp;fops);\n\tpr_info(\"[=]driver was initialized\\n\");\n\n\n\t\/\/\\uc2dc\\uc2a4\\ud15c\\uc5d0 \\ucd94\\uac00\n\tif((cdev_add(&amp;my_cdev, dev, 1)) &lt; 0)\n\t{\n\t\tpr_err(\"[!]cannot add device to kernel\\n\");\n\t\tgoto r_del;\n\n\t}\n\n\n\t\/\/class \\ub9cc\\ub4e6.\n\tif((dev_class=class_create(THIS_MODULE, \"my_class\")) == NULL)\n\t{\n\t\tpr_err(\"[!]cannot add class\\n\");\n\t\tgoto r_class;\n\t}\n\n\n\tif((device_create(dev_class, NULL, dev, NULL, \"my_device\")) == NULL)\n\t{\n\n\t\tpr_err(\"[!]cannot create device\\n\");\n\t\tgoto r_device;\n\t}\n\n\n\t\/\/gpio 10\\ubc88\\uc744 \\uc0ac\\uc6a9.\n\t\/\/export\\ud558\\uc5ec \\uac04\\ub2e8\\ud788 \\uc0ac\\uc6a9.\n\t\/\/\\uc785\\ub825\\uc740 \\uac12\\uc744 \\uc368 \\ub123\\uc744 \\uc218 \\uc5c6\\uc74c. \\ucd9c\\ub825\\uc73c\\ub85c \\uc124\\uc815.\n\tGPIO_irqNumber = gpio_to_irq(GPIO_10_OUT);\n\tpr_info(\"[=]irq %d was assinged\\n\",GPIO_irqNumber);\n\n\t\/\/interrupt \\ub4f1\\ub85d \\ud544\\uc694\n\tif (request_irq(GPIO_irqNumber,\n\t\t\t\t(void*)gpio_irq_handler,\n\t\t\t\tIRQF_TRIGGER_RISING,\n\t\t\t\t\"my_device\",\n\t\t\t\tNULL))\n\t{\n\t\tpr_err(\"[!]my_device: cannot register IRQ\\n\");\n\t\tgoto r_gpio;\n\t}\n\tpr_info(\"[=]module was installed\\n\");\n\n\t\/\/timer setup\n\tinit_timer();\n\treturn 0;\nr_gpio:\n\tgpio_free(GPIO_10_OUT);\nr_device:\n\tdevice_destroy(dev_class,dev);\n\nr_class:\n\tclass_destroy(dev_class);\nr_del:\n\tcdev_del(&amp;my_cdev);\n\nr_unreg:\n\tunregister_chrdev_region(dev,1);\n\n\treturn -1;\n}\n\nstatic void __exit exit_hw(void) {\n\tfree_irq(GPIO_irqNumber, NULL);\n\tgpio_free(GPIO_10_OUT);\n\t\/\/flush_work(struct work_struct *work);\n\tdevice_destroy(dev_class,dev);\n\t\/\/class_unregister(dev_class);\n\tclass_destroy(dev_class);\n\tcdev_del(&amp;my_cdev);\n\tunregister_chrdev_region(dev,1);\n\t\/\/timer delete\n\tdel_timer(&amp;my_data1.simple_timer);\n\tprintk(KERN_INFO \"module was removed\\n\");\n}\n\n\nmodule_init(init_hw);\nmodule_exit(exit_hw);\nMODULE_LICENSE(\"GPL\");\nMODULE_AUTHOR(\"noname\");\nMODULE_DESCRIPTION(\"Hello, world!\");\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[146685.757495] [=]236-0, was allocated\n[146685.757504] [=]driver was initialized\n[146685.757960] [=]irq 54 was assinged\n[146685.757984] [=]module was installed\n[146685.757991] 14638325: timer was setup\n[146686.818385] 14638432: timer function was activated\n[146686.818398] index is 1\n[146687.858398] 14638536: timer function was activated\n[146687.858402] index is 2\n[146688.898415] 14638640: timer function was activated\n[146688.898428] index is 3\n[146689.938432] 14638744: timer function was activated\n[146689.938445] index is 4\n[146690.978452] 14638848: timer function was activated\n[146690.978457] index is 5\n[146692.018469] 14638952: timer function was activated\n[146692.018483] index is 6\n[146693.058494] 14639056: timer function was activated\n[146693.058500] index is 7\n[146694.098512] 14639160: timer function was activated\n[146694.098548] index is 8\n[146695.138529] 14639264: timer function was activated\n[146695.138563] index is 9\n[146696.178545] 14639368: timer function was activated\n[146696.178573] index is 10\n[146697.218575] 14639472: timer function was activated\n[146697.218607] index is 11\n[146698.258571] 14639576: timer function was activated\n[146698.258587] index is 12<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1080\" src=\"https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2021\/07\/2021-07-21-033444_1920x1080_scrot.png\" alt=\"\" class=\"wp-image-4708\" srcset=\"https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2021\/07\/2021-07-21-033444_1920x1080_scrot.png 1920w, https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2021\/07\/2021-07-21-033444_1920x1080_scrot-768x432.png 768w, https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2021\/07\/2021-07-21-033444_1920x1080_scrot-1536x864.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-c-2-p-labs wp-block-embed-c-2-p-labs\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"3gsMkNzidz\"><a href=\"https:\/\/c2plabs.com\/blog\/2019\/06\/09\/linux-kernel-timer-usage-example-simple-module\/\">Linux kernel timer usage example simple module<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Linux kernel timer usage example simple module&#8221; &#8212; C2P Labs\" src=\"https:\/\/c2plabs.com\/blog\/2019\/06\/09\/linux-kernel-timer-usage-example-simple-module\/embed\/#?secret=D4w5nMHIY9#?secret=3gsMkNzidz\" data-secret=\"3gsMkNzidz\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><a href=\"https:\/\/embetronicx.com\/tutorials\/linux\/device-drivers\/using-kernel-timer-in-linux-device-driver\/\">https:\/\/embetronicx.com\/tutorials\/linux\/device-drivers\/using-kernel-timer-in-linux-device-driver\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/14953871\/how-to-pass-custom-argument-to-linux-timers-function\">https:\/\/stackoverflow.com\/questions\/14953871\/how-to-pass-custom-argument-to-linux-timers-function<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>kernel timer\ub97c \uc2e4\uc2b5\ud588\ub2e4. callback \ud568\uc218 argrument\ub85c timer\ub97c \uac00\uc9c0\uace0 \uc788\ub294 \uad6c\uc870\uccb4 \ub370\uc774\ud130\ub97c \uc804\ub2ec\ud560 \uc218 \uc788\ub2e4. work queue\uc640 \uac19\uc740 \ubc29\uc2dd\uc774\ub2e4. \ucee4\ub110 \ud2b9\uc815 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12],"tags":[53,798,935,912],"class_list":["post-4706","post","type-post","status-publish","format-standard","hentry","category-12","tag-linux","tag-raspberry","tag-timer","tag-912"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/4706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=4706"}],"version-history":[{"count":2,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/4706\/revisions"}],"predecessor-version":[{"id":4709,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/4706\/revisions\/4709"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=4706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=4706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=4706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}