本帖最后由 KING_阿飞 于 2022-8-3 11:49 编辑
【雅特力AT32WB415系列蓝牙BLE 5.0 MCU】USB CDC 串口Printf打印调试信息
很荣幸能获得雅特力AT32WB415系列蓝牙BLE 5.0 MCU的评测机会,为此按照我以前的评测的习惯,附上我的Github:,所有评测代码均开源分享。
如果因为网络问题无法进入Github可以在Gitee中下载,可能会存在没有及时更新。Gitee:My_AT32WB415_Demo: 雅特力科技AT32WB415系列学习,从各个外设入手,学习各个功能。 (gitee.com)
一、USB协议移植
关于USB的协议大家可以去USB的官网:Front Page | USB-IF
我们主要是完成USB CDC 虚拟串口的移植,主要有以下几个文件:
库文件在官网的资料包中就有,没有找到可以去我仓库复制;官网做了一个适配这个MCU的配置文件,usb_conf.h
/**
**************************************************************************
* [url=home.php?mod=space&uid=1307177]@File[/url] usb_conf.h
* [url=home.php?mod=space&uid=252314]@version[/url] v2.0.2
* [url=home.php?mod=space&uid=311857]@date[/url] 2022-06-28
* [url=home.php?mod=space&uid=159083]@brief[/url] 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
二、移植过程中的问题
在移植过程中,我代码卡在了B处,这里一看就是没有中断处理函数,应该是OTGFS1_IRQHandler这个没有,我们经过查找,官方将其宏定义了,我们要重新给他写一下。
/**
* @brief this function handles otgfs interrupt.
* @param none
* @retval none
*/
void OTG_IRQ_HANDLER(void)
{
usbd_irq_handler(&otg_core_struct);
}
然后根据上篇文章的经验,我们还需要再写一个延时函数
void usb_delay_ms(uint32_t ms){
vTaskDelay(ms);
}
编写USB CDC 的Printf
//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;
}
}
/**
* @brief usb 48M clock select
* @param clk_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;
}
}
有这个USB串口调试也挺方便的,现在是有一个板载的AT-LINK暂时还看不到它的价值,之后如果实际使用的话,它的价值就大了。
三、效果展示