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

📄 f3xx_usb0_interruptserviceroutine.c

📁 这是nrf24lu1的无线鼠标源代码,应用平台是keil c
💻 C
📖 第 1 页 / 共 2 页
字号:
            Set_Address();
            break;
          case GET_DESCRIPTOR:
            Get_Descriptor();
            break;
          case GET_CONFIGURATION:
            Get_Configuration();
            break;
          case SET_CONFIGURATION:
            Set_Configuration();
            break;
          case GET_INTERFACE:
            Get_Interface();
            break;
          case SET_INTERFACE:
            Set_Interface();
            break;
          default:
            Force_Stall();               // Send stall to host if invalid request
            break;
        }
      }
    }
  }
  if (EP_STATUS[0] == EP_TX)              // See if endpoint should transmit
  {
    if (!(ControlReg & rbINPRDY) )        // Don't overwrite last packet
    {
      POLL_READ_BYTE (E0CSR, ControlReg);  // Read control register
      
      // Check to see if SETUP End or Out Packet received, if so do not put any new data on FIFO
      if ((!(ControlReg & rbSUEND)) || (!(ControlReg & rbOPRDY)))
      {
        ControlReg = rbINPRDY; // Add In Packet ready flag to E0CSR bitmask
        if (DATASIZE >= EP0_PACKET_SIZE)
        {
          // Break Data into multiple packets if larger than Max Packet
          Fifo_Write_InterruptServiceRoutine (FIFO_EP0, EP0_PACKET_SIZE, (uint8_t*)DATAPTR);
          DATAPTR  += EP0_PACKET_SIZE;  // Advance data pointer
          DATASIZE -= EP0_PACKET_SIZE;  // Decrement data size        
          DATASENT += EP0_PACKET_SIZE;  // Increment data sent counter
        }
        else
        {
          // If data is less than Max Packet size or zero
          Fifo_Write_InterruptServiceRoutine (FIFO_EP0, DATASIZE, (uint8_t*)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
  {
    POLL_READ_BYTE (E0CSR, ControlReg); // Read control register
    if (ControlReg & rbOPRDY)        // Verify packet was received
    {
      ControlReg = rbSOPRDY;
 		  if (DATASIZE >= EP0_PACKET_SIZE)
   		{
        Fifo_Read(FIFO_EP0, EP0_PACKET_SIZE, (uint8_t*)DATAPTR);        
        DATAPTR  += EP0_PACKET_SIZE;  // Advance data pointer
        DATASIZE -= EP0_PACKET_SIZE;  // Decrement data size
        DATASENT += EP0_PACKET_SIZE;  // Increment data sent counter
   		}
   		else
   		{
        Fifo_Read (FIFO_EP0, DATASIZE, (uint8_t*) DATAPTR); // read bytes from FIFO

        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_In(uint8_t ep_num)
//-----------------------------------------------------------------------------
//
// Handler will be entered after the endpoint's buffer has been
// transmitted to the host. In<ep_num>_StateMachine is set to Idle, which
// signals the foreground routine SendPacket that the Endpoint
// is ready to transmit another packet.
//-----------------------------------------------------------------------------
void Handle_In(uint8_t ep_num)
{
  EP_STATUS[ep_num] = 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 ()
{
  uint8_t 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_Resume
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Resume normal USB operation
//
//-----------------------------------------------------------------------------

void Usb_Resume(void)
{
}

//-----------------------------------------------------------------------------
// Usb_Suspend
//-----------------------------------------------------------------------------
// Enter suspend mode after suspend signalling is present on the bus
//
void Usb_Suspend(void)
{
}

//-----------------------------------------------------------------------------
// 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(uint8_t addr, uint16_t uNumBytes, uint8_t* pData)
{
uint16_t 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
//
// and Fifo_Write_InterruptServiceRoutine is used for calls made in an ISR.

//-----------------------------------------------------------------------------
void Fifo_Write_InterruptServiceRoutine(uint8_t addr, uint16_t uNumBytes, uint8_t * pData)
{
uint16_t i;
  if(uNumBytes)                   // If >0 bytes requested,
  {
    while (USB0ADR & 0x80);       // Wait for BUSY->'0' (register available)
    USB0ADR = addr;               // Set address (mask out bits7-6)
    
    for(i=0;i<uNumBytes;i++)      // Write <NumBytes> to the selected FIFO
    {
      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
}

/** @} */

⌨️ 快捷键说明

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