⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_ep1.c

📁 HID-Ukey底层源码实现(st72651芯片) windows上层驱动
💻 C
字号:
/**************** (c) 2000  STMicroelectronics *******************************

NAME:		usb_ep1.c
PROJECT:	USB - ST7 FULL SPEED
VERSION:	v 1.0
CREATION:	03/01/2002
AUTHOR:		MICROCONTROLLER DIVISION / ST Rousset

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		Functions for sending and/or receiving data from/to EP1
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

MODIFICATIONS : 

******************************************************************************/
#include "mcu_conf.h"
#include "usb_reg.h"
#include "usb_def.h"
#include "usb_libs.h"
#include "usb_app.h"

//#ifdef	DECLARE_EP1

//extern unsigned char _EP_RxTx_Flag;	// D0=0: Transmiter is free
									// D0=1: Transmiter is busy
									// D1=0: Receiver is free
									// D1=1: Receiver is busy
#pragma DATA_SEG USB_RAM
static unsigned char *EP1_XBuffer;	// Current pointer to transmit buffer
static unsigned char EP1_XLength;	// Remain length to be sent
#ifdef	MCU_ST7265
static unsigned char *EP1_RBuffer;	// Current pointer to receive buffer
static unsigned char EP1_RLength;	// Remain length to be received
static unsigned char EP1_RSize;		// Bytes received in the receiving buffer
#endif

#pragma CODE_SEG USB_CODE 
/*-----------------------------------------------------------------------------
ROUTINE NAME : USB_SendDataEP1
INPUT/OUTPUT : *DataAddress: points to the buffer to be sent
			   Length: gives the length of data to be sent. Range is 1-255
RETURN       : REQ_ERROR if the transmitter is busy
			   REQ_SUCCESS if the transmit starts correctly
DESCRIPTION  : Transmit "Length" data from the given buffer to host PC
-----------------------------------------------------------------------------*/
char USB_SendDataEP1(unsigned char *DataAddress, unsigned char LengthToXmit)
{
	if (ValBit(_EP_RxTx_Flag, EV_EP1_IN))		// Check if the transmitter is busy
		return REQ_ERROR;
	if ((EP1TXR & EP_GOOD) == 0)				// Check if the EP is in DISABLE or STALL
		return REQ_ERROR;

	if (LengthToXmit > MAX_EP1_PACKET_SIZE) {
		EP1_XLength = LengthToXmit - MAX_EP1_PACKET_SIZE;	// Bytes remain to xmit
		EP1_XBuffer = DataAddress + MAX_EP1_PACKET_SIZE;	// Address for the next xmit
		asm LD		X, #MAX_EP1_PACKET_SIZE;	// Bytes to copy to EP buffer
	}
	else {
		EP1_XLength = 0;						// All data can be sent in one shot
		asm LD		X, LengthToXmit;			// Bytes to copy to EP buffer
	}

	asm {										// Copy data to EP buffer
		LD		CNT1TXR, X
		DEC		X
loop_copy:
		LD		A, ([DataAddress.w], X)
		LD		(EP1_IN, X), A
		DEC		X
		JRPL	loop_copy
	}

	USB_SetTxEP1Status(EP_VALID);
	SetBit(_EP_RxTx_Flag, EV_EP1_IN);
	return REQ_SUCCESS;
}

/*-----------------------------------------------------------------------------
ROUTINE NAME : USB_EP1_isSent
RETURN       : Non-zero if the data is sent on EP1
DESCRIPTION  : This function is called to enquire if the data is sent on EP1
-----------------------------------------------------------------------------*/
//char USB_EP1_isSent(void)
//{
//	return !ValBit(_EP_RxTx_Flag, EV_EP1_IN);
//}

/*-----------------------------------------------------------------------------
ROUTINE NAME : USB_EP1_XEvent
DESCRIPTION  : This function is called when there is a USB CTR on EP1 transmitter
-----------------------------------------------------------------------------*/
void _USB_EP1_XEvent(void)
{
	if (EP1_XLength == 0) {
		ClrBit(_EP_RxTx_Flag, EV_EP1_IN);		// Finish sending if there is no remain data
		return;
	}

	if (EP1_XLength > MAX_EP1_PACKET_SIZE) {	// If more than one packet to send
		EP1_XLength -= MAX_EP1_PACKET_SIZE;		// Descrease number of remain bytes
		EP1_XBuffer += MAX_EP1_PACKET_SIZE;		// Increase sending pointer for next send
		asm LD		X, #MAX_EP1_PACKET_SIZE;	// Number of bytes to copy to EP buffer
	}
	else {
		asm LD		X, EP1_XLength;				// Number of bytes to copy to EP buffer
		EP1_XLength = 0;						// All data can be sent in one shot
	}

	asm {										// Copy data from user buffer to EP buffer
		LD		A, EP1_XBuffer
		LD		_LEX, A
		LD		A, EP1_XBuffer:1
		LD		_LEX:1, A

		LD		CNT1TXR, X
		DEC		X
loop_copy:
		LD		A, ([_LEX.w], X)
		LD		(EP1_IN, X), A
		DEC		X
		JRPL	loop_copy
	}

	USB_SetTxEP1Status(EP_VALID);
}

#ifdef	MCU_ST7265
/*-----------------------------------------------------------------------------
ROUTINE NAME : USB_RecvDataEP1
INPUT/OUTPUT : "DataAddress" is the buffer to receive the data
			   "Length" is the size of the buffer
RETURN       : REQ_ERROR if the receiver is busy
			   REQ_SUCCESS if the receiving starts correctly
DESCRIPTION  : Enable the receiver and receive the data to the buffer
			   Use USB_TakeDataEP1() to check if the receiving is finished
-----------------------------------------------------------------------------*/
char USB_RecvDataEP1(unsigned char *DataAddress, unsigned char Length)
{
	if (ValBit(_EP_RxTx_Flag, EV_EP1_OUT))	// Check if the receiver is busy
		return REQ_ERROR;
	if ((EP1RXR & EP_GOOD) == 0)			// Check if the EP is in DISABLE or STALL
		return REQ_ERROR;

	EP1_RLength = Length;					// The number of bytes to receive
	EP1_RBuffer = DataAddress;				// The user buffer pointer
	EP1_RSize = 0;							// The number of bytes received
	SetBit(_EP_RxTx_Flag, EV_EP1_OUT);		// Mark the receiver as busy

	CNT1RXR = MAX_EP1_PACKET_SIZE;
	USB_SetRxEP1Status(EP_VALID);
	return REQ_SUCCESS;
}

/*-----------------------------------------------------------------------------
ROUTINE NAME : USB_TakeDataEP1
INPUT/OUTPUT : none
RETURN       : 0xFF if the receiving does not finish
			   otherwise the number of actual received bytes
DESCRIPTION  : check if the receiving finishes
-----------------------------------------------------------------------------*/
unsigned char USB_TakeDataEP1(void)
{
	if (ValBit(_EP_RxTx_Flag, EV_EP1_OUT))
		return 0xFF;			// The receiver is empty, try later

	return EP1_RSize;
}

/*-----------------------------------------------------------------------------
ROUTINE NAME : USB_EP1_REvent
DESCRIPTION  : This function is called when there is a USB CTR on EP1 receiver
WARNING      : This function may be called from interrupt routine
-----------------------------------------------------------------------------*/
void _USB_EP1_REvent(void)
{
	unsigned char size = MAX_EP1_PACKET_SIZE - CNT1RXR;	// Number of bytes received

	if (size < EP1_RLength) {					// Not all bytes have been received
		EP1_RSize += size;						// Increase total byte number
		EP1_RLength -= size;					// Decrease remain byte number
		if (CNT1RXR != 0)
			ClrBit(_EP_RxTx_Flag, EV_EP1_OUT);	// Finish receiving on a short packet

		asm LD	X, size							// Going to copy data to user buffer
	}
	else {										// All bytes have been received
		ClrBit(_EP_RxTx_Flag, EV_EP1_OUT);		// Finish receiving
		EP1_RSize += EP1_RLength;				// Increase total byte number
		asm LD	X, EP1_RLength;					// Going to copy data to user buffer
		EP1_RLength = 0;						// No byte remain to receive
	}

	asm {							// Copy received bytes to the user buffer
		DEC		X
		JRMI	recv_end
		LD		A, EP1_RBuffer
		LD		_LEX, A
		LD		A, EP1_RBuffer:1
		LD		_LEX:1, A
recv_copy:
		LD		A, (EP1_OUT, X)
		LD		([_LEX.w], X), A
		DEC		X
		JRPL	recv_copy
recv_end:
	}

	if (ValBit(_EP_RxTx_Flag, EV_EP1_OUT)) {	// When there are more date to receive
		EP1_RBuffer += size;					// Increase user buffer pointer
		CNT1RXR = MAX_EP1_PACKET_SIZE;
		USB_SetRxEP1Status(EP_VALID);			// Enable the next receiving
	}
}
#endif	// MCU_ST7265

//#endif	// DECLARE_EP1

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -