qinyunti 发表于 2023-1-30 23:53

【米尔ARM+FPGA架构之作MYD-JX8MMA7开发板】C开发环境搭建,HelloWorld,LED,按键测试

<div class='showpostmsg'><h1><b>前言</b></h1>

<p >&nbsp; &nbsp; 开发板体验第一步少不了HelloWorld一下。这里先进行C开发环境搭建,然后编写一个简单的HelloWorld,LED点灯,按键输入程序测试。这里推荐使用WIN11+WSL2+UBUNTU的方式搭建开发环境,而不是采用基于VMWARE等虚拟机的方式。下载虚拟机,安装系统等太复杂,如果仅仅是Linux C开发的话基于WSL,安装编译器即可,更便捷。</p>

<p >&nbsp;</p>

<h1 ><b>过程</b></h1>

<p >WIN11下安装WSL2,并安装UBUNTU系统,该过程网上有很多资料就不再赘述。我们基于该环境搭建Linux C开发环境。</p>

<h2 ><b>准备</b></h2>

<p >接上J3的TTL,调试串口</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<p >串口终端登录,这里使用crt</p>

<p >115200-8-N-1</p>

<p >用户名为root 无需密码</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<p >可以通过rz和sz指令进行文件的导入导出</p>

<p > &nbsp;</p>

<p >sz key导出到PC</p>

<p > &nbsp;</p>

<p >导出文件的路径位于</p>

<p > &nbsp;</p>

<h2 ><b>安装编译器</b></h2>

<p >打开WSL的ubuntu终端</p>

<p >sudo apt-get install gcc-aarch64-linux-gnu</p>

<p >&nbsp;</p>

<p >aarch64-linux-gnu-gcc -v查看版本</p>

<div class="parsedown-markdown">
<p>lhj@lhj:~$ aarch64-linux-gnu-gcc -v</p>

<p >Using built-in specs.</p>

<p >COLLECT_GCC=aarch64-linux-gnu-gcc</p>

<p >COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/aarch64-linux-gnu/9/lto-wrapper</p>

<p >Target: aarch64-linux-gnu</p>

<p >Configured with: ../src/configure -v --with-pkgversion=&#39;Ubuntu 9.4.0-1ubuntu1~20.04.1&#39; --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --without-target-system-zlib --enable-libpth-m2 --enable-multiarch --enable-fix-cortex-a53-843419 --disable-werror --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=aarch64-linux-gnu --program-prefix=aarch64-linux-gnu- --includedir=/usr/aarch64-linux-gnu/include</p>

<p >Thread model: posix</p>

<p >gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)</p>

<p >lhj@lhj:~$</p>
</div>

<p >&nbsp;</p>

<p >&nbsp;</p>

<h2 ><b>HelloWorld</b></h2>

<p >&nbsp;</p>

<p >vi helloworld.c 编辑源文件</p>

<p >输入i进入插入模式</p>

<p >输入以下内容</p>

<div class="parsedown-markdown">
<p>#include &lt;stdio.h&gt;</p>

<p >int main(void)</p>

<p >{</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello World\r\n&quot;);</p>

<p >&nbsp;</p>

<p >}</p>
</div>

<p >&nbsp;</p>

<p >按ESC</p>

<p >按:进入命令模式</p>

<p >输入:wq保存</p>

<p >&nbsp;</p>

<p >&nbsp;</p>

<p >编译aarch64-linux-gnu-gcc helloworld.c -o helloworld生成可执行文件helloworld</p>

<p >将源文件导出到windows下 cp helloworld /mnt/d</p>

<p >&nbsp;</p>

<p >然后windows下通过串口或者网口ssh的rz指令导入文件helloworld</p>

<p >&nbsp;</p>

<p >添加可执行权限chmod +x helloworld</p>

<p >&nbsp;</p>

<p >运行 ./helloworld</p>

<p >&nbsp;</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<h2 ><b>LED</b></h2>

<p >从原理图可以看到,提供了一个用户LED</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<p >Led设备位于/sys/class/leds/ 下,有一个user1用户LED,蓝色的,默认闪烁。</p>

<p >&nbsp;</p>

<p >root@myd-jx8mma7:~# ls /sys/class/leds/ &nbsp;</p>

<p >cpu &nbsp;input3::capslock &nbsp;input3::numlock &nbsp;input3::scrolllock &nbsp;mmc0:: &nbsp;mmc1:: &nbsp;mmc2:: &nbsp;pwr_pg &nbsp;pwr_stby &nbsp;user1</p>

<p >root@myd-jx8mma7:~#</p>

<p >&nbsp;</p>

<p >用户要使用可先取消自动闪烁</p>

<p >echo none &gt; /sys/class/leds/user1/trigger</p>

<p >查看LED状态</p>

<p >cat /sys/class/leds/user1/brightness</p>

<p >&nbsp;</p>

<p >用户控制</p>

<p >echo 1 &gt; /sys/class/leds/user1/brightness</p>

<p >echo 0&nbsp;&gt; /sys/class/leds/user1/brightness</p>

<p >&nbsp;</p>

<p >测试代码</p>

<p >新建led.c文件代码如下</p>

<div class="parsedown-markdown">
<p>#include &lt;stdio.h&gt;</p>

<p >#include &lt;stdlib.h&gt;</p>

<p >#include &lt;unistd.h&gt;</p>

<p >int main(void)</p>

<p >{</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello World\r\n&quot;);</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(1)</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system(&quot;echo 1 &gt; /sys/class/leds/user1/brightness&quot;);</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system(&quot;echo 0 &gt; /sys/class/leds/user1/brightness&quot;);</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>

<p >}</p>
</div>

<p >&nbsp;</p>

<p >&nbsp;</p>

<p >编译aarch64-linux-gnu-gcc ledd.c -o led生成可执行文件led</p>

<p >将源文件导出到windows下 cp led /mnt/d</p>

<p >&nbsp;</p>

<p >然后windows下通过串口或者网口ssh的rz指令导入文件led</p>

<p >&nbsp;</p>

<p >添加可执行权限chmod +x led</p>

<p >&nbsp;</p>

<p >运行 ./led</p>

<p >&nbsp;</p>

<p >可以看到LED1秒间隔闪烁</p>

<p > &nbsp;</p>

<p >&nbsp;</p>

<h2 ><b>按键</b></h2>

<p >原理图可以看到,有两个用户按键OnOFF和User</p>

<p >其中OnFF是长按开关机按键</p>

<p >所以只有User可以使用</p>

<p >&nbsp;</p>

<p > &nbsp;</p>

<p >设备位于/dev/input/</p>

<p >root@myd-jx8mma7:~# ls /dev/input/ &nbsp;&nbsp;&nbsp;&nbsp;</p>

<p >by-id &nbsp;by-path &nbsp;event0 &nbsp;event1 &nbsp;event2 &nbsp;event3 &nbsp;event4 &nbsp;event5 &nbsp;event6</p>

<p >root@myd-jx8mma7:~#</p>

<p >&nbsp;</p>

<p >测试</p>

<p >&nbsp;</p>

<p >输入evtest 可以看到1为gpio-keys</p>

<p >2为bd718xx-pwrkey</p>

<p >再选择1</p>

<p >再按User按键,可以看到打印信息</p>

<div class="parsedown-markdown">
<p>root@myd-jx8mma7:~# evtest</p>

<p >No device specified, trying to scan all of /dev/input/event*</p>

<p >Available devices:</p>

<p >/dev/input/event0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30370000.snvs:snvs-powerkey</p>

<p >/dev/input/event1: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gpio-keys</p>

<p >/dev/input/event2: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bd718xx-pwrkey</p>

<p >/dev/input/event3: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gtech Lenovo Eos wireless KM combo</p>

<p >/dev/input/event4: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gtech Lenovo Eos wireless KM combo Mouse</p>

<p >/dev/input/event5: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gtech Lenovo Eos wireless KM combo Consumer Control</p>

<p >/dev/input/event6: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gtech Lenovo Eos wireless KM combo</p>

<p >Select the device event number : 1</p>

<p >Input driver version is 1.0.1</p>

<p >Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100</p>

<p >Input device name: &quot;gpio-keys&quot;</p>

<p >Supported events:</p>

<p >&nbsp;&nbsp;Event type 0 (EV_SYN)</p>

<p >&nbsp;&nbsp;Event type 1 (EV_KEY)</p>

<p >&nbsp;&nbsp;&nbsp;&nbsp;Event code 2 (KEY_1)</p>

<p >Properties:</p>

<p >Testing ... (interrupt to exit)</p>

<p >[ &nbsp;&nbsp;35.806595] WLAN_EN: disabling</p>

<p >Event: time 3029530572.3029530572, type 1 (EV_KEY), code 2 (KEY_1), value 1</p>

<p >Event: time 3029530572.3029530572, -------------- SYN_REPORT ------------</p>

<p >Event: time 3029530573.3029530573, type 1 (EV_KEY), code 2 (KEY_1), value 0</p>

<p >Event: time 3029530573.3029530573, -------------- SYN_REPORT ------------</p>
</div>

<p >&nbsp;</p>

<p >新建key.c文件代码如下</p>

<div class="parsedown-markdown">
<p>#include&lt;stdint.h&gt;</p>

<p >#include&lt;stdio.h&gt;</p>

<p >#include&lt;stdlib.h&gt;</p>

<p >#include&lt;assert.h&gt;</p>

<p >#include&lt;string.h&gt;</p>

<p >#include&lt;unistd.h&gt;</p>

<p >&nbsp;</p>

<p >#include&lt;pthread.h&gt;</p>

<p >&nbsp;</p>

<p >#include&nbsp;&lt;linux/input.h&gt;</p>

<p >#include&nbsp;&lt;sys/types.h&gt;</p>

<p >#include&nbsp;&lt;sys/stat.h&gt;</p>

<p >#include&nbsp;&lt;fcntl.h&gt;</p>

<p >&nbsp;</p>

<p >int&nbsp;s_keys_fd = -1;</p>

<p >uint32_t&nbsp;s_keys_state = 0;</p>

<p >&nbsp;</p>

<p >void* key_poll(void* arg)</p>

<p >{</p>

<p >&nbsp; &nbsp; char&nbsp;ret;</p>

<p >&nbsp; &nbsp; struct&nbsp;input_event t;</p>

<p >&nbsp; &nbsp; s_keys_fd = open((char*)arg, O_RDONLY);</p>

<p >&nbsp; &nbsp; if(s_keys_fd &lt;= 0)</p>

<p >&nbsp; &nbsp; {</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;open %s&nbsp;device error!\n&quot;,(char*)arg);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;0;</p>

<p >&nbsp; &nbsp; }</p>

<p >&nbsp; &nbsp; while(1)</p>

<p >&nbsp; &nbsp; {</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; if(read(s_keys_fd, &amp;t, sizeof(t)) == sizeof(t))</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; {</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(t.type==EV_KEY)</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(t.value==0&nbsp;|| t.value==1)</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;key %d&nbsp;%s\n&quot;, t.code, (t.value) ? &quot;Pressed&quot;&nbsp;: &quot;Released&quot;);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if(t.code == KEY_ESC)</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;break;</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///printf(&quot;type %d code %d value %d\n&quot;, t.type, t.code, t.value);</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>

<p >&nbsp; &nbsp; &nbsp; &nbsp; }</p>

<p >&nbsp; &nbsp; }</p>

<p >&nbsp; &nbsp; return&nbsp;0;</p>

<p >}</p>

<p >&nbsp;</p>

<p >void&nbsp;key_init(void* arg)</p>

<p >{</p>

<p >&nbsp; &nbsp; pthread_t&nbsp;id;</p>

<p >&nbsp; &nbsp; /* 创建函数线程,并且指定函数线程要执行的函数 */</p>

<p >&nbsp; &nbsp; int&nbsp;res = pthread_create(&amp;id,NULL,key_poll,arg);</p>

<p >&nbsp; &nbsp; assert(res == 0);</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; while(1)</p>

<p >&nbsp; &nbsp; {</p>

<p >&nbsp;</p>

<p >&nbsp; &nbsp; }</p>

<p >&nbsp; &nbsp; exit(0);</p>

<p >}</p>

<p >&nbsp;</p>

<p >int&nbsp;key_getstate(int&nbsp;key)</p>

<p >{</p>

<p >&nbsp;</p>

<p >}</p>

<p >&nbsp;</p>

<p >int&nbsp;main(int&nbsp;argc, char* argv[])</p>

<p >{</p>

<p >&nbsp; &nbsp; key_init(argv);</p>

<p >&nbsp; &nbsp; while(1);</p>

<p >}</p>
</div>

<p >&nbsp;</p>

<p >&nbsp;</p>

<p >编译aarch64-linux-gnu-gcc key.c -o key -lpthread生成可执行文件key</p>

<p >将源文件导出到windows下 cp key /mnt/d</p>

<p >&nbsp;</p>

<p >然后windows下通过串口或者网口ssh的rz指令导入文件key</p>

<p >添加可执行权限chmod +x key</p>

<p >&nbsp;</p>

<p >运行 ./key /dev/input/event1</p>

<p >&nbsp;</p>

<p >按User按键打印如下</p>

<p > &nbsp;</p>

<h1 ><b>总结</b></h1>

<p >&nbsp; &nbsp; &nbsp;以上基于WIN11+WSL2+UBUNTU快速搭建了C开发环境,以一个Helloworld和LED测试,按键测试的例子进行演示。相对于使用虚拟机安装UBUNTU的方式搭建开发环境,更推荐使用该方式,更快捷简单。</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

盛世辉煌电子科学 发表于 2023-1-31 10:43

<p>确实强悍</p>

lugl4313820 发表于 2023-2-24 07:55

<p>以上基于WIN11+WSL2+UBUNTU快速搭建了C开发环境,以一个Helloworld和LED测试,按键测试的例子进行演示。相对于使用虚拟机安装UBUNTU的方式搭建开发环境,更推荐使用该方式,更快捷简单。</p>

<p>感谢分享!</p>
页: [1]
查看完整版本: 【米尔ARM+FPGA架构之作MYD-JX8MMA7开发板】C开发环境搭建,HelloWorld,LED,按键测试