【雅特力AT32WB415系列蓝牙BLE 5.0 MCU】USB CDC 串口Printf打印调试信息
<div class='showpostmsg'> 本帖最后由 KING_阿飞 于 2022-8-3 11:49 编辑<p style="text-align: center;"><strong>【雅特力AT32WB415系列蓝牙BLE 5.0 MCU】USB CDC 串口Printf打印调试信息</strong></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><strong>很荣幸能获得雅特力AT32WB415系列蓝牙BLE 5.0 MCU的评测机会,为此按照我以前的评测的习惯,附上我的<a href="https://github.com/kings669/My_AT32WB415_Demo" target="_blank">Github</a>:,所有评测代码均开源分享。</strong></p>
<p style="text-align: center;">如果因为网络问题无法进入Github可以在Gitee中下载,可能会存在没有及时更新。Gitee:<a href="https://gitee.com/king-a-fei/My_AT32WB415_Demo">My_AT32WB415_Demo: 雅特力科技AT32WB415系列学习,从各个外设入手,学习各个功能。 (gitee.com)</a></p>
<p><strong>一、USB协议移植</strong></p>
<p><strong> </strong>关于USB的协议大家可以去USB的官网:<a href="https://usb.org/">Front Page | USB-IF</a></p>
<p> 我们主要是完成USB CDC 虚拟串口的移植,主要有以下几个文件:</p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle"> 库文件在官网的资料包中就有,没有找到可以去我仓库复制;官网做了一个适配这个MCU的配置文件,<strong>usb_conf.h</strong></p>
<pre>
<code class="language-cpp">/**
**************************************************************************
* <a href="home.php?mod=space&uid=1307177" target="_blank">@File </a> usb_conf.h
* <a href="home.php?mod=space&uid=252314" target="_blank">@version </a> v2.0.2
* <a href="home.php?mod=space&uid=311857" target="_blank">@date </a> 2022-06-28
* <a href="home.php?mod=space&uid=159083" target="_blank">@brief </a> usb config header file
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_CONF_H
#define __USB_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
#include "at32wb415_usb.h"
#include "at32wb415.h"
#include "stdio.h"
/** @addtogroup AT32WB415_periph_examples
* @{
*/
/** @addtogroup 415_USB_device_vcp_loopback
* @{
*/
/**
* @brief enable usb device mode
*/
#define USE_OTG_DEVICE_MODE
/**
* @brief enable usb host mode
*/
/* #define USE_OTG_HOST_MODE */
#define USB_ID 0
#define OTG_CLOCK CRM_OTGFS1_PERIPH_CLOCK
#define OTG_IRQ OTGFS1_IRQn
#define OTG_IRQ_HANDLER OTGFS1_IRQHandler
#define OTG_WKUP_IRQ OTGFS1_WKUP_IRQn
#define OTG_WKUP_HANDLER OTGFS1_WKUP_IRQHandler
#define OTG_WKUP_EXINT_LINE EXINT_LINE_18
#define OTG_PIN_GPIO GPIOA
#define OTG_PIN_GPIO_CLOCK CRM_GPIOA_PERIPH_CLOCK
#define OTG_PIN_SOF_GPIO GPIOA
#define OTG_PIN_SOF_GPIO_CLOCK CRM_GPIOA_PERIPH_CLOCK
#define OTG_PIN_SOF GPIO_PINS_8
/**
* @brief usb device mode config
*/
#ifdef USE_OTG_DEVICE_MODE
/**
* @brief usb device mode fifo
*/
/* otg1 device fifo */
#define USBD_RX_SIZE 128
#define USBD_EP0_TX_SIZE 24
#define USBD_EP1_TX_SIZE 20
#define USBD_EP2_TX_SIZE 20
#define USBD_EP3_TX_SIZE 20
/**
* @brief usb endpoint max num define
*/
#ifndef USB_EPT_MAX_NUM
#define USB_EPT_MAX_NUM 4
#endif
#endif
/**
* @brief usb host mode config
*/
#ifdef USE_OTG_HOST_MODE
#ifndef USB_HOST_CHANNEL_NUM
#define USB_HOST_CHANNEL_NUM 8
#endif
/**
* @brief usb host mode fifo
*/
/* otg1 host fifo */
#define USBH_RX_FIFO_SIZE 128
#define USBH_NP_TX_FIFO_SIZE 96
#define USBH_P_TX_FIFO_SIZE 96
#endif
/**
* @brief usb sof output enable
*/
/* #define USB_SOF_OUTPUT_ENABLE */
#define USB_VBUS_IGNORE
/**
* @brief usb low power wakeup handler enable
*/
/* #define USB_LOW_POWER_WAKUP */
void usb_delay_ms(uint32_t ms);
void usb_delay_us(uint32_t us);
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
</code></pre>
<p class="imagemiddle"><strong>二、移植过程中的问题</strong></p>
<p class="imagemiddle"> 在移植过程中,我代码卡在了B处,这里一看就是没有中断处理函数,应该是OTGFS1_IRQHandler这个没有,我们经过查找,官方将其宏定义了,我们要重新给他写一下。</p>
<p> </p>
<p class="imagemiddle" style="text-align: center;"></p>
<pre>
<code class="language-cpp">/**
* @briefthis function handles otgfs interrupt.
* @paramnone
* @retval none
*/
void OTG_IRQ_HANDLER(void)
{
usbd_irq_handler(&otg_core_struct);
}
</code></pre>
<p> 然后根据上篇文章的经验,我们还需要再写一个延时函数</p>
<pre>
<code class="language-cpp">void usb_delay_ms(uint32_t ms){
vTaskDelay(ms);
}
</code></pre>
<p>编写USB CDC 的Printf</p>
<pre>
<code class="language-cpp">//printf redefine
void usb_printf(const char *fmt,...)
{
static va_list ap;
uint16_t len = 0;
va_start(ap, fmt);
len = vsprintf((char *)usb_buf, fmt, ap);
va_end(ap);
/* send data to host */
if(usb_vcp_send_data(&otg_core_struct.dev, usb_buf, len) == SUCCESS)
{
return;
}
}</code></pre>
<p> </p>
<pre>
<code class="language-cpp">/**
* @briefusb 48M clock select
* @paramclk_s:USB_CLK_HICK, USB_CLK_HEXT
* @retval none
*/
void usb_clock48m_select(usb_clk48_s clk_s)
{
switch(system_core_clock)
{
/* 48MHz */
case 48000000:
crm_usb_clock_div_set(CRM_USB_DIV_1);
break;
/* 72MHz */
case 72000000:
crm_usb_clock_div_set(CRM_USB_DIV_1_5);
break;
/* 96MHz */
case 96000000:
crm_usb_clock_div_set(CRM_USB_DIV_2);
break;
/* 120MHz */
case 120000000:
crm_usb_clock_div_set(CRM_USB_DIV_2_5);
break;
/* 144MHz */
case 144000000:
crm_usb_clock_div_set(CRM_USB_DIV_3);
break;
default:
break;
}
}
</code></pre>
<p>有这个USB串口调试也挺方便的,现在是有一个板载的AT-LINK暂时还看不到它的价值,之后如果实际使用的话,它的价值就大了。</p>
<p><strong>三、效果展示</strong></p>
<p style="text-align: center;"><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=19B4y1b799&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</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> 感谢楼主的分享,补充了中断函数,USB调试各种波特率都可以自适应吧?
页:
[1]