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

📄 f3xx_usb0_interruptserviceroutine.c

📁 8051F340/320的USB鼠标例程
💻 C
📖 第 1 页 / 共 2 页
字号:
               									  (unsigned char*)DATAPTR);
               // Advance data pointer
               DATAPTR  += EP0_PACKET_SIZE;
               // Decrement data size
               DATASIZE -= EP0_PACKET_SIZE;
               // Increment data sent counter
               DATASENT += EP0_PACKET_SIZE;
            }
            else
            {
               // If data is less than Max Packet size or zero
               Fifo_Write_InterruptServiceRoutine (FIFO_EP0, DATASIZE,
               									  (unsigned char*)DATAPTR);
               ControlReg |= rbDATAEND;// Add Data End bit to bitmask
               EP_STATUS[0] = EP_IDLE; // Return EP 0 to idle state
            }
            if (DATASENT == SETUP.wLength.i)
            {
               // 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
               ControlReg |= rbDATAEND;// Add Data End bit to mask
               EP_STATUS[0] = EP_IDLE; // Return EP 0 to idle state
            }
            // Write mask to E0CSR
            POLL_WRITE_BYTE(E0CSR, ControlReg);
         }
      }
   }

   if (EP_STATUS[0] == EP_RX)          // See if endpoint should transmit
   {
      // Read control register
      POLL_READ_BYTE (E0CSR, ControlReg);
      if (ControlReg & rbOPRDY)        // Verify packet was received
      {
         ControlReg = rbSOPRDY;
   		if (DATASIZE >= EP0_PACKET_SIZE)
   		{
            Fifo_Read(FIFO_EP0, EP0_PACKET_SIZE, (unsigned char*)DATAPTR);
            // Advance data pointer
            DATAPTR  += EP0_PACKET_SIZE;
            // Decrement data size
            DATASIZE -= EP0_PACKET_SIZE;
            // Increment data sent counter
            DATASENT += EP0_PACKET_SIZE;
   		}
   		else
   		{
            // read bytes from FIFO
            Fifo_Read (FIFO_EP0, DATASIZE, (unsigned char*) DATAPTR);

            ControlReg |= rbDATAEND;   // signal end of data
            EP_STATUS[0] = EP_IDLE;    // set Endpoint to IDLE
   		}
   		if (DATASENT == SETUP.wLength.i)
   		{
   			ControlReg |= rbDATAEND;
   			EP_STATUS[0] = EP_IDLE;
   		}
         // if EP_RX mode was entered through a SET_REPORT request,
         // call the ReportHandler_OUT function and pass the Report
         // ID, which is the first by the of DATAPTR's buffer
         if ( (EP_STATUS[0] == EP_IDLE) && (SETUP.bRequest == SET_REPORT) )
         {
            ReportHandler_OUT (*DATAPTR);
         }

         if (EP_STATUS[0] != EP_STALL) POLL_WRITE_BYTE (E0CSR, ControlReg);
      }
   }

}

//-----------------------------------------------------------------------------
// Handle_In1
//-----------------------------------------------------------------------------
//
// Handler will be entered after the endpoint's buffer has been
// transmitted to the host.  In1_StateMachine is set to Idle, which
// signals the foreground routine SendPacket that the Endpoint
// is ready to transmit another packet.
//-----------------------------------------------------------------------------
void Handle_In1 ()
{
      EP_STATUS[1] = EP_IDLE;
}

//-----------------------------------------------------------------------------
// Handle_Out1
//-----------------------------------------------------------------------------
// Take the received packet from the host off the fifo and put it into
// the Out_Packet array.
//
//-----------------------------------------------------------------------------
void Handle_Out1 ()
{

   unsigned char Count = 0;
   unsigned char ControlReg;

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

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

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

      Setup_OUT_BUFFER ();             // configure buffer to save
                                       // received data
      Fifo_Read(FIFO_EP1, OUT_BUFFER.Length, OUT_BUFFER.Ptr);

      // process data according to received Report ID.
      // In systems with Report Descriptors that do not define report IDs,
      // the host will still format OUT packets with a prefix byte
      // of '0x00'.

      ReportHandler_OUT (OUT_BUFFER.Ptr[0]);

      POLL_WRITE_BYTE (EOUTCSR1, 0);   // Clear Out Packet ready bit
   }
}

//-----------------------------------------------------------------------------
// Usb_Suspend
//-----------------------------------------------------------------------------
// Enter suspend mode after suspend signalling is present on the bus
//
void Usb_Suspend (void)
{
   volatile int k;
   k++;
}

//-----------------------------------------------------------------------------
// 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 (unsigned char addr, unsigned int uNumBytes,
               unsigned char * 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);i++)
      {
         while (USB0ADR & 0x80);       // Wait for BUSY->'0' (data ready)
         pData[i] = USB0DAT;           // Copy data byte
      }

      //while(USB0ADR & 0x80);         // Wait for BUSY->'0' (data ready)
      USB0ADR = 0;                     // Clear auto-read
   }
}

//-----------------------------------------------------------------------------
// 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
//
// Fifo_Write_Foreground is used for function calls made in the foreground routines,
// and Fifo_Write_InterruptServiceRoutine is used for calls made in an ISR.

//-----------------------------------------------------------------------------

void Fifo_Write_Foreground (unsigned char addr, unsigned int uNumBytes,
                    unsigned char * 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)
      }
   }
}

void Fifo_Write_InterruptServiceRoutine (unsigned char addr,
                                         unsigned int uNumBytes,
                                         unsigned char * 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
}


//-----------------------------------------------------------------------------
// SendPacket
//-----------------------------------------------------------------------------
//
// Return Value - None
// Parameters - Report ID that's used to call the appropriate IN handler
//
// This function can be called by other routines to force an IN packet
// transmit.  It takes as an input the Report ID of the packet to be
// transmitted.
//-----------------------------------------------------------------------------

void SendPacket (unsigned char ReportID)
{
   bit EAState;
   unsigned char ControlReg;

   EAState = EA;
   EA = 0;

   POLL_WRITE_BYTE (INDEX, 1);         // Set index to endpoint 1 registers

   // Read contol register for EP 1
    POLL_READ_BYTE (EINCSR1, ControlReg);

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

   else if(EP_STATUS[1] == EP_IDLE)
   {
      // the state will be updated inside the ISR handler
      EP_STATUS[1] = EP_TX;

      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);
      }

      ReportHandler_IN_Foreground (ReportID);

      // Put new data on Fifo
      Fifo_Write_Foreground (FIFO_EP1, IN_BUFFER.Length,
                    (unsigned char *)IN_BUFFER.Ptr);
      POLL_WRITE_BYTE (EINCSR1, rbInINPRDY);
                                       // Set In Packet ready bit,
   }                                   // indicating fresh data on FIFO 1

   EA = EAState;
}

⌨️ 快捷键说明

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