//------------------------
//主函数
//------------------------
int main(void)
{
Set_System();
Set_USBClock();
USB_Interrupts_Config();
USB_Init();
while(1) {
switch (usart_2_usb_process)
{
case USART_RECEIVE:
break;
case START_USART2USB:
usart_2_usb_send();
break;
case WAIT_USART2USB_END:
usart_2_usb_waitend();
break;
default:
break;
}
}
}
//--------------------
//hw_config.c
//--------------------
#include "stm32f10x_it.h"
#include "stm32lib\\usblib\\usb_lib.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "hw_config.h"
#include "platform_config.h"
#include "usb_pwr.h"
#include "stm32f10x_tim.h"
USART_InitTypeDef USART_InitStructure;
unsigned char buffer_in[BUFFER_SIZE];
unsigned char buffer_out[BUFFER_SIZE];
unsigned char *volatile pbuffer_in_usart;
unsigned char *volatile pbuffer_in_usb;
unsigned char *pbuffer_out_usart;
unsigned int TimeBase;
volatile unsigned short usart_2_usb_count;
volatile bool usart_2_usb_complete;
extern LINE_CODING linecoding;
void Set_System(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
initialize the PLL and update the SystemFrequency variable. */
SystemInit();
/* Enable GPIOA, GPIOD and USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 , ENABLE);
/* Enable TIM2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* PA Configure: USB DISCONNECT(PA8) as output */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
pbuffer_in_usart = buffer_in;
pbuffer_in_usb = buffer_in;
pbuffer_out_usart = buffer_out;
}
void Set_USBClock(void)
{
/* USBCLK = PLLCLK / 1.5 */
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
/* Enable USB clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
}
void Enter_LowPowerMode(void)
{
/* Set the device state to suspend */
bDeviceState = SUSPENDED;
}
void Leave_LowPowerMode(void)
{
DEVICE_INFO *pInfo = &Device_Info;
/* Set the device state to the correct state */
if (pInfo->Current_Configuration != 0)
{
/* Device configured */
bDeviceState = CONFIGURED;
}
else
{
bDeviceState = ATTACHED;
}
}
void USB_Interrupts_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USB_Cable_Config (FunctionalState NewState)
{
if (NewState == DISABLE)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
else
{
GPIO_SetBits(GPIOA, GPIO_Pin_8);
}
}
void USART_Config_Default(void)
{
/* USART1 default configuration */
USART_InitStructure.USART_BaudRate = 115200;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART Receive interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
bool USART_Config(void)
{
/* set the Stop bit*/
switch (linecoding.format)
{
case 0:
USART_InitStructure.USART_StopBits = USART_StopBits_1;
break;
case 1:
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
break;
case 2:
USART_InitStructure.USART_StopBits = USART_StopBits_2;
break;
default :
{
USART_Config_Default();
return (FALSE);
}
}
/* set the parity bit*/
switch (linecoding.paritytype)
{
case 0:
USART_InitStructure.USART_Parity = USART_Parity_No;
break;
case 1:
USART_InitStructure.USART_Parity = USART_Parity_Even;
break;
case 2:
USART_InitStructure.USART_Parity = USART_Parity_Odd;
break;
default :
{
USART_Config_Default();
return (FALSE);
}
}
/*set the data type : only 8bits and 9bits is supported */
switch (linecoding.datatype)
{
case 0x07:
/* With this configuration a parity (Even or Odd) should be set */
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
break;
case 0x08:
if (USART_InitStructure.USART_Parity == USART_Parity_No)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
}
else
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
}
break;
default :
{
USART_Config_Default();
return (FALSE);
}
}
if (linecoding.bitrate < 115200)
TimeBase = TIMER_ARR_10MS;
else if (linecoding.bitrate >= 256000)
TimeBase = TIMER_ARR_1MS;
else
TimeBase = TIMER_ARR_5MS;
Timer_Init(TimeBase);
USART_InitStructure.USART_BaudRate = linecoding.bitrate;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
return (TRUE);
}
*******************************************************************************/
void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
{
uint32_t i;
for (i = 0; i < Nb_bytes; i++)
{
USART_SendData(USART1, *(data_buffer + i));
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
void USART_2_USB_Send_Data(void)
{
if (linecoding.datatype == 7)
{
*pbuffer_in_usart++ = USART_ReceiveData(USART1) & 0x7F;
}
else if (linecoding.datatype == 8)
{
*pbuffer_in_usart++ = USART_ReceiveData(USART1);
}
if (pbuffer_in_usart == &buffer_in[BUFFER_SIZE])
pbuffer_in_usart = buffer_in;
}
void Get_SerialNum(void)
{
uint32_t Device_Serial0, Device_Serial1, Device_Serial2;
Device_Serial0 = *(__IO uint32_t*)(0x1FFFF7E8);
Device_Serial1 = *(__IO uint32_t*)(0x1FFFF7EC);
Device_Serial2 = *(__IO uint32_t*)(0x1FFFF7F0);
if (Device_Serial0 != 0)
{
Virtual_Com_Port_StringSerial[2] = (uint8_t)(Device_Serial0 & 0x000000FF);
Virtual_Com_Port_StringSerial[4] = (uint8_t)((Device_Serial0 & 0x0000FF00) >> 8);
Virtual_Com_Port_StringSerial[6] = (uint8_t)((Device_Serial0 & 0x00FF0000) >> 16);
Virtual_Com_Port_StringSerial[8] = (uint8_t)((Device_Serial0 & 0xFF000000) >> 24);
Virtual_Com_Port_StringSerial[10] = (uint8_t)(Device_Serial1 & 0x000000FF);
Virtual_Com_Port_StringSerial[12] = (uint8_t)((Device_Serial1 & 0x0000FF00) >> 8);
Virtual_Com_Port_StringSerial[14] = (uint8_t)((Device_Serial1 & 0x00FF0000) >> 16);
Virtual_Com_Port_StringSerial[16] = (uint8_t)((Device_Serial1 & 0xFF000000) >> 24);
Virtual_Com_Port_StringSerial[18] = (uint8_t)(Device_Serial2 & 0x000000FF);
Virtual_Com_Port_StringSerial[20] = (uint8_t)((Device_Serial2 & 0x0000FF00) >> 8);
Virtual_Com_Port_StringSerial[22] = (uint8_t)((Device_Serial2 & 0x00FF0000) >> 16);
Virtual_Com_Port_StringSerial[24] = (uint8_t)((Device_Serial2 & 0xFF000000) >> 24);
}
}
void Timer_Init(uint16_t Time_ARR)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM2); /* deinitiate */
TIM_TimeBaseStructure.TIM_Period = Time_ARR; /* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 23;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; /* Timing Mode :Channel1 */
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); /* TIM IT enable */
TIM_Cmd(TIM2, ENABLE); /* TIM3 enable counter */
}
volatile USB_USART_STATE usart_2_usb_process;
void usart_2_usb_send(void)
{
unsigned char *ptemp = NULL;
if (pbuffer_in_usb == &buffer_in[BUFFER_SIZE])
pbuffer_in_usb = buffer_in;
if(pbuffer_in_usb == pbuffer_in_usart) {
usart_2_usb_process = WAIT_USART2USB_END; // no data needed to send from USB
usart_2_usb_complete = TRUE;
return;
}
else if(pbuffer_in_usb > pbuffer_in_usart) { // if the pbuffer_in_usart is rollback
usart_2_usb_count = &buffer_in[BUFFER_SIZE] - pbuffer_in_usb;
}
else {
usart_2_usb_count = pbuffer_in_usart - pbuffer_in_usb;
}
if (usart_2_usb_count > VIRTUAL_COM_PORT_DATA_SIZE){
ptemp = pbuffer_in_usb;
UserToPMABufferCopy(ptemp, ENDP1_TXADDR, VIRTUAL_COM_PORT_DATA_SIZE);
SetEPTxCount(ENDP1, VIRTUAL_COM_PORT_DATA_SIZE);
usart_2_usb_count -= VIRTUAL_COM_PORT_DATA_SIZE;
pbuffer_in_usb += VIRTUAL_COM_PORT_DATA_SIZE;
}
else {
ptemp = pbuffer_in_usb;
UserToPMABufferCopy(ptemp, ENDP1_TXADDR, usart_2_usb_count);
SetEPTxCount(ENDP1, usart_2_usb_count);
pbuffer_in_usb += usart_2_usb_count;
usart_2_usb_count = 0;
}
usart_2_usb_process = WAIT_USART2USB_END;
SetEPTxValid(ENDP1);
}
#define CR1_CEN_Set ((uint16_t)0x0001)
void usart_2_usb_waitend(void)
{
if (usart_2_usb_complete == TRUE){
usart_2_usb_process = USART_RECEIVE;
TIM2->CR1 |= CR1_CEN_Set; // enable timer
usart_2_usb_complete = FALSE;
}
else {
}
}
extern __IO uint32_t count_out;
void usb_2_usart_send_data(void)
{
if (count_out == 0) {
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
SetEPRxValid(ENDP3);
}
else {
USART1->DR = *pbuffer_out_usart & (uint16_t)0x01FF;
pbuffer_out_usart++;
count_out--;
}
}