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

📄 d12.c

📁 单片机和D12通讯的C程序.实现了单片机通过USB口和电脑通讯.
💻 C
字号:

#include "InsUsb.h"

//
//*************************************************************************
//	Parameter:
//		In : bAddress	: The USB address that PDIUSBD12 will be set
//			 bEnable	: If 1, new USB address will be enabled,vice visa
//		Out: None
//	Function:
//		PDIUSBD12 Register: 0xD0H
//		Set USB address of PDIUSBD12,and if bEanble is 1,the address will 
//		be valid immediately.
void D12SetAddressEnable(unsigned char bAddress, BOOL bEnable)
{
	D12_COMMAND_ADDRESS =  0xD0;

	if(bEnable)		
		bAddress |= 0x80;
	else
		bAddress &= 0x7F;

	D12_DATA_ADDRESS = bAddress;
}

//
//*************************************************************************
//	Parameter:
//		In : bEndpointEnable: Only the last bit is meaningful,if it is 1,
//					          the select endpont will be enabled
//		Out: None
//	Function:
//		PDIUSBD12 Register: 0xD8H
//		The function is to set generic/isochronous endpoint enabled or disabled
//		0 disable endpoint1 and endpoint2,1 enable
void D12SetEndpointEnable(unsigned char bEndpointEnable)
{
	D12_COMMAND_ADDRESS =  0xD8;
	D12_DATA_ADDRESS = bEndpointEnable;
}

//
//*************************************************************************
//	Parameter:
//		In : bConfiguration	: The first byte 
//			 bClockkDivision: The second byte 
//		Out: None
//	Function:
//		PDIUSBD12 Register: 0xF3H
//		The function set PDIUSBD12 work mode
void D12SetMode(unsigned char bConfiguration, unsigned char bClockkDivision)
{
	D12_COMMAND_ADDRESS =  0xF3;
	D12_DATA_ADDRESS = bConfiguration;
	D12_DATA_ADDRESS = bClockkDivision;
}

//
//*************************************************************************
//	Parameter:
//		In : 
//		Out: 
//	Function:

void D12SetDMA(unsigned char bValue)
{
	D12_COMMAND_ADDRESS =  0xFB;	
	D12_DATA_ADDRESS = bValue;
}


//
//*************************************************************************
//	Parameter:
//		In : None
//		Out: One byte
//	Function:
//		PDIUSBD12 Register: 0xF4H
//		Read interrupt register of PDIUSBD12,and will read two bytes,but the 
//		second byte is only one bit is meaningful and relative only to DMA,
//		since PDIUSBD12 Develop Board doesn't implementate DMA,so the second 
//		byte has no use and will not return in this function.
unsigned char D12ReadInterruptRegister(void)
{
	unsigned char bUsbIntByte1;
	unsigned char bUsbIntByte2;

	D12_COMMAND_ADDRESS =  0xF4;
	bUsbIntByte1 = D12_DATA_ADDRESS ;	//read the first byte
	bUsbIntByte2 = D12_DATA_ADDRESS ;	//read the second byte
	return (bUsbIntByte1);
}

//
//*************************************************************************
//	Parameter:
//		In :bEndpointIndex:
//		Out:One byte
//	Function:
//		PDIUSBD12 Register: 0x00H-0x05H
//		Select Endpoint command will initialezes an internal pointer  to the
//		start of the selected buffer.The last two bits of returned byte mans
//		whether the buffer is full/empty and whether the selected endpoint is
//		in stall status
unsigned char D12SelectEndpoint(unsigned char bEndpointIndex)
{
	unsigned char data bEndpointStatus;
	D12_COMMAND_ADDRESS =  bEndpointIndex;
	bEndpointStatus = D12_DATA_ADDRESS ;

	return bEndpointStatus;
	
}


//
//*************************************************************************
//	Parameter:
//		In : bEndpointAddress : select this endpoint to read last transaction status
//		Out: One byte,Last transaction status
//	Function:
//		PDIUSBD12 Register: 0x40H-0x45H
//		Select Endpoint and read last transaction status

unsigned char D12ReadLastTransactionStatus(unsigned char bEndpointIndex)
{
	unsigned char data bLastTransactionStatus;
	D12_COMMAND_ADDRESS =  bEndpointIndex;
	bLastTransactionStatus = D12_DATA_ADDRESS ;
	return bLastTransactionStatus;
}

//
//*************************************************************************
//	Parameter:
//		In : bEndpointIndex	: the endpont index of PDIUSBD12
//			 bExpectedLength: length want to read
//			 pBuffer		: pointer,the start address of buffer which received
//							  data will be write to
//		Out: One byte		: the byte count of receive buffer ,
//							  if bExpectedLength is less than length that the buffer
//							  actually received,return 0xFF as an error flag.
//	Function:
//		PDIUSBD12 Register: 0xF0H
//		Read buffer of PDIUSBD12.
unsigned char D12ReadBuffer(unsigned char bEndpointIndex, unsigned int bExpectedLength,unsigned char * pBuffer)
{
	unsigned char data i, bByteCount=0;
	unsigned char data bEndpointBufferFullEmpty;

	D12_COMMAND_ADDRESS =  bEndpointIndex;	//select endpoint,the optional read one byte is not read

	bEndpointBufferFullEmpty = D12_DATA_ADDRESS; //probe: whether this buffer has valid data packet

	if (bEndpointBufferFullEmpty & D12_BUFFER_FULL_EMPTY_MASK)
	{
		//if buffer is full
		D12_COMMAND_ADDRESS =  D12_READ_BUFFER_SELECTED_ENDPOINT;
		bByteCount = D12_DATA_ADDRESS ;			//the first byte has no meaning
		bByteCount = D12_DATA_ADDRESS ;			//the data byte count
	



		if ((unsigned int)(bByteCount) <= bExpectedLength)
		{
			#ifndef _INT_MODE_	
				#ifdef _Debug_
					printf("Receive buffer:");
				#endif
			#endif
			for(i=0; i<bByteCount; i++)	
			{
				*pBuffer = D12_DATA_ADDRESS ;
				#ifndef _INT_MODE_	
					#ifdef _Debug_
						printf("%x,",(unsigned int)(*pBuffer));
					#endif
				#endif
				pBuffer++;
			}
			#ifndef _INT_MODE_	
				#ifdef _Debug_	
					printf("\n");
				#endif
			#endif
		}
		else
		{
			//When received data length is longer than expected length,it show an error happneed.
			//For PDIUSBD12,no endpoint buffer length is longer than 64,so we return 0xff to 
			//flag an error happened.
			bByteCount = D12_BUFFER_LENGTH_ERROR;
		}	
		//whether error is happened or not,we should clear buffer
		D12_COMMAND_ADDRESS =  D12_CLEAR_BUFFER_SELECTED_ENDPOINT;			//after read buffer finished,clear buffer


	}

	return bByteCount;		
}

//
//*************************************************************************
//	Parameter:
//		In : bEndpointIndex	: the endpoint index of PDIUSBD12
//			 bLength		: data byte count that will write to transmit buffer
//			 pBuffer		: pointer,start address of data source,which data 
//							  will  write to transmit buffer
//		Out: None
//	Function:
//		PDIUSBD12 Register: 0xF0H
//		Write data to transmit buffer and validae it. When received a corresponding IN token in
//		interrupt routine,those data will be transmit to HOST.
void D12WriteBuffer(unsigned char bEndpointIndex,unsigned char bLength, unsigned char * pBuffer)
{
	unsigned char i;

	D12_COMMAND_ADDRESS =  bEndpointIndex;	//select endpoint,the optional read one byte is not read

	D12_COMMAND_ADDRESS =  0xF0;			//write endpoint buffer
	D12_DATA_ADDRESS = 0;					//first byte is reserved,should wirte 0x00
	D12_DATA_ADDRESS = bLength;				//the second byte is the data length that will be 
											//write to buffer
	#ifdef _Debug_
	printf("write buffer:");
	#endif
	for(i=0; i<bLength; i++)				//write buffer
	{
		D12_DATA_ADDRESS = *pBuffer;
		#ifdef _Debug_
		printf("%x,",(unsigned int)(*pBuffer));
		#endif
		pBuffer++;
	}
	#ifdef _Debug_	
	printf("\n");
	#endif
	D12_COMMAND_ADDRESS =  0xFA;			//validate buffer
}

//
//*************************************************************************
//	Parameter:
//		In : bEndpointIndex : The endpoint index number of  PDIUSBD12 
//			 bStalled		: If 1, this index endpoint will be set STALL status
//		Out: None
//	Function:
//		PDIUSBD12 Register: bEndpointIndex
//		Set USB address of PDIUSBD12,and if bEanble is 1,the address will 
//		be valid immediately.
void D12SetEndpointStatus(unsigned char bEndpointIndex, unsigned char bStalled)
{
	D12_COMMAND_ADDRESS = bEndpointIndex;
	D12_DATA_ADDRESS = bStalled;
}

//
//*************************************************************************
//	Paremeter:
//		In : bAddress	: The USB address that PDIUSBD12 will be set
//			 bEnable	: If 1, bAddress will be enabled,vice visa
//		Out: None
//	Function:
//		PDIUSBD12 Register: 0xD0H
//		Set USB address of PDIUSBD12,and if bEanble is 1,the address will 
//		be validata immediately.
void D12AcknowledgeSetup(void)
{
	D12_COMMAND_ADDRESS =  D12_SELECT_ENDPOINT_CONTROL_OUT;			//select endpoint0 OUT
	D12_COMMAND_ADDRESS =  D12_ACKNOWLEDGE_SETUP_SELECTED_ENDPOINT;	//acknowledge Setup command
	D12_COMMAND_ADDRESS =  D12_CLEAR_BUFFER_SELECTED_ENDPOINT;		//setup packet is 8 byte,clear it after read it
	D12_COMMAND_ADDRESS =  D12_SELECT_ENDPOINT_CONTROL_IN;			//select endpoint0 IN
	D12_COMMAND_ADDRESS =  D12_ACKNOWLEDGE_SETUP_SELECTED_ENDPOINT;	//acknowledge Setup command

}

//
//*************************************************************************
//	Parameter:
//		In :
//		Out:
//	Function:
//		PDIUSBD12 Register: 0xF2H
//		It is very simple and no necessary to implement it as an function
/*
D12ClearBuffer()
{
;
}
*/
//
//*************************************************************************
//	Parameter:
//		In :
//		Out:
//	Function:
//		PDIUSBD12 Register: 0xFAH
//		It is very simple and no necessary to implement as an function
/*
D12VaidateBuffer()
{
;
}
*/

//
//*************************************************************************
//	Parameter:
//		In :
//		Out:
//	Function:
//		PDIUSBD12 Register: 0xF6H
//		If remote wake is enabled,and Host is in suspend state,this function
//		will awake Host. 
//		Interface studio USB1.1 develop board does not implement this function
//		in main program,so here comment out this function.User can use it
//		if necessary.
/*
void D12SendResume(void)
{
	D12_COMMAND_ADDRESS = 0xF6;
}
*/

//
//*************************************************************************
//	Parameter:
//		In :
//		Out:
//	Function:
//		PDIUSBD12 Register: 0xF5H
//		Although this firmware does not support isochronous transfer,
//		synchronization frame request still is implementated.

void D12ReadCurrentFrameNumber(unsigned char *pBuffer)
{
	
	D12_COMMAND_ADDRESS = D12_READ_CURRENT_FRAME_NUMBER;
	*pBuffer = D12_DATA_ADDRESS;
	*(pBuffer+1) = D12_DATA_ADDRESS;

}



⌨️ 快捷键说明

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