sumoon_yao 发表于 2021-6-21 16:16

【小熊派-鸿蒙-季 BearPi-HM Nano】之NFC测试

<div class='showpostmsg'> 本帖最后由 sumoon_yao 于 2021-6-28 17:34 编辑

<p><span style="font-size: 12pt;">根据官方资料,小熊派主芯片Hi3861有2组IIC通讯接口 ,1组以E53方式引出,</span><span style="font-size: 12pt;">另</span><span style="font-size: 12pt;">一组被NFC芯片使用,参看下列原理图:</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">这次就来测试下小熊派的NFC功能。</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">例程中的B5就是NFC功能的演示,进入</span><span style="font-size: 12pt;">B5_basic_i2c_nfc</span><span style="font-size: 12pt;">目录,打开主程序</span><span style="font-size: 12pt;">i2c_example</span><span style="font-size: 12pt;">.c:</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">static void I2CTask(void)</span></p>

<p><span style="font-size: 12pt;">{</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;uint8_t ret;</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;GpioInit();</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">//GPIO_0复用为I2C1_SDA</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;IoSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA);</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">//GPIO_1复用为I2C1_SCL</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;IoSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL);</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">//baudrate: 400kbps</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;I2cInit(WIFI_IOT_I2C_IDX_1, 400000);</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;I2cSetBaudrate(WIFI_IOT_I2C_IDX_1, 400000);</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;printf(&quot;I2C Test Start\n&quot;);</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;ret = storeText(NDEFFirstPos, (uint8_t *)TEXT);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;if (ret != 1)</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;{</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; printf(&quot;NFC Write Data Falied :%d &quot;, ret);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;}</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;ret = storeUrihttp(NDEFLastPos, (uint8_t *)WEB);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;if (ret != 1)</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;{</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; printf(&quot;NFC Write Data Falied :%d &quot;, ret);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;}</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;while (1)</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;{</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; printf(&quot;=======================================\r\n&quot;);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; printf(&quot;***********I2C_NFC_example**********\r\n&quot;);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; printf(&quot;=======================================\r\n&quot;);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; printf(&quot;Please use the mobile phone with NFC function close to the development board!\r\n&quot;);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp; &nbsp; usleep(1000000);</span></p>

<p><span style="font-size: 12pt;">&nbsp; &nbsp;}</span></p>

<p><span style="font-size: 12pt;">}</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">程序看上去也比较简单,设置了IIC总线的2个引脚和波特率</span><span style="font-size: 12pt;">,然后向NFC存储区写入了2组数据,一组是纯文本信息,另一组是URI数据,最后程序进入循环,等待带有NFC功能的手机来读取这2组信息。</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">将 sample目录下的build.gn文件做如下修改:</span></p>

<p><span style="font-size: 12pt;">#&quot;B1_basic_led_blink:led_example&quot;,</span></p>

<p><span style="font-size: 12pt;">#</span><span style="font-size: 12pt;">&quot;B2_basic_button:button_example&quot;,</span></p>

<p><span style="font-size: 12pt;">#&quot;B3_basic_pwm_led:pwm_example&quot;,</span></p>

<p><span style="font-size: 12pt;">#&quot;B4_basic_adc:adc_example&quot;,</span></p>

<p><span style="font-size: 12pt;">&quot;B5_basic_i2c_nfc:i2c_example&quot;,</span></p>

<p><span style="font-size: 12pt;">#&quot;B6_basic_uart:uart_example&quot;,</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">也即注释掉B2,去掉B5的注释。</span></p>

<p><span style="font-size: 12pt;">编译和烧写过程与上一篇一样。</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">打开串口终端,</span><span style="font-size: 12pt;">程序运行结果如下图所示:</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">循环等待带有NFC功能的手机来读取NFC数据</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">手机中装有2个NFC工具,一个是ST公司的NFC Tap,另一个是NXP公司的NFC Taginfo,使用这2个app分别读取。</span></p>

<p><span style="font-size: 12pt;">先来看下NFC Tap的读取情况。NFC Tap有2种读取方式:Read Tag和Read NDEF。</span></p>

<p>&nbsp;</p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">先看下Read Tag方式:</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">手机靠近板子的NFC TAG区域,NFC指示灯亮,但是手机没有读取到信息。</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">再使用Read NDEF方式读取,手机靠近板子的NFC TAG区域后,马上响起了读取成功的提示音,且手机屏幕上显示出了读取的内容:</span></p>

<p></p>

<p><span style="font-size: 12pt;">上图中可以看出NFC读取app已经成功读到了板子主程序运行写入的2组数据。</span></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">再看下NXP Taginfo 读取情况,点击&ldquo;Sacn &amp; Show&rdquo;按钮,手机靠近板子的NFC TAG区域,很快屏幕上显示已读取到</span><span style="font-size: 12pt;">数据:</span></p>

<p></p>

<p>&nbsp;</p>

<p><span style="font-size: 12pt;">点击&ldquo;NTAG I2C Plus&rdquo;按钮,屏幕显示了该NFC Tag的详细信息:</span></p>

<p></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">如何向小熊派通过NFC写入数据,经过了解小熊派新版有个读取NFC数据的例子,参考之后对现有的NFC例程做些改动即可实现。</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">首先修改主程序i2c_example.c:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">在nfc目录下的程序NT3H.c文件中增加函数:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">在nfc目录下的头文件NT3H.h文件中增加一行:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-indent:23.25pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:12.0pt"><span style="font-family:宋体">编译、烧写后程序运行结果如下:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">等待外部NFC读卡器读取的同时,板子本身也在循环读取初始写入的数据,此时如果有外部NFC Writer向板子写入数据,那么就实现了通过NFC向板子传送数据的目的。</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">打开手机上的NFC Writer,先测试写入文本数据:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">按下WRITE按钮,将手机靠近板子的NFC区域,嘀嗒一声后写入成功,测试串口终端显示:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">因为输出显示方式的问题,看不出板子读到的数据,用NFC Tap APP再来读取下,结果如下:</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-indent:23.25pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-size:12.0pt"><span style="font-family:宋体">说明数据被成功写入到了板子的NFC数据存储区。</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">再用NFC Write写入URL数据:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">同样的在嘀嗒一声后写入成功,测试串口终端显示:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">用NFC Tap APP再来读取下,结果如下:</span></span></span></span></p>

<p style="text-align:justify"></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">&nbsp;&nbsp;&nbsp; <span style="font-size:12.0pt"><span style="font-family:宋体">至此,NFC双向通讯测试完毕。当然对NFC的一些具体的技术细节,后面还要抽时间研究。</span></span></span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p>&nbsp;</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>

nmg 发表于 2021-6-21 16:23

<p><img height="50" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan21.gif" width="63" />看到碰一碰了</p>

w494143467 发表于 2021-6-21 16:45

<p>不错哈,效果确实挺好!</p>

freebsder 发表于 2021-6-21 22:27

<p>谢谢分享,期待后续!</p>

tommy1987 发表于 2021-6-23 16:09

<p>看着很不错,蹲一个后续!</p>

minihang 发表于 2021-6-24 09:46

<p>厉害了</p>

haitao2018 发表于 2021-6-25 11:44

<p>【小熊派-鸿蒙-季 BearPi-HM Nano】之NFC测试</p>

sumoon_yao 发表于 2021-6-27 10:19

本帖最后由 sumoon_yao 于 2021-6-28 17:36 编辑

<p>反向操作更新完毕。</p>
页: [1]
查看完整版本: 【小熊派-鸿蒙-季 BearPi-HM Nano】之NFC测试