主机环境:Windows 7 SP1
开发环境:MDK5.18
目标板:STM32F103C8T6
开发库:STM32F1Cube库和STM32_USB_Device_Library
距离之前的STM32 USB学习又过去了N个月,想起最初想学习USB的初衷就是学习一下UVC协议,了解一下图像的传输,在逛STM32社区的时候有看到一句话:以前使用单片机必须熟悉I2C、SPI、UART等通信协议,但现在也必须熟悉USB通信协议了,因为目前主流的设备几乎都是USB接口的,在USB接口上又可以实现I2C、SPI、UART等协议,USB不可谓不强大。废话就到这吧。
经过之前的USB学习,基本上了解了STM32的USB外设,因此开始了USB接口之上的UVC的学习,中间可以说是一波三折,其中芯片还烧坏了一次,又买的新的芯片焊上去的,历史的经验告诉我们,学习一个新的东西,总不是一帆风顺的,UVC全程为USB Video Class--USB视频设备类,用于实现在USB接口上传输视频的功能,目前的UVC规范版本是1.5,可以从USB官网下载,其清单如下:
其中我们需要熟悉的是UVC 1.5 Class Specification文档,USB_Video_Example 1.5文档以及USB_Video_Payload_XX_1.5(一种)文档,由于是初学者因此这里我看的是USB_Video_Payload_MJPEG_1.5文档,也建议大家先学习该文档,毕竟新东西要从简单的开始学起。USB_Video_Example_1.5文档给出了使用MJPEG负载的示例,我们敲代码时可以用到其中的描述符。
UVC规范中说明了一个UVC设备需要实现一个VC(Video Control)接口和若干个VS(Video Streaming)接口,其中VC接口用于控制设备的功能,而VS接口用于传输视频数据流。在最简单的情况下,有一个VC接口和一个VS接口,这就是接下来我们需要实现的。在未熟悉UVC规范的情况下我们也可以把代码框架搭建起来,STM32_USB_Device_Library库是一个很方便扩展的库,因为它把内核和设备类区分出来了,我们要想实现UVC就要新建一个设备类文件夹,刚好UVC和UAC有那么一点类似之处,我们可以把AUDIO中的文件拷贝一份到UVC文件夹下并修改文件名,这样我们就有了usbd_uvc以及usbd_uvc_if文件了,至于usbd_conf,usbd_desc文件只要把之前的VCP例程中的文件稍作修改就可以使用了,这样我们的UVC代码框架就算完成了,在代码实现中就要参看Example文档一步步来完善就可以了,由于是首次学习,UVC的代码框架并不是很好,因此usbd_uvc_if文件其实是没有用到的,在以后再修改吧。因为在实现中,我们只需要把视频流数据发往USB主机即可,没有什么其他的功能要实现,当然在以后有更多功能要求时类接口文件就需要好好实现了,其中usbd_uvc_if.h文件内容如下:
/**
******************************************************************************
* @file USB_Device/UVC_Standalone/Inc/usbd_uvc_if.h
* @author MCD Application Team
* @version V1.2.0
* @date 19-June-2015
* @brief Header for usbd_uvc_if.c file.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USBD_UVC_IF_H
#define __USBD_UVC_IF_H
/* Includes ------------------------------------------------------------------*/
#include "usbd_uvc.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
extern USBD_UVC_ItfTypeDef USBD_UVC_fops;
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
#endif /* __USBD_UVC_IF_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
可以看到文件基本上是空的,而usbd_uvc_if.c文件内容如下:
/**
******************************************************************************
* @file usbd_uvc_if.c
* @author MCD Application Team
* @version V2.4.1
* @date 19-June-2015
* @brief Generic media access Layer.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/** @addtogroup STM32_USB_DEVICE_LIBRARY
* @{
*/
/** @defgroup USBD_UVC
* @brief usbd core module
* @{
*/
/** @defgroup USBD_UVC_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup USBD_UVC_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup USBD_UVC_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_UVC_Private_FunctionPrototypes
* @{
*/
static int8_t UVC_Itf_Init (void);
static int8_t UVC_Itf_DeInit (void);
static int8_t UVC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length);
USBD_UVC_ItfTypeDef USBD_UVC_fops =
{
UVC_Itf_Init,
UVC_Itf_DeInit,
UVC_Itf_Control,
};
/* TIM handler declaration */
/* USB handler declaration */
/* Private functions ---------------------------------------------------------*/
/**
* @brief TEMPLATE_Init
* Initializes the UVC media low layer
* @param None
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t UVC_Itf_Init(void)
{
/*
Add your initialization code here
*/
return (0);
}
/**
* @brief TEMPLATE_DeInit
* DeInitializes the UVC media low layer
* @param None
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t UVC_Itf_DeInit(void)
{
/*
Add your deinitialization code here
*/
return (0);
}
/**
* @brief TEMPLATE_Control
* Manage the UVC class requests
* @param Cmd: Command code
* @param Buf: Buffer containing command data (request parameters)
* @param Len: Number of data to be sent (in bytes)
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t UVC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
{
return (0);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
该文件也是空,但我们在UVC类文件中需要用到这个接口(为了日后扩展使用),当然如果闲麻烦的话这两个文件可以直接删除的。代码实现还是从最简单的开始即usbd_desc文件,头文件当然不需要改动了,我们改动的只有usbd_desc.c文件,在该文件中我们改动的也只是描述符而已,如下:
/**
******************************************************************************
* @file USB_Device/UVC_Standalone/Src/usbd_desc.c
* @author MCD Application Team
* @version V1.2.0
* @date 19-June-2015
* @brief This file provides the USBD descriptors and string formating method.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distribut