📄 usb.c
字号:
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
//
// Get the general interrupt status, these bits go into the upper 8 bits
// of the returned value.
//
ulStatus = HWREGB(ulBase + USB_O_IS);
//
// Add the power fault status.
//
if(HWREG(ulBase + USB_O_EPCISC) & USB_EPCISC_PF)
{
//
// Indicate a power fault was detected.
//
ulStatus |= USB_INTCTRL_POWER_FAULT;
//
// Clear the power fault interrupt.
//
HWREGB(ulBase + USB_O_EPCISC) |= USB_EPCISC_PF;
}
if(HWREG(USB0_BASE + USB_O_IDVISC) & USB_IDVRIS_ID)
{
//
// Indicate a id detection was detected.
//
ulStatus |= USB_INTCTRL_MODE_DETECT;
//
// Clear the id detection interrupt.
//
HWREG(USB0_BASE + USB_O_IDVISC) |= USB_IDVRIS_ID;
}
//
// Return the combined interrupt status.
//
return(ulStatus);
}
//*****************************************************************************
//
//! Disable endpoint interrupts on a given USB controller.
//!
//! \param ulBase specifies the USB module base address.
//! \param ulFlags specifies which endpoint interrupts to disable.
//!
//! This function will disable endpoint interrupts for the USB controller
//! specified by the \e ulBase parameter. The \e ulFlags parameter specifies
//! which endpoint interrupts to disable. The flags passed in the \e ulFlags
//! parameters should be the definitions that start with \b USB_INTEP_* and not
//! any other \b USB_INT flags.
//!
//! \return None.
//
//*****************************************************************************
void
USBIntDisableEndpoint(unsigned long ulBase, unsigned long ulFlags)
{
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
//
// If any transmit interrupts were disabled then write the transmit
// interrupt settings out to the hardware.
//
HWREGH(ulBase + USB_O_TXIE) &=
~(ulFlags & (USB_INTEP_HOST_OUT | USB_INTEP_DEV_IN | USB_INTEP_0));
//
// If any receive interrupts were disabled then write the receive interrupt
// settings out to the hardware.
//
HWREGH(ulBase + USB_O_RXIE) &=
~((ulFlags & (USB_INTEP_HOST_IN | USB_INTEP_DEV_OUT)) >>
USB_INTEP_RX_SHIFT);
}
//*****************************************************************************
//
//! Enable endpoint interrupts on a given USB controller.
//!
//! \param ulBase specifies the USB module base address.
//! \param ulFlags specifies which endpoint interrupts to enable.
//!
//! This function will enable endpoint interrupts for the USB controller
//! specified by the \e ulBase parameter. The \e ulFlags parameter specifies
//! which endpoint interrupts to enable. The flags passed in the \e ulFlags
//! parameters should be the definitions that start with \b USB_INTEP_* and not
//! any other \b USB_INT flags.
//!
//! \return None.
//
//*****************************************************************************
void
USBIntEnableEndpoint(unsigned long ulBase, unsigned long ulFlags)
{
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
//
// Enable any transmit endpoint interrupts.
//
HWREGH(ulBase + USB_O_TXIE) |=
ulFlags & (USB_INTEP_HOST_OUT | USB_INTEP_DEV_IN | USB_INTEP_0);
//
// Enable any receive endpoint interrupts.
//
HWREGH(ulBase + USB_O_RXIE) |=
((ulFlags & (USB_INTEP_HOST_IN | USB_INTEP_DEV_OUT)) >>
USB_INTEP_RX_SHIFT);
}
//*****************************************************************************
//
//! Returns the endpoint interrupt status on a given USB controller.
//!
//! \param ulBase specifies the USB module base address.
//!
//! This function will read endpoint interrupt status for a USB controller.
//! This call will return the current status for endpoint interrupts only, the
//! control interrupt status is retrieved by calling USBIntStatusControl().
//! The bit values returned should be compared against the \b USB_INTEP_*
//! values. These are grouped into classes for \b USB_INTEP_HOST_* and
//! \b USB_INTEP_DEV_* values to handle both host and device modes with all
//! endpoints.
//!
//! \note This call will clear the source of all of the endpoint interrupts.
//!
//! \return Returns the status of the endpoint interrupts for a USB controller.
//
//*****************************************************************************
unsigned long
USBIntStatusEndpoint(unsigned long ulBase)
{
unsigned long ulStatus;
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
//
// Get the transmit interrupt status.
//
ulStatus = HWREGH(ulBase + USB_O_TXIS);
ulStatus |= (HWREGH(ulBase + USB_O_RXIS) << USB_INTEP_RX_SHIFT);
//
// Return the combined interrupt status.
//
return(ulStatus);
}
//*****************************************************************************
//
//! Registers an interrupt handler for the USB controller.
//!
//! \param ulBase specifies the USB module base address.
//! \param pfnHandler is a pointer to the function to be called when a USB
//! interrupt occurs.
//!
//! This sets the handler to be called when a USB interrupt occurs. This will
//! also enable the global USB interrupt in the interrupt controller. The
//! specific desired USB interrupts must be enabled via a separate call to
//! USBIntEnable(). It is the interrupt handler's responsibility to clear the
//! interrupt sources via a calls to USBIntStatusControl() and
//! USBIntStatusEndpoint().
//!
//! \sa IntRegister() for important information about registering interrupt
//! handlers.
//!
//! \return None.
//
//*****************************************************************************
void
USBIntRegister(unsigned long ulBase, void(*pfnHandler)(void))
{
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
//
// Register the interrupt handler.
//
IntRegister(INT_USB0, pfnHandler);
//
// Enable the USB interrupt.
//
IntEnable(INT_USB0);
}
//*****************************************************************************
//
//! Unregisters an interrupt handler for the USB controller.
//!
//! \param ulBase specifies the USB module base address.
//!
//! This function unregister the interrupt handler. This function will also
//! disable the USB interrupt in the interrupt controller.
//!
//! \sa IntRegister() for important information about registering or
//! unregistering interrupt handlers.
//!
//! \return None.
//
//*****************************************************************************
void
USBIntUnregister(unsigned long ulBase)
{
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
//
// Unregister the interrupt handler.
//
IntUnregister(INT_USB0);
//
// Disable the CAN interrupt.
//
IntDisable(INT_USB0);
}
//*****************************************************************************
//
//! Returns the current status of an endpoint.
//!
//! \param ulBase specifies the USB module base address.
//! \param ulEndpoint is the endpoint to access.
//!
//! This function will return the status of a given endpoint. If any of these
//! status bits need to be cleared, then these these values must be cleared by
//! calling the USBDevEndpointStatusClear() or USBHostEndpointStatusClear()
//! functions.
//!
//! The following are the status flags for host mode:
//!
//! - \b USB_HOST_IN_PID_ERROR - PID error on the given endpoint.
//! - \b USB_HOST_IN_NOT_COMP - The device failed to respond to an IN request.
//! - \b USB_HOST_IN_STALL - A stall was received on an IN endpoint.
//! - \b USB_HOST_IN_DATA_ERROR - There was a CRC or bit-stuff error on an IN
//! endpoint in Isochronous mode.
//! - \b USB_HOST_IN_NAK_TO - NAKs received on this IN endpoint for more than
//! the specified timeout period.
//! - \b USB_HOST_IN_ERROR - Failed to communicate with a device using this IN
//! endpoint.
//! - \b USB_HOST_IN_FIFO_FULL - This IN endpoint's FIFO is full.
//! - \b USB_HOST_IN_PKTRDY - Data packet ready on this IN endpoint.
//! - \b USB_HOST_OUT_NAK_TO - NAKs received on this OUT endpoint for more than
//! the specified timeout period.
//! - \b USB_HOST_OUT_NOT_COMP - The device failed to respond to an OUT
//! request.
//! - \b USB_HOST_OUT_STALL - A stall was received on this OUT endpoint.
//! - \b USB_HOST_OUT_ERROR - Failed to communicate with a device using this
//! OUT endpoint.
//! - \b USB_HOST_OUT_FIFO_NE - This endpoint's OUT FIFO is not empty.
//! - \b USB_HOST_OUT_PKTPEND - The data transfer on this OUT endpoint has not
//! completed.
//! - \b USB_HOST_EP0_NAK_TO - NAKs received on endpoint zero for more than the
//! specified timeout period.
//! - \b USB_HOST_EP0_ERROR - The device failed to respond to a request on
//! endpoint zero.
//! - \b USB_HOST_EP0_IN_STALL - A stall was received on endpoint zero for an
//! IN transaction.
//! - \b USB_HOST_EP0_IN_PKTRDY - Data packet ready on endpoint zero for an IN
//! transaction.
//!
//! The following are the status flags for device mode:
//!
//! - \b USB_DEV_OUT_SENT_STALL - A stall was sent on this OUT endpoint.
//! - \b USB_DEV_OUT_DATA_ERROR - There was a CRC or bit-stuff error on an OUT
//! endpoint.
//! - \b USB_DEV_OUT_OVERRUN - An OUT packet was not loaded due to a full FIFO.
//! - \b USB_DEV_OUT_FIFO_FULL - The OUT endpoint's FIFO is full.
//! - \b USB_DEV_OUT_PKTRDY - There is a data packet ready in the OUT
//! endpoint's FIFO.
//! - \b USB_DEV_IN_NOT_COMP - A larger packet was split up, more data to come.
//! - \b USB_DEV_IN_SENT_STALL - A stall was sent on this IN endpoint.
//! - \b USB_DEV_IN_UNDERRUN - Data was requested on the IN endpoint and no
//! data was ready.
//! - \b USB_DEV_IN_FIFO_NE - The IN endpoint's FIFO is not empty.
//! - \b USB_DEV_IN_PKTPEND - The data transfer on this IN endpoint has not
//! completed.
//! - \b USB_DEV_EP0_SETUP_END - A control transaction ended before Data End
//! condition was sent.
//! - \b USB_DEV_EP0_SENT_STALL - A stall was sent on endpoint zero.
//! - \b USB_DEV_EP0_IN_PKTPEND - The data transfer on endpoint zero has not
//! completed.
//! - \b USB_DEV_EP0_OUT_PKTRDY - There is a data packet ready in endpoint
//! zero's OUT FIFO.
//!
//! \return The current status flags for the endpoint depending on mode.
//
//*****************************************************************************
unsigned long
USBEndpointStatus(unsigned long ulBase, unsigned long ulEndpoint)
{
unsigned long ulStatus;
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
(ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
(ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
(ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
(ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
(ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
(ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
(ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
//
// Get the TX portion of the endpoint status.
//
ulStatus = HWREGH(ulBase + EP_OFFSET(ulEndpoint) + USB_O_TXCSRL1);
//
// Get the RX portion of the endpoint status.
//
ulStatus |= ((HWREGH(ulBase + EP_OFFSET(ulEndpoint) + USB_O_RXCSRL1)) <<
USB_RX_EPSTATUS_SHIFT);
//
// Return the endpoint status.
//
return(ulStatus);
}
//*****************************************************************************
//
//! Clears the status bits in this endpoint in host mode.
//!
//! \param ulBase specifies the USB module base address.
//! \param ulEndpoint is the endpoint to access.
//! \param ulFlags are the status bits that will be cleared.
//!
//! This function will clear the status of any bits that are passed in the
//! \e ulFlags parameter. The \e ulFlags parameter can take the value returned
//! from the USBEndpointStatus() call.
//!
//! \note This function should only be called in host mode.
//!
//! \return None.
//
//*****************************************************************************
void
USBHostEndpointStatusClear(unsigned long ulBase, unsigned long ulEndpoint,
unsigned long ulFlags)
{
//
// Check the arguments.
//
ASSERT(ulBase == USB0_BASE);
ASSERT((ulEndpoint == USB_EP_0) || (ulEndpoint == USB_EP_1) ||
(ulEndpoint == USB_EP_2) || (ulEndpoint == USB_EP_3) ||
(ulEndpoint == USB_EP_4) || (ulEndpoint == USB_EP_5) ||
(ulEndpoint == USB_EP_6) || (ulEndpoint == USB_EP_7) ||
(ulEndpoint == USB_EP_8) || (ulEndpoint == USB_EP_9) ||
(ulEndpoint == USB_EP_10) || (ulEndpoint == USB_EP_11) ||
(ulEndpoint == USB_EP_12) || (ulEndpoint == USB_EP_13) ||
(ulEndpoint == USB_EP_14) || (ulEndpoint == USB_EP_15));
//
// Clear the specified flags for the endpoint.
//
if(ulEndpoint == USB_EP_0)
{
HWREGB(ulBase + USB_O_CSRL0) &= ~ulFlags;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -