bsp驱动开发(bsp驱动开发怎么学)

bsp驱动开发(bsp驱动开发怎么学)

大家好,我是讯享网,很高兴认识大家。




讯享网

 <img src="https://files.sekorm.com/opt/fileStore/portal/brand/images/b0b1bec06fbd40d860fc295cba.PNG" alt="单片机,MCU,先楫半导体" style="position: absolute; left: -10000px;"/> <img src="https://www.sekorm.com/front/website/images/sekormContent.jpg" alt="单片机,MCU,先楫半导体" style="position: absolute; left: -10000px;"/> <img src="https://files.sekorm.com/opt/fileStore/portal/brand/images/b0b1bec06fbd40d860fc295cba.PNG" alt="单片机,MCU,先楫半导体" style="width: 0;height: 0;z-index: 0;display: block;"/> <img src="https://www.sekorm.com/front/website/images/sekormContent.jpg" alt="单片机,MCU,先楫半导体" style="width: 0;height: 0;z-index: 0;display: block;"/> <p><strong>一、概述</strong></p><p><strong>(一)RT-Thread设备驱动</strong></p><p>《RT-Thread设备驱动开发指南》书籍是RT-thread官方出品撰写,系统讲解RT-thread IO设备驱动开发方法,从三方面进行讲解。</p><p>基础篇:对RT-thread以及设备框架进行介绍,再分别介绍基于IO框架中常用的外设设备,分别是UART、PIN、SPI/QSPI、HWTIMER、PWM、RTC、ADC、DAC、WDT。</p><p>进阶篇:介绍基于IO框架中稍比基础篇复杂的外设设备,分别是SDIO、Touch、LCD、sensor、MTD nor、MTD nand、脉冲编码器、加解密设备、PM设备。</p><p>高级篇:介绍更为复杂的外设设备,分别是WLAN、ETH、audio mic、audio sound、USB、CAN。</p><p><br/></p><p><strong>(二)先楫hpmicro bsp</strong></p><p>为了深入了解本书的内容,得有一个单片机平台进行案例剖析,除了本书籍以STM32的arm平台进行介绍,还可以以先楫hpmicro单片机系列的riscv平台进行讲解。</p><p>刚好先楫在rtthread的bsp贡献上,也是十分丰富。无论是在rtthread studio的bsp包上,还是rtthread仓库的bsp支持上,都有提供了对应的支持。</p><p style="text-align: center;">rtthread studio的SDK支持,包括了先楫目前所有的MCU系列。<br/><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="768" height="310" alt="" style="width: 768px; height: 310px;"/></p><p>而在RT-thread上,bsp包的驱动支持上,也包括了rtthread大部分设备驱动。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="770" height="158" alt="" style="width: 770px; height: 158px;"/></p><p><strong>二、开发方法</strong></p><p><strong>(一)层级结构</strong></p><p>RT-Thread 提供了一套简单的 I/O 设备模型框架,如下图所示,它位于硬件和应用程序之间,共分成三层,从上到下分别是 I/O 设备管理层、设备驱动框架层、设备驱动层。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="839" height="372" alt=""/></p><p>而作为单片机的定时器外设hwtimer功能,要对接到该IO设备框架,需要实现hwtimer设备的操作方法、设备的注册、以及驱动的配置和驱动验证。本文也是基于该开发方法进行阐述。</p><p>上述对应的三层驱动层,在源码上,可以device-&gt;hwtimer-&gt;drv_hwtimer,其中drv_hwtimer是先楫官方实现的对接框架的驱动层。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="712" height="77" alt=""/></p><p>而设备驱动框架层,提供了ops操作接口,分别以下的接口需要驱动开发者实现注册。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="726" height="168" alt="" style="width: 726px; height: 168px;"/></p><p><strong>(二)创建hwtimer设备</strong><br/></p><p><br/></p><p>对hwtimer设备来说,在驱动开发时,需要先从rt_hwtimer_t结构中派生出新的hwtimer设备模型,然后根据自己的设备类型定义私有数据域。</p><p>同样先楫的hwtimer drivers也定义了自己的由rt_hwtimer_t派生出来的hwtimer设备。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="805" height="686" alt="" style="width: 805px; height: 686px;"/></p><p>当然一个MCU也支持多个hwtimer,一个hwtimer驱动可以支持多个hwtimer设备,可以通过使能BSP_USING_GPTMRx进行开启</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="720" height="340" alt="" style="width: 720px; height: 340px;"/></p><p><strong>(三)实现hwtimer设备的操作方法</strong></p><p>该操作方法就是上述所说的ops操作结构,属于驱动共用部分,也就是多个hwtimer设备可以共用一个ops操作。同样先楫也实现了该操作方法,并且注册到自身的hwtimer的父类hwtimer的ops中。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="690" height="218" alt=""/></p><p>简单举例个操作方法的实现,比如获取设备当前值count_get,原型如下:<br/></p><p>rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);</p><p>而先楫实现的count_get是以上的hpm_hwtimer_count_get,可以看到,内部是先从timer获取厂家自身定义的hwtimer设备,再从里面获取硬件定时器的基地址控制句柄,然后使用先楫自身的驱动API获取定时器的计数值进行返回。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="831" height="126" alt="" style="width: 831px; height: 126px;"/></p><p><strong>(四)注册hwtimer设备<br/></strong></p><p><br/></p><p>注册hwtimer设备,rtthread的IO设备框架提供了一个API:rt_device_hwtimer_register</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="754" height="64" alt="" style="width: 754px; height: 64px;"/></p><p>从传参句柄可知道,注册时需要提供设备句柄timer,设备名称name,用户自定义data作为传参。</p><p>在先楫的drv_hwtimer的驱动中,创建了个全局的hwtimer设备数组,里面包含了hwtimer的所有信息,包括设备名称。然后在注册初始化当中,轮询该数组使能的定时器设备依次注册到rtthread的hwtimer设备当中。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="755" height="908" alt="" style="width: 755px; height: 908px;"/></p><p>在开发指南当中,特别提到了一个注意点,hwtimer设备句柄需要实现rt_hwtimer_info,定义硬件定时器的特征信息,如计数的最大最小频率,最大计数值以及计数方向,而先楫也同样进行了实现。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="669" height="179" alt=""/></p><p><strong>(五)hwtimer设备中断处理</strong></p><p>在开发指南中,该部分也需要实现,目的是将定时时间导致的中断事件通知到hwtimer设备驱动框架,让驱动框架完成后续的处理并通知应用层。而该通知事件API则是rt_device_hwtimer_isr,该函数由rtthread的hwtimer设备驱动框架提供。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="590" height="85" alt=""/></p><p>MCU下的hwtimer驱动,需要在中断处理函数调用rt_device_hwtimer_isr函数,以便通知hwtimer设备驱动框架对应中断的发生。</p><p>在先楫的hwtimer驱动中也是基于这个实现通知事件。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="822" height="599" alt="" style="width: 822px; height: 599px;"/></p><p>从以上代码可知,hpmicro实现的hwtimer设备驱动接管了定时器的通用中断服务函数hpm_hwtmr_isr,然后内部调用了rtthread的hwtimer设备驱动框架提供的rt_device_hwtimer_isr中断处理函数,通<br/></p><p>知设备框架对应的定时器定时时间到达。</p><p><br/></p><p><strong>(六)驱动配置</strong></p><p>1、Kconfig配置<br/></p><p>驱动配置主要通过Kconfig实现,这里使用hpm6750evk2的bsp V1.4.1作为说明。</p><p>在board文件夹中,有个Kconfig配置了hpm6750evk2的板载驱动信息。对于定时器驱动的相关选项如下:</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="511" height="711" alt=""/></p><p>RT_USING_HWTIMER:RT-thread的hwtimer设备驱动框架下的代码对应的宏定义,控制hwtimer的驱动框架的相关代码是否会添加到工程中</p><p>BSP_USING_GPTMR:先楫的hwtimer驱动下的代码对应的宏定义,控制先楫的hwtimer驱动是否会添加到工程中</p><p>BSP_USING_GPTMR1: 使用先楫的定时器1<br/></p><p><br/></p><p>2、SConscript配置</p><p>libraries/drivers/SConscript文件为hwtimer驱动添加判断选项,如果定义了BSP_USING_GPTMR,则对应的驱动文件drv_hwtimer就会被添加到工程的源文件中。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//0th.png" title="" width="572" height="212" alt=""/></p><p><strong>三、驱动验证</strong></p><p>使用hpm6750evk2的bsp V1.4.1作为说明,新建timer_demo的示例工程。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//0th.png" title="" width="497" height="430" alt=""/></p><p>编译烧录代码,使用list_device查看设备,可以看到注册的设备已经包含了hwtimer这个设备。</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="471" height="208" alt=""/></p><p>运行自带的shell命令hwtimer_sample,该命令会运行5s的定时,查看readme文档</p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//0th.png" title="" width="424" height="138" alt=""/></p><p style="text-align: center;"><img src="https://files.sekorm.com/opt/fileStore/cms/nps/editor//th.png" title="" width="398" height="200" alt=""/></p><p><strong>四、总结</strong></p><p>1、基础篇包含UART、PIN、SPI/QSPI、HWTIMER、PWM、RTC、ADC、DAC、WDT这些外设,本文以先楫适配的HWTIMER驱动进行说明,能感受到先楫对于rtthread驱动适配的完整性。<br/></p><p>2、除了本文阐述的外设,其他基础外设同样适配完整。</p><p><br/></p> 

讯享网
小讯
上一篇 2025-05-13 21:11
下一篇 2025-04-24 11:44

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/184282.html