X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fcpu%2Fcortex-m3%2Fdrv%2Fusb_stm32.c;h=794aa8916f2e050461d5858b2972ca36964170e0;hb=039caa9fe6ff6a0a0aa1b0bc7644e41e1a1c0689;hp=154f0a04743a1b8f9b874a633031162730cfa93d;hpb=93308de29d9092161217cfff1b1375b482937214;p=bertos.git diff --git a/bertos/cpu/cortex-m3/drv/usb_stm32.c b/bertos/cpu/cortex-m3/drv/usb_stm32.c index 154f0a04..794aa891 100644 --- a/bertos/cpu/cortex-m3/drv/usb_stm32.c +++ b/bertos/cpu/cortex-m3/drv/usb_stm32.c @@ -54,13 +54,13 @@ #include #include +#include + #include /* memcpy() */ #include "usb_stm32.h" -#define ALIGN_UP(value, align) (((value) & ((align) - 1)) ? \ - (((value) + ((align) - 1)) & ~((align) - 1)) : \ - (value)) +/* XXX: consider to move this to cfg/macros.h */ /* XXX: redefine this to make it usable within C expression */ #define _MIN(a,b) (((a) < (b)) ? (a) : (b)) @@ -88,7 +88,8 @@ struct stm32_usb static struct stm32_usb *usb = (struct stm32_usb *)USB_BASE_ADDR; /* Endpoint descriptors: used for handling requests to use with endpoints */ -static stm32_UsbEp ep_cnfg[ENP_MAX_NUMB]; +static stm32_UsbEp ep_cnfg[EP_MAX_NUM]; +STATIC_ASSERT(EP_MAX_NUM <= EP_MAX_HW_NUM); /* USB EP0 control descriptor */ static const UsbEndpointDesc USB_CtrlEpDescr0 = @@ -116,7 +117,7 @@ static const UsbEndpointDesc USB_CtrlEpDescr1 = static UsbCtrlRequest setup_packet; /* USB device controller: max supported interfaces */ -#define USB_MAX_INTERFACE 1 +#define USB_MAX_INTERFACE CONFIG_USB_INTERFACE_MAX /* USB device controller features */ #define STM32_UDC_FEATURE_SELFPOWERED BV(0) @@ -144,8 +145,20 @@ static UsbDevice *usb_dev; static stm32_UsbMemSlot *mem_use; /* USB packet memory management: memory buffer metadata */ -#define EP_MAX_SLOTS 16 -static stm32_UsbMemSlot memory_buffer[EP_MAX_SLOTS]; +static stm32_UsbMemSlot memory_buffer[EP_MAX_NUM]; + +/* Endpoint TX and RX buffers */ +/// \cond +/* XXX: use the empty cond section to silent a buggy doxygen warning */ +static size_t rx_size, tx_size; + +#define EP_BUFFER_SIZE _MIN(CONFIG_USB_BUFSIZE, USB_XFER_MAX_SIZE) +STATIC_ASSERT(!(EP_BUFFER_SIZE & 0x03)); + +static uint8_t ep_buffer[EP_MAX_NUM][EP_BUFFER_SIZE] ALIGNED(4); +/// \endcond + +static Event usb_event_done[EP_MAX_SLOTS]; /* Allocate a free block of the packet memory */ static stm32_UsbMemSlot *usb_malloc(void) @@ -198,13 +211,13 @@ static bool usb_alloc_buffer(uint16_t *pOffset, uint32_t *size, mem = mem->next; } /* Check for out-of-memory condition */ - if ((*pOffset + max_size) >= USB_BDT_OFFSET) + if (UNLIKELY((*pOffset + max_size) >= USB_BDT_OFFSET)) return false; /* * Allocate a new memory block, next to the last allocated block. */ mem_useNew = usb_malloc(); - if (mem_useNew == NULL) + if (UNLIKELY(mem_useNew == NULL)) return false; /* Insert the block to the list of allocated blocks */ if (mem_use == NULL) @@ -675,12 +688,18 @@ out: \ static stm32_UsbIoStatus __usb_ep_read(int ep, void *buffer, ssize_t size, void (*complete)(int)) { - if (UNLIKELY(ep >= ENP_MAX_NUMB)) + if (UNLIKELY((ep >= EP_MAX_NUM) || (ep & 0x01))) { + LOG_ERR("%s: invalid EP number %d\n", __func__, ep); + ASSERT(0); + return STALLED; + } + if (UNLIKELY((size_t)buffer & 0x03)) + { + LOG_ERR("%s: unaligned buffer @ %p\n", __func__, buffer); ASSERT(0); return STALLED; } - ASSERT(!(ep & 0x01)); return USB_EP_IO(ep, read, buffer, size, complete); } @@ -688,12 +707,18 @@ __usb_ep_read(int ep, void *buffer, ssize_t size, void (*complete)(int)) static stm32_UsbIoStatus __usb_ep_write(int ep, const void *buffer, ssize_t size, void (*complete)(int)) { - if (UNLIKELY(ep >= ENP_MAX_NUMB)) + if (UNLIKELY((ep >= EP_MAX_NUM) || !(ep & 0x01))) { + LOG_ERR("%s: invalid EP number %d\n", __func__, ep); + ASSERT(0); + return STALLED; + } + if (UNLIKELY((size_t)buffer & 0x03)) + { + LOG_ERR("%s: unaligned buffer @ %p\n", __func__, buffer); ASSERT(0); return STALLED; } - ASSERT(ep & 0x01); return USB_EP_IO(ep, write, buffer, size, complete); } @@ -789,22 +814,27 @@ static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable) switch (ep_hw->type) { case USB_ENDPOINT_XFER_CONTROL: - LOG_INFO("EP%d: CONTROL IN\n", EP >> 1); + LOG_INFO("EP%d: CONTROL %s\n", EP >> 1, + EP & 1 ? "IN" : "OUT"); ep_ctrl_set_ep_type(hw, EP_CTRL); ep_ctrl_set_ep_kind(hw, 0); break; case USB_ENDPOINT_XFER_INT: - LOG_INFO("EP%d: INTERRUPT IN\n", EP >> 1); + LOG_INFO("EP%d: INTERRUPT %s\n", EP >> 1, + EP & 1 ? "IN" : "OUT"); ep_ctrl_set_ep_type(hw, EP_INTERRUPT); ep_ctrl_set_ep_kind(hw, 0); break; case USB_ENDPOINT_XFER_BULK: - LOG_INFO("EP%d: BULK IN\n", EP >> 1); + LOG_INFO("EP%d: BULK %s\n", EP >> 1, + EP & 1 ? "IN" : "OUT"); ep_ctrl_set_ep_type(hw, EP_BULK); ep_ctrl_set_ep_kind(hw, 0); break; case USB_ENDPOINT_XFER_ISOC: - LOG_ERR("EP%d: ISOCHRONOUS IN: not supported\n", EP >> 1); + LOG_ERR("EP%d: ISOCHRONOUS %s: not supported\n", + EP >> 1, + EP & 1 ? "IN" : "OUT"); /* Fallback to default */ default: ASSERT(0); @@ -1080,34 +1110,23 @@ static void usb_status_handler(UNUSED_ARG(int, EP)) } } -static bool rx_done; -static size_t rx_size; -static uint8_t rx_buffer[_MIN(CONFIG_USB_RXBUFSIZE, USB_RX_MAX_SIZE)] - __attribute__ ((__aligned__(4))); - static void usb_endpointRead_complete(int ep) { - if (UNLIKELY(ep >= ENP_MAX_NUMB)) + if (UNLIKELY(ep >= EP_MAX_NUM)) { ASSERT(0); return; } ASSERT(!(ep & 0x01)); - rx_done = true; + event_do(&usb_event_done[ep >> 1]); rx_size = ep_cnfg[ep].size; } ssize_t usb_endpointRead(int ep, void *buffer, ssize_t size) { int ep_num = usb_ep_logical_to_hw(ep); - ssize_t max_size = sizeof(rx_buffer); - - if (UNLIKELY((size_t)buffer & 0x03)) - { - LOG_ERR("unaligned buffer @ %p\n", buffer); - ASSERT(0); - } + ssize_t max_size = sizeof(ep_buffer[ep_num]); /* Non-blocking read for EP0 */ if (ep_num == CTRL_ENP_OUT) @@ -1115,7 +1134,7 @@ ssize_t usb_endpointRead(int ep, void *buffer, ssize_t size) size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength)); if (UNLIKELY(size > max_size)) { - LOG_ERR("%s: rx_buffer exceeded, try to enlarge CONFIG_USB_RXBUFSIZE\n", + LOG_ERR("%s: ep_buffer exceeded, try to enlarge CONFIG_USB_BUFSIZE\n", __func__); ASSERT(0); return -USB_BUF_OVERFLOW; @@ -1124,55 +1143,44 @@ ssize_t usb_endpointRead(int ep, void *buffer, ssize_t size) usb_status_handler(ep_num); else { - __usb_ep_read(ep_num, rx_buffer, size, + __usb_ep_read(ep_num, ep_buffer[ep_num], size, usb_status_handler); - memcpy(buffer, rx_buffer, size); + memcpy(buffer, ep_buffer[ep_num], size); } return size; } if (UNLIKELY(!size)) return 0; size = MIN(size, max_size); - rx_done = false; + event_initGeneric(&usb_event_done[ep_num >> 1]); rx_size = 0; /* Blocking read */ - __usb_ep_read(ep_num, rx_buffer, size, usb_endpointRead_complete); - while (!rx_done) - cpu_relax(); - memcpy(buffer, rx_buffer, rx_size); + __usb_ep_read(ep_num, ep_buffer[ep_num], size, + usb_endpointRead_complete); + event_wait(&usb_event_done[ep_num >> 1]); + memcpy(buffer, ep_buffer[ep_num], rx_size); return rx_size; } -static bool tx_done; -static size_t tx_size; -static uint8_t tx_buffer[_MIN(CONFIG_USB_TXBUFSIZE, USB_TX_MAX_SIZE)] - __attribute__ ((__aligned__(4))); - static void usb_endpointWrite_complete(int ep) { - if (UNLIKELY(ep >= ENP_MAX_NUMB)) + if (UNLIKELY(ep >= EP_MAX_NUM)) { ASSERT(0); return; } ASSERT(ep & 0x01); - tx_done = true; + event_do(&usb_event_done[ep >> 1]); tx_size = ep_cnfg[ep].size; } ssize_t usb_endpointWrite(int ep, const void *buffer, ssize_t size) { int ep_num = usb_ep_logical_to_hw(ep); - ssize_t max_size = sizeof(tx_buffer); - - if (UNLIKELY((size_t)buffer & 0x03)) - { - LOG_ERR("unaligned buffer @ %p\n", buffer); - ASSERT(0); - } + ssize_t max_size = sizeof(ep_buffer[ep_num]); /* Non-blocking write for EP0 */ if (ep_num == CTRL_ENP_IN) @@ -1180,7 +1188,7 @@ ssize_t usb_endpointWrite(int ep, const void *buffer, ssize_t size) size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength)); if (UNLIKELY(size > max_size)) { - LOG_ERR("%s: tx_buffer exceeded, try to enlarge CONFIG_USB_TXBUFSIZE\n", + LOG_ERR("%s: ep_buffer exceeded, try to enlarge CONFIG_USB_BUFSIZE\n", __func__); ASSERT(0); return -USB_BUF_OVERFLOW; @@ -1189,8 +1197,8 @@ ssize_t usb_endpointWrite(int ep, const void *buffer, ssize_t size) usb_status_handler(ep_num); else { - memcpy(tx_buffer, buffer, size); - __usb_ep_write(ep_num, tx_buffer, size, + memcpy(ep_buffer[ep_num], buffer, size); + __usb_ep_write(ep_num, ep_buffer[ep_num], size, usb_status_handler); } return size; @@ -1198,14 +1206,14 @@ ssize_t usb_endpointWrite(int ep, const void *buffer, ssize_t size) if (UNLIKELY(!size)) return 0; size = MIN(size, max_size); - tx_done = false; + event_initGeneric(&usb_event_done[ep_num >> 1]); tx_size = 0; /* Blocking write */ - memcpy(tx_buffer, buffer, size); - __usb_ep_write(ep_num, tx_buffer, size, usb_endpointWrite_complete); - while (!tx_done) - cpu_relax(); + memcpy(ep_buffer[ep_num], buffer, size); + __usb_ep_write(ep_num, ep_buffer[ep_num], size, + usb_endpointWrite_complete); + event_wait(&usb_event_done[ep_num >> 1]); return tx_size; } @@ -1487,11 +1495,8 @@ static void usb_get_descriptor_handler(void) if ((setup_packet.mRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) usb_get_descriptor(); - /* Getting descriptor for a device is a standard request */ - else if ((setup_packet.mRequestType & USB_DIR_MASK) == USB_DIR_IN) - usb_event_handler(usb_dev); else - ep_cnfg[CTRL_ENP_OUT].status = STALLED; + usb_event_handler(usb_dev); } /* USB setup packet: SET_ADDRESS handler */ @@ -1577,6 +1582,7 @@ static int usb_set_config_state(uint32_t conf) udc.alt[i] = 0; usb_set_device_state(USB_STATE_CONFIGURED); usb_dev->configured = true; + event_do(&usb_event_done[0]); LOG_INFO("%s: device configured\n", __func__); } else @@ -1913,8 +1919,11 @@ int usb_deviceRegister(UsbDevice *dev) MOD_CHECK(proc); #endif usb_dev = dev; + usb_dev->configured = false; + + event_initGeneric(&usb_event_done[0]); usb_init(); - while (!usb_dev->configured) - cpu_relax(); + event_wait(&usb_event_done[0]); + return 0; }