更改

跳转至: 导航搜索

Orange Pi CM4

添加725字节, 2023年9月13日 (三) 14:38
安装内核头文件的方法
<ol style="list-style-type: decimal;">
<li><p>OPi发布的Linux镜像默认自带了内核头文件的deb包,存放的位置为'''/opt/'''</p>
{| class="wikitable" style="width:800px;"
|-
|
<p>orangepi@orangepi:~$ '''ls /opt/linux-headers*'''</p>
<p>/opt/linux-headers-legacy-rockchip-rk356x_x.x.x_arm64.deb</p>|}</li>
<li><p>使用下面的命令可以安装内核头文件的deb包</p>
{| class="wikitable" style="background-color:#ffffdc;width:800px;" |-| <big><p>'''内核头文件deb包的名字需要替换为实际的名字,请不要照抄。'''</p></big>|}{| class="wikitable" style="width:800px;" |-| <p>orangepi@orangepi:~$ '''sudo dpkg -i /opt/linux-headers-legacy-rockchip-rk356x_1.x.x_arm64.deb'''</p>|}</li>
<li><p>安装完后在'''/usr/src'''下就能看到内核头文件所在的文件夹</p>
{| class="wikitable" style="width:800px;"
|-
|
<p>orangepi@orangepi:~$ '''ls /usr/src'''</p>
<p>linux-headers-5.10.160-rockchip-rk356x</p>|}</li>
<li><p>然后可以编写一个hello内核模块测试下内核头文件</p>
<ol style="list-style-type: lower-alpha;">
<li><p>首先编写hello内核模块的代码,如下所示:</p>
{| class="wikitable" style="width:800px;"
|-
|
<p>orangepi@orangepi:~$ '''vim hello.c'''</p>
<p>#include &lt;linux/init.h&gt;</p>
<p>#include &lt;linux/module.h&gt;</p>
 
 
<p>static int hello_init(void)</p>
<p>{</p>
:<p>printk(&quot;Hello Orange Pi -- init\n&quot;);</p>  :<p>return 0;</p>
<p>}</p>
<p>static void hello_exit(void)</p>
<p>{</p>
:<p>printk(&quot;Hello Orange Pi -- exit\n&quot;);</p>  :<p>return;</p>
<p>}</p>
 
 
<p>module_init(hello_init);</p>
<p>module_exit(hello_exit);</p>
  <p>MODULE_LICENSE(&quot;GPL&quot;);</p>|}</li>
<li><p>然后编写编译hello内核模块的Makefile文件,如下所示:</p>
{| class="wikitable" style="width:800px;"
|-
|
<p>orangepi@orangepi:~$ '''vim Makefile'''</p>
<p>ifneq ($(KERNELRELEASE),)</p>
<p>PWD :=$(shell pwd)</p>
<p>all:</p>
:<p>make -C $(KDIR) M=$(PWD) modules</p>
<p>clean:</p>
:<p>rm -f *.ko *.o *.mod.o *.mod *.symvers *.cmd *.mod.c *.order</p><p>endif</p>|}</li>
<li><p>然后使用make命令编译hello内核模块,编译过程的输出如下所示:</p>
{| class="wikitable" style="background-color:#ffffdc;width:800px;" |-| <big><p>'''如果自己复制的代码这里编译如果有问题,请去[http://www.orangepi.cn/html/hardWare/computerAndMicrocontrollers/service-and-support/Orange-Pi-CM4-1.html 官方工具]中下载源码然后上传到开发板的Linux系统中测试。'''</p></big> <p>[[File:media/image362.png|253x41pxcenter]]</p>|}{| class="wikitable" style="width:800px;" |-|
<p>orangepi@orangepi:~$ '''make'''</p>
<p>make -C /lib/modules/5.10.160-rockchip-35xx/build M=/home/orangepi modules</p>
<p>make[1]: Entering directory '/usr/src/linux-headers-5.10.160-rockchip-rk35xx'</p>
:<p>CC [M] /home/orangepi/hello.o</p>:<p>MODPOST /home/orangepi/Module.symvers</p>:<p>CC [M] /home/orangepi/hello.mod.o</p>:<p>LD [M] /home/orangepi/hello.ko</p><p>make[1]: Leaving directory '/usr/src/linux-headers-5.10.160-rockchip-rk35xx'</p>|}</li>
<li><p>编译完后会生成'''hello.ko'''内核模块</p>
{| class="wikitable" style="width:800px;"
|-
|
<p>orangepi@orangepi:~$ '''ls *.ko'''</p>
<p>hello.ko</p>|}</li>
<li><p>使用'''insmod'''命令可以将'''hello.ko'''内核模块插入内核中</p>
{| class="wikitable" style="width:800px;" |-| <p>orangepi@orangepi:~$ '''sudo insmod hello.ko'''</p>|}</li>
<li><p>然后使用'''demsg'''命令可以查看下'''hello.ko'''内核模块的输出,如果能看到下面的输出说明'''hello.ko'''内核模块加载正确</p>
{| class="wikitable" style="width:800px;"
|-
|
<p>orangepi@orangepi:~$ '''dmesg | grep &quot;Hello&quot;'''</p>
<p>[ 2871.893988] '''Hello Orange Pi -- init'''</p>|}</li>
<li><p>使用'''rmmod'''命令可以卸载'''hello.ko'''内核模块</p>
{| class="wikitable" style="width:800px;"
|-
|
<p>orangepi@orangepi:~$ '''sudo rmmod hello'''</p>
<p>orangepi@orangepi:~$ '''dmesg | grep &quot;Hello&quot;'''</p>
<p>[ 2871.893988] Hello Orange Pi -- init</p>
<p>[ 3173.800892] '''Hello Orange Pi -- exit'''</p>|}</li></ol>
</li></ol>
8,460
个编辑

导航菜单