{"id":278109,"date":"2023-07-22T04:04:36","date_gmt":"2023-07-21T20:04:36","guid":{"rendered":"https:\/\/lrxjmw.cn\/qdsmpkfdxxlc.html"},"modified":"2023-07-22T04:04:36","modified_gmt":"2023-07-21T20:04:36","slug":"qdsmpkfdxxlc","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/qdsmpkfdxxlc.html","title":{"rendered":"\u9a71\u52a8\u6811\u8393\u6d3e\u5f00\u53d1\u7684\u8be6\u7ec6\u6d41\u7a0b\u53ca\u64cd\u4f5c\u53e3"},"content":{"rendered":"
\u4e00\u3001\u9a71\u52a8\u8ba4\u77e51.1\u4e3a\u4f55\u8981\u5b66\u4e60\u5199\u9a71\u52a8<\/p>\n
\u84dd\u8393\u6d3e\u5f00\u53d1\u7b80\u5355\u662f\u7531\u4e8e\u6709\u5382\u5bb6\u63d0\u4f9b\u7684wiringPi\u5e93\uff0c\u5b9e\u73b0\u8d85\u58f0\u6ce2\uff0c\u5b9e\u73b0\u7194\u65ad\u5668\u64cd\u4f5c\uff0c\u505a\u706f\u7684\u7167\u4eae\u2026\u90fd\u5341\u5206\u7b80\u5355\u3002<\/p>\n
\u4f46\u672a\u6765\u505a\u5f00\u53d1\u65f6\uff0c\u4e0d\u4e00\u5b9a\u90fd\u662f\u7528\u8986\u76c6\u5b50\u6d3e\uff0c\u5219\u6ca1\u6709wiringPi\u5e93\u53ef\u4ee5\u7528\u3002\u4f46\u53ea\u8981\u80fd\u8fd0\u884cLinux<\/a>\uff0clinux\u7684\u6807\u51c6C\u5e93\u4e00\u5b9a\u6709\u3002<\/p>\n \u5b66\u4f1a\u4f9d\u7167\u6807\u51c6C\u5e93\u7f16\u64b0\u9a71\u52a8\uff0c\u53ea\u8981\u80fd\u9886\u5230linux\u5185\u6838\u6e90\u7801\uff0c\u9886\u5230\u82af\u7247\u6307\u5357\uff0c\u7535\u8def\u56fe\u2026\u5c31\u80fd\u505a\u5f00\u53d1\u3002<\/p>\n \u7528\u8986\u76c6\u5b50\u6d3e\u5b66\u4e60\u7684\u76ee\u7684\u9664\u4e86\u662f\u4e3a\u662f\u4f53\u9a8c\u5176\u5f3a\u608d\u65b9\u4fbf\u7684wiringPi\u5e93\uff0c\u66f4\u8981\u901a\u8fc7\u7315\u7334\u6843\u6d3e\u5b66\u4f1alinux\u5185\u6838\u5f00\u53d1\uff0c\u9a71\u52a8\u7f16\u64b0\u7b49\uff0c\u505a\u4e00\u4e2a\u5c5e\u4e8e\u81ea\u5df1\u7684\u5e93\u3002<\/p>\n 1.2\u6587\u4ef6\u540d\u4e0e\u8bbe\u5907\u53f7<\/p>\n linux\u4e00\u5207\u7686\u4e3a\u6587\u4ef6\uff0c\u5176\u8bbe\u5907\u7ba1\u7406\u540c\u6837\u662f\u548c\u6587\u4ef6\u7cfb\u7edf<\/a>\u7d27\u5bc6\u7ed3\u5408\u3002\u5728\u76ee\u5f55\/dev\u4e0b\u90fd\u80fd\u89c1\u5230\u952e\u76d8\uff0c\u9f20\u6807\uff0c\u5c4f\u5e55\uff0c\u5e76\u53e3\u7b49\u8bbe\u5907\u6587\u4ef6\uff0c\u786c\u4ef6\u8981\u6709\u76f8\u5bf9\u5e94\u7684\u9a71\u52a8\uff0c\u8fd9\u4e48open\u5982\u4f55\u5206\u8fa8\u8fd9\u79cd\u786c\u4ef6\u5462\uff1f<\/p>\n \u501f\u52a9\u6587\u4ef6\u540d\u4e0e\u8bbe\u5907\u53f7\u3002\u5728\/dev\u4e0bls-l\u53ef\u4ee5\u770b\u89c1<\/p>\n \u8bbe\u5907\u53f7\u53c8\u5206\u4e3a\uff1a\u4e3b\u8bbe\u5907\u53f7\u7528\u4e8e\u533a\u522b\u4e0d\u540c\u79cd\u7c7b\u7684\u8bbe\u5907\uff1b\u6b21\u8bbe\u5907\u53f7\u533a\u522b\u540c\u79cd\u7c7b\u578b\u7684\u591a\u4e2a\u8bbe\u5907\u3002<\/p>\n \u5185\u6838\u4e2d\u5b58\u5728\u4e00\u4e2a\u9a71\u52a8\u6570\u7ec4\uff0c\u7ba1\u7406\u6240\u6709\u8bbe\u5907\u7684\u9a71\u52a8\u3002\u9a71\u52a8\u5f00\u53d1\u65e0\u975e\u4ee5\u4e0b\u4e24\u4ef6\u4e8b\uff1a<\/p>\n \u7f16\u64b0\u5b8c\u9a71\u52a8\u7a0b\u5e8f\uff0c\u52a0\u8f7d\u5230\u5185\u6838<\/p>\n \u7528\u6237\u7a7a\u95f4open\u540e\uff0c\u8c03\u7528\u9a71\u52a8\u7a0b\u5e8f\uff08\u9a71\u52a8\u7a0b\u5e8f\u5c31\u662f\u64cd\u4f5c\u5bc4\u5b58\u5668\u6765\u9a71\u52a8IO\u53e3\uff0c\u5355\u7247\u673a51,32\u5c31\u662f\u8fd9\u4e9b\u64cd\u4f5c\uff09<\/p>\n \u9a71\u52a8\u63d2\u5165\u5230\u6570\u7ec4\u7684\u4f4d\u7f6e\uff08\u6b21\u5e8f\uff09\u7531\u8bbe\u5907\u53f7\u68c0\u7d22\u3002<\/p>\n 1.3open\u51fd\u6570\u6253\u901a\u4e0b\u5c42\u5230\u5e95\u5c42\u786c\u4ef6\u7684\u8be6\u5c3d\u8fc7\u7a0b<\/p>\n \u7528\u6237\u7a7a\u95f4\u8c03\u7528open\uff08\u4f8b\u5982open(\u201c\/dev\/pin4\u201d,O_RDWR)\uff09\u5f62\u6210\u4e00\u4e2a\u8f6f\u4e2d\u65ad\uff08\u4e2d\u65ad\u53f7\u662f0x80\uff09\uff0c\u6b65\u5165\u5185\u6838\u7a7a\u95f4\u8c03\u7528sys_call\uff0c\u8fd9\u4e2asys_call\u5728\u5185\u6838\u4e0a\u9762\u662f\u6c47\u7f16\u7684\uff0c\u7528SourceInsight\u641c\u7d22\u4e0d\u5230\u3002<\/p>\n sys_calll\u771f\u6b63\u8c03\u7528\u7684\u662fsys_open\uff08\u5c5e\u4e8eVFS\u5c42\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff0c\u7531\u4e8ec\u76d8\u7684\u5206\u533a\u548c\u5f15\u811a\u5206\u533a\u4e0d\u4e00\u6837\uff0c\u4e3a\u4e86\u5b9e\u73b0\u4e0b\u5c42\u7edf\u4e00\u5316\uff09,\u6309\u7167\u4f60\u7684\u8bbe\u5907\u540d\u4f8b\u5982pin4\u53bb\u5230\u5185\u6838\u7684\u9a71\u52a8\u6570\u7ec4\uff0c\u4f9d\u636e\u5176\u4e3b\u8bbe\u5907\u53f7\u4e0e\u6b21\u8bbe\u5907\u53f7\u627e\u5230\u76f8\u5173\u9a71\u52a8\u51fd\u6570\u3002<\/p>\n \u8c03\u7528\u9a71\u52a8\u51fd\u6570\u4e0a\u9762\u7684open\uff0c\u8fd9\u4e2aopen\u5c31\u662f\u5bf9\u5bc4\u5b58\u5668\u7684\u64cd\u4f5c\uff0c\u8fdb\u800c\u8bbe\u7f6eIO\u53e3\u5f15\u811a\u7535\u5e73\u3002\u8fd9\u4ef6\u4e8b\u5bf9\u4e8e\u5355\u7247\u673a\u6765\u8bf4\u7279\u53d8\u5bb9\u6613\uff0c\u5c31\u4e24\u53e5\u8bdd\u641e\u5b9a\uff1a<\/p>\n \n \u4e8c\u3001\u57fa\u4e8e\u6846\u67b6\u7f16\u64b0\u9a71\u52a8\u4ee3\u78012.1\u7f16\u64b0\u4e0b\u5c42\u5e94\u7528\u4ee3\u7801<\/p>\n \u76ee\u7684\u662f\u7528\u7b80\u5355\u7684\u53cd\u4f8b\u5c55\u793a\u4ece\u7528\u6237\u7a7a\u95f4\u5230\u5185\u6838\u7a7a\u95f4\u7684\u6574\u5957\u6d41\u7a0b\u3002<\/p>\n \u5728\u4e0b\u5c42\u8bbf\u95ee\u4e00\u4e2a\u8bbe\u5907\u8ddf\u8bbf\u95ee\u666e\u901a\u7684\u6587\u4ef6\u6ca1\u54ea\u4e9b\u533a\u522b\u3002\u8bd5\u5199\u4e00\u4e2a\u7b80\u5355\u7684open\u548cwrite\u53bb\u64cd\u4f5c\u8bbe\u5907\"pin4\"\u3002<\/p>\n \n \u6309\u7167\u524d\u9762\u63d0\u53ca\u7684\u9a71\u52a8\u8ba4\u77e5\uff0c\u6709\u4e2a\u5927\u81f4\u7684\u6982\u5ff5\uff0c\u4ee5open\u4e3a\u53cd\u4f8b\uff1a<\/p>\n \u4e0b\u5c42open\u2192sys_call\u2192sys_open\u2192\u5185\u6838\u9a71\u52a8\u6570\u7ec4\u8282\u70b9\u2192\u6267\u884c\u8282\u70b9\u91cc\u7684open<\/p>\n \u5176\u5b9e\uff0c\u6ca1\u6709\u88c5\u8f7d\u9a71\u52a8\u7684\u8bdd\u8fd9\u4e2a\u7a0b\u5e8f\u6267\u884c\u4e00\u5b9a\u4f1a\u62a5\u9519\u3002\u53ea\u6709\u5728\u5185\u6838\u88c5\u8f7d\u4e86\u9a71\u52a8\u4f46\u662f\u5728\/dev\u4e0b\u751f\u6210\u4e86\u201cpin4\u201d\u8fd9\u6837\u4e00\u4e2a\u8bbe\u5907\u80fd\u591f\u8fd0\u884c\u3002<\/p>\n \u63a5\u51fa\u6765\u4ecb\u7ecd\u6700\u7b80\u5355\u7684\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u6846\u67b6\u3002<\/p>\n 2.2\u66f4\u6539\u5185\u6838\u9a71\u52a8\u6846\u67b6\u4ee3\u7801<\/p>\n \u6240\u8c13\u6846\u67b6\uff0c\u5c31\u662f\u5728\u5f80\u9a71\u52a8\u6570\u7ec4\u4e0a\u9762\u52a0\u9a71\u52a8\u7684\u65f6\u4faf\u8981\u7b26\u5408\u5185\u6838\u89c4\u5219\uff0c\u5b83\u662f\u5b9a\u6b7b\u7684\u4e1c\u897f\uff0c\u57fa\u672c\u7684\u53e5\u5b50\u5fc5\u9700\u8981\u6709\uff0c\u5c11\u4e00\u4e2a\u90fd\u4e0d\u884c\u3002<\/p>\n \u5c3d\u7ba1\u6709\u5982\u6b64\u591a\u7684\u4ee3\u7801\uff0c\u4f46\u6838\u5fc3\u8fd0\u884c\u7684\u5c31\u4e24\u4e2aprintk\u3002<\/p>\n \nsbit pin4 =<\/span> P1^<\/span>4<\/span>;<\/span>\npin4=<\/span>1<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n
#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\n#<\/span>include<\/span> <\/span><\/span>\nint<\/span> main<\/span>(<\/span>)<\/span>\n{<\/span>\n\tint<\/span> fd;<\/span>\n\tfd =<\/span> open<\/span>(<\/span>\"\/dev\/pin4\"<\/span>,<\/span>O_RDWR)<\/span>;<\/span>\n\tif<\/span>(<\/span>fd <<\/span> 0<\/span>)<\/span>{<\/span>\n\t\tprintf<\/span>(<\/span>\"open failedn\"<\/span>)<\/span>;<\/span>\n\t\tperror<\/span>(<\/span>\"reson\"<\/span>)<\/span>;<\/span>\n\t}<\/span>else<\/span>{<\/span>\n\t\tprintf<\/span>(<\/span>\"open successn\"<\/span>)<\/span>;<\/span>\n\t}<\/span>\n\tfd =<\/span> write<\/span>(<\/span>fd,<\/span>'1'<\/span>,<\/span>1<\/span>)<\/span>;<\/span>\/\/\u5199\u4e00\u4e2a\u5b57\u7b26'1',\u5199\u4e00\u4e2a\u5b57\u8282<\/span>\n\treturn<\/span> 0<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n<\/p>\n
#<\/span>include<\/span> <\/span>\t\t \/\/file_operations\u58f0\u660e<\/span><\/span>\n#<\/span>include<\/span> <\/span> \/\/module_init module_exit\u58f0\u660e<\/span><\/span>\n#<\/span>include<\/span> <\/span> \/\/__init __exit \u5b8f\u5b9a\u4e49\u58f0\u660e<\/span><\/span>\n#<\/span>include<\/span> <\/span>\t \/\/class devise\u58f0\u660e<\/span><\/span>\n#<\/span>include<\/span> <\/span> \/\/copy_from_user \u7684\u5934\u6587\u4ef6<\/span><\/span>\n#<\/span>include<\/span> <\/span> \/\/\u8bbe\u5907\u53f7 dev_t \u7c7b\u578b\u58f0\u660e<\/span><\/span>\n#<\/span>include<\/span> <\/a><\/span> \/\/ioremap iounmap\u7684\u5934\u6587\u4ef6<\/span><\/span>\nstatic<\/span> struct<\/span> class<\/span> *<\/span>pin4_class;<\/span> \nstatic<\/span> struct<\/span> device<\/span> *<\/span>pin4_class_dev;<\/span>\nstatic<\/span> dev_t<\/span> devno;<\/span> \/\/\u8bbe\u5907\u53f7<\/span>\nstatic<\/span> int<\/span> major =<\/span>231<\/span>;<\/span> \t\t \/\/\u4e3b\u8bbe\u5907\u53f7<\/span>\nstatic<\/span> int<\/span> minor =<\/span>0<\/span>;<\/span>\t\t\t \/\/\u6b21\u8bbe\u5907\u53f7<\/span>\nstatic<\/span> char<\/span> *<\/span>module_name=<\/span>\"pin4\"<\/span>;<\/span> \/\/\u6a21\u5757\u540d \u4e0a\u5c42\u7684\u540d\u5b57<\/span>\n\/\/pin4_open\u51fd\u6570<\/span>\nstatic<\/span> int<\/span> pin4_open<\/span>(<\/span>struct<\/span> inode<\/span> *<\/span>inode,<\/span>struct<\/span> file<\/span> *<\/span>file)<\/span>\n{<\/span>\n printk<\/span>(<\/span>\"pin4_openn\"<\/span>)<\/span>;<\/span> \/\/\u5185\u6838\u7684\u6253\u5370\u51fd\u6570\uff0c\u548cprintf\u7c7b\u4f3c<\/span>\n \n return<\/span> 0<\/span>;<\/span>\n}<\/span>\n\/\/pin4_write\u51fd\u6570 \u56e0\u4e3a\u4e0a\u5c42\u9700\u8981open\u548cwrite\u8fd9\u4e24\u4e2a\u51fd\u6570 <\/span>\n\/\/ \u5982\u679c\u4e0a\u5c42\u9700\u8981\u8c03\u7528read\u7b49\u5176\u4ed6\u51fd\u6570\uff0c\u53ef\u7528SourceInsight\u53bb\u5185\u6838\u6e90\u7801\u641c\u7d22\uff0c\u7167\u7740\u683c\u5f0f\u4fee\u6539\u5373\u53ef\u4f7f\u7528 \u5728file_operations\u7ed3\u6784\u4f53\u91cc\u9762<\/span>\nstatic<\/span> ssize_t<\/span> pin4_write<\/span>(<\/span>struct<\/span> file<\/span> *<\/span>file1,<\/span>const<\/span> char<\/span> __user *<\/span>buf,<\/span>size_t<\/span> count,<\/span> loff_t<\/span> *<\/span>ppos)<\/span>\n{<\/span>\n\tprintk<\/span>(<\/span>\"pin4_writen\"<\/span>)<\/span>;<\/span>\n return<\/span> 0<\/span>;<\/span>\n}<\/span>\nstatic<\/span> struct<\/span> file_operations<\/span> pin4_fops =<\/span> {<\/span>\/\/\u5185\u6838\u5b9a\u4e49\u597d\u7684\u7ed3\u6784\u4f53 \u5185\u6838\u6e90\u7801\u91cc\u6709<\/span>\n \/\/\u5c31\u662f\u9a71\u52a8\u7684\u7ed3\u6784\u4f53 \u8981\u52a0\u8f7d\u5230\u5185\u6838\u9a71\u52a8\u94fe\u8868<\/span>\n .<\/span>owner =<\/span> THIS_MODULE,<\/span> \n .<\/span>open =<\/span> pin4_open,<\/span> \/\/\u4e0a\u5c42\u6709\u8bfb \u5e95\u5c42\u5c31\u8981\u6709open\u7684\u652f\u6301<\/span>\n .<\/span>write =<\/span> pin4_write,<\/span> \/\/\u4e0a\u5c42\u6709\u5199 \u5e95\u5c42\u5c31\u8981\u6709write\u7684\u652f\u6301<\/span>\n}<\/span>;<\/span>\nint<\/span> __init pin4_drv_init<\/span>(<\/span>void<\/span>)<\/span> \/\/\u9a71\u52a8\u7684\u771f\u6b63\u5165\u53e3<\/span>\n{<\/span>\n int<\/span> ret;<\/span>\n devno =<\/span> MKDEV<\/span>(<\/span>major,<\/span>minor)<\/span>;<\/span>\/\/\u521b\u5efa\u8bbe\u5907\u53f7 <\/span>\n \n \/\/********************\u6ce8\u518c\u9a71\u52a8 \u52a0\u8f7d\u5230\u5185\u6838\u9a71\u52a8\u94fe\u8868***********<\/span>\n \/\/\u4e3b\u8bbe\u5907\u53f7231 \u6a21\u5757\u540dpin4 \u4e0a\u9762\u7684\u7ed3\u6784\u4f53<\/span>\n ret =<\/span> register_chrdev<\/span>(<\/span>major,<\/span> module_name,<\/span>&<\/span>pin4_fops)<\/span>;<\/span> \/\/\u6ce8\u518c\u9a71\u52a8 \u544a\u8bc9\u5185\u6838\uff0c\u628a\u8fd9\u4e2a\u9a71\u52a8\u52a0\u5165\u5230\u5185\u6838\u9a71\u52a8\u7684\u94fe\u8868\u4e2d<\/span>\n pin4_class