From c9335332df86fbc895a8868a34abcc98817d53f0 Mon Sep 17 00:00:00 2001 From: Joshua Drake Date: Sat, 6 Dec 2025 14:22:21 -0600 Subject: Inital Commit --- .../usb/usb_common/usb_common_elements.h | 99 ++++ mcc_generated_files/usb/usb_common/usb_core.c | 212 +++++++ mcc_generated_files/usb/usb_common/usb_core.h | 95 +++ .../usb/usb_common/usb_core_descriptors.c | 658 ++++++++++++++++++++ .../usb/usb_common/usb_core_descriptors.h | 181 ++++++ .../usb/usb_common/usb_core_events.c | 170 ++++++ .../usb/usb_common/usb_core_events.h | 158 +++++ .../usb/usb_common/usb_core_requests.c | 194 ++++++ .../usb/usb_common/usb_core_requests.h | 105 ++++ .../usb/usb_common/usb_core_requests_device.c | 181 ++++++ .../usb/usb_common/usb_core_requests_device.h | 107 ++++ .../usb/usb_common/usb_core_requests_endpoint.c | 137 +++++ .../usb/usb_common/usb_core_requests_endpoint.h | 84 +++ .../usb/usb_common/usb_core_requests_interface.c | 145 +++++ .../usb/usb_common/usb_core_requests_interface.h | 101 ++++ .../usb/usb_common/usb_core_transfer.c | 175 ++++++ .../usb/usb_common/usb_core_transfer.h | 116 ++++ .../usb/usb_common/usb_protocol_headers.h | 660 +++++++++++++++++++++ 18 files changed, 3578 insertions(+) create mode 100644 mcc_generated_files/usb/usb_common/usb_common_elements.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core_descriptors.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core_descriptors.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core_events.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core_events.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests_device.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests_device.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests_interface.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core_requests_interface.h create mode 100644 mcc_generated_files/usb/usb_common/usb_core_transfer.c create mode 100644 mcc_generated_files/usb/usb_common/usb_core_transfer.h create mode 100644 mcc_generated_files/usb/usb_common/usb_protocol_headers.h (limited to 'mcc_generated_files/usb/usb_common') diff --git a/mcc_generated_files/usb/usb_common/usb_common_elements.h b/mcc_generated_files/usb/usb_common/usb_common_elements.h new file mode 100644 index 0000000..688bdd3 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_common_elements.h @@ -0,0 +1,99 @@ +/** + * USBCOMMONELEMENTS Common Elements Header File + * @file usb_common_elements.h + * @defgroup usb_common USB Common Elements + * @ingroup usb_core + * @brief Common elements for the USB stack. + * @version USB Common Elements 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_COMMON_ELEMENTS_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_COMMON_ELEMENTS_H + +#ifndef TEST +#define STATIC static +#else +#define STATIC +#endif + +/** + * @ingroup usb_common + * @enum RETURN_CODE_enum + * @brief Describes the different function return reserved codes used by the USB stack. + */ + +typedef enum RETURN_CODE_enum +{ + UNSUPPORTED = 2, /** +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +RETURN_CODE_t USB_SetupProcess(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (USB_REQUEST_TYPE_STANDARD == (USB_REQUEST_TYPE_t)setupRequestPtr->bmRequestType.type) + { + // Checks that an IN request actually requests data. + if ((USB_REQUEST_DIR_IN == (USB_REQUEST_DIR_t)setupRequestPtr->bmRequestType.dataPhaseTransferDirection) && (0u == setupRequestPtr->wLength)) + { + status = CONTROL_SETUP_DIRECTION_ERROR; + } + else + { + // Makes sure the data out transfer is reset before handling requests. + USB_ControlTransferDataSet(NULL, 0u); + + switch (setupRequestPtr->bmRequestType.recipient) + { + case USB_REQUEST_RECIPIENT_DEVICE: + { + status = USB_SetupProcessDeviceRequest(setupRequestPtr); + break; + } + case USB_REQUEST_RECIPIENT_ENDPOINT: + { + status = USB_SetupProcessEndpointRequest(setupRequestPtr); + break; + } + case USB_REQUEST_RECIPIENT_INTERFACE: + { + status = USB_SetupProcessInterfaceRequest(setupRequestPtr); + break; + } + case USB_REQUEST_RECIPIENT_OTHER: + { + if (NULL != event.OtherRequest) + { + status = event.OtherRequest(setupRequestPtr); + } + else + { + status = UNSUPPORTED; + } + break; + } + default: + status = UNSUPPORTED; + break; + } + } + } + else if (USB_REQUEST_TYPE_CLASS == (USB_REQUEST_TYPE_t)setupRequestPtr->bmRequestType.type) + { + if (NULL != event.ClassRequest) + { + status = event.ClassRequest(setupRequestPtr); + } + else + { + status = UNSUPPORTED; + } + } + else if (USB_REQUEST_TYPE_VENDOR == (USB_REQUEST_TYPE_t)setupRequestPtr->bmRequestType.type) + { + if (NULL != event.VendorRequest) + { + status = event.VendorRequest(setupRequestPtr); + } + else + { + status = UNSUPPORTED; + } + } + else + { + status = UNSUPPORTED; + } + + return status; +} + +RETURN_CODE_t USB_Start(void) +{ + RETURN_CODE_t status = SUCCESS; + + // Configures setup callback. + USB_ControlProcessSetupCallbackRegister(USB_SetupProcess); + + // Sets up the peripheral. + USB_PeripheralInitialize(); + + // Initializes and configures the endpoints. + USB_PIPE_t pipe = { .address = 0 }; + while (pipe.address < USB_EP_NUM) + { + if (status == SUCCESS) + { + pipe.direction = USB_EP_DIR_OUT; + status = USB_PipeReset(pipe); + } + if (status == SUCCESS) + { + pipe.direction = USB_EP_DIR_IN; + status = USB_PipeReset(pipe); + } + pipe.address++; + } + + // Initializes the control endpoints. + if (status == SUCCESS) + { + status = USB_ControlEndpointsInit(); + } + + // Attaches the device to the bus. + if (status == SUCCESS) + { + status = USB_ControlTransferReset(); + } + + if (status == SUCCESS) + { + USB_BusAttach(); + } + + return status; +} + +RETURN_CODE_t USB_Stop(void) +{ + RETURN_CODE_t status = SUCCESS; + + // Detaches from the bus and disables peripheral. + USB_BusDetach(); + USB_PeripheralDisable(); + + // Aborts any ongoing transfers. + USB_PIPE_t pipe = { .address = 0 }; + while (pipe.address < USB_EP_NUM) + { + if (status == SUCCESS) + { + pipe.direction = USB_EP_DIR_OUT; + status = USB_TransferAbort(pipe); + } + if (status == SUCCESS) + { + pipe.direction = USB_EP_DIR_IN; + status = USB_TransferAbort(pipe); + } + pipe.address++; + } + + return status; +} + +RETURN_CODE_t USB_Reset(void) +{ + RETURN_CODE_t status = UNINITIALIZED; + status = USB_Stop(); + if (status == SUCCESS) + { + status = USB_Start(); + } + return status; +} diff --git a/mcc_generated_files/usb/usb_common/usb_core.h b/mcc_generated_files/usb/usb_common/usb_core.h new file mode 100644 index 0000000..cf43dd8 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core.h @@ -0,0 +1,95 @@ +/** + * USBCORE CORE Header File + * @file usb_core.h + * @defgroup usb_core USB Core Layer + * @brief Core functionality for the USB stack. + * @version USB Device Core 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_CORE_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_H + +#include +#include + +#include +#include +#include +#include + +/** + * @ingroup usb_core + * @brief Setup function for the Standard Device Request USB 2.0 Specification Ch 9.4. + * | bRequest | wValue | wIndex | wLength | Data | + * |-------------------|---------------------------------------|-------------------------|-------------------|-------------------------------------| + * | CLEAR_FEATURE | Feature Selector | Zero | None | | + * | CLEAR_FEATURE | Feature Selector | Interface | None | | + * | CLEAR_FEATURE | Feature Selector | Endpoint | None | | + * | GET_CONFIGURATION | Zero | Zero | One | Configuration Value | + * | GET_DESCRIPTOR | Descriptor type and Descriptor index | Zero or Language ID | Descriptor Length | Descriptor | + * | GET_INTERFACE | Zero | Interface | One | Alternate Interface | + * | GET_STATUS | Zero | Zero Interface Endpoint | Two | Device status | + * | GET_STATUS | Zero | Interface | Two | Interface Status | + * | GET_STATUS | Zero | Endpoint | Two | Endpoint Status | + * | SET_ADDRESS | Device Address | Zero | Zero | None | + * | SET_CONFIGURATION | Configuration Value | Zero | Zero | None | + * | SET_DESCRIPTOR | Descriptor type and Descriptor index | Zero or Language ID | Descriptor Length | Descriptor | + * | SET_FEATURE | Feature Selector | Zero Interface Endpoint | Zero | None | + * | SET_FEATURE | Feature Selector | Interface | Zero | | + * | SET_FEATURE | Feature Selector | Endpoint | Zero | | + * | SET_INTERFACE | Alternate Setting | Interface | Zero | None | + * | SYNCH_FRAME | Zero | Endpoint | Two | Frame Number | + * @param *setupRequestPtr - Pointer to the setup request and its data + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupProcess(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core + * @brief Starts the USB peripheral, configures the callbacks and attaches it to the bus. + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_Start(void); + +/** + * @ingroup usb_core + * @brief Stops the USB peripheral and detaches it from the bus. + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_Stop(void); + +/** + * @ingroup usb_core + * @brief Resets the USB peripheral. + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_Reset(void); + +#endif /* USB_CORE_H */ diff --git a/mcc_generated_files/usb/usb_common/usb_core_descriptors.c b/mcc_generated_files/usb/usb_common/usb_core_descriptors.c new file mode 100644 index 0000000..1ea29db --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_descriptors.c @@ -0,0 +1,658 @@ +/** + * USBCOREDESCRIPTOR Core Descriptors Source File + * @file usb_core_descriptors.h + * @ingroup usb_core_descriptors + * @brief descriptors for the USB Core Stack. + * @version USB Device Core 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +/** + * @ingroup usb_core_descriptors + * @def USB_DEFAULT_INTERFACE + * @brief Default interface number. + */ +#define USB_DEFAULT_INTERFACE 0u + +/** + * @ingroup usb_core_descriptors + * @def USB_DEFAULT_ALTERNATE_SETTING + * @brief Default alternate setting. + */ +#define USB_DEFAULT_ALTERNATE_SETTING 0u + +/** + * @ingroup usb_core_descriptors + * @def USB_DESCRIPTOR_SEARCH_LIMIT + * @brief The number of descriptors NextDescriptorPointerGet will search through before returning an error. + */ +#define USB_DESCRIPTOR_SEARCH_LIMIT 30u + +/** + * @typedef USB_DESCRIPTOR_PTR_t + * @brief Union of a uint8_t pointer and pointers to the different descriptor types. + * @misradeviation{@advisory,19.2} Needed for the stack to parse through the configuration descriptors + * without pointer casting between the different descriptor types and uint8_t. + */ +// cppcheck-suppress misra-c2012-19.2 +typedef union USB_DESCRIPTOR_PTR_union +{ + uint8_t *bytePtr; + USB_DESCRIPTOR_HEADER_t *headerPtr; + USB_ENDPOINT_DESCRIPTOR_t *endpointPtr; + USB_INTERFACE_DESCRIPTOR_t *interfacePtr; + USB_CONFIGURATION_DESCRIPTOR_t *configurationPtr; +} USB_DESCRIPTOR_PTR_t; + + +STATIC USB_CONFIGURATION_DESCRIPTOR_t *activeConfigurationPtr = NULL; +STATIC uint8_t activeInterfaces[USB_INTERFACE_NUM]; +STATIC USB_DESCRIPTOR_POINTERS_t *applicationPointers = NULL; + +RETURN_CODE_t USB_DescriptorPointersSet(USB_DESCRIPTOR_POINTERS_t *descriptorPointersPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (NULL != descriptorPointersPtr) + { + // Checks that the device pointer is pointing to a struct with the device type. + if ((NULL == descriptorPointersPtr->devicePtr) || (USB_DESCRIPTOR_TYPE_DEVICE != (USB_DESCRIPTOR_TYPE_t)descriptorPointersPtr->devicePtr->header.bDescriptorType)) + { + status = DESCRIPTOR_POINTER_ERROR; + } + // Checks that the first configuration pointer is pointing to a struct with the configuration type. + else if ((NULL == descriptorPointersPtr->configurationsPtr) || (USB_DESCRIPTOR_TYPE_CONFIGURATION != (USB_DESCRIPTOR_TYPE_t)descriptorPointersPtr->configurationsPtr->header.bDescriptorType)) + { + status = DESCRIPTOR_POINTER_ERROR; + } + else + { + status = SUCCESS; + } + } + else + { + status = DESCRIPTOR_POINTER_ERROR; + } + + if (SUCCESS == status) + { + // Saves the pointer structure address. + applicationPointers = descriptorPointersPtr; + } + + return status; +} + +RETURN_CODE_t USB_DescriptorConfigurationEnable(uint8_t configurationValue) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Descriptor pointer to search through the different descriptors + // cppcheck-suppress misra-c2012-19.2 + USB_DESCRIPTOR_PTR_t currentDescriptor; + + if (NULL == applicationPointers) + { + status = DESCRIPTOR_POINTER_ERROR; + } + else + { + if (NULL != activeConfigurationPtr) + { + status = SUCCESS; + + // Find and disable all active interfaces in the current configuration + currentDescriptor.configurationPtr = activeConfigurationPtr; + uint8_t numInterfaces = activeConfigurationPtr->bNumInterfaces; + while ((SUCCESS == status) && (numInterfaces > 0u)) + { + status = NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_INTERFACE, ¤tDescriptor.headerPtr); + + if (SUCCESS == status) + { + if (activeInterfaces[currentDescriptor.interfacePtr->bInterfaceNumber] == currentDescriptor.interfacePtr->bAlternateSetting) + { + status = USB_DescriptorInterfaceConfigure(currentDescriptor.interfacePtr->bInterfaceNumber, USB_DEFAULT_ALTERNATE_SETTING, false); + numInterfaces--; + } + } + } + } + else + { + // No current configuration, nothing to disable + status = SUCCESS; + } + + if (SUCCESS == status) + { + if (USB_REQUEST_DEVICE_DISABLE_CONFIGURATION == configurationValue) + { + // Active configuration is disabled, so clear pointer + activeConfigurationPtr = NULL; + } + else + { + // Get new configuration pointer and enable its interfaces + status = ConfigurationPointerGet(configurationValue, &activeConfigurationPtr); + + if (SUCCESS == status) + { + // Find and enable all interfaces in the set configuration with bAlternateSetting == 0 + currentDescriptor.configurationPtr = activeConfigurationPtr; + uint8_t numInterfaces = activeConfigurationPtr->bNumInterfaces; + while ((SUCCESS == status) && (numInterfaces > 0u)) + { + status = NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_INTERFACE, ¤tDescriptor.headerPtr); + if (SUCCESS == status) + { + if (USB_DEFAULT_ALTERNATE_SETTING == currentDescriptor.interfacePtr->bAlternateSetting) + { + status = USB_DescriptorInterfaceConfigure(currentDescriptor.interfacePtr->bInterfaceNumber, USB_DEFAULT_ALTERNATE_SETTING, true); + numInterfaces--; + } + } + } + } + } + } + } + + return status; +} + +uint8_t USB_DescriptorActiveConfigurationValueGet(void) +{ + uint8_t configurationValue = USB_REQUEST_DEVICE_DISABLE_CONFIGURATION; + + if (NULL != activeConfigurationPtr) + { + configurationValue = activeConfigurationPtr->bConfigurationValue; + } + + return configurationValue; +} + +bool USB_DescriptorActiveConfigurationSelfPoweredGet(void) +{ + return (activeConfigurationPtr->bmAttributes & USB_CONFIG_ATTR_SELF_POWERED) == USB_CONFIG_ATTR_SELF_POWERED; +} + +bool USB_DescriptorActiveConfigurationRemoteWakeupGet(void) +{ + return (activeConfigurationPtr->bmAttributes & USB_CONFIG_ATTR_REMOTE_WAKEUP) == USB_CONFIG_ATTR_REMOTE_WAKEUP; +} + +RETURN_CODE_t ConfigurationPointerGet(uint8_t configurationValue, USB_CONFIGURATION_DESCRIPTOR_t **configurationPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Checks if the requested configuration is an existing configuration. + if (configurationValue > applicationPointers->devicePtr->bNumConfigurations) + { + status = DESCRIPTOR_CONFIGURATIONS_ERROR; + } + else + { + // If there is only one configuration, return the pointer to the configuration. + if (1u == applicationPointers->devicePtr->bNumConfigurations) + { + *configurationPtr = applicationPointers->configurationsPtr; + status = SUCCESS; + } + else + { + // Pointer initialized to the address of the first configuration descriptor. + // cppcheck-suppress misra-c2012-19.2 + USB_DESCRIPTOR_PTR_t currentDescriptor = { .configurationPtr = applicationPointers->configurationsPtr }; + + uint8_t i = applicationPointers->devicePtr->bNumConfigurations; + while (i-- > 0u) + { + if (currentDescriptor.configurationPtr->bConfigurationValue != configurationValue) + { + status = NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_CONFIGURATION, ¤tDescriptor.headerPtr); + } + else + { + status = SUCCESS; + + // Ends loop on success + i = 0; + } + + if (SUCCESS != status) + { + // Ends loop on error. + i = 0; + } + } + + if (SUCCESS == status) + { + if (currentDescriptor.configurationPtr->bConfigurationValue == configurationValue) + { + *configurationPtr = currentDescriptor.configurationPtr; + } + else + { + // configurationValue not found + status = DESCRIPTOR_SEARCH_ERROR; + } + } + } + } + + return status; +} + +RETURN_CODE_t NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_t descriptorType, USB_DESCRIPTOR_HEADER_t **descriptorHeaderPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // cppcheck-suppress misra-c2012-19.2 + USB_DESCRIPTOR_PTR_t currentDescriptor = { .headerPtr = *descriptorHeaderPtr }; + + uint8_t incrementCount = 0u; + while (UNINITIALIZED == status) + { + // Increments to the start of the next pointer. + if ((USB_DESCRIPTOR_TYPE_CONFIGURATION == descriptorType) && (USB_DESCRIPTOR_TYPE_CONFIGURATION == (USB_DESCRIPTOR_TYPE_t)currentDescriptor.headerPtr->bDescriptorType)) + { + // If the device is looking for the next configuration, it increments the pointer with total length of the configuration. + currentDescriptor.bytePtr = ¤tDescriptor.bytePtr[currentDescriptor.configurationPtr->wTotalLength]; + } + else + { + // Else it only increments with the descriptor length. + currentDescriptor.bytePtr = ¤tDescriptor.bytePtr[currentDescriptor.headerPtr->bLength]; + } + + // Checks whether it has found the correct descriptor type or if it needs to continue looping. + if (descriptorType == (USB_DESCRIPTOR_TYPE_t)currentDescriptor.headerPtr->bDescriptorType) + { + status = SUCCESS; + *descriptorHeaderPtr = currentDescriptor.headerPtr; + } + else + { + // If it has looped through too many descriptors, it is assumed that the descriptor structure is set up incorrectly and the loop is exited. + if (incrementCount++ > USB_DESCRIPTOR_SEARCH_LIMIT) + { + status = DESCRIPTOR_SEARCH_ERROR; + } + } + } + + return status; +} + +RETURN_CODE_t USB_DescriptorInterfaceConfigure(uint8_t interfaceNumber, uint8_t alternateSetting, bool enable) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (NULL != activeConfigurationPtr) + { + // Pointer initialized to the address of the active configuration descriptor + // cppcheck-suppress misra-c2012-19.2 + USB_DESCRIPTOR_PTR_t currentDescriptor = { .configurationPtr = activeConfigurationPtr }; + + // Limit to end of configuration to make sure the loop does not overflow + uint8_t *endOfConfiguration = ¤tDescriptor.bytePtr[currentDescriptor.configurationPtr->wTotalLength]; + + // Find first interface descriptor before entering loop + status = NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_INTERFACE, ¤tDescriptor.headerPtr); + + // Loop through all the descriptors in the current configuration + USB_INTERFACE_DESCRIPTOR_t *enableInterfacePtr = NULL; + while ((SUCCESS == status) && (currentDescriptor.bytePtr < endOfConfiguration)) + { + // Check if interface number and alternate setting correspond to the active interface before disabling endpoints + if ((interfaceNumber == currentDescriptor.interfacePtr->bInterfaceNumber) && (activeInterfaces[interfaceNumber] == currentDescriptor.interfacePtr->bAlternateSetting)) + { + // Disable endpoints for the active alternate interface + status = DescriptorEndpointsConfigure(currentDescriptor.interfacePtr, false); + if (SUCCESS == status) + { + // Reset the active alternate interface to 0 + status = ActiveAlternateSettingSet(interfaceNumber, USB_DEFAULT_ALTERNATE_SETTING); + } + } + + if (enable) + { + if (interfaceNumber == currentDescriptor.interfacePtr->bInterfaceNumber) + { + if (alternateSetting == currentDescriptor.interfacePtr->bAlternateSetting) + { + // Requested interface found + enableInterfacePtr = currentDescriptor.interfacePtr; + } + } + } + + status = NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_INTERFACE, ¤tDescriptor.headerPtr); + if (DESCRIPTOR_SEARCH_ERROR == status) + { + // Search error from NextDescriptorPointerGet means search is complete + if ((false == enable) || (NULL != enableInterfacePtr)) + { + // Search was successful, correcting status + status = SUCCESS; + + // Set byte pointer to end of config to exit loop + currentDescriptor.bytePtr = endOfConfiguration; + } + } + } + + if (SUCCESS == status) + { + if (true == enable) + { + if (NULL != enableInterfacePtr) + { + // Enable the endpoints for the activated interface + status = DescriptorEndpointsConfigure(enableInterfacePtr, true); + if (SUCCESS == status) + { + status = ActiveAlternateSettingSet(interfaceNumber, alternateSetting); + } + } + else + { + status = DESCRIPTOR_SEARCH_ERROR; + } + } + } + } + else + { + status = DESCRIPTOR_POINTER_ERROR; + } + + return status; +} + +RETURN_CODE_t ActiveAlternateSettingSet(uint8_t interfaceNumber, uint8_t alternateSetting) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (interfaceNumber < USB_INTERFACE_NUM) + { + activeInterfaces[interfaceNumber] = alternateSetting; + status = SUCCESS; + } + else + { + status = INTERFACE_SET_ERROR; + } + + return status; +} + +RETURN_CODE_t ActiveAlternateSettingGet(uint8_t interfaceNumber, uint8_t *alternateSetting) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (interfaceNumber < USB_INTERFACE_NUM) + { + *alternateSetting = activeInterfaces[interfaceNumber]; + status = SUCCESS; + } + else + { + status = INTERFACE_GET_ERROR; + } + + return status; +} + +RETURN_CODE_t DescriptorEndpointsConfigure(USB_INTERFACE_DESCRIPTOR_t *interfacePtr, bool enable) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if ((uint8_t)USB_DESCRIPTOR_TYPE_INTERFACE == interfacePtr->header.bDescriptorType) + { + // The number of endpoints to enable/disable is found from the interface. + uint8_t numEndpoints = interfacePtr->bNumEndpoints; + if (numEndpoints > 0u) + { + // cppcheck-suppress misra-c2012-19.2 + USB_DESCRIPTOR_PTR_t currentDescriptor = { .interfacePtr = interfacePtr }; + + while (numEndpoints > 0u) + { + // Pre-increments to the next descriptor, since the initial descriptor is the interface. + currentDescriptor.bytePtr = ¤tDescriptor.bytePtr[currentDescriptor.headerPtr->bLength]; + + if (USB_DESCRIPTOR_TYPE_ENDPOINT == (USB_DESCRIPTOR_TYPE_t)currentDescriptor.headerPtr->bDescriptorType) + { + if (true == enable) + { + // Configures endpoint according to descriptor. + status + = USB_EndpointConfigure(currentDescriptor.endpointPtr->bEndpointAddress, currentDescriptor.endpointPtr->wMaxPacketSize, currentDescriptor.endpointPtr->bmAttributes.type); + } + else + { + // Aborts any ongoing transfer and disable endpoint. + status = USB_TransferAbort(currentDescriptor.endpointPtr->bEndpointAddress); + if (SUCCESS == status) + { + status = USB_EndpointDisable(currentDescriptor.endpointPtr->bEndpointAddress); + } + } + + numEndpoints--; + + if (SUCCESS != status) + { + // Exits loop immediately. + numEndpoints = 0; + } + } + } + } + else + { + // Interface has no endpoints. + status = SUCCESS; + } + } + else + { + status = DESCRIPTOR_ENDPOINT_ERROR; + } + + return status; +} + +RETURN_CODE_t USB_DescriptorPointerGet(USB_DESCRIPTOR_TYPE_t descriptor, uint8_t attribute, uint8_t **descriptorPtr, uint16_t *descriptorLength) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // cppcheck-suppress misra-c2012-19.2 + USB_DESCRIPTOR_PTR_t localDescriptorPtr; + + switch (descriptor) + { + case USB_DESCRIPTOR_TYPE_DEVICE: + // Returns pointer to device descriptor. + if (applicationPointers != NULL) + { + *descriptorPtr = (uint8_t *)applicationPointers->devicePtr; + *descriptorLength = (uint16_t)applicationPointers->devicePtr->header.bLength; + status = SUCCESS; + } + else + { + status = DESCRIPTOR_POINTER_ERROR; + } + break; + case USB_DESCRIPTOR_TYPE_CONFIGURATION:; + // Returns pointer to configuration, with the total length. + + status = ConfigurationPointerGet(attribute, &localDescriptorPtr.configurationPtr); + if (SUCCESS == status) + { + *descriptorPtr = localDescriptorPtr.bytePtr; + *descriptorLength = localDescriptorPtr.configurationPtr->wTotalLength; + } + break; + case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER: + // Only for high-speed. + status = UNSUPPORTED; + break; + case USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION: + // Only for high-speed. + status = UNSUPPORTED; + break; + case USB_DESCRIPTOR_TYPE_BOS: + if (NULL != applicationPointers->deviceBOSptr) + { + *descriptorPtr = (uint8_t *)applicationPointers->deviceBOSptr; + *descriptorLength = (uint16_t)applicationPointers->deviceBOSptr->wTotalLength; + status = SUCCESS; + } + else + { + status = UNSUPPORTED; + } + break; + default: + // Find class or vendor descriptor types in the active configuration + if (descriptor >= USB_DESCRIPTOR_TYPE_CLASS) + { + localDescriptorPtr.configurationPtr = activeConfigurationPtr; + + // Find the first descriptor available of the current type + status = NextDescriptorPointerGet(descriptor, &localDescriptorPtr.headerPtr); + if (SUCCESS == status) + { + *descriptorPtr = (uint8_t *)localDescriptorPtr.headerPtr; + *descriptorLength = localDescriptorPtr.headerPtr->bLength; + } + } + else + { + status = DESCRIPTOR_REQUEST_ERROR; + } + + break; + } + + if (SUCCESS != status) + { + *descriptorPtr = NULL; + *descriptorLength = 0u; + } + + return status; +} + +RETURN_CODE_t USB_DescriptorStringPointerGet(uint8_t stringIndex, uint16_t langID, uint8_t **descriptorAddressPtr, uint16_t *descriptorLength) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (NULL != applicationPointers->langIDptr) + { + if (stringIndex == 0u) + { + // Index 0 is the language id index, writes pointer and length. + *descriptorAddressPtr = (uint8_t *)applicationPointers->langIDptr; + *descriptorLength = (uint16_t)applicationPointers->langIDptr->header.bLength; + status = SUCCESS; + } + else + { + // Iterates through the supported language IDs to find the index. + uint8_t langIdx = 0; + for (langIdx = 0; langIdx < LANG_ID_NUM; langIdx++) + { + if (langID == applicationPointers->langIDptr->id_array[langIdx]) + { + // Language match, langIdx found. + break; + } + } + + if (LANG_ID_NUM == langIdx) + { + // Language ID not in langID struct, returns UNSUPPORTED. + status = UNSUPPORTED; + } + else if (NULL != applicationPointers->stringPtrs[langIdx]) + { + // Iterates through string descriptors to account for different string lengths. + USB_DESCRIPTOR_HEADER_t *stringHeader = applicationPointers->stringPtrs[langIdx]; + if (1u == stringIndex) + { + status = SUCCESS; + } + else + { + for (uint8_t i = 1u; i < stringIndex; i++) + { + status = NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_STRING, &stringHeader); + } + } + + // Writes pointer and length. + if (SUCCESS == status) + { + *descriptorAddressPtr = (uint8_t *)stringHeader; + *descriptorLength = (uint16_t)stringHeader->bLength; + } + } + else + { + // stringPtrs not set up correctly, returns error. + status = DESCRIPTOR_POINTER_ERROR; + } + } + } + else + { + // langIDptr not set up, returns unsupported and stalls request. + status = UNSUPPORTED; + } + + return status; +} diff --git a/mcc_generated_files/usb/usb_common/usb_core_descriptors.h b/mcc_generated_files/usb/usb_common/usb_core_descriptors.h new file mode 100644 index 0000000..c5dd4e3 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_descriptors.h @@ -0,0 +1,181 @@ +/** + * USBCOREDESCRIPTOR Core Descriptors Header File + * @file usb_core_descriptors.h + * @defgroup usb_core_descriptors USB Descriptor Definitions + * @ingroup usb_core + * @brief In this file the active configuration and interfaces can be changed. + * The active configuration and active interface is referenced by a pointer as two global variables. + * @version USB Device Core 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_CORE_DESCRIPTORS_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_DESCRIPTORS_H + +#include + +#include +#include + +/** + * @ingroup usb_core + * @brief Handles Descriptor pointer setup. + * Sets the address to the application descriptor pointers. + * Checks if the device pointer and a pointer to the start of the application configuration(s) are set before saving the address to the USB Core Stack. + * @param *descriptorPtr - The address of the application descriptor pointers + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_DescriptorPointersSet(USB_DESCRIPTOR_POINTERS_t *descriptorPtr); + +/** + * @ingroup usb_core + * @brief Enables endpoint configuration descriptor. + * + * The USB Device Enable Endpoint function, from USB 2.0 Specification Ch. 9.6.6. + * | Offset | Field | Size | Value | Description | + * |--------|------------------|------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| + * | 0 | bLength | 1 | Number | Size of this descriptor in bytes | + * | 1 | bDescriptorType | 1 | Constant | CONFIGURATION Descriptor Type | + * | 2 | bEndpointAddress | 1 | Endpoint | Address of the endpoint on the USB device described by this descriptor. Address is encoded by the following: Bit 3-0: The endpoint number, Bit 6-4: Reserved, reset to zero, Bit 7: Direction, ignored for control endpoint. 0 = OUT endpoint, 1 = IN endpoint | + * | 3 | bmAttributes | 1 | Bitmap | Describes the endpoint attributes when it is configured using the bConfigurationValue. Bit 1-0: Transfer type, 00=Control, 01=Isochronous, 10=Bulk, 11=Interrupt. If not isochronous endpoint, bit 5-2 are reserved and must be set to zero. If isochronous: Bit 3-2: Synchronization type, 00 = No Sync, 01 = Async, 10 = Adaptive, 11 = Sync. Bit 5-4: Usage type, 00 = Data endpoint, 01 = Feedback endpoint, 10 = Implicit feedback Data endpoint, 11 = Reserved. All other bits are reserved and must be reset to zero. Reserved bits must be ignored by the host. | + * | 4 | wMaxPacketSize | 2 | Number | Maximum packet size this endpoint is capable of RX/TX when this configuration is selected. For isochronous endpoints, this value is used to reserve bus time in the schedule. For all endpoints, bit 10-0 specify maximum packet size. For high-speed isochronous and interrupt endpoints: Bit 12-11 number of additional transaction opportunities per microframe, 00 = None (1 transaction per microframe), 01 = 1 additional, 10 = 2 additional, 11 = Reserved. Bits 15-13 are reserved and must be set to zero. | + * + * @param configurationValue - The value of the configuration to be enabled + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_DescriptorConfigurationEnable(uint8_t configurationValue); + +/** + * @ingroup usb_core + * @brief Gets the Self-Powered setting from the active configuration. + * @param None. + * @retval 0 - Self-Powered is not enabled + * @retval 1 - Self-Powered is enabled + */ +bool USB_DescriptorActiveConfigurationSelfPoweredGet(void); + +/** + * @ingroup usb_core + * @brief Gets the Remote Wake-up setting from the active configuration. + * @param None. + * @retval 0 - Remote Wake-up is not enabled + * @retval 1 - Remote Wake-up is enabled + */ +bool USB_DescriptorActiveConfigurationRemoteWakeupGet(void); + +/** + * @ingroup usb_core + * @brief Gets the active configuration value. + * @param None. + * @return The active configuration value + */ +uint8_t USB_DescriptorActiveConfigurationValueGet(void); + +/** + * @ingroup usb_core + * @brief Enables or Disables an Interface Descriptor. + * + * The USB Device Enable Interface Descriptor, from USB 2.0 Specification Ch. 9.6.5. + * | Offset | Field | Size | Value | Description | + * |--------|---------------------|------|----------|---------------------------------------------------------------------------------------------------------------------------------------| + * | 2 | bInterfaceNumber | 1 | Number | Number of this interface. Zero-based value identifying the index in the array of concurrent interfaces supported by this configuration| + * | 3 | bAlternateSetting | 1 | Number | Value used to select this alternate setting for the interface identified in the prior field | + * + * @param interfaceNumber - Interface number value + * @param alternateSetting - Alternative settings value, ignored if enable is false + . @param enable - Enable or disable the interface. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_DescriptorInterfaceConfigure(uint8_t interfaceNumber, uint8_t alternateSetting, bool enable); + +/** + * @ingroup usb_core + * @brief Gets the pointer to the descriptor. + * @param descriptor - Descriptor type + * @param attribute - Attribute type + * @param **descriptorPtr - Pointer to the descriptor + * @param *descriptorLength - Length of the descriptor + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_DescriptorPointerGet(USB_DESCRIPTOR_TYPE_t descriptor, uint8_t attribute, uint8_t **descriptorPtr, uint16_t *descriptorLength); + +/** + * @ingroup usb_core + * @brief Gets the pointer to the string descriptor. + * @param stringIndex - Index of the string + * @param langID - Language ID + * @param **descriptorAddressPtr - Pointer to the descriptor + * @param *descriptorLength - Length of the descriptor + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_DescriptorStringPointerGet(uint8_t stringIndex, uint16_t langID, uint8_t **descriptorAddressPtr, uint16_t *descriptorLength); + +/** + * @ingroup usb_core + * @brief Get the active alternate interface number for an interface. + * @param interfaceNumber - The interface number to get the alternate setting for + * @param *alternateSetting - The requested alternate setting + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t ActiveAlternateSettingGet(uint8_t interfaceNumber, uint8_t *alternateSetting); + +/** + * @ingroup usb_core + * @brief Set the active alternate interface number for an interface. + * @param interfaceNumber - The interface number to set the alternate setting for + * @param alternateSetting - The alternate setting to set + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t ActiveAlternateSettingSet(uint8_t interfaceNumber, uint8_t alternateSetting); + +/** + * @ingroup usb_core + * @brief Collects the configuration pointer. + * @param configurationValue - Value of the referenced configuration + * @param **configurationPtr - Pointer to the configuration + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t ConfigurationPointerGet(uint8_t configurationValue, USB_CONFIGURATION_DESCRIPTOR_t **configurationPtr); + +/** + * @ingroup usb_core + * @brief Configures the endpoints as given in the descriptor. + * @param *interfacePtr - Pointer to an interface + * @param enable - Boolean to enable or disable the endpoint + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t DescriptorEndpointsConfigure(USB_INTERFACE_DESCRIPTOR_t *interfacePtr, bool enable); + +/** + * @ingroup usb_core + * @brief Gets the next descriptor. + * @param descriptorType - Selected descriptor type + * @param **descriptorHeaderPtr - Pointer to the descriptor header + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t NextDescriptorPointerGet(USB_DESCRIPTOR_TYPE_t descriptorType, USB_DESCRIPTOR_HEADER_t **descriptorHeaderPtr); + +#endif /* USB_CORE_DESCRIPTORS_H */ \ No newline at end of file diff --git a/mcc_generated_files/usb/usb_common/usb_core_events.c b/mcc_generated_files/usb/usb_common/usb_core_events.c new file mode 100644 index 0000000..8fd11b9 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_events.c @@ -0,0 +1,170 @@ +/** + * USBCOREEVENTS USB Core Events Source File + * @file usb_core_events.h + * @ingroup usb_core_events + * @brief Event handling for the USB Core Stack. + * @version USB Device Core 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +USB_EVENT_HANDLERS_t event; + +RETURN_CODE_t USB_EventHandler(void) +{ + RETURN_CODE_t status = SUCCESS; + + if (USB_EventSOFIsReceived() == true) + { + USB_EventSOFClear(); + if (NULL != event.SOFCallback) + { + event.SOFCallback(); + } + } + if (USB_EventResetIsReceived() == true) + { + USB_EventResetClear(); + if (NULL != event.ResetCallback) + { + event.ResetCallback(); + } + USB_PIPE_t pipe = { .address = 0 }; + while (pipe.address < USB_EP_NUM) + { + pipe.direction = USB_EP_DIR_IN; + if (SUCCESS == status) + { + status = USB_TransferAbort(pipe); + } + pipe.direction = USB_EP_DIR_OUT; + if (SUCCESS == status) + { + status = USB_TransferAbort(pipe); + } + pipe.address++; + } + status = USB_Reset(); + } + uint8_t eventOverUnderflow = USB_EventOverUnderflowIsReceived(); + if (0u < eventOverUnderflow) + { + USB_EventOverUnderflowClear(); + uint8_t controlOverUnderflow = USB_ControlOverUnderflowIsReceived(); + if (0u < controlOverUnderflow) + { + status = USB_ControlProcessOverUnderflow(controlOverUnderflow); + } + else + { + // Non-control overunderflows currently ignored by event handler + status = SUCCESS; + } + } + if (USB_EventSuspendIsReceived() == true) + { + USB_EventSuspendClear(); + if (NULL != event.SuspendCallback) + { + event.SuspendCallback(); + } + } + if (USB_EventResumeIsReceived() == true) + { + USB_EventResumeClear(); + if (NULL != event.ResumeCallback) + { + event.ResumeCallback(); + } + } + if (USB_EventStalledIsReceived() == true) + { + USB_EventStalledClear(); + USB_PIPE_t pipe = { .address = 0x00, .direction = USB_EP_DIR_OUT }; + status = USB_HandleEventStalled(pipe); + } + return status; +} + +void USB_SetConfigurationCallbackRegister(USB_SETUP_EVENT_CALLBACK_t callback) +{ + event.SetConfiguration = callback; +} + +void USB_SetInterfaceCallbackRegister(USB_SETUP_EVENT_CALLBACK_t callback) +{ + event.SetInterface = callback; +} + +void USB_InterfaceDisabledCallbackRegister(USB_EVENT_CALLBACK_t callback) +{ + event.InterfaceDisabled = callback; +} + +void USB_VendorRequestCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback) +{ + event.VendorRequest = callback; +} + +void USB_ClassRequestCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback) +{ + event.ClassRequest = callback; +} + +void USB_OtherRequestCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback) +{ + event.OtherRequest = callback; +} + +void USB_SOFCallbackRegister(USB_EVENT_CALLBACK_t callback) +{ + event.SOFCallback = callback; +} + +void USB_ResetCallbackRegister(USB_EVENT_CALLBACK_t callback) +{ + event.ResetCallback = callback; +} + +void USB_SuspendCallbackRegister(USB_EVENT_CALLBACK_t callback) +{ + event.SuspendCallback = callback; +} + +void USB_ResumeCallbackRegister(USB_EVENT_CALLBACK_t callback) +{ + event.ResumeCallback = callback; +} diff --git a/mcc_generated_files/usb/usb_common/usb_core_events.h b/mcc_generated_files/usb/usb_common/usb_core_events.h new file mode 100644 index 0000000..bed84e7 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_events.h @@ -0,0 +1,158 @@ +/** + * USBCOREEVENTS USB Core Events Header File + * @file usb_core_events.h + * @defgroup usb_core_events USB Core Events + * @ingroup usb_core + * @brief Event handling for the USB Core Stack. + * @version USB Device Core 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_CORE_EVENTS_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_EVENTS_H + +#include +#include + +#include + +#include +#include +#include +#include +#include + +/** + * @ingroup usb_core + * @struct USB_EVENT_HANDLERS_struct + * @brief Represents the event callbacks, the configuration and the enumeration setups. + */ +typedef struct USB_EVENT_HANDLERS_struct +{ + USB_SETUP_EVENT_CALLBACK_t SetConfiguration; + USB_SETUP_EVENT_CALLBACK_t SetInterface; + USB_EVENT_CALLBACK_t InterfaceDisabled; + USB_SETUP_PROCESS_CALLBACK_t VendorRequest; + USB_SETUP_PROCESS_CALLBACK_t ClassRequest; + USB_SETUP_PROCESS_CALLBACK_t OtherRequest; + USB_EVENT_CALLBACK_t SOFCallback; + USB_EVENT_CALLBACK_t ResetCallback; + USB_EVENT_CALLBACK_t SuspendCallback; + USB_EVENT_CALLBACK_t ResumeCallback; +} USB_EVENT_HANDLERS_t; + +extern USB_EVENT_HANDLERS_t event; + +/** + * @ingroup usb_core + * @brief Handles the different types of events. + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ + +RETURN_CODE_t USB_EventHandler(void); + +/** + * @ingroup usb_core + * @brief Registers a callback for Set Configuration requests. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_SetConfigurationCallbackRegister(USB_SETUP_EVENT_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for Set Interface requests. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_SetInterfaceCallbackRegister(USB_SETUP_EVENT_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for disabling interfaces. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_InterfaceDisabledCallbackRegister(USB_EVENT_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for vendor requests. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_VendorRequestCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for class requests. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_ClassRequestCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for other requests. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_OtherRequestCallbackRegister(USB_SETUP_PROCESS_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for Start-Of-Frame (SOF) events. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_SOFCallbackRegister(USB_EVENT_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for Reset events. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_ResetCallbackRegister(USB_EVENT_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for Suspend events. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_SuspendCallbackRegister(USB_EVENT_CALLBACK_t callback); + +/** + * @ingroup usb_core + * @brief Registers a callback for Resume events. + * @param callback - Reference for the callback function + * @return None. + */ +void USB_ResumeCallbackRegister(USB_EVENT_CALLBACK_t callback); + +#endif /* USB_CORE_EVENTS_H */ diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests.c b/mcc_generated_files/usb/usb_common/usb_core_requests.c new file mode 100644 index 0000000..ebfaab4 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests.c @@ -0,0 +1,194 @@ +/** + * USBCOREREQUESTS USB Core Requests Source File + * @file usb_core_requests.c + * @ingroup usb_core_requests + * @brief USB Device Core Requests handling. + * @version USB Device Core 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +RETURN_CODE_t USB_SetupProcessDeviceRequest(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + switch (setupRequestPtr->bRequest) + { + case USB_REQUEST_GET_STATUS: + { + status = SetupDeviceRequestGetStatus(); + break; + } + case USB_REQUEST_CLEAR_FEATURE: + { + status = SetupDeviceRequestClearFeature(setupRequestPtr); + break; + } + case USB_REQUEST_SET_FEATURE: + { + status = SetupDeviceRequestSetFeature(setupRequestPtr); + break; + } + case USB_REQUEST_SET_ADDRESS: + { + status = SetupDeviceRequestSetAddress((uint8_t)setupRequestPtr->wValue & 0xffu); + break; + } + case USB_REQUEST_GET_DESCRIPTOR: + { + status = SetupDeviceRequestGetDescriptor(setupRequestPtr); + break; + } + case USB_REQUEST_SET_DESCRIPTOR: + { + // Set Descriptor not supported, please STALL + status = UNSUPPORTED; + break; + } + case USB_REQUEST_GET_CONFIGURATION: + { + status = SetupDeviceRequestGetConfiguration(); + break; + } + case USB_REQUEST_SET_CONFIGURATION: + { + status = SetupDeviceRequestSetConfiguration((uint8_t)(setupRequestPtr->wValue & 0xffu)); + break; + } + default: + // Invalid request, please STALL + status = UNSUPPORTED; + break; + } + + return status; +} + +RETURN_CODE_t USB_SetupProcessEndpointRequest(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Gets the requested endpoint, endpoint address and its direction + USB_PIPE_t endpoint = EndpointFromRequestGet(setupRequestPtr->wIndex); + + // Checks if the request is for a valid endpoint + if (endpoint.address >= (uint8_t)USB_EP_NUM) + { + status = ENDPOINT_ADDRESS_ERROR; + } + else + { + // Handles the actual endpoint requests + switch (setupRequestPtr->bRequest) + { + case USB_REQUEST_GET_STATUS: + { + status = SetupEndpointRequestGetStatus(setupRequestPtr); + break; + } + case USB_REQUEST_CLEAR_FEATURE: + { + status = SetupEndpointRequestClearFeature(setupRequestPtr); + break; + } + case USB_REQUEST_SET_FEATURE: + { + status = SetupEndpointRequestSetFeature(setupRequestPtr); + break; + } + case USB_REQUEST_SYNCH_FRAME: + { + status = SetupEndpointRequestSynchFrame(); + break; + } + default: + // Invalid request, please STALL + status = UNSUPPORTED; + break; + } + } + + return status; +} + +RETURN_CODE_t USB_SetupProcessInterfaceRequest(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Gets the requested interface number + // Handles the actual interface requests + switch (setupRequestPtr->bRequest) + { + case USB_REQUEST_GET_STATUS: + { + status = USB_SetupInterfaceRequestGetStatus(); + break; + } + case USB_REQUEST_CLEAR_FEATURE: + { + // Features are not specified for USB 2 interfaces, please STALL + status = UNSUPPORTED; + break; + } + case USB_REQUEST_SET_FEATURE: + { + // Features are not specified for USB 2 interfaces, please STALL + status = UNSUPPORTED; + break; + } + case USB_REQUEST_GET_INTERFACE: + { + status = USB_SetupInterfaceRequestGetInterface(setupRequestPtr); + break; + } + case USB_REQUEST_SET_INTERFACE: + { + status = USB_SetupInterfaceRequestSetInterface(setupRequestPtr); + break; + } + case USB_REQUEST_GET_DESCRIPTOR: + { + status = USB_SetupInterfaceRequestGetDescriptor(setupRequestPtr); + break; + } + default: + // Invalid request, please STALL + status = UNSUPPORTED; + break; + } + + return status; +} diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests.h b/mcc_generated_files/usb/usb_common/usb_core_requests.h new file mode 100644 index 0000000..3cdc993 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests.h @@ -0,0 +1,105 @@ +/** + * USBCOREREQUESTS USB Core Requests Header File + * @file usb_core_requests.h + * @defgroup usb_core_requests USB Core Requests + * @ingroup usb_core + * @brief USB Device Core Requests handling. + * @version USB Device Core 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_CORE_REQUESTS_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_REQUESTS_H + +#include +#include + +#include +#include +#include +#include +#include + +/** + * @ingroup usb_core_requests + * @brief Setup function for the device requests + * + * USB 2.0 Specification Ch 9.4. + * | bRequest | wValue | wIndex | wLength | Data | + * |-------------------|-------------------|------------|---------|---------------------| + * | CLEAR_FEATURE | Feature selector | Zero | Zero | None | + * | GET_CONFIGURATION | Zero | Zero | One | Config value | + * | GET_DESCRIPTOR | Type and index | Zero or ID | Length | Descriptor | + * | GET_STATUS | Zero | Endpoint | Two | Device status | + * | SET_ADDRESS | Device address | Zero | Zero | None | + * | SET_CONFIGURATION | Config value | Zero | Zero | None | + * | SET_DESCRIPTOR | Type and index | Zero or ID | Length | Descriptor | + * | SET_FEATURE | Feature selector | Zero | Zero | None | + * + * @param setupRequestPtr - Pointer to the setup request and its data + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupProcessDeviceRequest(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Setup function for the endpoint requests + * + * USB 2.0 Specification Ch. 9.4. + * | bRequest | wValue | wIndex | wLength | Data | + * |---------------|------------------|----------|---------|-----------------| + * | CLEAR_FEATURE | Feature selector | Endpoint | Zero | None | + * | GET_STATUS | Zero | Endpoint | Two | Endpoint status | + * | SET_FEATURE | Feature selector | Endpoint | Zero | None | + * | SYNCH_FRAME | Zero | Endpoint | Two | Frame number | + * + * @param *setupRequestPtr - Pointer to the request and its data + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupProcessEndpointRequest(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Setup function for the interface requests + * + * USB 2.0 Specification Ch 9.4. + * | bRequest | wValue | wIndex | wLength | Data | + * |-----------------|-------------------|-----------|---------|---------------------| + * | CLEAR_FEATURE | Feature selector | Interface | Zero | None | + * | GET_INTERFACE | Zero | Interface | One | Alternate interface | + * | GET_STATUS | Zero | Interface | Two | Interface | + * | SET_FEATURE | Feature selector | Interface | Zero | None | + * | SET_INTERFACE | Alternate setting | Interface | Zero | None | + * | GET_DESCRIPTOR | Type and index | Zero | Length | Descriptor | + * + * @param setupRequestPtr - Pointer to the setup request and its data + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupProcessInterfaceRequest(USB_SETUP_REQUEST_t *setupRequestPtr); + +#endif /* USB_CORE_REQUESTS_H */ + diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests_device.c b/mcc_generated_files/usb/usb_common/usb_core_requests_device.c new file mode 100644 index 0000000..eda9772 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests_device.c @@ -0,0 +1,181 @@ +/** + * USBCOREREQUESTSDEVICE USB Core Requests Device Core File + * @file usb_core_requests_device.c + * @ingroup usb_core_requests + * @brief USB Device Core Requests handling. + * @version USB Device Core 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +STATIC uint8_t deviceAddress = 0; + +RETURN_CODE_t SetupDeviceRequestGetStatus(void) +{ + // Return IN transaction with Remote Wake-up and Self Powered. + uint8_t data[] = {0, 0}; + + if (USB_DescriptorActiveConfigurationSelfPoweredGet()) + { + data[0] |= USB_REQUEST_DEVICE_SELF_POWERED; + } + if (USB_DescriptorActiveConfigurationRemoteWakeupGet()) + { + data[0] |= USB_REQUEST_DEVICE_REMOTE_WAKEUP; + } + + return USB_ControlTransferDataWriteBuffer(data, sizeof(data)); +} + +RETURN_CODE_t SetupDeviceRequestClearFeature(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Clear feature for DEVICE_REMOTE_WAKEUP and TEST_MODE not yet supported + (void)setupRequestPtr; + + return status; +} + +RETURN_CODE_t SetupDeviceRequestSetFeature(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Set feature for DEVICE_REMOTE_WAKEUP and TEST_MODE not yet supported + (void)setupRequestPtr; + + return status; +} + +RETURN_CODE_t SetupDeviceRequestSetAddress(uint8_t address) +{ + // Must register the callback here since device address must be set after completion of status stage. + deviceAddress = address; + USB_ControlEndOfRequestCallbackRegister(&SetupDeviceAddressCallback); + + return SUCCESS; +} + +void SetupDeviceAddressCallback(void) +{ + USB_DeviceAddressConfigure(deviceAddress); + USB_ControlEndOfRequestCallbackRegister(NULL); +} + +RETURN_CODE_t SetupDeviceRequestGetDescriptor(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + uint8_t descriptorType = (uint8_t)(setupRequestPtr->wValue >> 8u); + uint8_t descriptorIndex = (uint8_t)(setupRequestPtr->wValue & 0xffu); + + if (USB_DESCRIPTOR_TYPE_VENDOR <= (USB_DESCRIPTOR_TYPE_t)descriptorType) + { + // Vendor Get_Descriptor Requests handled by VendorRequest callback. + if (NULL != event.VendorRequest) + { + status = event.VendorRequest(setupRequestPtr); + } + else + { + status = UNSUPPORTED; + } + } + else if (USB_DESCRIPTOR_TYPE_CLASS <= (USB_DESCRIPTOR_TYPE_t)descriptorType) + { + // Class Get_Descriptor Requests handled by ClassRequest callback. + if (NULL != event.ClassRequest) + { + status = event.ClassRequest(setupRequestPtr); + } + else + { + status = UNSUPPORTED; + } + } + else + { + uint8_t *descriptorPtr = NULL; + uint16_t descriptorLength = 0; + + if (USB_DESCRIPTOR_TYPE_STRING == (USB_DESCRIPTOR_TYPE_t)descriptorType) + { + status = USB_DescriptorStringPointerGet(descriptorIndex, setupRequestPtr->wIndex, &descriptorPtr, &descriptorLength); + } + else + { + // USB_DescriptorPointerGet will handle remaining invalid descriptorTypes. + status = USB_DescriptorPointerGet(descriptorType, descriptorIndex, &descriptorPtr, &descriptorLength); + } + + if (SUCCESS == status) + { + if (descriptorLength > setupRequestPtr->wLength) + { + descriptorLength = setupRequestPtr->wLength; + } + + status = USB_ControlTransferDataSet(descriptorPtr, descriptorLength); + } + } + + return status; +} + +RETURN_CODE_t SetupDeviceRequestGetConfiguration(void) +{ + uint8_t configurationValue = USB_DescriptorActiveConfigurationValueGet(); + + return USB_ControlTransferDataWriteBuffer(&configurationValue, sizeof(configurationValue)); +} + +RETURN_CODE_t SetupDeviceRequestSetConfiguration(uint8_t configurationValue) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if ((deviceAddress == 0u)) + { + status = USB_CONNECTION_ERROR; + } + else + { + // Enables configuration, clears it if configurationValue is zero. + status = USB_DescriptorConfigurationEnable(configurationValue); + } + + return status; +} diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests_device.h b/mcc_generated_files/usb/usb_common/usb_core_requests_device.h new file mode 100644 index 0000000..d04b6ea --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests_device.h @@ -0,0 +1,107 @@ +/** + * USBCOREREQUESTSDEVICE USB Core Requests Device Header File + * @file usb_core_requests_device.h + * @ingroup usb_core_requests + * @brief USB Device Core Requests handling. + * @version USB Device Core 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_CORE_REQUESTS_DEVICE_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_REQUESTS_DEVICE_H + +#include +#include + +#include "usb_protocol_headers.h" +#include "usb_common_elements.h" + +/** + * @ingroup usb_core_requests + * @brief Returns the status of the device features. + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupDeviceRequestGetStatus(void); + +/** + * @ingroup usb_core_requests + * @brief Clears the device feature. + * @param *setupRequestPtr - Pointer to the setup request + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupDeviceRequestClearFeature(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Sets the device feature. + * @param *setupRequestPtr - Pointer to the setup request + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupDeviceRequestSetFeature(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Sets the device address. + * @param address - Address to be set + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupDeviceRequestSetAddress(uint8_t address); + +/** + * @ingroup usb_core_requests + * @brief Callback function for the address. + * @param None. + * @return None. + */ +void SetupDeviceAddressCallback(void); + +/** + * @ingroup usb_core_requests + * @brief Gets the device descriptor. + * @param *setupRequestPtr - Pointer to the setup request + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupDeviceRequestGetDescriptor(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Gets the device configuration. + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupDeviceRequestGetConfiguration(void); + +/** + * @ingroup usb_core_requests + * @brief Sets the device configuration. + * @param configurationValue - Configuration value to be set + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupDeviceRequestSetConfiguration(uint8_t configurationValue); + +#endif /* USB_CORE_REQUESTS_DEVICE_H */ + diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.c b/mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.c new file mode 100644 index 0000000..6e8ed3b --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.c @@ -0,0 +1,137 @@ +/** + * USBCOREREQUESTSENDPOINT USB Core Requests Endpoint Source File + * @file usb_core_requests_endpoint.c + * @ingroup usb_core_requests + * @brief USB Endpoint Core Requests handling. + * @version USB Device Core Version USB 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +/** + * @ingroup usb_core_requests + * @def GET_STATUS_ENDPOINT_STALLED + * @brief Mask for the endpoint stall status in the first byte of the data stage of the setup request. + */ +#define GET_STATUS_ENDPOINT_STALLED (1u << 0u) + +/** + * @ingroup usb_core_requests + * @def ENDPOINT_ADDRESS_MASK + * @brief Mask for the endpoint address in the wIndex field of the setup request. + */ +#define ENDPOINT_ADDRESS_MASK (0x7fu) + +/** + * @ingroup usb_core_requests + * @def ENDPOINT_DIRECTION_BITPOSITION + * @brief Bit position for the endpoint direction in the wIndex field of the setup request. + */ +#define ENDPOINT_DIRECTION_BITPOSITION (7u) + +USB_PIPE_t EndpointFromRequestGet(uint16_t wIndex) +{ + USB_PIPE_t endpoint; + endpoint.address = (uint8_t)wIndex & ENDPOINT_ADDRESS_MASK; + endpoint.direction = (uint8_t)wIndex >> ENDPOINT_DIRECTION_BITPOSITION; + + return endpoint; +} + +RETURN_CODE_t SetupEndpointRequestGetStatus(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + // Return IN transaction with ENDPOINT_HALT status (2 bytes) + USB_PIPE_t endpoint = EndpointFromRequestGet(setupRequestPtr->wIndex); + + uint8_t data[] = {0, 0}; + if (USB_EndpointIsStalled(endpoint) == true) + { + + data[0] |= GET_STATUS_ENDPOINT_STALLED; + } + + return USB_ControlTransferDataWriteBuffer(data, sizeof (data)); +} + +RETURN_CODE_t SetupEndpointRequestClearFeature(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Only ENDPOINT_HALT available for endpoints + if (setupRequestPtr->wValue == USB_ENDPOINT_FEATURE_HALT) + { + USB_PIPE_t endpoint = EndpointFromRequestGet(setupRequestPtr->wIndex); + + status = USB_EndpointStallClear(endpoint); + } + else + { + status = UNSUPPORTED; + } + + return status; +} + +RETURN_CODE_t SetupEndpointRequestSetFeature(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Only ENDPOINT_HALT available for endpoints + if (setupRequestPtr->wValue == USB_ENDPOINT_FEATURE_HALT) + { + USB_PIPE_t endpoint = EndpointFromRequestGet(setupRequestPtr->wIndex); + + status = USB_TransferAbort(endpoint); + if (SUCCESS == status) + { + status = USB_EndpointStall(endpoint); + } + } + else + { + status = UNSUPPORTED; + } + + return status; +} + +RETURN_CODE_t SetupEndpointRequestSynchFrame(void) +{ + uint16_t framenum = USB_FrameNumberGet(); + + return USB_ControlTransferDataWriteBuffer((uint8_t*) & framenum, sizeof (framenum)); +} diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.h b/mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.h new file mode 100644 index 0000000..878b939 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests_endpoint.h @@ -0,0 +1,84 @@ +/** + * USBCOREREQUESTSENDPOINT USB Core Requests Endpoint Header File + * @file usb_core_requests_endpoint.h + * @ingroup usb_core_requests + * @brief USB Endpoint Core Requests handling. + * @version USB Device Core Version USB 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_CORE_REQUESTS_ENDPOINT_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_REQUESTS_ENDPOINT_H + +#include +#include + +#include "usb_protocol_headers.h" +#include "usb_common_elements.h" + +/** + * @ingroup usb_core_requests + * @brief Gets the endpoint status. + * @param wIndex - Endpoint address and direction + * @return A structure with the endpoint status + */ +USB_PIPE_t EndpointFromRequestGet(uint16_t wIndex); + +/** + * @ingroup usb_core_requests + * @brief Gets the endpoint status. + * @param *setupRequestPtr - Pointer to the setup request + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupEndpointRequestGetStatus(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Clears the endpoint feature. + * @param *setupRequestPtr - Pointer to the setup request + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupEndpointRequestClearFeature(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Sets the endpoint feature. + * @param *setupRequestPtr - Pointer to the setup request + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupEndpointRequestSetFeature(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Gets the current frame number. + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t SetupEndpointRequestSynchFrame(void); + +#endif /* USB_CORE_REQUESTS_ENDPOINT_H */ + diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests_interface.c b/mcc_generated_files/usb/usb_common/usb_core_requests_interface.c new file mode 100644 index 0000000..8725b20 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests_interface.c @@ -0,0 +1,145 @@ +/** + * USBCOREREQUESTSINTERFACE USB Core Requests Interface Source File + * @file usb_core_requests_interface.c + * @ingroup usb_core_requests + * @brief USB Interface Core Requests handling. + * @version USB Device Core 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +/** + * @ingroup usb_core_requests + * @def GET_INTERFACE_REQUEST_NUMBER_MASK + * @brief Mask for the interface number in the wIndex field of the setup request. + */ +#define GET_INTERFACE_REQUEST_NUMBER_MASK (0xffu) + +/** + * @ingroup usb_core_requests + * @def GET_INTERFACE_REQUEST_WVALUE + * @brief Value for the wValue field of the setup request. + */ +#define GET_INTERFACE_REQUEST_WVALUE 0u + +/** + * @ingroup usb_core_requests + * @def GET_INTERFACE_RESPONSE_SIZE + * @brief Size of the response to the Get Interface request. + */ +#define GET_INTERFACE_RESPONSE_SIZE 1u + +RETURN_CODE_t USB_SetupInterfaceRequestGetStatus(void) +{ + uint8_t data[] = {0, 0}; + + return USB_ControlTransferDataWriteBuffer(data, sizeof (data)); +} + +RETURN_CODE_t USB_SetupInterfaceRequestGetInterface(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if ((GET_INTERFACE_REQUEST_WVALUE == setupRequestPtr->wValue) && (GET_INTERFACE_RESPONSE_SIZE == setupRequestPtr->wLength)) + { + uint8_t alternateSetting; + status = ActiveAlternateSettingGet((GET_INTERFACE_REQUEST_NUMBER_MASK & setupRequestPtr->wIndex), &alternateSetting); + if (SUCCESS == status) + { + status = USB_ControlTransferDataWriteBuffer(&alternateSetting, sizeof(alternateSetting)); + } + } + else + { + status = INTERFACE_GET_ERROR; + } + + return status; +} + +RETURN_CODE_t USB_SetupInterfaceRequestSetInterface(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = USB_DescriptorInterfaceConfigure(setupRequestPtr->wIndex, setupRequestPtr->wValue, true); + if (SUCCESS == status) + { + if (NULL != event.SetInterface) + { + event.SetInterface(setupRequestPtr); + } + } + + return status; +} + +RETURN_CODE_t USB_SetupInterfaceRequestGetDescriptor(USB_SETUP_REQUEST_t *setupRequestPtr) +{ + RETURN_CODE_t status = UNINITIALIZED; + + uint8_t descriptorType = (uint8_t)(setupRequestPtr->wValue >> 8u); + + if (USB_DESCRIPTOR_TYPE_VENDOR <= (USB_DESCRIPTOR_TYPE_t)descriptorType) + { + // Vendor Get_Descriptor Requests handled by VendorRequest callback + if (NULL != event.VendorRequest) + { + status = event.VendorRequest(setupRequestPtr); + } + else + { + status = UNSUPPORTED; + } + } + else if (USB_DESCRIPTOR_TYPE_CLASS <= (USB_DESCRIPTOR_TYPE_t)descriptorType) + { + // Class Get_Descriptor Requests handled by ClassRequest callback + if (NULL != event.ClassRequest) + { + status = event.ClassRequest(setupRequestPtr); + } + else + { + status = UNSUPPORTED; + } + } + else + { + // Standard descriptor types not supported on interface requests + status = UNSUPPORTED; + } + + return status; +} \ No newline at end of file diff --git a/mcc_generated_files/usb/usb_common/usb_core_requests_interface.h b/mcc_generated_files/usb/usb_common/usb_core_requests_interface.h new file mode 100644 index 0000000..fa7487b --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_requests_interface.h @@ -0,0 +1,101 @@ +/** + * USBCOREREQUESTSINTERFACE USB Core Requests Interface Header File + * @file usb_core_requests_interface.h + * @ingroup usb_core_requests + * @brief USB Interface Core Requests handling. + * @version USB Device Core 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_CORE_REQUESTS_INTERFACE_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_REQUESTS_INTERFACE_H + +#include +#include + +#include "usb_protocol_headers.h" +#include "usb_common_elements.h" + +/** + * @ingroup usb_core_requests + * @brief Returns status for the specified interface. + * + * Get status from interface request according to USB 2.0 specification Ch. 9.4.5. + * | bRequest | wValue | wIndex | wLength | Data | + * |------------|--------|-------------|---------|-----------| + * | GET_STATUS | Zero | Interface | Two | Interface | + * + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupInterfaceRequestGetStatus(void); + +/** + * @ingroup usb_core_requests + * @brief Returns the alternate setting for the specified interface. + * + * Format for GET_INTERFACE request according to USB 2.0 specification Ch 9.4.4. + * Document: Universal Serial Bus Specification for USB 2.0. + * | bRequest | wValue | wIndex | wLength | Data | + * |---------------|--------|-----------|---------|-------------------| + * | GET_INTERFACE | Zero | Interface | One | Alternate setting | + * + * @param *setupRequestPtr - Pointer to the request and its data + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupInterfaceRequestGetInterface(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Setup function for the interface request to select the alternate setting. + * + * A request to set interface according to USB 2.0 specification Ch. 9.4.10. + * Document: Universal Serial Bus Specification for USB 2.0 + * | bRequest | wValue | wIndex | wLength | Data | + * |---------------|-------------------|-----------|---------|------| + * | SET_INTERFACE | Alternate setting | Interface | Zero | None | + * + * @param *setupRequestPtr - Pointer to the request and its data + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupInterfaceRequestSetInterface(USB_SETUP_REQUEST_t *setupRequestPtr); + +/** + * @ingroup usb_core_requests + * @brief Setup function for the interface request for class-specific descriptors. + * + * | bRequest | wValue | wIndex | wLength | Data | + * |----------------|-------------------|-----------|---------|------------| + * | GET_DESCRIPTOR | Type and index | Zero | Length | Descriptor | + * + * @param *setupRequestPtr - Pointer to the request and its data + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_SetupInterfaceRequestGetDescriptor(USB_SETUP_REQUEST_t *setupRequestPtr); + +#endif /* USB_CORE_REQUESTS_INTERFACE_H */ + diff --git a/mcc_generated_files/usb/usb_common/usb_core_transfer.c b/mcc_generated_files/usb/usb_common/usb_core_transfer.c new file mode 100644 index 0000000..8d82f24 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_transfer.c @@ -0,0 +1,175 @@ +/** + * USBCORETRANSFER USB Core Transfer Source File + * @file usb_core_transfer.c + * @ingroup usb_core_transfer + * @brief USB core layer implementation file. + * @version USB Core 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +RETURN_CODE_t USB_TransferWriteStart(USB_PIPE_t pipe, uint8_t *dataPtr, uint16_t dataSize, bool useZLP, USB_TRANSFER_END_CALLBACK_t callback) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (USB_PipeStatusIsBusy(pipe) == true) + { + status = PIPE_BUSY_ERROR; + } + else + { + status = USB_PipeReset(pipe); + } + + if (status == SUCCESS) + { + USB_PipeDataPtrSet(pipe, dataPtr); + USB_PipeDataToTransferSizeSet(pipe, dataSize); + USB_PipeDataTransferredSizeReset(pipe); + if (true == useZLP) + { + USB_PipeTransferZLP_Enable(pipe); + } + USB_PipeTransferEndCallbackRegister(pipe, callback); + status = USB_InTransactionRun(pipe); + } + return status; +} + +RETURN_CODE_t USB_TransferReadStart(USB_PIPE_t pipe, uint8_t *dataPtr, uint16_t dataSize, bool useZLP, USB_TRANSFER_END_CALLBACK_t callback) +{ + RETURN_CODE_t status = UNINITIALIZED; + + if (USB_PipeStatusIsBusy(pipe) == true) + { + status = PIPE_BUSY_ERROR; + } + else + { + status = USB_PipeReset(pipe); + } + + if (status == SUCCESS) + { + USB_PipeDataPtrSet(pipe, dataPtr); + USB_PipeDataToTransferSizeSet(pipe, dataSize); + USB_PipeDataTransferredSizeReset(pipe); + if (true == useZLP) + { + USB_PipeTransferZLP_Enable(pipe); + } + USB_PipeTransferEndCallbackRegister(pipe, callback); + status = USB_OutTransactionRun(pipe); + } + + return status; +} + +RETURN_CODE_t USB_TransferControlDataSet(uint8_t *dataPtr, uint16_t dataSize, USB_SETUP_ENDOFREQUEST_CALLBACK_t callback) +{ + USB_ControlEndOfRequestCallbackRegister(callback); + return USB_ControlTransferDataSet(dataPtr, dataSize); +} + +RETURN_CODE_t USB_TransferAbort(USB_PIPE_t pipe) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // Checks if abort is needed. + if (USB_PipeStatusIsBusy(pipe) == true) + { + //Stops transfer. + status = USB_TransactionAbort(pipe); + + if (status == SUCCESS) + { + // Calls callback. + USB_PipeTransferEndCallback(pipe); + } + } + else + { + // Pipe is not busy, so no need to abort. + status = SUCCESS; + } + + return status; +} + +RETURN_CODE_t USB_TransferHandler(void) +{ + RETURN_CODE_t status = UNINITIALIZED; + + // If it's the initial setup packet, handle that separately. + if (USB_SetupIsReceived() == true) + { + status = USB_ControlSetupReceived(); + } + // If a transaction is complete, handle that one. + else if (USB_TransactionIsCompleted() == true) + { + // Finds out which pipe has a completed transaction. + USB_PIPE_t pipe; + + status = USB_TransactionCompletedPipeGet(&pipe); + + if (status == SUCCESS) + { + // Acks the transaction. + status = USB_TransactionCompleteAck(pipe); + } + + if (status == SUCCESS) + { + // Handles control transactions separately. + if (pipe.address == 0U) + { + status = USB_ControlTransactionComplete(pipe); + } + else + { + // Regular handling of all regular endpoints. + status = USB_PipeTransactionComplete(pipe); + } + } + } + else + { + // No handling needed, return SUCCESS. + status = SUCCESS; + } + + return status; +} diff --git a/mcc_generated_files/usb/usb_common/usb_core_transfer.h b/mcc_generated_files/usb/usb_common/usb_core_transfer.h new file mode 100644 index 0000000..afa85f3 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_core_transfer.h @@ -0,0 +1,116 @@ +/** + * USBCORETRANSFER USB Core Transfer Header File + * @file usb_core_transfer.h + * @defgroup usb_core_transfer USB Core Transfer + * @ingroup usb_core + * @brief USB core layer implementation file. + * @version USB Core 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_CORE_TRANSFER_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_CORE_TRANSFER_H + +#include +#include + +#include +#include + +#include +#include +#include +#include + +/** + * @ingroup usb_core_transfer + * @brief Sets up the pipe for the write transfers. + * + * Sets up the pipe for the write transfers and checks to see if it is busy. If it is not busy, then the routine resets the pipe, setting it up for write transfer, then starts the transfer. + * + * @param pipe - A combination of endpoint address and direction + * @param *dataPtr - The pointer to the data to write + * @param dataSize - The size of the data to write + * @param useZLP - Enable zero-length package at the end of transfer + * @param callback - A combination of pipe, status and transferred bytes + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_TransferWriteStart(USB_PIPE_t pipe, uint8_t *dataPtr, uint16_t dataSize, bool useZLP, USB_TRANSFER_END_CALLBACK_t callback); + +/** + * @ingroup usb_core_transfer + * @brief Sets up the pipe for the read transfers. + * + * Sets up the pipe for the write transfers and checks to see if it is busy. If it is not busy, then the routine resets the pipe, setting it up for read transfer, then starts the transfer. + * + * @param pipe - A combination of endpoint address and direction + * @param *dataPtr - The pointer to the data to read + * @param dataSize - The size of the data to read + * @param useZLP - Enable zero-length package at the end of transfer + * @param callback - A combination of pipe, status and transferred bytes + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_TransferReadStart(USB_PIPE_t pipe, uint8_t *dataPtr, uint16_t dataSize, bool useZLP, USB_TRANSFER_END_CALLBACK_t callback); + +/** + * @ingroup usb_core_transfer + * @brief Sets up vendor or class control request data transfers. + * + * Sets up the pointer and size of the read or write trnasfer in the control data stage. + * + * @param *dataPtr - The pointer to the data to read or write + * @param dataSize - The size of the data to read or write + * @param callback - Pointer to a function to be called at the end of the control request + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_TransferControlDataSet(uint8_t *dataPtr, uint16_t dataSize, USB_SETUP_ENDOFREQUEST_CALLBACK_t callback); + +/** + * @ingroup usb_core_transfer + * @brief Aborts an ongoing transfer. + * + * Will call the pipe transferEndCallback with the abort status, if configured. + * + * @param pipe - A combination of endpoint address and direction + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_TransferAbort(USB_PIPE_t pipe); + +/** + * @ingroup usb_core_transfer + * @brief Handles the different types of packages received or transferred. + * + * Checks if a setup package is received or if a transaction is completed and which pipe has a completed transaction, then it handles them accordingly. + * Sends an ACK upon completed transaction confirmation. + * + * @param None. + * @return SUCCESS or an Error code according to RETURN_CODE_t + */ +RETURN_CODE_t USB_TransferHandler(void); + +#endif /* USB_CORE_TRANSFER_H */ + diff --git a/mcc_generated_files/usb/usb_common/usb_protocol_headers.h b/mcc_generated_files/usb/usb_common/usb_protocol_headers.h new file mode 100644 index 0000000..f6b6145 --- /dev/null +++ b/mcc_generated_files/usb/usb_common/usb_protocol_headers.h @@ -0,0 +1,660 @@ +/** + * USBPROTOCOLHEADERS USB Protocol Headers Header File + * @file usb_protocol_headers.h + * @defgroup usb_protocol USB Protocol + * @ingroup usb_core + * @brief Common data structures, enumerations and macro definitons based on the USB 2.0 base protocol. + * @version USB Protocol 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_PROTOCOL_HEADERS_H +// cppcheck-suppress misra-c2012-2.5 +#define USB_PROTOCOL_HEADERS_H + +#include "usb_common_elements.h" +#include +#include +#include + +/* + * ENDPOINT LAYER + */ + +/** + * @ingroup usb_protocol + * @def USB_EP_DIR_IN + * @brief Endpoint direction IN. + */ +#define USB_EP_DIR_IN 0x01u + +/** + * @ingroup usb_protocol + * @def USB_EP_DIR_OUT + * @brief Endpoint direction OUT. + */ +#define USB_EP_DIR_OUT 0x00u + +/** + * @ingroup usb_protocol + * @def OVERFLOW_EVENT + * @brief Overflow event for the endpoint. + */ +#define OVERFLOW_EVENT 1u + +/** + * @ingroup usb_protocol + * @def UNDERFLOW_EVENT + * @brief Underflow event for the endpoint. + */ +#define UNDERFLOW_EVENT 2u + +/** + * @ingroup usb_protocol + * @enum USB_ENDPOINT_enum + * @brief Defines labels for the different endpoint types as per the USB 2.0 base specification. + */ +typedef enum USB_ENDPOINT_enum +{ + CONTROL = 0, /**