当BLE遇到MEMS——LSM6DSL驱动移植到NRF52832
<div class='showpostmsg'><p style="text-indent:21.0pt; text-align:justify"><span style="font-size:16px;"><span style="font-family:Times;">NRF52832自带了很多的外设的驱动;在nRF5_SDK_XX\components\drivers_ext文件夹中,可惜没有ST芯片的驱动。</span></span></p><p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Times;"> 接下来介绍ST的MEMS驱动移植到NRF52832的方法。</span></span></p>
<h2 style="text-align:justify; margin-top:18px; margin-bottom:18px"><span style="font-family:Times;"><span style="font-size:16pt"><span style="line-height:173%">下载驱动库</span></span></span></h2>
<p style="text-align:justify"><span style="font-family:Times;"><span style="font-size:10.5pt"> <span style="font-size:16px;"> 首先下载NRF52832的驱动库,在官网下载SDK12.3的版本,现在并不是最新的,但是比较精简。</span></span></span></p>
<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Times;"> 然后下载LSM6DSL的驱动库,进入ST的官网,搜索<span lang="EN-US"><span style="color:black">X-CUBE-MEMS1</span></span>,将该固件下载下来,要是官网出bug,可以通过我之前的<a href="https://bbs.eeworld.com.cn/thread-1084108-1-1.html" target="_blank">文章</a>方法下载。</span></span></p>
<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Times;">在STM32CubeExpansion_MEMS1_V6.2.0\Drivers\BSP\Components路径下,有各个传感器的驱动,由于X-NUCLEO-IKS01A这样的板子都是使用的I2C通信接口,我个人不喜欢I2C接口,买了个有SPI接口的LSM6DSL模块。</span></span></p>
<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Times;"> 复制LSM6DSL_ACC_GYRO_driver.c和LSM6DSL_ACC_GYRO_driver.h两个文件就够了,其实最需要的只是头文件里面的寄存器定义,因为库文件的功能最终也就是操作寄存器。</span></span></p>
<h2 style="text-align:justify; margin-top:18px; margin-bottom:18px"><span style="font-family:Times;"><span style="font-size:16pt"><span style="line-height:173%">SPI通信接口移植</span></span></span></h2>
<p align="left" style="text-align:left"><span style="font-family:Times;"><span style="font-size:10.5pt"> <span style="font-size:16px;">SPI通信,最重要的就是读写函数的编写,如下:</span></span></span></p>
<h2 style="text-align:justify; margin-top:18px; margin-bottom:18px"><span style="font-family:Times;"></span></h2>
<h2 style="text-align:justify; margin-top:18px; margin-bottom:18px"><span style="font-family:Times;"><span style="font-size:16pt"><span style="line-height:173%">SPI初始化</span></span></span></h2>
<p style="text-align:justify"><span style="font-family:Times;"></span></p>
<h2 style="text-align:justify; margin-top:18px; margin-bottom:18px"><span style="font-family:Times;"><span style="font-size:16pt"><span style="line-height:173%">SPI回调函数</span></span></span></h2>
<p style="text-align:justify"><span style="font-size:16px;"><span style="font-family:Times;"> SPI回调函数用一个全局变量用来判断SPI是否传输完成。</span></span></p>
<p style="text-align:justify"><span style="font-family:Times;"></span></p>
<h2 style="text-align:justify; margin-top:18px; margin-bottom:18px"><span style="font-family:Times;"><span style="font-size:16pt"><span style="line-height:173%">LSM6DSL初始化</span></span></span></h2>
<p style="text-align:justify"><span style="font-size:16px;"><span style="font-family:Times;"> 我只使用了库函数中的寄存器,没有使用库函数,然后每次写入寄存器都读出来,确保每次都写入成功。</span></span></p>
<p style="text-align:justify"><span style="font-family:Times;"></span></p>
<h2 style="text-align:justify; margin-top:18px; margin-bottom:18px"><span style="font-family:Times;"><span style="font-size:16pt"><span style="line-height:173%"> 获取传感器的值</span></span></span></h2>
<p style="text-align:justify"><span style="font-family:Times;"><span style="font-size:10.5pt"> <span style="font-size:16px;">读出传感器的值,采用一次性全部读出的方法,这样最高效,库函数中是一个一个的读取,这样不推荐。然后是将寄存器的原始值转化为实际值,也即是乘以Sensitivity,库函数的方法是每次都从寄存器中读取Sensitivity,这样没有必要,直接将该值变成常量然后相乘,可以更少的操作SPI,提高效率。</span></span></span></p>
<p align="left" style="text-align:left"><span style="font-family:Times;"></span></p>
<p align="left" style="text-align:left"><span style="font-family:Times;"><span style="font-size:16px;"><span style="color:black"> 最后就是将读取出来的值,进行姿态解算,姿态解算详细可以参考我的这篇<a href="https://bbs.eeworld.com.cn/thread-1055540-1-1.html" target="_blank">帖子</a>。</span></span></span></p>
<p align="left" style="text-align:left"><span style="font-family:Times;"></span></p>
<p align="left" style="text-align:left"><span style="font-family:Times;"><span style="font-size:16px;"><span style="color:black"> 最后可以使用Jscope来查看波形,方法参考我之前的<a href="https://bbs.eeworld.com.cn/thread-869853-1-1.html" target="_blank">帖子</a>。</span></span></span></p>
<p align="left" style="text-align:left"><span style="font-family:Times;"></span></p>
<p align="left" style="text-align:left"><span style="font-size:16px;"><span style="font-family:Times;"><span lang="EN-US"><span style="color:black">GitHub</span></span><span style="color:black">工程下载地址:</span><a href="https://github.com/flyloong/NRF52832_LSM6DSL" style="color:blue; text-decoration:underline">https://github.com/flyloong/NRF52832_LSM6DSL</a></span></span></p>
<p><br />
<span style="font-family:Times;"><b><font color="#5E7384">此内容由EEWORLD论坛网友<font size="3">lb8820265</font>原创,如需转载或用于商业用途需征得作者同意并注明出处</font></b></span></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> <p>可以的。加油~ <span style="color:black">GitHub</span></p>
<p>大神太厉害了!</p>
页:
[1]