码迷,mamicode.com
首页 > 其他好文 > 详细

BulkLoop例程解读

时间:2014-08-27 00:22:56      阅读:474      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   使用   io   for   

bulkloop例程是USB固件开发中的基础例程,通过它我们可以学到很多基础知识,我在这里也利用下午的时间来学习一下bulkloop例程。   

bulkloop这个名字就说明了该固件的作用:以bulk型endpoint作为输出和输入端口,让数据"转一圈"。就是在主机端输出一组数据到FX2LP的某一个bulk型endpoint(比如EP2)的缓存中,然后固件将EP2缓存中的数据转移到另一个bulk型endpoint(比如EP6)的缓存中去,当主机端从EP6输入数据的时候,就会发现得到的数据正是之前输出到EP2的数据。   

图1 固件工程Keil界面 :   

bubuko.com,布布扣

bulkloop工程本身用到的源文件有:fw.c、bulkloop.c、dscr.a51、EZUSB.LIB、USBjmpTb.OBJ。

fw.c:固件框架程序FrameWork,它包含了固件程序的主程序。框架程序使FX2LP固件有一个相对固定的运行模式,这使得开发者能够更清楚在什么地方、什么时候应该干什么。

下面是fw.c文件中的代码和我的注释:

 明天去实验室再上传

下面是bulkloop.c文件中的代码和我的注释:

bubuko.com,布布扣
//-----------------------------------------------------------------------------
//   File:      bulkloop.c
//   Contents:  Hooks required to implement USB peripheral function.
//
// $Archive: /USB/Examples/FX2LP/bulkloop/bulkloop.c $
//
//
//-----------------------------------------------------------------------------
// Copyright (c) 2011, Cypress Semiconductor Corporation All rights reserved
//-----------------------------------------------------------------------------
#pragma NOIV               // Do not generate interrupt vectors //F:告诉编译器,不要使用默认的中断向量表

#include "..\inc\fx2.h"
#include "..\inc\fx2regs.h"
#include "..\inc\syncdly.h"            // SYNCDELAY macro

extern BOOL GotSUD;             // Received setup data flag
extern BOOL Sleep;
extern BOOL Rwuen;
extern BOOL Selfpwr;

BYTE Configuration;             // Current configuration
BYTE AlternateSetting;          // Alternate settings

#define VR_NAKALL_ON    0xD0    //F: 1101 0000
#define VR_NAKALL_OFF   0xD1    //F: 1101 0001

//-----------------------------------------------------------------------------
// Task Dispatcher hooks        //F:任务分配挂钩
//   The following hooks are called by the task dispatcher. //F:挂钩函数是被任务分配器调用的
//-----------------------------------------------------------------------------

void TD_Init(void)             // Called once at startup
{
   // set the CPU clock to 48MHz
   CPUCS = ((CPUCS & ~bmCLKSPD) | bmCLKSPD1) ;    //F:CLKSPD1=1且CLKSPD0=0 意思是48MHz

   // set the slave FIFO interface to 48MHz    
   //F:时钟来源定为外部,内部FIFO\GPIF时钟设为48MHz,IFCLK输出端口为三态,IFCLK极性不翻转,同步方式,PE012为端口,ABD端口为端口模式
   IFCONFIG |= 0x40;

  //F: 下面的寄存器的修改之间需要添加同步延时
  // Registers which require a synchronization delay, see section 15.14
  // FIFORESET        FIFOPINPOLAR
  // INPKTEND         OUTPKTEND
  // EPxBCH:L         REVCTL
  // GPIFTCB3         GPIFTCB2
  // GPIFTCB1         GPIFTCB0
  // EPxFIFOPFH:L     EPxAUTOINLENH:L
  // EPxFIFOCFG       EPxGPIFFLGSEL
  // PINFLAGSxx       EPxFIFOIRQ
  // EPxFIFOIE        GPIFIRQ
  // GPIFIE           GPIFADRH:L
  // UDMACRCH:L       EPxGPIFTRIG
  // GPIFTRIG
  
  // Note: The pre-REVE EPxGPIFTCH/L register are affected, as well...
  //      ...these have been replaced by GPIFTC[B3:B0] registers

  // default: all endpoints have their VALID bit set
  // default: TYPE1 = 1 and TYPE0 = 0 --> BULK  
  // default: EP2 and EP4 DIR bits are 0 (OUT direction)
  // default: EP6 and EP8 DIR bits are 1 (IN direction)
  // default: EP2, EP4, EP6, and EP8 are double buffered

  // we are just using the default values, yes this is not necessary...
  EP1OUTCFG = 0xA0;
  EP1INCFG = 0xA0;
  SYNCDELAY;                    // see TRM section 15.14
  EP2CFG = 0xA2; //F:1010 0010意思是:有效,OUT,Bulk,512,0,Double.
  SYNCDELAY;                    
  EP4CFG = 0xA0; //F:1010 0000意思是:有效,OUT,Bulk,512,0,00(4和8端点的末尾两位只能是0,在2和6都是Double情况下,意味着Double).
  SYNCDELAY;                    
  EP6CFG = 0xE2; //F:1110 0010意思是:有效,IN,Bulk,512,0,Double.
  SYNCDELAY;                    
  EP8CFG = 0xE0; //F:1110 0000意思是:有效,OUT,Bulk,512,0,00.

  // out endpoints do not come up armed //F:输出端点一开始没有被arm.
  
  // since the defaults are double buffered we must write dummy byte counts twice
  //F:因为端点默认是双倍缓冲(512*2),我们必须用无用数据写两次字节计数,用来arm输出端点.
  SYNCDELAY;                    
  EP2BCL = 0x80;                // arm EP2OUT by writing byte count w/skip.
  SYNCDELAY;                    
  EP2BCL = 0x80;
  SYNCDELAY;                    
  EP4BCL = 0x80;                // arm EP4OUT by writing byte count w/skip.
  SYNCDELAY;                    
  EP4BCL = 0x80;    

  // enable dual autopointer feature //F:使能自动指针
  AUTOPTRSETUP |= 0x01;

}


void TD_Poll(void)              // Called repeatedly while the device is idle //F:重复调用
{
  WORD i;
  WORD count;

  if(!(EP2468STAT & bmEP2EMPTY))    //F:如果EP2的buff不空.EP2468STAT中的各个位其实就是EPxCS中的F和E位,标识满\空.
  { // check EP2 EMPTY(busy) bit in EP2468STAT (SFR), core set‘s this bit when FIFO is empty
     if(!(EP2468STAT & bmEP6FULL))    //F:如果EP6的buff不满.
     {  // check EP6 FULL(busy) bit in EP2468STAT (SFR), core set‘s this bit when FIFO is full
        APTR1H = MSB( &EP2FIFOBUF );    //F:自动指针1指向EP2的buffer
        APTR1L = LSB( &EP2FIFOBUF );

        AUTOPTRH2 = MSB( &EP6FIFOBUF );    //F:自动指针2指向EP6的buffer
        AUTOPTRL2 = LSB( &EP6FIFOBUF );

        count = (EP2BCH << 8) + EP2BCL;    //F:计算EP2有多少字节

        // loop EP2OUT buffer data to EP6IN
        for( i = 0x0000; i < count; i++ )
        {
           // setup to transfer EP2OUT buffer to EP6IN buffer using AUTOPOINTER(s)
           // F:利用自动指针进行EP2和EP6之间的数据转移
           EXTAUTODAT2 = EXTAUTODAT1; // F:自动指针1指向的数据到自动指针2指向的空间
        }
        EP6BCH = EP2BCH;      //F:宝贝数据长度到EP6的计数,准备接下来的IN操作
        SYNCDELAY;  
        EP6BCL = EP2BCL;        // arm EP6IN
        SYNCDELAY;                    
        EP2BCL = 0x80;          // re(arm) EP2OUT
     }
  }

  if(!(EP2468STAT & bmEP4EMPTY))
  { // check EP4 EMPTY(busy) bit in EP2468STAT (SFR), core set‘s this bit when FIFO is empty
     if(!(EP2468STAT & bmEP8FULL))
     {  // check EP8 FULL(busy) bit in EP2468STAT (SFR), core set‘s this bit when FIFO is full
        APTR1H = MSB( &EP4FIFOBUF );
        APTR1L = LSB( &EP4FIFOBUF );

        AUTOPTRH2 = MSB( &EP8FIFOBUF );
        AUTOPTRL2 = LSB( &EP8FIFOBUF );

        count = (EP4BCH << 8) + EP4BCL;

        // loop EP4OUT buffer data to EP8IN
        for( i = 0x0000; i < count; i++ )
        {
           // setup to transfer EP4OUT buffer to EP8IN buffer using AUTOPOINTER(s)
           EXTAUTODAT2 = EXTAUTODAT1;
        }
        EP8BCH = EP4BCH;  
        SYNCDELAY;  
        EP8BCL = EP4BCL;        // arm EP8IN
        SYNCDELAY;                    
        EP4BCL = 0x80;          // re(arm) EP4OUT
     }
  }
}

BOOL TD_Suspend(void)          // Called before the device goes into suspend mode 可自定义
{
   return(TRUE);
}

BOOL TD_Resume(void)          // Called after the device resumes 可自定义
{
   return(TRUE);
}

//-----------------------------------------------------------------------------
// Device Request hooks 设备请求执行函数,大多数可自定义
//   The following hooks are called by the end point 0 device request parser.
//-----------------------------------------------------------------------------

BOOL DR_GetDescriptor(void)
{
   return(TRUE);
}

BOOL DR_SetConfiguration(void)   // Called when a Set Configuration command is received
{
   Configuration = SETUPDAT[2];    //F:Configuration这个变量是哪里定义的?还是编译器内部定义的?如何与描述符表联系在一起???
   return(TRUE);            // Handled by user code
}

BOOL DR_GetConfiguration(void)   // Called when a Get Configuration command is received
{
   EP0BUF[0] = Configuration;
   EP0BCH = 0;
   EP0BCL = 1;                //F: arm EP0
   return(TRUE);            // Handled by user code
}

BOOL DR_SetInterface(void)       // Called when a Set Interface command is received
{
   AlternateSetting = SETUPDAT[2];
   return(TRUE);            // Handled by user code
}

BOOL DR_GetInterface(void)       // Called when a Set Interface command is received
{
   EP0BUF[0] = AlternateSetting;
   EP0BCH = 0;
   EP0BCL = 1;
   return(TRUE);            // Handled by user code
}

BOOL DR_GetStatus(void)
{
   return(TRUE);
}

BOOL DR_ClearFeature(void)
{
   return(TRUE);
}

BOOL DR_SetFeature(void)
{
   return(TRUE);
}

BOOL DR_VendorCmnd(void)    //F:生产商请求
{
  BYTE tmp;
  
  switch (SETUPDAT[1])
  {
     case VR_NAKALL_ON:        //F:NAK所有transfer请求
        tmp =FIFORESET;        //F:为什么不直接 FIFORESET|=bmNAKALL ???
        tmp |= bmNAKALL;      
        SYNCDELAY;                    
        FIFORESET = tmp;    //F:这样费周折是因为FIFORESET不可以按位访问吗???
        break;
     case VR_NAKALL_OFF:    
        tmp = FIFORESET;
        tmp &= ~bmNAKALL;      
        SYNCDELAY;                    
        FIFORESET = tmp;
        break;
     default:
        return(TRUE);
  }

  return(FALSE);
}

//-----------------------------------------------------------------------------
// USB Interrupt Handlers
//   The following functions are called by the USB interrupt jump table.
//-----------------------------------------------------------------------------

// Setup Data Available Interrupt Handler
void ISR_Sudav(void) interrupt 0    //F:有控制传输的8字节数据到达
{
   GotSUD = TRUE;            // Set flag
   EZUSB_IRQ_CLEAR();        //F:重置中断请求,write 0 to EXIF.5
   USBIRQ = bmSUDAV;         // Clear SUDAV IRQ //F:向指定的位写1以清楚终端请求
}

// Setup Token Interrupt Handler
void ISR_Sutok(void) interrupt 0    //F:USB内核接收到Setup传输的Token 
{
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmSUTOK;         // Clear SUTOK IRQ
}

void ISR_Sof(void) interrupt 0        //F:USB内核收到 Start of Frame packet
{
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmSOF;            // Clear SOF IRQ
}

void ISR_Ures(void) interrupt 0    //F:USB Reset Interrupt Request
{
   // whenever we get a USB reset, we should revert to full speed mode
   //任何时刻接收到USB reset,都应该滚回全速模式
   pConfigDscr = pFullSpeedConfigDscr;
   ((CONFIGDSCR xdata *) pConfigDscr)->type = CONFIG_DSCR;
   pOtherConfigDscr = pHighSpeedConfigDscr;
   ((CONFIGDSCR xdata *) pOtherConfigDscr)->type = OTHERSPEED_DSCR;

   EZUSB_IRQ_CLEAR();
   USBIRQ = bmURES;         // Clear URES IRQ
}

void ISR_Susp(void) interrupt 0
{
   Sleep = TRUE;
   EZUSB_IRQ_CLEAR();
   USBIRQ = bmSUSP;
}

void ISR_Highspeed(void) interrupt 0
{
   if (EZUSB_HIGHSPEED())
   {
      pConfigDscr = pHighSpeedConfigDscr;
      ((CONFIGDSCR xdata *) pConfigDscr)->type = CONFIG_DSCR;
      pOtherConfigDscr = pFullSpeedConfigDscr;
      ((CONFIGDSCR xdata *) pOtherConfigDscr)->type = OTHERSPEED_DSCR;
   }

   EZUSB_IRQ_CLEAR();
   USBIRQ = bmHSGRANT;
}
void ISR_Ep0ack(void) interrupt 0
{
}
void ISR_Stub(void) interrupt 0
{
}
void ISR_Ep0in(void) interrupt 0
{
}
void ISR_Ep0out(void) interrupt 0
{
}
void ISR_Ep1in(void) interrupt 0
{
}
void ISR_Ep1out(void) interrupt 0
{
}
void ISR_Ep2inout(void) interrupt 0
{
}
void ISR_Ep4inout(void) interrupt 0
{
}
void ISR_Ep6inout(void) interrupt 0
{
}
void ISR_Ep8inout(void) interrupt 0
{
}
void ISR_Ibn(void) interrupt 0
{
}
void ISR_Ep0pingnak(void) interrupt 0
{
}
void ISR_Ep1pingnak(void) interrupt 0
{
}
void ISR_Ep2pingnak(void) interrupt 0
{
}
void ISR_Ep4pingnak(void) interrupt 0
{
}
void ISR_Ep6pingnak(void) interrupt 0
{
}
void ISR_Ep8pingnak(void) interrupt 0
{
}
void ISR_Errorlimit(void) interrupt 0
{
}
void ISR_Ep2piderror(void) interrupt 0
{
}
void ISR_Ep4piderror(void) interrupt 0
{
}
void ISR_Ep6piderror(void) interrupt 0
{
}
void ISR_Ep8piderror(void) interrupt 0
{
}
void ISR_Ep2pflag(void) interrupt 0
{
}
void ISR_Ep4pflag(void) interrupt 0
{
}
void ISR_Ep6pflag(void) interrupt 0
{
}
void ISR_Ep8pflag(void) interrupt 0
{
}
void ISR_Ep2eflag(void) interrupt 0
{
}
void ISR_Ep4eflag(void) interrupt 0
{
}
void ISR_Ep6eflag(void) interrupt 0
{
}
void ISR_Ep8eflag(void) interrupt 0
{
}
void ISR_Ep2fflag(void) interrupt 0
{
}
void ISR_Ep4fflag(void) interrupt 0
{
}
void ISR_Ep6fflag(void) interrupt 0
{
}
void ISR_Ep8fflag(void) interrupt 0
{
}
void ISR_GpifComplete(void) interrupt 0
{
}
void ISR_GpifWaveform(void) interrupt 0
{
}
View Code

 

 

 

 

   

BulkLoop例程解读

标签:des   style   blog   http   color   os   使用   io   for   

原文地址:http://www.cnblogs.com/sunmaoduo/p/3938500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!