小螃蟹呀 发表于 2022-11-5 16:49

飞凌嵌入式i.MX8MP开发板移植驱动,其实可以很简单

<p>各位工程师用户在对<strong>飞凌嵌入式OKMX8MP-C开发板</strong>进行开发的过程中,可能会遇到需要移植驱动的情况。为避免用户因不了解移植驱动的过程而影响开发进度,今天小编会以写一个hello驱动为例,演示移植驱动的过程,有需求的小伙伴可参考此方法自行操作。</p>

<p>&nbsp;</p>

<p><a data-linktype="1" href="https://www.forlinx.com/product/135.html" target="_blank"></a></p>

<p>&nbsp;</p>

<section data-id="101169" data-role="title" data-tools="135编辑器">
<section>
<section>
<section>
<section><strong>01</strong></section>

<section>
<section data-width="100%">&nbsp;</section>
</section>
</section>
</section>
</section>
</section>

<p>&nbsp;</p>

<p>进入源码的drivers目录下,并创建一个名为hello的目录:</p>

<section>
<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~$cd/home/forlinx/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$mkdirhello</code></pre>
</section>

<p>&nbsp;</p>

<section data-id="101169" data-role="title" data-tools="135编辑器">
<section>
<section>
<section>
<section><strong>02</strong></section>

<section>
<section data-width="100%">&nbsp;</section>
</section>
</section>
</section>
</section>
</section>

<p>进入hello目录,创建hello.c:<code style="color: rgb(221, 221, 221); white-space: pre-wrap;">forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ cd hello</code><code style="color: rgb(221, 221, 221); white-space: pre-wrap;">forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi hello.c</code></p>

<p>&nbsp;</p>

<p>在hello.c中写入如下内容:</p>

<section>
<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>#include&lt;linux/init.h&gt;</code><code>#include&lt;linux/module.h&gt;</code><code>staticint hello_init(void)</code><code>{</code><code>printk(KERN_ALERT&quot;Hello world\n&quot;);</code><code>return0;</code><code>}</code><code>staticvoid hello_exit(void)</code><code>{</code><code>printk(KERN_ALERT&quot;Goodbye world\n&quot;);</code><code>}</code><code>module_init(hello_init);</code><code>module_exit(hello_exit);</code><code>MODULE_LICENSE(&quot;DualBSD/GPL&quot;);</code></pre>
</section>

<p>&nbsp;</p>

<p><strong>程序含义:</strong>insmod驱动挂载时打印Hello world,rmmod驱动卸载时打印&nbsp;Goodbye world</p>

<p>&nbsp;</p>

<section data-id="101169" data-role="title" data-tools="135编辑器">
<section>
<section>
<section>
<section><strong>03</strong></section>

<section>
<section data-width="100%">&nbsp;</section>
</section>
</section>
</section>
</section>
</section>

<p>&nbsp;</p>

<p>在该文件夹下创建Kconfig,Makefile两个文件。</p>

<section>
<p>&nbsp;</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$viKconfig</code></pre>
</section>

<p>&nbsp;</p>

<p>在Kconfig文件中写入如下内容:</p>

<section>
<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>configHAVE_HELLO</code><code>tristate       &quot;hello driver&quot;</code><code>help</code><code>Thishellodriverisjusttoshowhowtodevelopdriverprocess.</code>

<code>Thisdrivercanalsobebuiltasamodule.Ifso,themodule willbecalled.</code><code>defaulty</code><code>#endmenu</code></pre>
</section>

<p>&nbsp;</p>

<p>表示如果使能了CONFIG_HAVE_HELLO,在内核裁剪配置文件中,将显示hellodrivers菜单,默认编译进内核:</p>

<blockquote>
<p><strong>y:编译进内核</strong></p>

<p><strong>m:编译为模块.ko文件</strong></p>

<p><strong>n:表示不编译,未使能。</strong></p>
</blockquote>

<section>
<p>&nbsp;</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$viKconfig</code></pre>
</section>

<p>&nbsp;</p>

<p>在Makefile文件中写入如下内容:</p>

<section>
<p>&nbsp;</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>obj-$(CONFIG_HAVE_HELLO)      +=      hello.o</code></pre>
</section>

<p>&nbsp;</p>

<p><strong>注意:</strong></p>

<p>宏定义的名字要和Kconfig中的一样。后面添加需要编译的文件名,因为内核会自动添加前缀CONFIG,所以我们这里也要在名字前面添加CONFIG_,表示CONFIG_HAVE_HELLO使能时,编译规则指定的文件为hello.c。</p>

<p>&nbsp;</p>

<p>给添加的这三个文件权限:</p>

<section>
<p>&nbsp;</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$chmod777 hello.c</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$chmod777 Kconfig</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$chmod777 Makefile</code></pre>
</section>

<p>&nbsp;</p>

<section data-id="101169" data-role="title" data-tools="135编辑器">
<section>
<section>
<section>
<section><strong>04</strong></section>
</section>
</section>
</section>
</section>

<p>&nbsp;</p>

<p>编辑drivers顶层的Kconfig,Makefile文件。</p>

<section>
<p>&nbsp;</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$cd..</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$vi Kconfig</code></pre>
</section>

<p>&nbsp;</p>

<p>在Kconfig文件中写入如下内容:</p>

<section>
<p>​​​​​​​</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>source&quot;drivers/counter/Kconfig&quot;</code><code>source&quot;drivers/mxc/Kconfig&quot;</code><code>source&quot;drivers/hello/Kconfig&quot;    //在endmenu前添加hello文件夹的配置文件解析</code><code>endmenu</code></pre>
</section>

<p>&nbsp;</p>

<p>如此一来,配置系统就会按照这个配置去解析hello文件夹下的Kconfig。</p>

<p>&nbsp;</p>

<p>编辑Makefile:</p>

<section>
<p>&nbsp;</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$vi Makefile</code></pre>
</section>

<p>&nbsp;</p>

<p>在Makefile文件中写入如下内容:</p>

<section>
<p>​​​​​​​</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>obj-$(CONFIG_COUNTER)         += counter/</code><code>obj-y                           += mxc/</code><code>obj-$(CONFIG_HAVE_HELLO)      +=      hello/   //在Makefile最后加入这一句</code></pre>
</section>

<p>&nbsp;</p>

<p>这句话的作用是当CONFIG_HAVE_HELLO使能后,在哪里去找源文件。再结合hello文件下模块Makefile就形成了层次式Makefile。注意不要少了/,这里添加自定义文件夹的名字,表示把这个文件夹编译进内核。</p>

<section data-id="101169" data-role="title" data-tools="135编辑器">
<section>
<section>
<section>
<section><strong>5</strong></section>
</section>
</section>
</section>
</section>

<p>开始编译:​​​​​​​</p>

<section>
<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$cd ../..</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk$./opt/fsl-imx-xwayland/5.4-zeus/environment-setup-aarch64-poky-linux</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk$.environment-setup-aarch64-poky-linux</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk$cdOK8MP-linux-kernel</code><code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel$make modules</code><code>scripts/kconfig/conf --syncconfig Kconfig</code><code>drivers/hello/Kconfig:7:warning:ignoring unsupported character &#39;�&#39;</code><code>drivers/hello/Kconfig:7:warning:ignoring unsupported character &#39;�&#39;</code><code>drivers/hello/Kconfig:7:warning:ignoring unsupported character &#39;�&#39;</code><code>drivers/hello/Kconfig:7:warning:ignoring unsupported character &#39;�&#39;</code><code>*</code><code>* Restart config...</code><code>*</code><code>*</code><code>* Device Drivers</code><code>*</code><code>Trust the bootloaderto initialize Linux&#39;s CRNG (RANDOM_TRUST_BOOTLOADER) n</code><code>Platform support forChrome hardware (transitional) (MFD_CROS_EC) y</code><code>Trusted ExecutionEnvironment support (TEE) y</code><code>hello driver(HAVE_HELLO) (NEW) m    //将hello驱动编译进内核就配置为m</code><code>CALL   scripts/checksyscalls.sh</code><code>CALL   scripts/atomic/check-atomics.sh</code><code>CHK      include/generated/compile.h</code><code>GZIP   kernel/config_data.gz</code><code>CC   kernel/configs.o</code><code>[&hellip;]</code><code>LD      vmlinux</code><code>SORTEXvmlinux</code><code>SYSMAPSystem.map</code><code>Building modules,stage 2.</code><code>MODPOST 536 modules</code><code>CC drivers/hello/hello.mod.o</code><code>LD drivers/hello/hello.ko</code></pre>
</section>

<p>&nbsp;</p>

<p>编译完成后,即可在OK8MP-linux-kernel/drivers/hello目录下看到编译生成的驱动了:</p>

<section>
<p>&nbsp;</p>

<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/$ls drivers/hello</code><code>hello.chello.kohello.modhello.mod.chello.mod.ohello.oKconfigMakefile modules.order</code>
</pre>
</section>

<section data-id="101169" data-role="title" data-tools="135编辑器">
<section>
<section>
<section>
<section><strong>6</strong></section>
</section>
</section>
</section>
</section>

<p>将hello.ko使用U盘或TF卡拷贝到开发板里进行验证:​​​​​​​</p>

<section>
<pre style="background:#555; padding:10px; color:#ddd !important;">
<code>rootK8MP:~#&nbsp;cd&nbsp;&nbsp;/run/media/sda1/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//进入U盘的路径下</code><code>root@OK8MP:/run/media/sda1#&nbsp;&nbsp;insmod&nbsp;&nbsp;hello.ko&nbsp;&nbsp;&nbsp;//挂载hello.ko</code><code>[&nbsp;&nbsp;138.679964]&nbsp;Hello&nbsp;&nbsp;world&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          &nbsp;//挂载驱动打印信息</code><code>root@OK8MP:/run/media/sda1#&nbsp;&nbsp;rmmod&nbsp;&nbsp;hello.ko&nbsp;&nbsp;&nbsp;&nbsp;//卸载hello.ko</code><code>[&nbsp;&nbsp;142.022115]&nbsp;&nbsp;Goodbye&nbsp;&nbsp;world&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //卸载驱动打印信息</code><code>root@OK8MP:/run/media/sda1#</code></pre>
</section>

<p>&nbsp;</p>

<p>由上述测试可看,hello.ko驱动可正常运行。</p>

<p>以上就是小编为大家演示的自行书写并添加一个驱动的过程,若您想要移植某一个模块,可向模块厂家索要现成的驱动.c文件,之后再按照上述步骤配置Makefile和Kconfig即可。</p>

lugl4313820 发表于 2022-11-6 09:23

这个编译,是要跟固件一起编译吗,还是单独的编译。如果全编译,需要时间久吗?

hades2015 发表于 2022-11-10 14:28

<p>感谢分享,学习了!!</p>

通途科技 发表于 2024-8-27 19:17

页: [1]
查看完整版本: 飞凌嵌入式i.MX8MP开发板移植驱动,其实可以很简单