From d5bfe1a35cc5b046782ddc4222f3344d9127f76e Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 24 Nov 2024 15:09:35 +0100 Subject: [PATCH] fix: usb should not rewrite DIEPCTL bits --- include/usb.h | 9 +++++++++ src/usb.c | 19 +++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/usb.h b/include/usb.h index 290cc5df069158e51379c06510693f3d35a4a5f4..b3b9791d4307fd697dd4b393e15705cdd7492015 100644 --- a/include/usb.h +++ b/include/usb.h @@ -420,4 +420,13 @@ task_result_t usb_send_unicode_string_descriptor( uint16_t max_size, volatile uint32_t *fifo_tx_target); +task_result_t usb_send_unicode_string_descriptor( + USB_OTG_INEndpointTypeDef *endpoint, + usb_unicode_string_descriptor_t *string_descriptor, + uint16_t max_size, + volatile uint32_t *fifo_tx_target); + +void usb_flush_tx_fifo(USB_OTG_GlobalTypeDef* core, uint16_t fifo_num); +void usb_flush_rx_fifo(USB_OTG_GlobalTypeDef* core); + #endif // USB_H diff --git a/src/usb.c b/src/usb.c index 28957ec610d3d70d030f2c7f9054b642a58d7d56..3694798be4c9ce84ebe4f47f8f760c69fe8b097e 100644 --- a/src/usb.c +++ b/src/usb.c @@ -1,5 +1,6 @@ #include "usb.h" #include "usb_device.h" +#include "registers.h" #include typedef union { @@ -110,7 +111,7 @@ task_result_t usb_generic_setup_in_endpoint(USB_OTG_INEndpointTypeDef *endpoint, } else { // just one packet, of zero length endpoint->DIEPTSIZ = (1 << USB_OTG_DIEPTSIZ_PKTCNT_Pos); } - endpoint->DIEPCTL = USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA; + endpoint->DIEPCTL |= USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA; return RES_OK; } @@ -127,7 +128,7 @@ task_result_t usb_generic_send(USB_OTG_INEndpointTypeDef *endpoint, usb_generic_fill_fifo(endpoint, data, size, fifo_tx_target); // After the fifo is filled... - endpoint->DIEPCTL = USB_OTG_DIEPCTL_CNAK; + endpoint->DIEPCTL |= USB_OTG_DIEPCTL_CNAK; return RES_OK; } @@ -226,7 +227,7 @@ task_result_t usb_send_string_descriptor_zero(USB_OTG_INEndpointTypeDef *endpoin usb_generic_fill_fifo(endpoint, (uint8_t*)&string_descriptor->wLANGID[1], string_descriptor->header.bLength - 4, fifo_tx_target); } - endpoint->DIEPCTL = USB_OTG_DIEPCTL_CNAK; + endpoint->DIEPCTL |= USB_OTG_DIEPCTL_CNAK; return RES_OK; } @@ -259,7 +260,7 @@ task_result_t usb_send_unicode_string_descriptor( usb_generic_fill_fifo(endpoint, &string_descriptor->bString[2], string_descriptor->header.bLength - 4, fifo_tx_target); } - endpoint->DIEPCTL = USB_OTG_DIEPCTL_CNAK; + endpoint->DIEPCTL |= USB_OTG_DIEPCTL_CNAK; return RES_OK; } @@ -270,3 +271,13 @@ bool usb_is_inendpoint_ready(USB_OTG_INEndpointTypeDef *endpoint) { bool usb_check_fifo_space(USB_OTG_INEndpointTypeDef *endpoint, uint16_t size) { return ((endpoint->DTXFSTS & (USB_OTG_DTXFSTS_INEPTFSAV_Msk)) >> USB_OTG_DTXFSTS_INEPTFSAV_Pos) >= (size + 3) / 4; } + +void usb_flush_tx_fifo(USB_OTG_GlobalTypeDef *core, uint16_t fifo_num) { + reg_write_bits_pos(&core->GRSTCTL, fifo_num, USB_OTG_GRSTCTL_TXFNUM_Pos, 0xF); + reg_write_bits_pos(&core->GRSTCTL, fifo_num, USB_OTG_GRSTCTL_TXFFLSH_Pos, 1); + while (reg_read_bits_pos(&core->GRSTCTL, USB_OTG_GRSTCTL_TXFFLSH_Pos, 1)); +} +void usb_flush_rx_fifo(USB_OTG_GlobalTypeDef *core) { + reg_set_bits_pos(&core->GRSTCTL, USB_OTG_GRSTCTL_RXFFLSH_Pos, 1); + while (reg_read_bits_pos(&core->GRSTCTL, USB_OTG_GRSTCTL_TXFFLSH_Pos, 1)); +}