/** * USBPERIPHERALAVRDU Peripheral AVR DU Specific Header File * @file usb_peripheral_avr_du.h * @defgroup usb_peripheral_avr_du USB Peripheral AVR DU * @ingroup usb_peripheral * @brief This file encompasses all the register settings of the AVR DU device in the form of inline functions. * It also abstracts the Read-Modify-Write loop for STATUS registers, which is required, as the hardware and software can both write * into the STATUS register. * @version USB Device Stack HAL Driver Version 1.0.0 */ /* (c) 2021 Microchip Technology Inc. and its subsidiaries. Subject to your compliance with these terms, you may use Microchip software and any derivatives exclusively with Microchip products. It is your responsibility to comply with third party license terms applicable to your use of third party software (including open source software) that may accompany Microchip software. THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. */ #ifndef USB_PERIPHERAL_AVR_DU_H // cppcheck-suppress misra-c2012-2.5 #define USB_PERIPHERAL_AVR_DU_H #ifdef __XC8 #include #else // avr-gcc #include #endif #include #include /** * @ingroup usb_peripheral_avr_du * @def ALWAYS_INLINE * @brief Alias that makes always inline function definitions more readable. */ #define ALWAYS_INLINE __attribute__((always_inline)) inline /** * @ingroup usb_peripheral_avr_du * @struct USB_ENDPOINT_TABLE_struct * @brief Represents the endpoint configuration table based on the number of endpoints in use. * The table data structure is defined by USB_EP_TABLE_struct in the device header file, * modified to support configuration of size from USB_EP_NUM. */ typedef struct USB_ENDPOINT_TABLE_struct { register8_t FIFO[USB_EP_NUM * 2u]; /**> USB_MAXEP_gp); } /** * @ingroup usb_peripheral_avr_du * @brief Sets the address of the endpoint table. * This is a device-specific function. * @param endpointTableAddress - Address of the endpoint table * @return None. */ static ALWAYS_INLINE void USB_EndpointTableAddressSet(USB_EP_PAIR_t *endpointTableAddress) { USB0.EPPTR = (uint16_t)endpointTableAddress; } /** * @ingroup usb_peripheral_avr_du * @brief Sets the address of the endpoint table to 0. * This is a device-specific function. * @param None. * @return None. */ static ALWAYS_INLINE void USB_EndpointTableAddressReset(void) { USB0.EPPTR = 0x0000; } /** * @ingroup usb_peripheral_avr_du * @brief Gets the address of the endpoint table. * This is a device-specific function. * @param None. * @return The address of the endpoint table */ static ALWAYS_INLINE uint16_t USB_EndpointTableAddressGet(void) { return (USB0.EPPTR); } /** * @ingroup usb_peripheral_avr_du * @brief Resets the read FIFO pointer. * This is a device-specific function. * @param None. * @return None. */ static ALWAYS_INLINE void USB_FifoReadPointerReset(void) { USB0.FIFORP |= (USB_FIFORP_gm); } /** * @ingroup usb_peripheral_avr_du * @brief Gets the read FIFO pointer. * This is a device-specific function. * @param None. * @return The FIFO read pointer */ static ALWAYS_INLINE int8_t USB_FifoReadPointerGet(void) { return (int8_t)(USB0.FIFORP); } /** * @ingroup usb_peripheral_avr_du * @brief Resets the write FIFO pointer. * This is a device-specific function. * @param None. * @return None. */ static ALWAYS_INLINE void USB_FifoWritePointerReset(void) { USB0.FIFOWP |= (USB_FIFOWP_gm); } /** * @ingroup usb_peripheral_avr_du * @brief Gets the write FIFO pointer. * This is a device-specific function. * @param None. * @return The FIFO write pointer */ static ALWAYS_INLINE int8_t USB_FifoWritePointerGet(void) { return (int8_t)(USB0.FIFOWP); } /** * @ingroup usb_peripheral_avr_du * @brief Sets the device address. * @param usbAddress - The device address to set * @return None. */ static ALWAYS_INLINE void USB_DeviceAddressSet(uint8_t usbAddress) { USB0.ADDR = ((USB0.ADDR & ~USB_ADDR_gm) | (((usbAddress) << USB_ADDR_gp) & USB_ADDR_gm)); } /** * @ingroup usb_peripheral_avr_du * @brief Resets the device address. * @param None. * @return None. */ static ALWAYS_INLINE void USB_DeviceAddressReset(void) { USB0.ADDR &= ~(USB_ADDR_gm); } /** * @ingroup usb_peripheral_avr_du * @brief Gets the device address. * @param None. * @return The device address */ static ALWAYS_INLINE uint8_t USB_DeviceAddressGet(void) { return ((USB0.ADDR & USB_ADDR_gm) >> USB_ADDR_gp); } /** * @ingroup usb_peripheral_avr_du * @brief Enables an upstream resume to be initated. * @param None. * @return None. */ static ALWAYS_INLINE void USB_UpstreamResumeEnable(void) { USB0.CTRLB |= USB_URESUME_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if upstream resume is enabled, but not yet initiated. * @param None. * @retval 0 - Upstream resume initiated or not enabled * @retval 1 - Upstream resume enabled */ static ALWAYS_INLINE bool USB_UpstreamResumeIsEnable(void) { return ((USB0.CTRLB & USB_URESUME_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Gets the USB bus state. * @param None. * @return The state of the USB bus */ static ALWAYS_INLINE uint8_t USB_BusStateGet(void) { return (USB0.BUSSTATE); } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB bus has any specific status flags set. * @param bus_state_bm - The bitmap of the specific status flags to check * @retval 0 - No status flags set * @retval 1 - The bus has one or more specified status flags set */ static ALWAYS_INLINE bool USB_BusStateIs(uint8_t bus_state_bm) { return ((USB0.BUSSTATE & (bus_state_bm)) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Start-Of-Frame interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SOFInterruptEnable(void) { USB0.INTCTRLA |= USB_SOF_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Start-Of-Frame interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SOFInterruptDisable(void) { USB0.INTCTRLA &= ~(USB_SOF_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Start-Of-Frame Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SOFInterruptClear(void) { USB0.INTFLAGSA = USB_SOF_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB Start-Of-Frame interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_SOFInterruptIs(void) { return ((USB0.INTFLAGSA & USB_SOF_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Suspend interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SuspendInterruptEnable(void) { USB0.INTCTRLA |= USB_SUSPEND_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Suspend interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SuspendInterruptDisable(void) { USB0.INTCTRLA &= ~(USB_SUSPEND_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Suspend Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SuspendInterruptClear(void) { USB0.INTFLAGSA = USB_SUSPEND_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB Suspend interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_SuspendInterruptIs(void) { return ((USB0.INTFLAGSA & USB_SUSPEND_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Resume interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_ResumeInterruptEnable(void) { USB0.INTCTRLA |= USB_RESUME_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Resume interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_ResumeInterruptDisable(void) { USB0.INTCTRLA &= ~(USB_RESUME_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Resume Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_ResumeInterruptClear(void) { USB0.INTFLAGSA = USB_RESUME_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB Resume interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_ResumeInterruptIs(void) { return ((USB0.INTFLAGSA & USB_RESUME_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Reset interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_ResetInterruptEnable(void) { USB0.INTCTRLA |= USB_RESET_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Reset interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_ResetInterruptDisable(void) { USB0.INTCTRLA &= ~(USB_RESET_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Reset Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_ResetInterruptClear(void) { USB0.INTFLAGSA = USB_RESET_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB Reset interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_ResetInterruptIs(void) { return ((USB0.INTFLAGSA & USB_RESET_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Stalled interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_StalledInterruptEnable(void) { USB0.INTCTRLA |= USB_STALLED_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Stalled interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_StalledInterruptDisable(void) { USB0.INTCTRLA &= ~(USB_STALLED_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Stalled Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_StalledInterruptClear(void) { USB0.INTFLAGSA = USB_STALLED_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB Stalled interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_StalledInterruptIs(void) { return ((USB0.INTFLAGSA & USB_STALLED_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Underflow interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_UnderflowInterruptEnable(void) { USB0.INTCTRLA |= USB_UNF_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Underflow interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_UnderflowInterruptDisable(void) { USB0.INTCTRLA &= ~(USB_UNF_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Underflow Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_UnderflowInterruptClear(void) { USB0.INTFLAGSA = USB_UNF_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if an Underflow interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_UnderflowInterruptIs(void) { return ((USB0.INTFLAGSA & USB_UNF_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Overflow interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_OverflowInterruptEnable(void) { USB0.INTCTRLA |= USB_OVF_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Overflow interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_OverflowInterruptDisable(void) { USB0.INTCTRLA &= ~(USB_OVF_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Overflow Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_OverflowInterruptClear(void) { USB0.INTFLAGSA = USB_OVF_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if an Overflow interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_OverflowInterruptIs(void) { return ((USB0.INTFLAGSA & USB_OVF_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Transaction Complete interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_TransactionCompleteInterruptEnable(void) { USB0.INTCTRLB |= USB_TRNCOMPL_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Transaction Complete interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_TransactionCompleteInterruptDisable(void) { USB0.INTCTRLB &= ~(USB_TRNCOMPL_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Transaction Complete Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_TransactionCompleteInterruptAck(void) { USB0.INTFLAGSB = USB_TRNCOMPL_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if a Transaction Complete interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_TransactionCompleteInterruptIs(void) { return ((USB0.INTFLAGSB & USB_TRNCOMPL_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB Read-Modify-Write Interrupt is enabled. * @param None. * @retval 0 - Interrupt not enabled * @retval 1 - Interrupt enabled */ static ALWAYS_INLINE bool USB_ReadModifyWriteInterruptIs(void) { return ((USB0.INTFLAGSB & USB_RMWBUSY_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Global NAK Done interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_GlobalNAKDoneInterruptEnable(void) { USB0.INTCTRLB |= USB_GNDONE_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Global NAK Done interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_GlobalNAKDoneInterruptDisable(void) { USB0.INTCTRLB &= ~(USB_GNDONE_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Global NAK Done Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_GlobalNAKDoneInterruptAck(void) { USB0.INTFLAGSB = USB_GNDONE_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if the USB Global NAK Done interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_GlobalNAKDoneInterruptIs(void) { return ((USB0.INTFLAGSB & USB_GNDONE_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Enables the USB Setup interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SetupInterruptEnable(void) { USB0.INTCTRLB |= USB_SETUP_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Disables the USB Setup interrupt. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SetupInterruptDisable(void) { USB0.INTCTRLB &= ~(USB_SETUP_bm); } /** * @ingroup usb_peripheral_avr_du * @brief Clears the USB Setup Interrupt flag. * @param None. * @return None. */ static ALWAYS_INLINE void USB_SetupInterruptClear(void) { USB0.INTFLAGSB = USB_SETUP_bm; } /** * @ingroup usb_peripheral_avr_du * @brief Checks if a USB Setup interrupt has been triggered. * @param None. * @retval 0 - Interrupt not triggered * @retval 1 - Interrupt triggered */ static ALWAYS_INLINE bool USB_SetupInterruptIs(void) { return ((USB0.INTFLAGSB & USB_SETUP_bm) != 0u); } /** * @ingroup usb_peripheral_avr_du * @brief Clears all the USB Interrupt flags. * @param None. * @return None. */ static ALWAYS_INLINE void USB_InterruptFlagsClear(void) { USB0.INTFLAGSA = 0xff; USB0.INTFLAGSB = 0xff; } #endif /* USB_PERIPHERAL_AVR_DU_H */