📄 usb.cpp
字号:
extern "C"
{
#include"stm32f10x_rcc.h"
#include"stm32f10x_gpio.h"
#include"stm32f10x_usart.h"
}
//-----------------------------------------------------------------------------
#include<stdio.h>
//-----------------------------------------------------------------------------
#include"stm32f10x_usb.hpp"
#include"usb.hpp"
#include"usb_descriptor.hpp"
//-----------------------------------------------------------------------------
USB_Class USB;
//-----------------------------------------------------------------------------
void USB_Class::Initialization(void)
{
RCC_APB2ENR_BITS[GPIOC_EN] =1;
//相应引脚配置
{ //复用功能(推挽)输出模式
GPIOC_CRH_BITS[CNF13_1] =1;
GPIOC_CRH_BITS[CNF13_0] =0;
//最大速度50MHz
GPIOC_CRH_BITS[MODE13_1]=1;
GPIOC_CRH_BITS[MODE13_0]=1;
}
GPIOC_BSRR_BITS[BS13]=1;
}
//-----------------------------------------------------------------------------
#include"stm32f10x_systick.hpp"
//-----------------------------------------------------------------------------
void USB_Class::Open(void)
{
usb0.Configuration();
this->Initialization();
CNTR_BITS[PWDN]=0;
CNTR_BITS[FRES]=1;
ISTR=0;
{
CNTR_BITS[CTRM]=1;
CNTR_BITS[ERRM]=1;
CNTR_BITS[WKUPM]=1;
CNTR_BITS[SUSPM]=1;
CNTR_BITS[RESETM]=1;
CNTR_BITS[SOFM]=1;
CNTR_BITS[ESOFM]=1;
}
CNTR_BITS[FRES]=0;
GPIOC_BSRR_BITS[BR13]=1;
}
//-----------------------------------------------------------------------------
void USB_Class::Reset(void)
{
{
usb0.EP0R.TYPE(CONTROL);
usb0.EP0R.STAT_RX_TX(VALID,NAK);
usb0.EP0R.KIND(0);
usb0.SetAddress(0);
EP0_ADDR_RX=0x18;
EP0_ADDR_TX=EP0_ADDR_RX+8;
usb0.EP0R.Set_COUNT_RX(8);
usb0.EP0R.Rx_Buffer=(volatile Four_Byte*)(0x40006000+(EP0_ADDR_RX<<1));
usb0.EP0R.Tx_Buffer=(volatile unsigned long*)(0x40006000+(EP0_ADDR_TX<<1));
this->Setup_Buffer=(Four_Byte*)usb0.EP0R.Rx_Buffer;
}
{
usb0.EP1R.BITS[EA_3]=0;
usb0.EP1R.BITS[EA_2]=0;
usb0.EP1R.BITS[EA_1]=0;
usb0.EP1R.BITS[EA_0]=1;
usb0.EP1R.TYPE(INTERRUPT);
usb0.EP1R.STAT_RX_TX(VALID,NAK);
EP1_ADDR_RX=EP0_ADDR_TX+0x40;
EP1_ADDR_TX=EP1_ADDR_RX+0x40;
usb0.EP1R.Set_COUNT_RX(64);
usb0.EP1R.Rx_Buffer=(volatile Four_Byte*)(0x40006000+(EP1_ADDR_RX<<1));
usb0.EP1R.Tx_Buffer=(volatile unsigned long*)(0x40006000+(EP1_ADDR_TX<<1));
}
#ifdef USB_BUFFER_DEBUG
{
printf("初始化\r\n");
printf("EP0_ADDR__TX:0x%.8x\r\n",EP0_ADDR_TX);
printf("EP0_COUNT_TX:0x%.8x\r\n",EP0_COUNT_TX);
printf("EP0_ADDR__RX:0x%.8x\r\n",EP0_ADDR_RX);
printf("EP0_COUNT_RX:0x%.8x\r\n",EP0_COUNT_RX);
printf("USB_EP0R=0x%.4x\r\n",usb0.EP0R.WORD[0]);
printf("EP1_ADDR__TX:0x%.8x\r\n",EP1_ADDR_TX);
printf("EP1_COUNT_TX:0x%.8x\r\n",EP1_COUNT_TX);
printf("EP1_ADDR__RX:0x%.8x\r\n",EP1_ADDR_RX);
printf("EP1_COUNT_RX:0x%.8x\r\n",EP1_COUNT_RX);
printf("USB_EP1R=0x%.4x\r\n",usb0.EP1R.WORD[0]);
}
#endif
}
//-----------------------------------------------------------------------------
#define USB_SETUP_REQUEST_TYPE this->Setup_Buffer[0].BYTE[0]
#define USB_SETUP_REQUEST this->Setup_Buffer[0].BYTE[1]
#define USB_SETUP_VALUE this->Setup_Buffer[1].WORD[0]
#define USB_SETUP_INDEX this->Setup_Buffer[2].WORD[0]
#define USB_SETUP_LENGTH this->Setup_Buffer[3].WORD[0]
//-----------------------------------------------------------------------------
void USB_Class::ISR(void)
{
switch(ISTR&0x000f)
{
case 0:
usb0.EP0R.STAT_RX_TX(NAK,NAK);
if(usb0.EP0R.BITS[CTR_RX])
{
usb0.EP0R.Clear_CTR_RX();
if(usb0.EP0R.BITS[SETUP])
{
#ifdef USB_SETUP
{
printf("\r\n");
printf("-- 范德伟 --\r\n");
printf("SETUP - EP0R=0x%.4x\r\n",usb0.EP0R.WORD[0]);
}
#endif
this->Setup();
}
else
{
EP0_COUNT_TX=0;
#ifdef USB_SETUP
{
printf("OUT - EP0R=0x%.4x\r\n",usb0.EP0R.WORD[0]);
}
#endif
}
}
else if(usb0.EP0R.BITS[CTR_TX])
{
usb0.EP0R.Clear_CTR_TX();
EP0_COUNT_TX=0;
#ifdef USB_SETUP
{
printf("IN - EP0R=0x%.4x\r\n",usb0.EP0R.WORD[0]);
printf("USB_SETUP_REQUEST=0x%.2x\r\n",USB_SETUP_REQUEST);
}
#endif
switch(USB_SETUP_REQUEST)
{
case 5:
usb0.SetAddress(USB_SETUP_VALUE);
break;
}
}
usb0.EP0R.Set_COUNT_RX(8);
usb0.EP0R.STAT_RX_TX(ACK,ACK);
break;
case 1:
usb0.EP1R.STAT_RX_TX(NAK,NAK);
if(usb0.EP1R.BITS[CTR_RX])
{
usb0.EP1R.Clear_CTR_RX();
EP1_COUNT_TX=0;
usb0.Reserived_Data=1;
usb0.EP1R.Set_COUNT_RX(64); //清接收缓冲区
usb0.EP1R.STAT_RX_TX(ACK,NAK);
}
else if(usb0.EP1R.BITS[CTR_TX])
{
usb0.EP1R.Clear_CTR_TX();
EP1_COUNT_TX=0;
usb0.EP1R.Set_COUNT_RX(64);
usb0.EP1R.STAT_RX_TX(ACK,NAK);
}
break;
}
}
//-----------------------------------------------------------------------------
void USB_Class::Setup(void)
{
#ifdef USB_SETUP
{
printf("0x%.8x\r\n",this->Setup_Buffer[0]);
printf("0x%.8x\r\n",this->Setup_Buffer[1]);
printf("0x%.8x\r\n",this->Setup_Buffer[2]);
printf("0x%.8x\r\n",this->Setup_Buffer[3]);
}
#endif
if(USB_SETUP_LENGTH)
{
switch(USB_SETUP_REQUEST)
{
case 6:
#ifdef USB_SETUP
{
printf("获取");
}
#endif
this->Get_Descriptor();
break;
}
#ifdef USB_SETUP
{
unsigned int temp;
temp =EP0_COUNT_TX>>1;
temp+=EP0_COUNT_TX%2;
for(int i=0;i<temp;i++)
printf("0x%.8x\r\n",usb0.EP0R.Tx_Buffer[i]);
printf("ACK Length %d\r\n",temp);
}
#endif
}
else
{
EP0_COUNT_TX=0;
#ifdef USB_SETUP
printf("ACK Length %d\r\n",0);
#endif
}
}
//-----------------------------------------------------------------------------
void USB_Class::Get_Descriptor(void)
{
switch(this->Setup_Buffer[1].BYTE[1])
{
case 1:
#ifdef USB_SETUP
{
printf("设备描述符\r\n");
}
#endif
usb0.EP0R.Send((unsigned short*)Device_Descriptor,Device_Descriptor[0]);
break;
case 2:
#ifdef USB_SETUP
{
printf("配置描述符\r\n");
}
#endif
if(USB_SETUP_LENGTH==Config_Descriptor[0])
usb0.EP0R.Send((unsigned short*)Config_Descriptor,Config_Descriptor[0]);
else
usb0.EP0R.Send((unsigned short*)Config_Descriptor,Config_Descriptor[2]);
break;
case 3:
#ifdef USB_SETUP
{
printf("字符串描述符 - ");
}
#endif
switch(this->Setup_Buffer[1].BYTE[0])
{
case 0:
#ifdef USB_SETUP
{
printf("语言代码\r\n");
}
#endif
usb0.EP0R.Send((unsigned short*)StringLangID,StringLangID[0]);
break;
case 1:
#ifdef USB_SETUP
{
printf("厂商描述符\r\n");
}
#endif
usb0.EP0R.Send((unsigned short*)StringVendor,StringVendor[0]);
break;
case 2:
#ifdef USB_SETUP
{
printf("产品描述符\r\n");
}
#endif
usb0.EP0R.Send((unsigned short*)StringProduct,StringProduct[0]);
break;
case 3:
#ifdef USB_SETUP
{
printf("序列号描述符\r\n");
}
#endif
break;
}
break;
case 6:
#ifdef USB_SETUP
{
printf("Qualifier\r\n");
}
#endif
EP0_COUNT_TX=0;
break;
case 0x22:
#ifdef USB_SETUP
{
printf("报表描述符\r\n");
}
#endif
usb0.EP0R.Send((unsigned short*)Report_Descriptor,REPORT_DESC_SIZE);
break;
default:
printf("\r\n");
EP0_COUNT_TX=0;
break;
}
}
//-----------------------------------------------------------------------------
void USB_Class::Set_Address(void)
{
EP0_COUNT_TX=0;
}
//-----------------------------------------------------------------------------
void USB_Class::Set_Configration(void)
{
EP1_ADDR_RX=EP0_ADDR_TX+0x40;
EP1_ADDR_TX=EP1_ADDR_RX+0x40;
usb0.EP1R.Set_COUNT_RX(64);
usb0.EP1R.Rx_Buffer=(volatile Four_Byte*)(0x40006000+(EP1_ADDR_RX<<1));
usb0.EP1R.Tx_Buffer=(volatile unsigned long*)(0x40006000+(EP1_ADDR_TX<<1));
EP0_COUNT_TX=0;
}
//-----------------------------------------------------------------------------
extern "C" void USB_LP_CAN_RX0_IRQHandler(void);
//-----------------------------------------------------------------------------
void USB_LP_CAN_RX0_IRQHandler(void)
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("\r\n");
printf("进入中断 ISTR:0x%.4x\r\n",ISTR);
}
#endif
if(ISTR_BITS[15])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("正确的传输\r\n");
}
#endif
USB.ISR();
}
if(ISTR_BITS[14])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("包缓冲区溢出\r\n");
}
#endif
ISTR_BITS[14]=0;
}
if(ISTR_BITS[13])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("出错\r\n");
}
#endif
ISTR_BITS[13]=0;
}
if(ISTR_BITS[12])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("唤醒请求\r\n");
}
#endif
ISTR_BITS[12]=0;
}
if(ISTR_BITS[11])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("挂起请求\r\n");
}
#endif
ISTR_BITS[11]=0;
}
if(ISTR_BITS[10])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("复位请求\r\n");
}
#endif
USB.Reset();
ISTR_BITS[10]=0;
}
if(ISTR_BITS[9])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("帧首标志\r\n");
}
#endif
ISTR_BITS[9]=0;
}
if(ISTR_BITS[8])
{
#ifdef USB_INTRUPTER_DEBUG
{
printf("出现期望的帧首\r\n");
}
#endif
ISTR_BITS[8]=0;
}
#ifdef USB_INTRUPTER_DEBUG
{
printf("出中断\r\n");
}
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -