📄 sx2.c
字号:
/********************************************************************************
*sx2.C v1.00 *
* 描述:完成对USB控制器CY7C68001的各项功能的控制 *
* 版权(c) 2003- 北京合众达电子技术有限责任公司 *
* 设计者: 段立锋 *
*********************************************************************************/
#include "type.h"
#include "sx2.h"
#include "cy7c68001.h"
#include "sysreg.h"
#define COMMAND 0x04
/*声明中断INT2的函数,用于USB的中断处理*/
#pragma INTERRUPT(int2_isr);
BOOL SX2_comwritebyte(unsigned int value);
/* global variables */
BYTE irqValue; /* interrupt register value */
BYTE setupBuff[8]; /* setup transaction buffer */
BOOL sx2Ready; /* status byte for POST */
BOOL sx2BusActivity; /* status byte for suspend */
BOOL sx2EnumOK; /* status byte for enumeration complete */
BOOL sx2EP0Buf; /* status for endpoint 0 buffer */
BOOL sx2Setup; /* setup flag set in Int0() ISR */
BOOL FLAGS_READ = FALSE; /*FIFO的状态读请求*/
/* shared between interrupt and register read routines */
/*中断与寄存器读之间共享,确定是否是由读寄存器引起的中断*/
BOOL readFlag;
unsigned int usbtimeout = 0x400;
unsigned int regdataread = 0;
static char setupCnt;
static BOOL setupDat = FALSE;
/*****************************************************************************/
/* Function: Load_descriptors */
/* Purpose: loads the descriptor memory of SX2 */
/* Input: count - number of bytes in the descriptor */
/* desc - pointer to descriptor table */
/* Output: TRUE on success */
/* FALSE on failure */
/* */
/*****************************************************************************/
BOOL Load_descriptors(char length, char* desc)
{
unsigned char i;
/* write LSB of descriptor length,and the address of the Descriptor */
if(!Write_SX2reg(SX2_DESC, (unsigned int)length))
{
return FALSE;
}
/* write high nibble of MSB of descriptor length */
SX2_comwritebyte((unsigned char)(length >> 12));
/* write low nibble of MSB of descriptor length */
SX2_comwritebyte((unsigned char)((length & 0x0F00)>>8));
for(i=0; i<length; i++)
{
/* write high nibble of MSB of descriptor length */
SX2_comwritebyte((desc[i] >> 4));
/* write low nibble of MSB of descriptor length */
SX2_comwritebyte((desc[i] & 0x0F));
}
return TRUE;
}
/**********************************************************************************/
/* Function: Write_SX2reg */
/* Purpose: Writes to a SX2 register */
/* Input: addr - address of register */
/* value - value to write to address */
/* Output: TRUE on success */
/* FALSE on failure */
/**********************************************************************************/
BOOL Write_SX2reg(unsigned char addr, unsigned int value)
{
unsigned int transovertime = 0 ;
/*clear the high two bit of the addr*/
addr = addr & 0x3f;
/* write register address to the SX2 */
if(!SX2_comwritebyte(0x80 | addr))
{
return FALSE;
}
/* write high nibble of register data */
SX2_comwritebyte((value >> 4) & 0xF);
/* write low nibble of register data */
SX2_comwritebyte(value & 0x0F);
/*wait the ready is ok*/
transovertime = 0;
while( (sysreg_read(sysstat0) & 0x4) ==0 )
{
if( transovertime++ > usbtimeout )
{
return FALSE;
}
}
/*the write is ok*/
return TRUE;
}
/**********************************************************************************/
/* Function: SX2_comwritebyte */
/* Purpose: Writes to a SX2 command interface */
/* Input: value - value to write to address */
/* Output: TRUE on success */
/* FALSE on failure */
/**********************************************************************************/
BOOL SX2_comwritebyte(unsigned int value)
{
unsigned int time_count = 0;
/*wait the ready is ok*/
while( (sysreg_read(sysstat0) & 0x4) ==0 )
{
if( time_count++ > usbtimeout )
{
return FALSE;
}
}
USB_Command_Write(value);
/*the write is ok*/
return TRUE;
}
/**********************************************************
*
* Function: Read_SX2reg
* Purpose: Reads a SX2 register
* Input: addr - address of register
* value - value read from register
* Output: TRUE on success
* FALSE on failure
*
**********************************************************/
BOOL Read_SX2reg(unsigned char addr, unsigned int *value)
{
unsigned int transovertime = 0;
/*wait the ready is ok*/
while( (sysreg_read(sysstat0) & 0x4) ==0 )
{
if( transovertime++ > usbtimeout )
{
return FALSE;
}
}
/*clear the high two bit of the addr*/
addr = addr & 0x3f;
/* write 'read register' command to SX2 */
USB_Command_Write(0xC0 | addr);
/* set read flag to indicate to the interrupt routine that we
are expecting an interrupt to read back the contents of the
addressed register. The interrupt latency of the SX2 is in
tens of microseconds, so it's safe to write this flag after
the initial 'read' byte is written. */
/*设置读标志,通知中断程序不做处理读中断,只要返回标志为假就可以了*/
readFlag = TRUE;
/* wait for read flag to be cleared by an interrupt */
/*等待读标志为假*/
while(readFlag);
/*wait the ready is ok*/
while( (sysreg_read(sysstat0) & 0x4) ==0 )
{
if( transovertime++ > usbtimeout )
{
return FALSE;
}
}
/*读取寄存器的数据*/
*value = USB_Command_Read();
return TRUE;
}
/*********************************************************/
/* */
/* Function: SX2_FifoWrite */
/* Purpose: write buffer to sx2fifo */
/* Input: channel,the endpoint you select */
/* pdata - the pointer to databuffer */
/* longth - the longth of the databuffer */
/* Output: TRUE on success */
/* FALSE on failure */
/* */
/*********************************************************/
/*BOOL SX2_FifoWrite(int channel,u16* pdata,unsigned int longth)
{
unsigned int i = 0;
for(i = 0;i<longth;i++)
{
if(!SX2_FifoWriteSingle(channel,pdata[i]))
{
return FALSE;
}
}
return TRUE;
}*/
/*********************************************************/
/* */
/* Function: SX2_FifoRead */
/* Purpose: read sx2fifo to data buffer */
/* Input: channel,the endpoint you select */
/* pdata - the pointer to databuffer */
/* longth - the longth of the databuffer */
/* Output: TRUE on success */
/* FALSE on failure */
/* */
/*********************************************************/
BOOL SX2_FifoRead(int channel,u16* pdata,unsigned int longth)
{
unsigned int i = 0;
for(i = 0;i<longth;i++)
{
pdata[i] = SX2_FifoReadSingle(channel);
}
return TRUE;
}
/*********************************************************/
/*************** INTERRUPT ROUTINES **********************/
/**********************************************************
*
* Function: Int2
* Purpose: Interrupts on external interrupt 2
* Input: None
* Output: None
*
**********************************************************/
void int2_isr()
{
/* during a read, an interrupt occurs after the host
CPU requests a register value to read. The host CPU
then reads the data from the SX2 */
if(readFlag)
{
readFlag = FALSE;
}
/* setup's are a special case. Whenever we get a setup
the next eight interrupts represent the data of the
setup packet */
else if(setupDat)
{
/* read the setup data */
setupBuff[setupCnt++] = USB_Command_Read();
/* stop when we have collected eight bytes */
if(setupCnt > 7)
{
setupDat = FALSE;
sx2Setup = TRUE;
}
else
{
USB_Command_Write(0xC0 | SX2_SETUP);
}
}
/* if this is a new request, then we have to read the
value and parse the interrupt value. The value
can't be parsed in the main loop, otherwise we could
get two interrupts back to back and trash the first
one in the series. */
else
{
/* read the interrupt register value */
irqValue = USB_Command_Read();
switch(irqValue)
{
case SX2_INT_SETUP:
/* endpoint 0 setup */
/* next eight interrupts are setup data */
/* parse the interrupt register value */
setupDat = TRUE;
setupCnt = 0;
/* send read register command to SX2 */
USB_Command_Write(0xC0 | SX2_SETUP);
break;
case SX2_INT_EP0BUF:
/* endpoint 0 ready */
sx2EP0Buf = TRUE;
break;
case SX2_INT_FLAGS:
/* FIFO flags -FF,PF,EF */
FLAGS_READ = TRUE;
break;
case SX2_INT_ENUMOK:
/* enumeration successful */
sx2EnumOK = TRUE;
break;
case SX2_INT_BUSACTIVITY:
/* detected either an absence or resumption of activity on the USB bus. */
/* Indicates that the host is either suspending or resuming or that a */
/* self-powered device has been plugged into or unplugged from the USB. */
/* If the SX2 is bus-powered, the host processor should put the SX2 into */
/* a low-power mode after detecting a USB suspend condition. */
sx2BusActivity = TRUE;
break;
case SX2_INT_READY:
/* awakened from low power mode via wakeup pin */
/* or completed power on self test */
sx2Ready = TRUE;
break;
default:
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -