📄 hal4d13.c
字号:
#include "usbdefs.h"
#include "Hal4sys.h"
#include "Hal4D13.h"
#include <stdio.h>
#define HALD13_DELAY 5
void Hal4D13_SetDevConfig( u16 wDevCnfg )
{
outportb( D13_COMMAND_PORT, D13CMD_DEV_WR_CNFG );
outportb( D13_DATA_PORT,( u8 )wDevCnfg );
outportb( D13_DATA_PORT,( u8 )( wDevCnfg >> 8 )); //high byte
usbdelay( HALD13_DELAY );
}
void Hal4D13_SetMode( u8 bMode )
{
outportb( D13_COMMAND_PORT, D13CMD_DEV_WR_MODE );
outportb( D13_DATA_PORT, bMode );
usbdelay( HALD13_DELAY );
}
void Hal4D13_SetAddressEnable( u8 bAddress, u8 bEnable )
{
outportb( D13_COMMAND_PORT, D13CMD_DEV_WR_ADDR );
if( bEnable )
bAddress |= D13REG_DEVADDR_EN;
else
bAddress &= D13REG_DEVADDR_MASK;
outportb( D13_DATA_PORT, bAddress );
usbdelay( HALD13_DELAY );
}
void Hal4D13_AcknowledgeSETUP( void )
{
outportb( D13_COMMAND_PORT, D13CMD_ACK_SETUP );
usbdelay( HALD13_DELAY );
}
void Hal4D13_ClearBuffer( u8 bEPIndex )
{
outportb( D13_COMMAND_PORT, ( u8 )( D13CMD_EP_CLEAR_BUF + bEPIndex ) );
usbdelay( HALD13_DELAY );
}
u16 Hal4D13_ReadEndpoint( u8 bEPIndex, u8 * buf, u16 len )
{
u16 i, j;
outportb( D13_COMMAND_PORT, ( u8 ) ( D13CMD_EP_RD_FIFO + bEPIndex ) );
// read Buffer read LSB first then MSB of length.
j = inportb( D13_DATA_PORT );// LSB of Length word
i = inportb( D13_DATA_PORT );
j += ( i << 8 );// MSB of Length word
if( j > len )
j = len;
for( i = 0; i < j; i ++/*, buf ++*/ )
* ( buf + i ) = inportb( D13_DATA_PORT );
// Clear Buffer
outportb( D13_COMMAND_PORT, ( u8 )( D13CMD_EP_CLEAR_BUF + bEPIndex ));
usbdelay( HALD13_DELAY * 2);
return j;
}
u16 Hal4D13_ReadEndpointWOClearBuffer( u8 bEPIndex, u8 * buf, u16 len )
{
u16 i, j;
outportb( D13_COMMAND_PORT, ( u8 ) ( D13CMD_EP_RD_FIFO + bEPIndex ));
// read Buffer read LSB first then MSB of length.
j = inportb( D13_DATA_PORT );// LSB of Length word
i = inportb( D13_DATA_PORT );//MSB of Length word
j += ( i << 8 ); //calculation
if( j > len )
j = len;
for( i = 0; i < j; i ++/*, buf ++*/ )
* ( buf + i ) = inportb( D13_DATA_PORT );
usbdelay( HALD13_DELAY * 2);
return j;
}
u16 Hal4D13_WriteEndpoint( u8 bEPIndex, u8 * buf, u16 len )
{
u16 i;
outportb( D13_COMMAND_PORT, ( u8 )( D13CMD_EP_WR_FIFO + bEPIndex ));
outportb( D13_DATA_PORT, ( u8 )len );
outportb( D13_DATA_PORT, ( u8 )( len >> 8 )); //high byte
for( i = 0; i < len; i ++ /*, buf ++ */)
outportb( D13_DATA_PORT, * ( buf + i ) );
outportb( D13_COMMAND_PORT,( u8 ) ( D13CMD_EP_VALID_BUF + bEPIndex ));
usbdelay( HALD13_DELAY * 2);
return i;
}
void Hal4D13_SetIntEnable( u32 dIntEn )
{
outportb( D13_COMMAND_PORT, D13CMD_DEV_WR_INTEN );
outportb( D13_DATA_PORT,( u8 )dIntEn ); //0 byte
outportb( D13_DATA_PORT,( u8 )( dIntEn >> 8 )); //1 byte
outportb( D13_DATA_PORT,( u8 )( dIntEn >> 16 )); //2 byte
outportb( D13_DATA_PORT,( u8 )0x0 ); //3 byte
usbdelay( HALD13_DELAY );
}
u32 Hal4D13_ReadInterruptRegister( void )
{
u32 i;
outportb( D13_COMMAND_PORT, D13CMD_DEV_INT_SRC );
i = (( u32 )inportb( D13_DATA_PORT )); //0 byte
i += ((( u32 )inportb( D13_DATA_PORT )) << 8 ); //1 byte
i += ((( u32 )inportb( D13_DATA_PORT )) << 16 ); //2 byte
i += ((( u32 )inportb( D13_DATA_PORT )) << 24 ); //3 byte
usbdelay( HALD13_DELAY );
return i;
}
u8 Hal4D13_GetEndpointStatusWInteruptClear( u8 bEPIndex )
{
u8 c;
outportb( D13_COMMAND_PORT, ( u8 ) ( D13CMD_EP_RDSTS_CLRINT + bEPIndex ) );
c = inportb( D13_DATA_PORT );
usbdelay( HALD13_DELAY );
return c;
}
u8 Hal4D13_GetEndpointStatusWOInteruptClear( u8 bEPIndex )
{
u8 c;
outportb( D13_COMMAND_PORT, ( u8 )( D13CMD_EP_RDSTS + bEPIndex ));
c = inportb( D13_DATA_PORT );
return c;
}
void Hal4D13_SetEndpointStatus( u8 bEPIndex, u8 bStalled )
{
if( bStalled & 0x80 ) // set endpoint stall 0x80
outportb( D13_COMMAND_PORT, ( u8 )( D13CMD_EP_WR_STS + bEPIndex ) );
else
outportb( D13_COMMAND_PORT, ( u8 )( D13CMD_EP_CLR_STALL + bEPIndex ));
usbdelay( HALD13_DELAY );
}
void Hal4D13_SetDMAConfig( u16 wDMACnfg )
{
outportb( D13_COMMAND_PORT, D13CMD_DMA_WR_CNFG );
outportb( D13_DATA_PORT,( u8 )wDMACnfg );
outportb( D13_DATA_PORT,( u8 )( wDMACnfg >> 8 )); //high byte
usbdelay( HALD13_DELAY );
}
void Hal4D13_SetDMACounter( u16 wDMACounter )
{
outportb( D13_COMMAND_PORT, D13CMD_DMA_WR_COUNT );
outportb( D13_DATA_PORT,( u8 )wDMACounter );
outportb( D13_DATA_PORT,( u8 )( wDMACounter >> 8 )); //high byte
usbdelay( HALD13_DELAY );
}
void Hal4D13_LockDevice( u16 wUnLockDev )
{
outportb( D13_COMMAND_PORT, D13CMD_DEV_LOCK );
outportb( D13_DATA_PORT,( u8 )wUnLockDev );
outportb( D13_DATA_PORT,( u8 )( wUnLockDev >> 8 )); //high byte
usbdelay( HALD13_DELAY );
}
void Hal4D13_SetEndpointConfig( u8 bEPConfig, u8 bEPIndex )
{
outportb( D13_COMMAND_PORT, ( u8 )( D13CMD_EP_WR_CNFG + bEPIndex ));
outportb( D13_DATA_PORT, bEPConfig );
usbdelay( HALD13_DELAY );
}
void Hal4D13_SingleTransmitEP0( u8 * buf, u16 len )
{
if( len <= EP0_PACKET_SIZE )
{
Hal4D13_WriteEndpoint( EPINDEX4EP0_CONTROL_IN, buf, len );
usbdelay( HALD13_DELAY );
}
}
u16 Hal4D13_ReadChipID( void )
{
u16 i;
outportb( D13_COMMAND_PORT, D13CMD_DEV_RD_CHIPID );
i = inportb( D13_DATA_PORT );
i += (( u16 )inportb( D13_DATA_PORT ) << 8 ); //high byte
return i;
}
void Hal4D13_ResetDevice( void )
{
outportb( D13_COMMAND_PORT, D13CMD_DEV_RESET );
usbdelay( HALD13_DELAY );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -