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

📄 f34x_usb_isr.c

📁 学习usb编程很好的例子 自己学习用的。
💻 C
📖 第 1 页 / 共 2 页
字号:
                                        // This case exists when the host requests an even multiple of
                                        // your endpoint zero max packet size, and you need to exit
                                        // transmit mode without sending a zero length packet
            {
               TempReg |= rbDATAEND;    // Add Data End bit to mask
               Ep_Status[0] = EP_IDLE;  // and return Endpoint 0 to an idle state
            }
            POLL_WRITE_BYTE(E0CSR, TempReg);                          // Write mask to E0CSR
         }
      }
   }
}

//-----------------------------------------------------------------------------
// Handle_In1
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine loads the current value from In_Packet on the Endpoint 1 fifo,
// after  an interrupt is received from the last packet being transmitted
//
//-----------------------------------------------------------------------------

void Handle_In1()
{
   BYTE ControlReg;

   POLL_WRITE_BYTE(INDEX, 1);           // Set index to endpoint 1 registers
   POLL_READ_BYTE(EINCSR1, ControlReg); // Read contol register for EP 1

   if (Ep_Status[1] == EP_HALT)         // If endpoint is currently halted, 
                                        // send a stall
   {
      POLL_WRITE_BYTE(EINCSR1, rbInSDSTL);
   }

   else                                 // Otherwise send last updated 
                                        // data to host
   {
      if (ControlReg & rbInSTSTL)       // Clear sent stall if last packet
	                                    // returned a stall
      {
         POLL_WRITE_BYTE(EINCSR1, rbInCLRDT);
      }

      if (ControlReg & rbInUNDRUN)      // Clear underrun bit if it was set
      {
         POLL_WRITE_BYTE(EINCSR1, 0x00);
      }

                                        // Put new data on Fifo
      Fifo_Write(FIFO_EP1, EP1_PACKET_SIZE, (BYTE *)IN_PACKET);
      POLL_WRITE_BYTE(EINCSR1, rbInINPRDY);
                                        // Set In Packet ready bit, indicating 
   }                                    // fresh data on Fifo 1
}

//-----------------------------------------------------------------------------
// Handle_Out2
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Take the received packet from the host off the fifo and put it into
// the Out_Packet array
//
//-----------------------------------------------------------------------------

void Handle_Out2()
{
   BYTE Count = 0;
   BYTE ControlReg;

   POLL_WRITE_BYTE(INDEX, 2);          // Set index to endpoint 2 registers
   POLL_READ_BYTE(EOUTCSR1, ControlReg);

   if (Ep_Status[2] == EP_HALT)        // If endpoint is halted, send a stall
   {
      POLL_WRITE_BYTE(EOUTCSR1, rbOutSDSTL);
   }

   else                                // Otherwise read packet from host
   {
      if (ControlReg & rbOutSTSTL)     // Clear sent stall bit if last packet 
                                       // was a stall
      {
         POLL_WRITE_BYTE(EOUTCSR1, rbOutCLRDT);
      }

      POLL_READ_BYTE(EOUTCNTL, Count);
      if (Count != EP2_PACKET_SIZE)    // If host did not send correct packet 
	                                   // size, flush buffer
      {
         POLL_WRITE_BYTE(EOUTCNTL, rbOutFLUSH);
      }
      else                             // Otherwise get the data packet
      {
         Fifo_Read(FIFO_EP2, EP2_PACKET_SIZE, (BYTE*)OUT_PACKET);
      }
      POLL_WRITE_BYTE(EOUTCSR1, 0);    // Clear Out Packet ready bit
   }
}

//-----------------------------------------------------------------------------
// Usb_Suspend
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Enter suspend mode after suspend signalling is present on the bus
//
//-----------------------------------------------------------------------------

void Usb_Suspend(void)
{          
   // Put the device in a low power configuration
                               
   P0MDIN  = 0x00;                     // Port 0 configured as analog input
   P1MDIN  = 0x00;                     // Port 1 configured as analog input
   P2MDIN  = 0x00;                     // Port 2 configured as analog input
   P3MDIN  = 0x00;                     // Port 3 configured as analog input

   ADC0CN &= ~0x80;                    // Disable ADC0
   REF0CN  = 0x00;                     // Disable voltage reference

   OSCICN |= 0x20;                     // Put oscillator 

   // When the device receives a non-idle USB event, it will resume execution
   // on the instruction that follows OSCICN |= 0x20.  

   // Re-enable everything that was disabled when going into Suspend

   P0MDIN  = 0xFF;                     // Port 0 configured as digital pins
   P1MDIN  = 0x7F;                     // Port 1 pin 7 set as digital pin
   P2MDIN  = 0xFF;                     // Port 2 configured as digital pins
   P3MDIN  = 0xFF;                     // Port 3 configured as digital pins

   REF0CN  = 0x0E;                     // Enable voltage reference VREF
   ADC0CN |= 0x80;                     // Re-enable ADC0
}

//-----------------------------------------------------------------------------
// Usb_Resume
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Resume normal USB operation
//
//-----------------------------------------------------------------------------

void Usb_Resume(void)
{
   volatile int k;

   k++;

   // Add code for resume
}

//-----------------------------------------------------------------------------
// Fifo_Read
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//                1) BYTE addr : target address
//                2) unsigned int uNumBytes : number of bytes to unload
//                3) BYTE * pData : read data destination
//
// Read from the selected endpoint FIFO
//
//-----------------------------------------------------------------------------

void Fifo_Read(BYTE addr, unsigned int uNumBytes, BYTE * pData)
{
   int i;

   if (uNumBytes)                         // Check if >0 bytes requested,
   {
      USB0ADR = (addr);                   // Set address
      USB0ADR |= 0xC0;                    // Set auto-read and initiate
                                          // first read

      // Unload <NumBytes> from the selected FIFO
      for(i=0;i<uNumBytes-1;i++)
      {
         while(USB0ADR & 0x80);           // Wait for BUSY->'0' (data ready)
         pData[i] = USB0DAT;              // Copy data byte
      }

      USB0ADR = 0;                           // Clear auto-read

      while(USB0ADR & 0x80);               // Wait for BUSY->'0' (data ready)
      pData[i] = USB0DAT;                  // Copy data byte
   }
}

//-----------------------------------------------------------------------------
// Fifo_Write
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//                1) BYTE addr : target address
//                2) unsigned int uNumBytes : number of bytes to unload
//                3) BYTE * pData : location of source data
//
// Write to the selected endpoint FIFO
//
//-----------------------------------------------------------------------------

void Fifo_Write(BYTE addr, unsigned int uNumBytes, BYTE * pData)
{
   int i;

   // If >0 bytes requested,
   if (uNumBytes)
   {
      while(USB0ADR & 0x80);              // Wait for BUSY->'0'
                                          // (register available)
      USB0ADR = (addr);                   // Set address (mask out bits7-6)

      // Write <NumBytes> to the selected FIFO
      for(i=0;i<uNumBytes;i++)
      {
         USB0DAT = pData[i];
         while(USB0ADR & 0x80);           // Wait for BUSY->'0' (data ready)
      }
   }
}

//-----------------------------------------------------------------------------
// Force_Stall
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Force a procedural stall to be sent to the host
//
//-----------------------------------------------------------------------------

void Force_Stall(void)
{
   POLL_WRITE_BYTE(INDEX, 0);
   POLL_WRITE_BYTE(E0CSR, rbSDSTL);       // Set the send stall bit
   Ep_Status[0] = EP_STALL;               // Put the endpoint in stall status
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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