{"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

sbit pin4 =<\/span> P1^<\/span>4<\/span>;<\/span>\npin4=<\/span>1<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\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

#<\/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

\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

\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=<\/span>class_create<\/span>(<\/span>THIS_MODULE,<\/span>\"myfirstdemo\"<\/span>)<\/span>;<\/span>  \/\/\u7531\u4ee3\u7801\u5728\/dev\u4e0b\u81ea\u52a8\u751f\u6210\u8bbe\u5907  \u4e5f\u53ef\u4ee5\u624b\u52a8\u751f\u6210\u8bbe\u5907<\/span>\n    pin4_class_dev =<\/span>device_create<\/span>(<\/span>pin4_class,<\/span>NULL<\/span>,<\/span>devno,<\/span>NULL<\/span>,<\/span>module_name)<\/span>;<\/span>  \/\/\u521b\u5efa\u8bbe\u5907\u6587\u4ef6<\/span>\n \n    return<\/span> 0<\/span>;<\/span>\n}<\/span>\nvoid<\/span> __exit pin4_drv_exit<\/span>(<\/span>void<\/span>)<\/span>\n{<\/span>\n    device_destroy<\/span>(<\/span>pin4_class,<\/span>devno)<\/span>;<\/span>       \/\/\u5220\u9664\u8bbe\u5907  \/dev\u5e95\u4e0b\u7684 \u4e0a\u9762\u4e5f\u662f\u521b\u5efa\u4e86\u8bbe\u5907\u548c\u7c7b<\/span>\n    class_destroy<\/span>(<\/span>pin4_class)<\/span>;<\/span>              \/\/\u5220\u9664\u7c7b<\/span>\n    unregister_chrdev<\/span>(<\/span>major,<\/span> module_name)<\/span>;<\/span>  \/\/\u5378\u8f7d\u9a71\u52a8 \u5c31\u662f\u5220\u9664\u94fe\u8868\u8282\u70b9\u7684\u9a71\u52a8<\/span>\n}<\/span>\nmodule_init<\/span>(<\/span>pin4_drv_init)<\/span>;<\/span>  \/\/\u5165\u53e3:\u5185\u6838\u52a0\u8f7d\u9a71\u52a8\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u5b8f(module_init\u5b83\u4e0d\u662f\u4e2a\u51fd\u6570)\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u771f\u6b63\u7684\u9a71\u52a8\u5165\u53e3\u662f\u5b83\u91cc\u9762\u8c03\u7528\u7684\u51fd\u6570<\/span>\nmodule_exit<\/span>(<\/span>pin4_drv_exit)<\/span>;<\/span>\nMODULE_LICENSE<\/span>(<\/span>\"GPL v2\"<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

2.3\u90e8\u4efd\u4ee3\u7801\u5256\u67902.3.1static\u7684\u4f5c\u7528<\/p>\n

\u5185\u6838\u4ee3\u7801\u6570\u76ee\u5e9e\u5927\uff0c\u4e3a\u4e86\u907f\u514d\u4e0e\u5176\u4ed6\u7684\u6587\u4ef6\u6709\u53d8\u91cf\u547d\u540d\u51b2\u7a81\uff0cstatic\u9650\u5b9a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ec5\u4ec5\u53ea\u5728\u8fd9\u4e2a\u6587\u4ef6\u3002\u5185\u6838\u6e90\u7801\u4e0a\u9762\u8fd0\u7528\u4e86\u5927\u91cf\u7684static\uff0c\u7531\u4e8e\u5185\u6838\u6e90\u7801\u8bf8\u591a\uff0c\u4e00\u4e07\u4e94\u5343\u591a\u4e2aC\u6587\u4ef6\uff0c\u5f88\u5bb9\u6613\u5bfc\u81f4\u4ee3\u7801\u547d\u540d\u7684\u51b2\u7a81\u3002<\/p>\n

2.3.2\u7ed3\u6784\u4f53file_operations(\u6700\u7ec8\u52a0\u8f7d\u5230\u5185\u6838\u9a71\u52a8\u6570\u7ec4)<\/p>\n

\u5728SourceInsight\u4e2d\u67e5\u770b\u7ed3\u6784\u4f53file_operations,\u53ef\u4ee5\u53d1\u89c9\u597d\u591a\u7684\u51fd\u6570\u8868\u9488\uff08\u6307\u5411\u51fd\u6570\u7684\u8868\u9488\uff0c\u51fd\u6570\u5185\u8fdb\u884c\u4e00\u4e9b\u7a0b\u5e8f\u7684\u6267\u884c\uff09\uff0c\u8fd9\u79cd\u51fd\u6570\u540d\u8ddf\u7cfb\u7edf\u4e0b\u5c42\u5bf9\u6587\u4ef6\u7684\u64cd\u4f5c\u5dee\u4e0d\u591a\u3002\uff08read,write,llseek\uff09<\/p>\n

\u5047\u5982\u4e0b\u5c42\u60f3\u8981\u5b9e\u73b0read,\u5c31\u590d\u5236\u8fc7\u6765\uff0c\u6839\u636e\u683c\u5f0f\u6539\u4e00\u6539\u624d\u80fd\u4f7f\u7528\u3002<\/p>\n

\u4e0b\u5c42\u5bf9\u5e94\u5e95\u5c42\uff0c\u4e0b\u5c42\u60f3\u8981\u7528read\uff0c\u5e95\u5c42\u5c31\u8981\u6709read\u7684\u652f\u6301\u3002<\/p>\n

2.3.3\u81ea\u52a8\u751f\u6210\u8bbe\u5907<\/p>\n

\u6846\u67b6\u4e2d\u6709\u624b\u52a8\u751f\u6210\u8bbe\u5907\u7684\u4ee3\u7801\uff0c\u8fd9\u4e48\u81ea\u52a8\u751f\u6210\u8bbe\u5907\u662f\u600e\u6837\u6837\u7684\u5462\uff1f<\/p>\n

\u6b65\u5165\/dev\u76ee\u5f55\uff0c\u67e5\u770b\u5e2e\u52a9\u53ef\u6653\u5f97\u521b\u5efa\u89c4\u5219<\/p>\n

\u5185\u6838\u9a71\u52a8\u529b\u662f\u4ec0\u4e48\u610f\u601d_linux\u5185\u6838\u9a71\u52a8_\u5185\u6838\u9a71\u52a8\u7a0b\u5e8f<\/p>\n

sudomknod\u8bbe\u5907\u540d\u79f0\u8bbe\u5907\u7c7b\u578b\u4e3b\u8bbe\u5907\u53f7\u6b21\u8bbe\u5907\u53f7<\/p>\n

\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4<\/a>\u521b\u5efa\u540d\u79f0\u4e3azhu\uff0c\u4e3b\u8bbe\u5907\u53f7\u4e3a8\uff0c\u6b21\u8bbe\u5907\u53f7\u4e3a1\u7684\u5b57\u7b26\u8bbe\u5907\u3002<\/p>\n

\n

sudo mknod zhu c 8<\/span> 1<\/span>\n<\/code><\/pre>\n<\/p>\n

\u4e09\u3001\u9a71\u52a8\u4ee3\u7801\u7f16\u8bd1\u548c\u6d4b\u8bd53.1\u9a71\u52a8\u6846\u67b6\u7684\u6a21\u5757\u7f16\u8bd1\u5e76\u53d1\u9001\u81f3\u7315\u7334\u6843\u6d3e<\/p>\n

\u5728ubuntu\u4e2d\uff0c\u6b65\u5165Linux\u5185\u6838\u6e90\u7801\uff08\u524d\u4e00\u7ae0\u8282\u7f16\u8bd1\u597d\u7684\uff09\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u76ee\u5f55linux-rpi-4.14.y\/drivers\/char\uff08IO\u53e3\u5c5e\u4e8e\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\uff09\u3002\u6b65\u5165\u6e90\u7801\u76ee\u5f55\u4e0b\u7684\u7f18\u7531\u662f\uff0c\u5199\u9a71\u52a8\u5fc5\u9700\u8981\u94fe\u63a5\u5230\u6e90\u7801(\u6e90\u7801\u5b9a\u4e49\u597d\u4e86\u7ed3\u6784\u4f53\u7b49\u7b49)\uff0c\u5fc5\u9700\u8981\u6709\u6e90\u7801\u3002<\/p>\n

\u62f7\u8d1d\u4e0a\u6587\u5256\u6790\u8fc7\u7684\u9a71\u52a8\u6846\u67b6\u4ee3\u7801\uff0c\u9886\u5230\u8fd9\u4e2a\u6587\u4ef6\u5939\u4e0b\uff0c\u5e76\u521b\u5efa\u6210\u540d\u5b57\u4e3apin44driver2.c\u7684\u6587\u4ef6<\/p>\n

\u2460Makefile\u5185\u6dfb\u52a0\u751f\u6210.o\u547d\u4ee4<\/p>\n

\u8fdb\u884c\u914d\u7f6e\uff0c\u81f4\u4f7f\u5de5\u7a0b\u7f16\u8bd1\u65f6\u53ef\u4ee5\u7f16\u8bd1\u8fd9\u4e2a\u6587\u4ef6<\/p>\n

\n

vi Makefile\n<\/code><\/pre>\n<\/p>\n

\u5176\u5b9e\u4e0d\u4e00\u5b9a\u8981\u7f6e\u4e8e\/char\u4e0b\u3002\u4f46\u6ce8\u610f\uff1a\u7f6e\u4e8e\u90a3\u4e2a\u6587\u4ef6\u5939\u4e0b\uff0c\u5c31\u66f4\u6539\u90a3\u79cd\u6587\u4ef6\u5939\u7684Makefile\u5373\u53ef\u3002<\/p>\n

Makefile:<\/p>\n

\u6a21\u4eff\u90a3\u4e9b\u6587\u4ef6\u7684\u7f16\u8bd1\u65b9\u6cd5\uff0c\u4ee5\u7f16\u8bd1\u6210\u6a21\u5757\u7684\u65b9\u5f0f\uff08\u8fd8\u6709\u4e00\u4e2a\u65b9\u6cd5\u4e3a\u7f16\u8bd1\u8fdb\u5185\u6838\uff09\u7f16\u8bd1pin44driver2.c<\/p>\n

\u5728Makefile\u4e0a\u9762\u6dfb\u52a0\uff1a<\/p>\n

\n

obj-<\/span>m                           +=<\/span> pin44driver2.<\/span>o\n<\/code><\/pre>\n<\/p>\n

-m\u5c31\u662f\u6a21\u5757\u7684\u65b9\u5f0f<\/p>\n

\u5982\u56fe\uff1a<\/p>\n

\u2461\u6a21\u5757\u7f16\u8bd1\u751f\u6210.ko\u6587\u4ef6<\/p>\n

\u4e4b\u524d\u7f16\u8bd1\u5185\u6838\u955c\u50cf\u7684\u65f6\u4faf\u7528\u7684\u662f\u8fd9\u4e2a\u547d\u4ee4\uff1a<\/p>\n

\u73b0\u4eca\u53ea\u9700\u8fdb\u884c\u6a21\u5757\u7f16\u8bd1\uff0c\u4e0d\u987b\u8981\u751f\u6210zImage,dtbs\u6587\u4ef6;<\/p>\n

\u56de\u5230\u6e90\u7801\u76ee\u5f55\/linux-rpi-4.14.y\u518d\u6267\u884c\u4e0b\u8fb9\u6307\u4ee4<\/p>\n

\n

ARCH=<\/span>arm CROSS_COMPILE=<\/span>arm-<\/span>linux-<\/span>gnueabihf-<\/span> KERNEL=<\/span>kernel7 make modules\n<\/code><\/pre>\n<\/p>\n

\u6ce8\uff1a\u5047\u5982\u8bf4\u7f16\u8bd1\u4e2d\u9014\u63d0\u793a\u51fa\u9519\uff0c\u7167\u7740\u9519\u8bef\u63d0\u793a\u53bb\u66f4\u6539.c\u6587\u4ef6\u5373\u53ef\uff0c\u548c\u4e0b\u5c42\u7f16\u8bd1\u7c7b\u4f3c\u3002<\/p>\n

\u7f16\u8bd1\u5b8c\u6210\u751f\u6210\u7684\u4e00\u4e9b\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n

\u2462\u628a.ko\u6587\u4ef6\u53d1\u9001\u81f3\u7315\u7334\u6843\u6d3e<\/p>\n

\n

scp pin44driver2.<\/span>ko pi@192.168<\/span>.43<\/span>.44<\/span>:<\/span>\/<\/span>home\/<\/span>pi\n<\/code><\/pre>\n<\/p>\n

3.2\u4e0b\u5c42\u4ee3\u7801\u4ea4\u53c9\u7f16\u8bd1\u53d1\u9001\u81f3\u7315\u7334\u6843\u6d3e<\/p>\n

\u62f7\u8d1d\u4e0a\u6587\u5256\u6790\u7684\u4e0b\u5c42\u4ee3\u7801\u5230ubuntu\u4e2d\uff0c\u6b64\u5904\u6211\u547d\u540d\u4e3apin44test.c<\/p>\n

\u4f7f\u7528\u4ea4\u53c9\u7f16\u8bd1\u5de5\u5177\u8fdb\u884c\u7f16\u8bd1<\/p>\n

\n

arm-<\/span>linux-<\/span>gnueabihf-<\/span>gcc pin44test.<\/span>c -<\/span>o pin4test\n<\/code><\/pre>\n<\/p>\n

\u53d1\u9001\u81f3\u7315\u7334\u6843\u6d3e<\/p>\n

\n

scp pin4test pi@192.168<\/span>.43<\/span>.44<\/span>:<\/span>\/<\/span>home\/<\/span>pi\n<\/code><\/pre>\n<\/p>\n

3.3\u7315\u7334\u6843\u6d3e\u88c5\u8f7d\u9a71\u52a8\u5e76\u8fd0\u884c\u2460\u6811\u8393\u6d3e\u52a0\u8f7d\u5185\u6838\u9a71\u52a8(insmod)<\/p>\n

\n

sudo insmod pin44driver2.<\/span>ko\n<\/code><\/pre>\n<\/p>\n

\u67e5\u770b\u662f\u5426\u65e9\u5df2\u6210\u529f\u6dfb\u52a0\u9a71\u52a8<\/p>\n

\u53ef\u4ee5\u53bb\u8bbe\u5907\u4e0b\u67e5\u770b<\/p>\n

\n

ls \/<\/span>dev\/<\/span>pin4 -<\/span>l\n<\/code><\/pre>\n<\/p>\n

\u89c1\u5230\u9a71\u52a8\u6dfb\u52a0\u6210\u529f\uff0c\u4e3b\u8bbe\u5907\u53f7231\uff0c\u6b21\u8bbe\u5907\u53f70\uff0c\u548c\u5185\u6838\u4e0a\u9762\u7684\u4ee3\u7801\u5bf9\u5e94\u4e0a\u3002<\/p>\n

\u5185\u6838\u9a71\u52a8\u529b\u662f\u4ec0\u4e48\u610f\u601d_linux\u5185\u6838\u9a71\u52a8_\u5185\u6838\u9a71\u52a8\u7a0b\u5e8f<\/p>\n

\u6216\u5219lsmod\u67e5\u770b\u5185\u6838\u6302\u8f7d\u7684\u9a71\u52a8<\/p>\n

\u5047\u5982\u987b\u8981\u5378\u8f7d\u9a71\u52a8\uff0c\u5c31sudormmodpin44driver2<\/p>\n

\u2461\u8fd0\u884c\u4e0b\u5c42\u4ee3\u7801(\u65e0\u6743\u9650)<\/p>\n

\n

.<\/span>\/<\/span>pin4test\n<\/code><\/pre>\n<\/p>\n

\u53d1\u89c9\u6ca1\u6709\u5bf9\u8bbe\u5907pin4\u7684\u8bbf\u95ee\u6743\u9650<\/p>\n

crw\u662f\u8d85\u7ea7\u7528\u6237\u6240\u62e5\u6709\u7684\u6743\u9650\uff0c\u800c\u6846\u4e2d\u4e24\u7c7b\u7528\u6237\u5219\u65e0\u8bfb\u5199\u7684\u6743\u9650\uff08\u4e0b\u8fb9\u6709\u8be6\u5c3d\u8bf4\u660e\uff09<\/p>\n

\u2462\u589e\u52a0\u8bbf\u95ee\u6743\u9650\u518d\u8fd0\u884c<\/p>\n

\u89e3\u51b3\u65b9\u5f0f1\uff1a\u52a0\u8d85\u7ea7\u7528\u6237<\/p>\n

\n

sudo .<\/span>\/<\/span>pin4test\n<\/code><\/pre>\n<\/p>\n

\u89e3\u51b3\u65b9\u5f0f2\uff1a\u964d\u4f4e\u201c\u6240\u6709\u7528\u6237\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\u6743\u9650\u201d\uff08\u5efa\u8bae\uff09<\/p>\n

\n

sudo chmod 666<\/span> \/<\/span>dev\/<\/span>pin4\n<\/code><\/pre>\n<\/p>\n

\u62d3\u5c55>>chmod\u547d\u4ee4\u7528\u4e8e\u4fee\u6539\u6587\u4ef6\/\u6587\u4ef6\u5939\u7684\u5c5e\u6027\uff08\u8bfb\uff0c\u5199\uff0c\u6267\u884c\uff09<\/p>\n

\n

permission to:<\/span>  user<\/span>(<\/span>u)<\/span>   group<\/span>(<\/span>g)<\/span>   other<\/span>(<\/span>o)<\/span>     \n                \/<\/span>\u00af\u00af\u00af      \/<\/span>\u00af\u00af\u00af      \/<\/span>\u00af\u00af\u00af\noctal:<\/span>            6<\/span>          6<\/span>          6<\/span>\nbinary:<\/span>         1<\/span> 1<\/span> 0<\/span>      1<\/span> 1<\/span> 0<\/span>      1<\/span> 1<\/span> 0<\/span>\nwhat to permit:<\/span> r w x      r w x      r w x\nwhat to permit -<\/span> r:<\/span> read,<\/span> w:<\/span> write,<\/span> x:<\/span> execute\npermission to  -<\/span> user:<\/span> the owner that create the file\/<\/span>folder\n                 group:<\/span> the users from group that owner is member\n                 other:<\/span> all other users\n<\/code><\/pre>\n<\/p>\n

chmod744\u4ec5\u5bb9\u8bb8\u7528\u6237(\u6240\u6709\u8005)\u6267\u884c\u6240\u6709\u64cd\u4f5c\uff0c\u800c\u7ec4\u548c\u5176\u4ed6\u7528\u6237\u53ea\u5bb9\u8bb8\u8bfb\u3002<\/p>\n

\u2463\u68c0\u67e5\u662f\u5426\u6267\u884c\u6210\u529f\uff1ademsg\u6307\u4ee4\u67e5\u770b\u5185\u6838\u590d\u5370\u4fe1\u606f<\/p>\n

\u7528dmesg\u547d\u4ee4\u663e\u793a\u5185\u6838\u7f13\u51b2\u533a\u4fe1\u606f\uff0c\u5e76\u901a\u8fc7\u7ba1\u7ebf\u7b5b\u9009\u4e0epin4\u76f8\u5173\u4fe1\u606f<\/p>\n

\n

dmesg |<\/span> grep pin4\n<\/code><\/pre>\n<\/p>\n

\u53ef\u4ee5\u770b\u89c1\u8fd9\u4e24\u4e2a\u590d\u5370\u4fe1\u606f\uff0c\u8bf4\u660e\u5185\u6838\u7684printk\u65e9\u5df2\u88ab\u6210\u529f\u8c03\u7528\uff0c\u6211\u4eec\u65e9\u5df2\u6210\u529f\u5b8c\u6210\u4e86\u4e0b\u5c42\u5bf9\u5185\u6838\u7684\u8c03\u7528!<\/p>\n

\u56db\u3001\u4e09\u79cd\u5730\u5740\u4ecb\u7ecd<\/p>\n

\u5199\u9a71\u52a8\u662f\u4e3a\u4e86\u64cd\u4f5cIO\u53e3\uff0c\u5b9e\u73b0\u81ea\u5df1\u7684wiringpi\u5e93\uff0c\u8ddf\u786c\u4ef6\u6253\u4ea4\u9053\u3002<\/p>\n

\u9996\u5148\u8981\u7406\u89e3\u4ee5\u4e0b3\u4e2a\u5730\u5740\u7684\u6982\u5ff5\uff1a<\/p>\n

4.1\u603b\u7ebf\u5730\u5740<\/p>\n

\u6d45\u663e\u6765\u8bf4\uff1acpu\u624d\u80fd\u8bbf\u95ee\u7684\u663e\u5b58\u8303\u56f4<\/p>\n

\u73b0\u8c61\uff1a\u7b14\u8bb0\u672c\u88c5\u4e8632\u4f4d\uff08bit\uff09\u7684\u7cfb\u7edf\uff0c\u660e\u660e\u663e\u5b58\u6761\u67098G\uff0c\u5374\u53ea\u80fd\u8fa8\u8bc63.8G\u5de6\u53f3linux\u5185\u6838\u9a71\u52a8<\/strong>\uff0c\u8fd9\u662f\u7531\u4e8e32\u4f4d\u4ec5\u80fd\u8868\u793a\/\u8bbf\u95ee232=4,294,967,296bit=4,194,304Kb=4096Mb=4G\u5de6\u53f3\u3002\u53ea\u6709\u88c5\u4e8664\u4f4d\u7684\uff0c\u624d\u624d\u80fd\u8fa8\u8bc6\u52308G\u300232\u4f4d\u300164\u4f4d\u662f\u8ba1\u7b97\u673aCPU\u4e00\u6b21\u5904\u7406\u6570\u636e\u80fd\u529b\u7684\u5927\u5c0f\u3002<\/p>\n

\u7315\u7334\u6843\u6d3e\u88c5\u8f7d32\u4f4d\u64cd\u4f5c\u7cfb\u7edf\uff0c\u8f6e\u8be2\u81ea\u7136\u662f4G\u3002<\/p>\n

\u7315\u7334\u6843\u6d3e\u7684\u663e\u5b58\uff1a\u5927\u7ea6\u662f927M<\/p>\n

\n

cat \/<\/span>proc\/<\/span>meminfo\n<\/code><\/pre>\n<\/p>\n

4.2\u5316\u5b66\u5730\u5740<\/p>\n

\u786c\u4ef6\u5b9e\u9645\u5730\u5740\u6216\u7edd\u5bf9\u5730\u5740\uff0c\u5c31\u662f\u786c\u789f\u4e0a\u7684\u6392\u5217\u5730\u5740<\/p>\n

4.3\u865a\u62df\u5730\u5740<\/p>\n

\u53c8\u53eb\u903b\u8f91\u5730\u5740\uff08\u57fa\u4e8e\u7b97\u6cd5\u7684\u5730\u5740\uff0c\u8f6f\u4ef6\u5c42\u9762\u7684\u5730\u5740\uff0c\u662f\u5047\u5730\u5740\uff09\u4fbf\u79f0\u4e3a\u865a\u62df\u5730\u5740<\/p>\n

\u865a\u62df\u5730\u5740\u7684\u4f5c\u7528\uff1a<\/p>\n

\u4ee5\u7315\u7334\u6843\u6d3e\u4e3a\u4f8blinux \u7248\u672c\uff0c\u603b\u7ebf\u53ef\u4ee5\u8bbf\u95ee4G\uff0c\u5316\u5b66\u5730\u5740\u53ea\u67091G\uff0c\u4f46\u987b\u8981\u8fd0\u884c\u7684\u7a0b\u5e8f\u5c0f\u4e8e1G\uff0c\u5047\u5982\u628a\u7a0b\u5e8f\u5168\u90e8\u90fd\u52a0\u8f7d\u5230\u663e\u5b58\u662f\u4e0d\u53ef\u53d6\u7684\u3002<\/p>\n

\u5316\u5b66\u5730\u5740\u6570\u636e\u7684\u8fd0\u884c\u771f\u6b63\u662f\u62ff\u865a\u62df\u5730\u5740\u6765\u64cd\u4f5c\u7684\uff0c\u865a\u62df\u5730\u5740\u53ef\u4ee5\u6bd41G\u5927\uff0c\u603b\u7ebf\u5730\u5740\uff08CPU\u80fd\u8bbf\u95ee\u7684\u5730\u5740\u8303\u56f4\uff09\u80fd\u770b\u89c14\u4e2aG\uff0c\u5c31\u53ef\u4ee5\u628a1\u4e2aG\u7684\u6570\u5b66\u5730\u5740\u6620\u5c04\u62104\u4e2aG\u7684\u865a\u62df\u5730\u5740\u3002\u5f53\u5316\u5b66\u5730\u5740\u4e0d\u80fd\u6ee1\u8db3\u7a0b\u5e8f\u8fd0\u884c\u7a7a\u95f4\u9700\u6c42\u65f6\uff0c\u5047\u82e5\u6ca1\u6709\u865a\u62df\u5730\u5740\uff0c\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u6b63\u5e38\u8fd0\u884c\u3002\u5355\u7247\u673a51\u548cSTM32\u5047\u5982\u7a0b\u5e8f\u8fc7\u5927\uff0c\u662f\u4e25\u7981\u4f60\u70e7\u5f55\u7684\uff0c\u800c\u5728Linux\u7cfb\u7edf\u73af\u5883\u4e0b\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n

\u7315\u7334\u6843\u6d3e3b\u7684cpu\u673a\u578b\u662fBCM2835\uff0c\u5b83\u662fARM-cotexA53\u6784\u67b6<\/p>\n

4.4MMU\u663e\u5b58\u7ba1\u7406\u5355\u5143<\/p>\n

\u5730\u5740\u6846\u56fe<\/p>\n

\u53ef\u4ee5\u770b\u89c1\u603b\u7ebf\u5730\u5740\u4e3aFFFFFFFF\uff0c\u5373\u4e3a4G\uff1b<\/p>\n

\u5185\u6838\u7684\u9875\u8868\u6620\u5c04<\/p>\n

\u5316\u5b66\u5730\u5740\u76841M\u901a\u8fc7\u6620\u5c04\u6210\u4e3a4M\u7684\u865a\u62df\u5730\u5740\uff08\u6211\u4eec\u5199\u7684\u6240\u6709\u7684\u4ee3\u7801\u90fd\u662f\u5728\u64cd\u63a7\u865a\u62df\u5730\u5740\uff0c\u90fd\u662f\u5047\u7684\uff09\uff0c\u8fd9\u4e2d\u95f4\u6709\u4e2a\u8bbe\u8ba1\u7684\u7b97\u6cd5\u53eb\u9875\u8868\u3002<\/p>\n

\u8fd9\u4e2a\u8868\u51b3\u5b9a\u4e86\u8fd9\u4e2a4M\u88ab\u6620\u5c04\u5230\u865a\u62df\u663e\u5b58\u7684\u54ea\u4e00\u4e2a\u6bb5\uff0c\u901a\u8fc7MMU\u8fdb\u884c\u7ba1\u7406\u3002\u5355\u7247\u673a\u548cARM\u5904\u7406\u5668\u7684\u533a\u522b\u5c31\u662fARM\u6709MMU\uff08\u663e\u5b58\u7ba1\u7406\u5355\u5143\uff09\u548cCACHE\uff08\u9ad8\u901f\u7f13\u5b58\uff09\uff0c\u5982\u53f3\u56fe\u6240\u793a\uff1a<\/p>\n

\u4e94\u3001\u535a\u901aBCM2835\u7b2c\u516d\u7ae0IO\u53e3\u914d\u7f6e\u5bc4\u5b58\u56685\u30011GeneralPurposeI\/O(GPIO)\u84dd\u7b79\u80a1<\/p>\n

\u67e5\u770b\u82af\u7247\u6307\u5357\u7684\u76ee\u7684\u6027\u5f88\u5f3a\uff1a\u505a\u54ea\u4e00\u5757\u7684\u5f00\u53d1\uff0c\u5c31\u53ea\u770b\u90a3\u4e00\u5757linux \u5e38\u7528\u547d\u4ee4\uff0c\u5982\u4eca\u8981\u5f00\u53d1\u7684\u662fGPIO\uff0c\u719f\u6089\u63a7\u5236IO\u53e3\u7684\u5bc4\u5b58\u5668\u6700\u4e3a\u91cd\u8981\u3002<\/p>\n

\u5047\u5982\u770b\u5b8c\u8fd9\u90e8\u4efd\u7684\u6587\u6863\uff0c\u4f60\u5bf9\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\uff08\u524d\u9762\u6709\u89e3\u6790\uff09\u6709\u6e05\u6670\u7684\u7b54\u6848\uff0c\u8bf4\u660e\u4f60\u771f\u6b63\u770b\u61c2\u4e86\u8fd9\u4e00\u90e8\u4efd\u7684\u5f00\u53d1\u3002<\/p>\n

\u2460\u64cd\u4f5c\u903b\u8f91\uff1a\u7b80\u8a00\u4e4b\u5c31\u662f\u5982\u4f55\u8fdb\u884c\u914d\u7f6e\u76f8\u5173\u5bc4\u5b58\u5668\uff0c\u8fd9\u79cd\u914d\u7f6e\u6b65\u9aa4\u548c\u601d\u60f3\u867d\u7136\u90fd\u5f88\u7c7b\u4f3c\u3002<\/p>\n

\u2461\u9700\u8981\u91cd\u70b9\u628a\u63e1\u7684\u5bc4\u5b58\u5668\u6709\u4ec0\u4e48\uff1f\u8bf8\u5982\u8f93\u5165\/\u8f93\u51fa\u63a7\u5236\u5bc4\u5b58\u5668\uff0c\u8f93\u51fa0\/1\u63a7\u5236\u5bc4\u5b58\u5668\uff0c\u6d88\u9664\u72b6\u6001\u5bc4\u5b58\u5668<\/p>\n

5.2\u3001RegisterView\u5bfc\u8bfb<\/p>\n

\u5728\u65b0\u7684\u5e73\u53f0\u4e5f\u8981\u5b66\u4f1a\u6355\u6349\u7c7b\u4f3c\u7684\u5173\u952e\u4fe1\u606f\uff1a\u9009\u62e9\u8f93\u5165\u8fd8\u662f\u8f93\u51fa\uff0c0\/1,\u5982\u4f55\u6d88\u9664\uff0c\u4e0a\u5347\u6cbf\u589e\u957f\u6cbf\u7b49\u3002\uff08\u914d\u7f6e\u8fc732\/51\u5bc4\u5b58\u5668\u7684\u5e94\u5f53\u5bf9\u90a3\u4e9b\u5f88\u719f\u6089\uff09<\/p>\n

\u4ece\u53f3\u56fe\u4e2d\u53ef\u4ee5\u5927\u7ea6\u4e86\u89e3\u5230\u6240\u6709\u7684IO\u53e3\u88ab\u5206\u6210\u4e860~5\u7ec4\u3002<\/p>\n

\u6709\u610f\u601d\u7684\u662f\uff0c\u53f3\u56fe\u6700\u7b2c\u4e00\u5217\u7684\u5730\u5740Address\u662f\u7315\u7334\u6843\u6d3e\u603b\u7ebf\u5730\u5740\uff0c\u901a\u5e38\u82af\u7247\u6307\u5357\u7ed9\u7684\u90fd\u662f\u771f\u6b63\u7684\u6570\u5b66\u5730\u5740\u3002\u7b2c\u4e8c\u5217\u662f\u5bc4\u5b58\u5668\u7684\u540d\u5b50\uff0c\u7b2c\u4e09\u5217\u5bc4\u5b58\u5668\u529f\u80fd\u63cf\u8ff0\u3002<\/p>\n

\u4e00\u5171\u670941\u4e2a\u5bc4\u5b58\u5668\uff0c\u6bcf\u4f4d\u5bc4\u5b58\u5668\u90fd\u662f32\u4f4d\u3002<\/p>\n

5.1.3\u914d\u7f6e\u5f15\u811a\u529f\u80fd\u4e3a\u8f93\u5165\/\u8f93\u51fa\u7684\u5bc4\u5b58\u5668<\/p>\n

\u8fd920~29\u7684IO\u53e3\uff08\u7b2c\u4e8c\u5217\uff09\u5c5e\u4e8e\u5206\u7ec42<\/p>\n

IO\u7f16\u53f7\u8981\u770b\u597d<\/p>\n

5.1.4\u914d\u7f6e\u5f15\u811a\u8f93\u51fa0\/1\u7684\u5bc4\u5b58\u5668<\/p>\n

5.1.5\u914d\u7f6e\u5f15\u811a\u53bb\u96640\/1\u72b6\u6001\u7684\u5bc4\u5b58\u5668<\/p>\n

\u6574\u7406\u5173\u952e\u5185\u5bb9<\/p>\n

\u901a\u8fc7\u6587\u6863\u9605\u8bfb\uff0c\u53ef\u4ee5\u6574\u7406\u51fa\u5173\u952e\u7684\u4fe1\u606f\uff1a<\/p>\n

\u67093\u4e2a\u6700\u57fa\u672c\u7684\u8981\u6e05\u695a\uff1a<\/p>\n

\u2460\u9009\u62e9IO\u662f\u8f93\u5165\/\u8f93\u51fa\u63a7\u5236\u5bc4\u5b58\u5668\uff1aGPFSEL<\/p>\n

\u2461\u8f93\u51fa0\/1\u5bc4\u5b58\u5668\uff1aGPSET<\/p>\n

\u2462\u6e05\u9664\u5bc4\u5b58\u5668:GPCLR<\/p>\n

\u64cd\u4f5c\u903b\u8f91:<\/p>\n

\u4ee5GPFSEL0\u5bc4\u5b58\u5668\u4e3e\u4f8b\uff0c\u5f15\u811apin4\u5bf9\u5e94\u7684\u5206\u7ec4\u5c31\u662f\u7b2c0\u7ec4\uff0851\u5355\u7247\u673a\u5f15\u811a\u4e5f\u662f\u5206\u6210\u7b2c0\u7ec4\u3001\u7b2c1\u7ec4\u3001\u7b2c2\u7ec4\u3001\u7b2c3\u7ec4\uff09\u3002\u53ea\u8981\u5728\u8fd9\u4e2a\u5206\u7ec4\u4e0b\uff0c\u628a14-12\u4f4d\u8bbe\u7f6e\u4e3a001\uff0c\u624d\u80fd\u914d\u7f6epin4\u5f15\u811a\u4e3a\u8f93\u51fa\u3002<\/p>\n

\u516d\u3001\u5bc4\u5b58\u5668\u5730\u5740\u914d\u7f6e(ioremap\u3001volatile\u5316\u5b66\u5730\u5740\u6620\u5c04\u6210\u865a\u62df\u5730\u5740)\u2460\u5728\u539f\u5148\u6846\u67b6\u7684\u57fa\u7840\u4e0a\uff0c\u6dfb\u52a0\u5bc4\u5b58\u5668\u7684\u5b9a\u4e49<\/p>\n

\n

volatile<\/span> unsigned<\/span> int<\/span>*<\/span> GPFSEL0 =<\/span> NULL<\/span>;<\/span>\nvolatile<\/span> unsigned<\/span> int<\/span>*<\/span> GPSET0  =<\/span> NULL<\/span>;<\/span>\nvolatile<\/span> unsigned<\/span> int<\/span>*<\/span> GPCLR0  =<\/span> NULL<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

\u8981\u60f3\u5199\u51fa\u91cc\u9762\u7684\u4ee3\u7801\uff0c\u8981\u628a\u63e1\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n

\u5f04\u6e05\u695a\u5bc4\u5b58\u5668\u7684\u5206\u7ec4<\/p>\n

\u5176\u4e2d\u5bc4\u5b58\u5668\u76840\u8868\u793a\u7684\u662f\u5206\u7ec4\uff0c\u76ee\u6807\u64cd\u4f5c\u7684IO\u662fpin4,\u7531\u6587\u6863\u53ef\u77e5\uff0c\u5c5e\u4e8e\u5bc4\u5b58\u5668\u5206\u7ec40\u3002<\/p>\n

volatile\u7684\u4f7f\u7528<\/p>\n

\u52a0volatile\u4f5c\u7528\u662f\uff1a1\u3001\u9632\u6b62\u7f16\u8bd1\u5668\u4f18\u5316\uff08\u4f60\u7ed9\u7684\u8fd9\u4e2a\u5730\u5740\u7f16\u8bd1\u5668\u53ef\u80fd\u89c9\u5f97\u4e0d\u597d\uff0c\u53ef\u80fd\u4f1a\u7701\u7565\uff0c\u4e5f\u53ef\u80fd\u4f1a\u8fdb\u884c\u4fee\u6539\uff09\u8fd9\u79cd\u5bc4\u5b58\u5668\u53d8\u91cf\uff1b2\u3001\u8981\u6c42\u6bcf\u6b21\u76f4\u63a5\u4ece\u5bc4\u5b58\u5668\u91cc\u8bfb\u503c\u3002\u56e0\u4e3a\u968f\u7740\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u4f1a\u6539\u53d8\u5bc4\u5b58\u5668\u5f53\u4e2d\u7684\u6570\u636elinux\u5185\u6838\u9a71\u52a8<\/strong>\uff0c\u800c\u8bfb\u53d6\u7684\u90fd\u662f\u663e\u5b58\u4e0a\u9762\u7684\u5907\u4efd\u6570\u636e\uff0c\u6570\u636e\u7684\u65f6\u6548\u6027\u6ca1\u6709\u8fd9\u4e48\u5f3a\uff0c\u8bfb\u7684\u53ef\u80fd\u662f\u4e00\u4e2a\u8001\u6570\u636e\u3002\u5728\u5185\u6838\u4e2d\u5bf9IO\u53e3\u8fdb\u884c\u64cd\u4f5c\u90fd\u8981\u6709volatile\u3002<\/p>\n

\u2461\u914d\u7f6e\u5bc4\u5b58\u5668\u7684\u5730\u5740<\/p>\n

\u5728\u2460\u7684\u57fa\u7840\u4e0a\uff0c\u5728\u9a71\u52a8\u7684\u521d\u59cb\u5316pin4_drv_init\u4e2d\u6dfb\u52a0\u5bc4\u5b58\u5668\u5730\u5740\u914d\u7f6e<\/p>\n

\n

GPFSEL0 =<\/span> (<\/span>volatile<\/span> unsigned<\/span> int<\/span> *<\/span>)<\/span>ioremap<\/span>(<\/span>0x3f200000<\/span>,<\/span>4<\/span>)<\/span>;<\/span>\nGPSET0  =<\/span> (<\/span>volatile<\/span> unsigned<\/span> int<\/span> *<\/span>)<\/span>ioremap<\/span>(<\/span>0x3f20001C<\/span>,<\/span>4<\/span>)<\/span>;<\/span>\nGPCLR0  =<\/span> (<\/span>volatile<\/span> unsigned<\/span> int<\/span> *<\/span>)<\/span>ioremap<\/span>(<\/span>0x3f200028<\/span>,<\/span>4<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

\u8981\u60f3\u5199\u51fa\u91cc\u9762\u7684\u4ee3\u7801\uff0c\u8981\u628a\u63e1\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n

\u5206\u522b\u627e\u5230\u51e0\u4e2aIO\u5bc4\u5b58\u5668\u7684\u6570\u5b66\u5730\u5740<\/p>\n

\u5f04\u6e05\u695aGPIO\u7684\u6570\u5b66\u5730\u5740\uff08\u771f\u5b9e\u5730\u5740\uff09<\/p>\n

\u5e76\u4e0d\u662f\u7528\u53f3\u56fe\u8fd9\u4e2a\u5730\u5740\u6765\u5bf9\u5e94GPIO\u529f\u80fd\u9009\u62e9\u5bc4\u5b58\u5668\uff10\u7684\u5730\u5740\uff0c\u5426\u5219\u7f16\u8bd1\u540e\u8fd0\u884c\u4f1a\u6709\u6bb5\u9519\u8bef<\/p>\n

IO\u53e3\u7684\u8d77\u59cb\u5730\u5740\u662f0x3f000000\uff08\u4ece\u7f51\u4e0a\u627e\u7684\uff0c\u7315\u7334\u6843\u6d3e\u6307\u5357\u7b2c\u4e00\u5217\u662f\u603b\u7ebf\u5730\u5740\uff09,\u52a0\u4e0aGPIO\u7684\u504f\u659c\u91cf0x200000,\u6240\u4ee5GPIO\u7684\u5b9e\u9645\u5316\u5b66\u5730\u5740\u5e94\u5f53\u662f\u4ece0x3f200000\u5f00\u59cb\u7684\uff0c\u4e4b\u540e\u5728\u8fd9\u4e2a\u57fa\u7840\u4e0a\u8fdb\u884cLinux\u7cfb\u7edf\u7684MMU\u663e\u5b58\u865a\u62df\u5316\u7ba1\u7406\uff0c\u6620\u5c04\u5230\u865a\u62df\u5730\u5740\u4e0a\uff0c\u7f16\u7a0b\u90fd\u662f\u64cd\u4f5c\u865a\u62df\u5730\u5740\u3002<\/p>\n

\u7ee7\u7eed\u6cbf\u7740\u64cd\u4f5c\u6307\u5357\u627e\u5230GPSET0\u548cGPCLR0\u5bc4\u5b58\u5668\u7684\u504f\u659c\u91cf\uff0c\u89c1\u53f3\u56fe\u3002\u867d\u7136\u5bc4\u5b58\u5668\u7684\u540d\u5b50\u662f\u4eba\u4e3a\u7684\u4f9d\u636e\u529f\u80fd\u547d\u540d\u7684\uff0c\u672c\u8d28\u662f\u4e00\u4e32\u5ba2\u89c2\u5b58\u5728\u7684\u5316\u5b66\u5730\u5740\u3002<\/p>\n

\u975e\u5e38\u6ce8\u610f\uff1aBCM2708\u548cBCM2709IO\u8d77\u59cb\u5730\u5740\u4e0d\u540c\uff0cBCM2708\u662f0x20000000,BCM2709\u662f0x3f000000<\/p>\n

\u6309\u7167\u504f\u659c\u503c\uff0c\u5f04\u6e05\u695a\u5bc4\u5b58\u5668\u7684\u5316\u5b66\u5730\u5740\uff08\u771f\u5b9e\u5730\u5740\uff09<\/p>\n

\u540c\u6837\u7684\u65b9\u5f0f\uff0c\u5bc4\u5b58\u5668GPCLR0\u7684\u504f\u659c\u503c\u4e3a28\u3002<\/p>\n

\u5316\u5b66\u5730\u5740\u8f6c\u6362\u4e3a\u865a\u62df\u5730\u5740\uff1aioremap\u51fd\u6570<\/p>\n

\u7531\u4e8e\u65e0\u8bba\u5185\u6838\u4ee3\u7801\u8fd8\u662f\u4e0b\u5c42\u4ee3\u7801\u64cd\u4f5c\u7684\u90fd\u662f\u865a\u62df\u5730\u5740\uff0c\u4ee3\u7801\u4e2d\u76f4\u63a5\u7528\u5316\u5b66\u5730\u5740\u80af\u5b9a\u4e0d\u884c\uff0c\u987b\u8981\u8fdb\u884c\u8f6c\u6362,\u5c06IO\u53e3\u5bc4\u5b58\u5668\u6620\u5c04\u6210\u666e\u901a\u663e\u5b58\u5355\u5143\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n

\u4f7f\u7528\u51fd\u6570ioremap:<\/p>\n

\u51fd\u6570\u539f\u578b\uff1avoid*ioremap(unsignedlongphys_addr,unsignedlongsize)<\/p>\n

phys_addr\uff1a\u8981\u6620\u5c04\u7684\u5316\u5b66\u5730\u5740\u7684\u57fa\u5730\u5740\uff1b<\/p>\n

size\uff1a\u8981\u6620\u5c04\u7684\u7a7a\u95f4\u7684\u5927\u5c0f\uff08\u4e00\u4e2a\u5bc4\u5b58\u56684\u4e2a\u5b57\u8282\uff09\uff1b<\/p>\n

\u4e03\u3001\u5bc4\u5b58\u5668\u529f\u80fd\u914d\u7f6e\u2460\u5728\u51fd\u6570pin4_open\u4e2d\u914d\u7f6epin4\u4e3a\u8f93\u51fa\u5f15\u811a<\/p>\n

\u53ea\u898132\u4f4d\u5bc4\u5b58\u5668GPFSEL0\u768414-12\u4f4d\u914d\u7f6e\u4e3a001\uff0c\u5176\u5b83\u4f4d\u4e0d\u7ba1\uff0c\u5373\u53ef\u914d\u7f6epin4\u4e3a\u8f93\u51fa\u5f15\u811a<\/p>\n

\u5176\u5b9e\u76f4\u63a5\u66b4\u529b\u5f62\u53c2\uff080000\u2026001\u20260000\uff09\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u4f1a\u628a\u5176\u4ed6\u7684IO\u53e3\u7ed9\u5f71\u54cd\u3002\u6700\u597d\u7684\u7ed3\u679c\u662f\u53ea\u6539\u53d8\u4e8614-12\u4f4d\u3002<\/p>\n

\u8fd0\u7528\u4e0e\uff08&\uff09\/\u6216\uff08|\uff09\u8fd0\u7b97\u8fdb\u884c\u4f4d\u64cd\u4f5c<\/p>\n

\n

*<\/span>GPFSEL0 &=<\/span> ~<\/span>(<\/span>0x6<\/span> <<<\/span> 12<\/span>)<\/span>;<\/span>\/\/110\u5de6\u79fb12\u4f4d \u53d6\u53cd \u4e0e\u8fd0\u7b97<\/span>\n*<\/span>GPFSEL0 |=<\/span> (<\/span>0x1<\/span> <<<\/span> 12<\/span>)<\/span>;<\/span> \/\/001\u5de6\u79fb12\u4f4d      \u6216\u8fd0\u7b97<\/span>\n<\/code><\/pre>\n<\/p>\n

\u60f3\u8981\u5199\u51fa\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5fc5\u987b\u6e05\u695a\u4e0b\u8fb9\u4e24\u4e2a\u6b65\u9aa4<\/p>\n

1)\u3001\u4e0e\u8fd0\u7b97\u7ed9\u6307\u5b9a\u4f4d\uff0814bit\u300113bit\uff09\u5f62\u53c20\uff0c\u5176\u4ed6\u4e0d\u53d8<\/p>\n

\u4e3a\u4e86\u4fbf\u6377\u63cf\u8ff0\uff0c\u8fd9\u513f\u628a\u987b\u8981\u201c\u4e0e\u201d\u4e0a\u7684\u6570\u79f0\u4e3a\u201c\u8f85\u52a9\u6570\u201d\u3002\uff08\u5bc4\u5b58\u5668\u4e2d\u7684\u6570\u662f\u5047\u5b9a\u7684\uff09<\/p>\n

\u4f46\u4e3a\u4e86\u4fbf\u6377\uff081\u8d8a\u5c11\uff0c\u7528\u4f30\u7b97\u5668\u6362\u7b97\u5c31\u8d8a\u7b80\u5355\uff09\u5f97\u5230\u8fd9\u4e2a\u7b2c13\u300114\u4f4d\u4e3a0\u7684\u6570\uff0c\u9009\u62e9\u5bf9\u8f85\u52a9\u6570\u201c\u53d6\u4e24\u6b21\u53cd\u201c\u3002<\/p>\n

\u7b2c\u4e00\u6b21\u53d6\u53cd\u4e3a\uff1a00000\u2026110\u202600000<\/p>\n

\u7528\u4f30\u7b97\u5668\u5728\u4e8c\u8865\u7801BIN\u4e2d\u8f93\u5165110\uff08\u4fbf\u6377\u5c31\u5728\u8fd9\uff0c\u4f60\u8981\u662f\u76f4\u63a5\u5728\u4f30\u7b97\u5668\u4e2d\u8f93\u5165\u76ee\u6807\u8f85\u52a9\u6570\u8fdb\u884c\u6362\u7b97\uff0c\u6570\u6709\u591a\u5c11\u4e2a1\u90fd\u5f88\u8d39\u529b\uff01\uff01\uff09<\/p>\n

0110,\u5411\u5de6\u79fb12\u4f4d\uff0c\u9ad8\u4f4d\u624b\u52a8\u88650\uff0c\u521911\u6070\u597d\u5bf9\u4e0a14\u300113\u4f4d\u3002<\/p>\n

\u518d\u53d6\u53cd\uff08~\uff09\uff0c\u5f97\u5230\u4e00\u5f00\u59cb\u60f3\u8981\u7684\u8ba9\u5bc4\u5b58\u5668\u7684\u657014\u300113\u4f4d\u4e0e\u4e0a0\u7684\u8f85\u52a9\u6570\u3002<\/p>\n

2)\u3001\u6216\u8fd0\u7b97\u7ed9\u6307\u5b9a\u4f4d\uff0812bit\uff09\u5f62\u53c21<\/p>\n

\u2461\u5728\u51fd\u6570pin4_write\u4e2d\u914d\u7f6epin4\u8f93\u51fa0\/1\uff08\u9ad8\u4f4e\u7535\u5e73\uff09<\/p>\n

\u83b7\u53d6\u4e0b\u5c42write\u51fd\u6570\u7684\u503c:copy_from_user\u51fd\u6570<\/p>\n

\u51fd\u6570\u4ecb\u7ecd<\/p>\n

unsignedlongcopy_from_user(void*to,constvoid__user*from,unsignedlongn)<\/p>\n

\u5185\u6838\u9a71\u52a8\u529b\u662f\u4ec0\u4e48\u610f\u601d_linux\u5185\u6838\u9a71\u52a8_\u5185\u6838\u9a71\u52a8\u7a0b\u5e8f<\/p>\n

\u6b64\u51fd\u6570\u5c06from\u8868\u9488\u6307\u5411\u7684\u7528\u6237\u7a7a\u95f4\u5730\u5740\u5f00\u59cb\u7684\u8fde\u7eedn\u4e2a\u5b57\u8282\u7684\u6570\u636e\u9001\u5230to\u8868\u9488\u6307\u5411\u7684\u5185\u6838\u7a7a\u95f4\u5730\u5740\uff0c\u7b80\u8a00\u4e4b\u662f\u7528\u4e8e\u5c06\u7528\u6237\u7a7a\u95f4\u7684\u6570\u636e\u4f20\u9001\u5230\u5185\u6838\u7a7a\u95f4<\/p>\n

\u7b2c\u4e00\u4e2a\u53c2\u6570to\u662f\u5185\u6838\u7a7a\u95f4\u7684\u6570\u636e\u76ee\u6807\u5730\u5740\u8868\u9488\uff0c<\/p>\n

\u7b2c\u4e8c\u4e2a\u53c2\u6570from\u662f\u7528\u6237\u7a7a\u95f4\u7684\u6570\u636e\u6e90\u5730\u5740\u8868\u9488\uff0c<\/p>\n

\u7b2c\u4e09\u4e2a\u53c2\u6570n\u662f\u6570\u636e\u5bbd\u5ea6\u3002<\/p>\n

\u5047\u5982\u6570\u636e\u62f7\u8d1d\u6210\u529f\uff0c\u5219\u8fd4\u56de\u96f6\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u6ca1\u6709\u62f7\u8d1d\u6210\u529f\u7684\u6570\u636e\u5b57\u8282\u6570\u3002<\/p>\n

\u6309\u7167\u503c\u6765\u64cd\u4f5cIO\u53e3<\/p>\n

\n

int<\/span> userCmd;<\/span>\u4e0a\u5c42\u5199\u7684\u662f\u6574\u578b\u65701<\/span>\uff0c\u5e95\u5c42\u5c31\u8981\u5bf9\u5e94\u8d77\u6765\u7528int<\/span>.<\/span>\u5982\u679c\u662f\u5b57\u7b26\u5219\u7528char<\/span>\ncopy_from_user<\/span>(<\/span>&<\/span>userCmd,<\/span>buf,<\/span>count)<\/span>;<\/span>\nif<\/span>(<\/span>userCmd ==<\/span> 1<\/span>)<\/span>{<\/span>\n\tprintk<\/span>(<\/span>\"set 1n\"<\/span>)<\/span>;<\/span>\/\/\u5185\u6838\u8c03\u8bd5\u4fe1\u606f<\/span>\n\t*<\/span>GPSET0 |=<\/span> 0x1<\/span> <<<\/span> 4<\/span>;<\/span>\n}<\/span>else<\/span> if<\/span>(<\/span>userCmd ==<\/span> 0<\/span>)<\/span>{<\/span>\n\tprintk<\/span>(<\/span>\"set 0n\"<\/span>)<\/span>;<\/span>\n\t*<\/span>GPCLR0 |=<\/span> 0x1<\/span> <<<\/span> 4<\/span>;<\/span>\n}<\/span>else<\/span>{<\/span>\n\tprintk<\/span>(<\/span>\"cmd errorn\"<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n<\/p>\n

\u8bf4\u660e\uff08\u8fd9\u4e5f\u662f\u64cd\u4f5c\u903b\u8f91\u7684\u4e00\u90e8\u4efd\u5566\uff09\uff1a<\/p>\n

\u2460\u8fd9\u4e2aGPSET0\uff0c0\u6307\u7684\u662f\u5206\u7ec4\uff0c\u4e0d\u662f\u8bbe\u7f6e\u6210\u4f4e\u7535\u5e73\u3002<\/p>\n

\u2461\u5de6\u79fb4\u4f4d\uff0c\u662f\u7531\u4e8eGPSET0\u5bc4\u5b58\u5668\u7684\u7b2c4\u4f4d\u5bf9\u5e94pin4,\u53ea\u8981\u628a\u7b2c4\u4f4d\u8bbe\u7f6e\u4e3a1\uff0c\u8868\u793a\u8fd9\u4e2a\u5bc4\u5b58\u5668\u5c31\u5bf9pin4\u53d1\u6325\u4f5c\u7528\uff0c\u8bbe\u7f6e\u6210\u9ad8\u7535\u5e73\uff0c\u5018\u82e5\u662f0\u5219noeffct\uff08\u6307\u5357\u5185\u5bb9\uff09\u3002<\/p>\n

3\u3001\u89e3\u9664\u865a\u62df\u5730\u5740\u6620\u5c04(iounmap)<\/p>\n

\u9000\u51fa\u7a0b\u5e8f\u5378\u8f7d\u9a71\u52a8\u7684\u65f6\u4faf\uff0c\u89e3\u9664\u6620\u5c04\uff1aiounmap\u51fd\u6570<\/p>\n

voidiounmap\uff08void*addr\uff09\/\/\u53d6\u6d88ioremap\u6240\u6620\u5c04\u7684IO\u5730\u5740<\/p>\n

\n

iounmap<\/span>(<\/span>GPFSEL0)<\/span>;<\/span>  \/\/init\u662f\u76f8\u53cd\u7684\u6267\u884c\u987a\u5e8f<\/span>\niounmap<\/span>(<\/span>GPSET0)<\/span>;<\/span>\niounmap<\/span>(<\/span>GPCLR0)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

\u516b\u3001\u5b8c\u6574\u4ee3\u78011\u3001\u5185\u6838\u9a71\u52a8\u6846\u67b6<\/p>\n

\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<\/span>\nvolatile<\/span> unsigned<\/span> int<\/span>*<\/span> GPFSEL0 =<\/span> NULL<\/span>;<\/span>\nvolatile<\/span> unsigned<\/span> int<\/span>*<\/span> GPSET0  =<\/span> NULL<\/span>;<\/span>\nvolatile<\/span> unsigned<\/span> int<\/span>*<\/span> GPCLR0  =<\/span> NULL<\/span>;<\/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    \/\/open\u7684\u65f6\u5019\u914d\u7f6epin4\u4e3a\u8f93\u51fa\u5f15\u811a<\/span>\n    *<\/span>GPFSEL0 &=<\/span> ~<\/span>(<\/span>0x6<\/span> <<<\/span> 12<\/span>)<\/span>;<\/span>\n\t*<\/span>GPFSEL0 |=<\/span> (<\/span>0x1<\/span> <<<\/span> 12<\/span>)<\/span>;<\/span>\n    \n    return<\/span> 0<\/span>;<\/span>\n}<\/span>\n\/\/pin4_write\u51fd\u6570<\/span>\nstatic<\/span> ssize_t<\/span> pin4_write<\/span>(<\/span>struct<\/span> file<\/span> *<\/span>file,<\/span>const<\/span> char<\/span> __user *<\/span>buf,<\/span>size_t<\/span> count,<\/span> loff_t<\/span> *<\/span>ppos)<\/span>\n{<\/span>\n\tint<\/span> userCmd;<\/span>\/\/\u4e0a\u5c42\u5199\u7684\u662f\u6574\u578b\u65701\uff0c\u5e95\u5c42\u5c31\u8981\u5bf9\u5e94\u8d77\u6765\u7528int.\u5982\u679c\u662f\u5b57\u7b26\u5219\u7528char<\/span>\n\tprintk<\/span>(<\/span>\"pin4_writen\"<\/span>)<\/span>;<\/span>\n\t\/\/\u83b7\u53d6\u4e0a\u5c42write\u7684\u503c<\/span>\n\tcopy_from_user<\/span>(<\/span>&<\/span>userCmd,<\/span>buf,<\/span>count)<\/span>;<\/span>\/\/\u7528\u6237\u7a7a\u95f4\u5411\u5185\u6838\u7a7a\u95f4\u4f20\u8f93\u6570\u636e<\/span>\n\t\n\t\/\/\u6839\u636e\u503c\u6765\u6267\u884c\u64cd\u4f5c<\/span>\n\tif<\/span>(<\/span>userCmd ==<\/span> 1<\/span>)<\/span>{<\/span>\n\t\tprintk<\/span>(<\/span>\"set 1n\"<\/span>)<\/span>;<\/span>\n\t\t*<\/span>GPSET0 |=<\/span> 0x1<\/span> <<<\/span> 4<\/span>;<\/span>\n\t}<\/span>else<\/span> if<\/span>(<\/span>userCmd ==<\/span> 0<\/span>)<\/span>{<\/span>\n\t\tprintk<\/span>(<\/span>\"set 0n\"<\/span>)<\/span>;<\/span>\n\t\t*<\/span>GPCLR0 |=<\/span> 0x1<\/span> <<<\/span> 4<\/span>;<\/span>\n\t}<\/span>else<\/span>{<\/span>\n\t\tprintk<\/span>(<\/span>\"cmd errorn\"<\/span>)<\/span>;<\/span>\/\/\u52a0\u5165\u8c03\u8bd5\u4fe1\u606f\uff0c\u65b9\u4fbf\u901a\u8fc7\u67e5\u770b\u5185\u6838\u4fe1\u606f\u8fdb\u884c\u4fee\u6539<\/span>\n\t}<\/span>\n\t\n    return<\/span> 0<\/span>;<\/span>\n}<\/span>\nstatic<\/span> struct<\/span> file_operations<\/span> pin4_fops =<\/span> {<\/span>\n    .<\/span>owner =<\/span> THIS_MODULE,<\/span>\n    .<\/span>open  =<\/span> pin4_open,<\/span>\n    .<\/span>write =<\/span> pin4_write,<\/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    printk<\/span>(<\/span>\"insmod driver pin4 successn\"<\/span>)<\/span>;<\/span>\n    devno =<\/span> MKDEV<\/span>(<\/span>major,<\/span>minor)<\/span>;<\/span>  \/\/\u521b\u5efa\u8bbe\u5907\u53f7<\/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=<\/span>class_create<\/span>(<\/span>THIS_MODULE,<\/span>\"myfirstdemo\"<\/span>)<\/span>;<\/span>  \/\/\u7531\u4ee3\u7801\u5728\/dev\u4e0b\u81ea\u52a8\u751f\u6210\u8bbe\u5907<\/span>\n    pin4_class_dev =<\/span>device_create<\/span>(<\/span>pin4_class,<\/span>NULL<\/span>,<\/span>devno,<\/span>NULL<\/span>,<\/span>module_name)<\/span>;<\/span>  \/\/\u521b\u5efa\u8bbe\u5907\u6587\u4ef6<\/span>\n\tGPFSEL0 =<\/span> (<\/span>volatile<\/span> unsigned<\/span> int<\/span> *<\/span>)<\/span>ioremap<\/span>(<\/span>0x3f200000<\/span>,<\/span>4<\/span>)<\/span>;<\/span>\n\tGPSET0  =<\/span> (<\/span>volatile<\/span> unsigned<\/span> int<\/span> *<\/span>)<\/span>ioremap<\/span>(<\/span>0x3f20001C<\/span>,<\/span>4<\/span>)<\/span>;<\/span>\n\tGPCLR0  =<\/span> (<\/span>volatile<\/span> unsigned<\/span> int<\/span> *<\/span>)<\/span>ioremap<\/span>(<\/span>0x3f200028<\/span>,<\/span>4<\/span>)<\/span>;<\/span>\n \t\n \treturn<\/span> 0<\/span>;<\/span>\n}<\/span>\nvoid<\/span> __exit pin4_drv_exit<\/span>(<\/span>void<\/span>)<\/span>\/\/\u53ef\u4ee5\u53d1\u73b0\u548cinit\u521a\u597d\u662f\u76f8\u53cd\u7684\u6267\u884c\u987a\u5e8f\u3002<\/span>\n{<\/span>\n\tiounmap<\/span>(<\/span>GPFSEL0)<\/span>;<\/span>\n\tiounmap<\/span>(<\/span>GPSET0)<\/span>;<\/span>\n\tiounmap<\/span>(<\/span>GPCLR0)<\/span>;<\/span>\n\t\n    device_destroy<\/span>(<\/span>pin4_class,<\/span>devno)<\/span>;<\/span>\n    class_destroy<\/span>(<\/span>pin4_class)<\/span>;<\/span>\n    unregister_chrdev<\/span>(<\/span>major,<\/span> module_name)<\/span>;<\/span>  \/\/\u5378\u8f7d\u9a71\u52a8<\/span>\n}<\/span>\nmodule_init<\/span>(<\/span>pin4_drv_init)<\/span>;<\/span>  \/\/\u5165\u53e3:\u5185\u6838\u52a0\u8f7d\u9a71\u52a8\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u5b8f\u4f1a\u88ab\u8c03\u7528\uff0c\u800c\u771f\u6b63\u7684\u9a71\u52a8\u5165\u53e3\u662f\u5b83\u8c03\u7528\u7684\u51fd\u6570<\/span>\nmodule_exit<\/span>(<\/span>pin4_drv_exit)<\/span>;<\/span>\nMODULE_LICENSE<\/span>(<\/span>\"GPL v2\"<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

2\u3001\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\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        int<\/span> fd;<\/span>\n        int<\/span> cmd;<\/span>\n        fd =<\/span> open<\/span>(<\/span>\"\/dev\/pin4\"<\/span>,<\/span>O_RDWR)<\/span>;<\/span>\n        if<\/span>(<\/span>fd <<\/span> 0<\/span>)<\/span>{<\/span>\n                printf<\/span>(<\/span>\"open failedn\"<\/span>)<\/span>;<\/span>\n                perror<\/span>(<\/span>\"reson\"<\/span>)<\/span>;<\/span>\n        }<\/span>else<\/span>{<\/span>\n                printf<\/span>(<\/span>\"open successn\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n        printf<\/span>(<\/span>\"\u8bf7\u8f93\u51650 \/ 1n 0:\u8bbe\u7f6epin4\u4e3a\u4f4e\u7535\u5e73n 1:\u8bbe\u7f6epin4\u4e3a\u9ad8\u7535\u5e73n\"<\/span>)<\/span>;<\/span>\n        scanf<\/span>(<\/span>\"%d\"<\/span>,<\/span>&<\/span>cmd)<\/span>;<\/span>\n        if<\/span>(<\/span>cmd ==<\/span> 0<\/span>)<\/span>{<\/span>\n                printf<\/span>(<\/span>\"pin4\u8bbe\u7f6e\u6210\u4f4e\u7535\u5e73n\"<\/span>)<\/span>;<\/span>\n        }<\/span>else<\/span> if<\/span>(<\/span>cmd ==<\/span> 1<\/span>)<\/span>{<\/span>\n                printf<\/span>(<\/span>\"pin4\u8bbe\u7f6e\u6210\u9ad8\u7535\u5e73n\"<\/span>)<\/span>;<\/span>\n        }<\/span>\n        fd =<\/span> write<\/span>(<\/span>fd,<\/span>&<\/span>cmd,<\/span>1<\/span>)<\/span>;<\/span>\/\/\u5199\u4e00\u4e2a\u5b57\u7b26'1',\u5199\u4e00\u4e2a\u5b57\u8282<\/span>\n        return<\/span> 0<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n<\/p>\n

3\u3001\u4ea4\u53c9\u7f16\u8bd1\u5e76\u53d1\u9001\u81f3\u7315\u7334\u6843\u6d3e\u2460\u6811\u8393\u6d3e\u4e0a\u5378\u8f7d\u4e4b\u524d\u7684pin4\u9a71\u52a8\u3001\u5220\u9664\u7315\u7334\u6843\u6d3e\u4e0b\u5c42\u53ef\u6267\u884c\u7a0b\u5e8fpin4test\u548cpin44driver2.ko\u6587\u4ef6<\/p>\n

\n

sudo rmmod pin44driver2\n<\/code><\/pre>\n<\/p>\n

\u7528lsmod\u67e5\u770b\u662f\u5426\u5378\u8f7d\u6210\u529f\u3002<\/p>\n

\u57fa\u672c\u4e0a\u5c31\u4f1a\u624b\u52a8\u5378\u8f7d\u9a71\u52a8\u7684\uff0c\u7531\u4e8e\u4e0a\u4e00\u8282\u6846\u67b6\u4ee3\u7801\u6700\u540e\u6709\u5378\u8f7d\u9a71\u52a8\u7684\u4ee3\u7801\u64cd\u4f5c\u3002<\/p>\n

\u2461\u9a71\u52a8\u6846\u67b6\u6a21\u5757\u5316\u5f62\u5f0f\u7f16\u8bd1\u548c\u4e0b\u5c42\u5e94\u7528\u7a0b\u5e8f\u5728Ubuntu\u4e2d\u8fdb\u884c\u4ea4\u53c9\u7f16\u8bd1\u5e76\u53d1\u9001\u81f3\u7315\u7334\u6843\u6d3e<\/p>\n

\u6ce8\u610f\uff1a<\/p>\n

\u5728Ubuntu\u7684\/char\u76ee\u5f55\u4e0b\u7531\u4e8e\u4e4b\u524d\u7684\u6a21\u5757\u7f16\u8bd1\u751f\u6210\u4e86.ko,.mod\u7b49\u6587\u4ef6<\/p>\n

\u6ca1\u5173\u7cfb\uff0c\u76f4\u63a5\u590d\u5236\u65b0\u7684\u9a71\u52a8\u6846\u67b6\u3001\u65b0\u7684\u4e0b\u5c42\u4ee3\u7801\u5230\u539f\u5148\u76842\u4e2a.c\u6587\u4ef6\u8986\u76d6\u4fdd\u5b58\u3002\u4e4b\u540e\u8fdb\u884c\u4ea4\u53c9\u7f16\u8bd1\uff0c\u65b0\u751f\u6210\u7684\u6587\u4ef6\u4f1a\u8986\u76d6\u6389\u539f\u5148\u7684\u6587\u4ef6\u3002<\/p>\n

4\u3001\u6811\u8393\u6d3e\u88c5\u8f7d\u9a71\u52a8<\/p>\n

\n

sudo insmod pin44driver.<\/span>ko\n<\/code><\/pre>\n<\/p>\n

\u7528dmesg\u53ef\u4ee5\u770b\u89c1\u5185\u6838\u590d\u5370\u51fa\u201c\u9a71\u52a8\u88c5\u8f7d\u6210\u529f\u201d\uff08\u590d\u5370\u4fe1\u606f\u6765\u81ea\u6846\u67b6\u4ee3\u7801\uff09<\/p>\n

\u7ed9\u6743\u9650<\/p>\n

\n

sudo chmod 666<\/span> \/<\/span>dev\/<\/span>pin4\n<\/code><\/pre>\n<\/p>\n

\u8fd0\u884c\u4e0b\u5c42\u5e94\u7528\u6587\u4ef6<\/p>\n

\n

.<\/span>\/<\/span>pin4test\n<\/code><\/pre>\n<\/p>\n

\u8fd0\u884c\u6210\u529f\uff01<\/p>\n

5\u3001\u9a71\u52a8\u8fd0\u884c\u6210\u529f<\/p>\n

\u8f93\u51651\u65f6\uff0c\u7528\u547d\u4ee4gpioreadall\u67e5\u770bpin4\u5f15\u811a\u53d8\u5316\uff0c\u5e94\u4e3aOUT1<\/p>\n

\u8f93\u51650\u65f6\uff0c\u518d\u7528\u547d\u4ee4gpioreadall\u67e5\u770bpin4\u5f15\u811a\u53d8\u5316\uff0c\u5e94\u4e3aOUT0<\/p>\n

\u7528dmesg\u6253\u5f00\u5185\u6838\u590d\u5370\u754c\u9762\uff0c\u53ef\u4ee5\u770b\u89c1\u5185\u6838\u7684printk\u65e9\u5df2\u88ab\u8c03\u7528\uff0c\u914d\u7f6e\u6267\u884c\u3002<\/p>\n

\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u7c7b\u4f3cWiringPi\u7684\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7528\u6811\u8393\u6d3e\u5b66\u4e60\u7684\u76ee\u7684\u4e0d\u4ec5\u662f\u4e3a\u662f\u4f53\u9a8c\u5176\u5f3a\u5927\u4fbf\u6377\u7684wiringPi\u5e93\uff0c\u66f4\u8981\u901a\u8fc7\u6811\u8393\u6d3e\u5b66\u4f1alinux\u5185\u6838\u5f00\u53d1\uff0c\u9a71\u52a8\u7f16\u5199\u7b49\uff0c\u505a\u4e00\u4e2a\u5c5e\u4e8e\u81ea\u5df1\u7684\u5e93\u3002\u5185\u6838\u4e2d\u5b58\u5728\u4e00\u4e2a\u9a71\u52a8\u94fe\u8868\uff0c\u7ba1\u7406\u6240\u6709\u8bbe\u5907\u7684\u9a71\u52a8\u3002\u9a71\u52a8\u6846\u67b6\u7684\u6a21\u5757\u7f16\u8bd1\u5e76\u53d1\u9001\u81f3\u6811\u8393\u6d3e\u2460\u6811\u8393\u6d3e\u52a0\u8f7d\u5185\u6838\u9a71\u52a8(insmod)1\u3001\u5185\u6838\u9a71\u52a8\u6846\u67b6\u2461\u9a71\u52a8\u6846\u67b6\u6a21\u5757\u5316\u65b9\u5f0f\u7f16\u8bd1\u548c\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\u5728Ubuntu\u4e2d\u8fdb\u884c\u4ea4\u53c9\u7f16\u8bd1\u5e76\u53d1\u9001\u81f3\u6811\u8393\u6d3e<\/p>\n","protected":false},"author":1,"featured_media":278110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-278109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/278109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=278109"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/278109\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/278110"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=278109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=278109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=278109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}