X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fcpu%2Fcortex-m3%2Fdrv%2Fusb_stm32.c;h=ac3f55fa40bc2af8ffb05a10130a43adf464647e;hb=80ac3ae104b48f42eb8911de0879a477cf812a4c;hp=a7f9d8c809461fc6ac6b1eb5e67f5e1d522943c6;hpb=f3a757550ae5b4f5da067eb36d9f59f3c5f21a55;p=bertos.git diff --git a/bertos/cpu/cortex-m3/drv/usb_stm32.c b/bertos/cpu/cortex-m3/drv/usb_stm32.c index a7f9d8c8..ac3f55fa 100644 --- a/bertos/cpu/cortex-m3/drv/usb_stm32.c +++ b/bertos/cpu/cortex-m3/drv/usb_stm32.c @@ -54,13 +54,17 @@ #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)) + /* STM32 USB registers */ struct stm32_usb { @@ -84,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_usb_ep_t 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 = @@ -112,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) @@ -137,14 +142,29 @@ static stm32_udc_t udc; static UsbDevice *usb_dev; /* USB packet memory management: list of allocated chunks */ -static pack_mem_slot_t *mem_use; +static stm32_UsbMemSlot *mem_use; /* USB packet memory management: memory buffer metadata */ -#define EP_MAX_SLOTS 16 -static pack_mem_slot_t 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]; + +/* Check if we're running in atomic (non-sleepable) context or not */ +static volatile bool in_atomic = false; /* Allocate a free block of the packet memory */ -static pack_mem_slot_t *usb_malloc(void) +static stm32_UsbMemSlot *usb_malloc(void) { unsigned int i; @@ -155,7 +175,7 @@ static pack_mem_slot_t *usb_malloc(void) } /* Release a block of the packet memory */ -static void usb_free(pack_mem_slot_t *pPntr) +static void usb_free(stm32_UsbMemSlot *pPntr) { pPntr->Size = 0; } @@ -164,7 +184,7 @@ static void usb_free(pack_mem_slot_t *pPntr) static bool usb_alloc_buffer(uint16_t *pOffset, uint32_t *size, int EndPoint) { - pack_mem_slot_t *mem = mem_use, + stm32_UsbMemSlot *mem = mem_use, *memNext, *mem_useNew; uint32_t max_size = *size; @@ -194,13 +214,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) @@ -226,7 +246,7 @@ static bool usb_alloc_buffer(uint16_t *pOffset, uint32_t *size, /* Release a chunk of the packet memory (inside a block) */ static void usb_free_buffer(int EndPoint) { - pack_mem_slot_t *mem, *memPrev = NULL; + stm32_UsbMemSlot *mem, *memPrev = NULL; mem = mem_use; while (mem != NULL) @@ -293,7 +313,7 @@ static int usb_ep_logical_to_hw(uint8_t ep_addr) } /* Set EP address */ -static void EpCtrlSet_EA(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_ea(reg32_t *reg, uint32_t val) { val &= 0x0f; val |= *reg & 0x0700; @@ -302,13 +322,13 @@ static void EpCtrlSet_EA(reg32_t *reg, uint32_t val) } /* Get EP IN status */ -static uint32_t EpCtrlGet_STAT_TX(reg32_t *reg) +static uint32_t ep_ctrl_get_stat_tx(reg32_t *reg) { return (*reg & (0x3UL << 4)) >> 4; } /* Set EP IN state */ -static void EpCtrlSet_STAT_TX(reg32_t *reg, ep_state_t val) +static void ep_ctrl_set_stat_tx(reg32_t *reg, stm32_UsbEpState val) { uint32_t state; int i; @@ -319,7 +339,7 @@ static void EpCtrlSet_STAT_TX(reg32_t *reg, ep_state_t val) */ for (i = 0; i < 2; i++) { - if (EpCtrlGet_STAT_TX(reg) == val) + if (ep_ctrl_get_stat_tx(reg) == val) return; state = val; state <<= 4; @@ -332,7 +352,7 @@ static void EpCtrlSet_STAT_TX(reg32_t *reg, ep_state_t val) } /* Set EP DTOG_TX bit (IN) */ -static void EpCtrlSet_DTOG_TX(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_dtog_tx(reg32_t *reg, uint32_t val) { val = val ? (*reg ^ (1UL << 6)) : *reg; /* Clear the toggle bits without DTOG_TX (6) */ @@ -342,7 +362,7 @@ static void EpCtrlSet_DTOG_TX(reg32_t *reg, uint32_t val) } /* Clear EP CTR_TX bit (IN) */ -static void EpCtrlClr_CTR_TX(reg32_t *reg) +static void ep_ctrl_clr_ctr_tx(reg32_t *reg) { uint32_t val = *reg; @@ -353,7 +373,7 @@ static void EpCtrlClr_CTR_TX(reg32_t *reg) } /* Clear EP CTR_RX bit (OUT) */ -static void EpCtrlClr_CTR_RX(reg32_t *reg) +static void ep_ctrl_clr_ctr_rx(reg32_t *reg) { uint32_t val = *reg; val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 15); @@ -363,7 +383,7 @@ static void EpCtrlClr_CTR_RX(reg32_t *reg) } /* Set EP KIND bit */ -static void EpCtrlSet_EP_KIND(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_ep_kind(reg32_t *reg, uint32_t val) { val = val ? (1UL << 8) : 0; val |= *reg & ~(USB_CTRL_TOGGLE_MASK | (1UL << 8)); @@ -372,7 +392,7 @@ static void EpCtrlSet_EP_KIND(reg32_t *reg, uint32_t val) } /* Set EP type */ -static int EpCtrlSet_EP_TYPE(reg32_t *reg, uint8_t val) +static int ep_ctrl_set_ep_type(reg32_t *reg, uint8_t val) { uint32_t type; @@ -391,14 +411,14 @@ static int EpCtrlSet_EP_TYPE(reg32_t *reg, uint8_t val) } /* Get EP STAT_RX (OUT) */ -static uint32_t EpCtrlGet_STAT_RX(reg32_t *reg) +static uint32_t ep_ctrl_get_stat_rx(reg32_t *reg) { uint32_t val = *reg & (0x3UL << 12); return val >> 12; } /* Set EP STAT_RX (OUT) */ -static void EpCtrlSet_STAT_RX(reg32_t *reg, ep_state_t val) +static void ep_ctrl_set_stat_rx(reg32_t *reg, stm32_UsbEpState val) { uint32_t state; int i; @@ -409,7 +429,7 @@ static void EpCtrlSet_STAT_RX(reg32_t *reg, ep_state_t val) */ for (i = 0; i < 2; i++) { - if (EpCtrlGet_STAT_RX(reg) == val) + if (ep_ctrl_get_stat_rx(reg) == val) return; state = val; state <<= 12; @@ -422,7 +442,7 @@ static void EpCtrlSet_STAT_RX(reg32_t *reg, ep_state_t val) } /* Set DTOG_RX bit */ -static void EpCtrlSet_DTOG_RX(reg32_t *reg, uint32_t val) +static void ep_ctrl_set_dtog_rx(reg32_t *reg, uint32_t val) { val = val ? (*reg ^ (1UL << 14)) : *reg; /* Clear the toggle bits without DTOG_RX (14) */ @@ -432,7 +452,7 @@ static void EpCtrlSet_DTOG_RX(reg32_t *reg, uint32_t val) } /* Get EP SETUP bit */ -static uint32_t EpCtrlGet_SETUP(reg32_t *reg) +static uint32_t ep_ctrl_get_setup(reg32_t *reg) { uint32_t val = *reg & (1UL << 11); return val ? 1 : 0; @@ -443,8 +463,7 @@ static void __usb_ep_io(int EP) { ssize_t Count, CountHold, Offset; uint32_t *pDst, *pSrc, Data; - bool CurrentBuffer; - stm32_usb_ep_t *epd = &ep_cnfg[EP]; + stm32_UsbEp *epd = &ep_cnfg[EP]; if (UNLIKELY(epd->hw == NULL)) { @@ -480,15 +499,14 @@ static void __usb_ep_io(int EP) epd->flags |= STM32_USB_EP_ZERO_PACKET; Offset = epd->offset; epd->offset += Count; - CurrentBuffer = true; switch (epd->type) { case USB_ENDPOINT_XFER_CONTROL: case USB_ENDPOINT_XFER_INT: - pDst = (uint32_t *)addr2usbmem(ReadEpDTB_AddrTx(EP >> 1)); + pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET)); break; case USB_ENDPOINT_XFER_BULK: - pDst = (uint32_t *)addr2usbmem(ReadEpDTB_AddrTx(EP >> 1)); + pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET)); break; case USB_ENDPOINT_XFER_ISOC: LOG_ERR("%s: isochronous transfer not supported\n", @@ -511,12 +529,8 @@ static void __usb_ep_io(int EP) *pDst++ = Data; } - if (CurrentBuffer) - WriteEpDTB_CountTx(EP >> 1, CountHold); - else - WriteEpDTB_CountRx(EP >> 1, CountHold); - - EpCtrlSet_STAT_TX(epd->hw, EP_VALID); + EP_DTB_WRITE(EP >> 1, COUNT_TX_OFFSET, CountHold); + ep_ctrl_set_stat_tx(epd->hw, EP_VALID); --ep_cnfg[EP].avail_data; Count = epd->size - epd->offset; @@ -540,15 +554,15 @@ static void __usb_ep_io(int EP) case USB_ENDPOINT_XFER_CONTROL: case USB_ENDPOINT_XFER_INT: /* Get received bytes number */ - Count = ReadEpDTB_CountRx(EP >> 1) & 0x3FF; + Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF; /* Get address of the USB packet buffer for corresponding EP */ - pSrc = (uint32_t *)addr2usbmem(ReadEpDTB_AddrRx(EP >> 1)); + pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET)); break; case USB_ENDPOINT_XFER_BULK: /* Get received bytes number */ - Count = ReadEpDTB_CountRx(EP >> 1) & 0x3FF; + Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF; /* Get address of the USB packet buffer for corresponding EP */ - pSrc = (uint32_t *)addr2usbmem(ReadEpDTB_AddrRx(EP >> 1)); + pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET)); break; case USB_ENDPOINT_XFER_ISOC: LOG_ERR("%s: isochronous transfer not supported\n", @@ -589,7 +603,7 @@ static void __usb_ep_io(int EP) } } - EpCtrlSet_STAT_RX(epd->hw, EP_VALID); + ep_ctrl_set_stat_rx(epd->hw, EP_VALID); --ep_cnfg[EP].avail_data; @@ -631,7 +645,7 @@ static size_t usb_size(size_t size, size_t host_size) #define USB_EP_IO(__EP, __op, __buf, __size, __complete) \ ({ \ cpu_flags_t flags; \ - stm32_usb_io_status_t ret; \ + stm32_UsbIoStatus ret; \ \ /* Fill EP descriptor */ \ IRQ_SAVE_DISABLE(flags); \ @@ -674,48 +688,60 @@ out: \ }) /* Configure and endponint and perform a read operation */ -static stm32_usb_io_status_t +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); } /* Configure and endponint and perform a write operation */ -static stm32_usb_io_status_t +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); } static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size) { - stm32_usb_ep_t *epc = &ep_cnfg[ep]; + stm32_UsbEp *epc = &ep_cnfg[ep]; /* IN EP */ if (ep & 0x01) { /* Disable EP */ - EpCtrlSet_STAT_TX(epc->hw, EP_DISABLED); + ep_ctrl_set_stat_tx(epc->hw, EP_DISABLED); /* Clear Tx toggle */ - EpCtrlSet_DTOG_TX(epc->hw, 0); + ep_ctrl_set_dtog_tx(epc->hw, 0); /* Clear Correct Transfer for transmission flag */ - EpCtrlClr_CTR_TX(epc->hw); + ep_ctrl_clr_ctr_tx(epc->hw); /* Update EP description table */ - WriteEpDTB_AddrTx(ep >> 1, offset); - WriteEpDTB_CountTx(ep >> 1, 0); + EP_DTB_WRITE(ep >> 1, ADDR_TX_OFFSET, offset); + EP_DTB_WRITE(ep >> 1, COUNT_TX_OFFSET, 0); } /* OUT EP */ else @@ -723,19 +749,19 @@ static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size) uint16_t rx_count = 0; /* Disable EP */ - EpCtrlSet_STAT_RX(epc->hw, EP_DISABLED); + ep_ctrl_set_stat_rx(epc->hw, EP_DISABLED); /* Clear Rx toggle */ - EpCtrlSet_DTOG_RX(epc->hw, 0); + ep_ctrl_set_dtog_rx(epc->hw, 0); /* Clear Correct Transfer for reception flag */ - EpCtrlClr_CTR_RX(epc->hw); + ep_ctrl_clr_ctr_rx(epc->hw); /* Descriptor block size field */ rx_count |= (size > 62) << 15; /* Descriptor number of blocks field */ rx_count |= (((size > 62) ? (size >> 5) - 1 : size >> 1) & 0x1f) << 10; /* Update EP description table */ - WriteEpDTB_AddrRx(ep >> 1, offset); - WriteEpDTB_CountRx(ep >> 1, rx_count); + EP_DTB_WRITE(ep >> 1, ADDR_RX_OFFSET, offset); + EP_DTB_WRITE(ep >> 1, COUNT_RX_OFFSET, rx_count); } } @@ -743,7 +769,7 @@ static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size) static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable) { int EP; - stm32_usb_ep_t *ep_hw; + stm32_UsbEp *ep_hw; reg32_t *hw; uint16_t Offset; uint32_t size; @@ -779,7 +805,7 @@ static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable) hw += EP >> 1; /* Set Ep Address */ - EpCtrlSet_EA(hw, EP >> 1); + ep_ctrl_set_ea(hw, EP >> 1); ep_hw->hw = hw; LOG_INFO("%s: EP%d-%s configured\n", __func__, EP >> 1, EP & 1 ? "IN" : "OUT"); @@ -791,22 +817,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); - EpCtrlSet_EP_TYPE(hw, EP_CTRL); - EpCtrlSet_EP_KIND(hw, 0); + 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); - EpCtrlSet_EP_TYPE(hw, EP_INTERRUPT); - EpCtrlSet_EP_KIND(hw, 0); + 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); - EpCtrlSet_EP_TYPE(hw, EP_BULK); - EpCtrlSet_EP_KIND(hw, 0); + 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); @@ -815,14 +846,14 @@ static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable) if (EP & 0x01) { /* Enable EP */ - EpCtrlSet_STAT_TX(hw, EP_NAK); + ep_ctrl_set_stat_tx(hw, EP_NAK); /* Clear Correct Transfer for transmission flag */ - EpCtrlClr_CTR_TX(hw); + ep_ctrl_clr_ctr_tx(hw); } else { /* Enable EP */ - EpCtrlSet_STAT_RX(hw, EP_VALID); + ep_ctrl_set_stat_rx(hw, EP_VALID); } } else if (ep_cnfg[EP].hw) @@ -834,17 +865,17 @@ static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable) if (EP & 0x01) { /* Disable IN EP */ - EpCtrlSet_STAT_TX(hw, EP_DISABLED); + ep_ctrl_set_stat_tx(hw, EP_DISABLED); /* Clear Correct Transfer for reception flag */ - EpCtrlClr_CTR_TX(hw); + ep_ctrl_clr_ctr_tx(hw); } /* OUT EP */ else { /* Disable OUT EP */ - EpCtrlSet_STAT_RX(hw, EP_DISABLED); + ep_ctrl_set_stat_rx(hw, EP_DISABLED); /* Clear Correct Transfer for reception flag */ - EpCtrlClr_CTR_RX(hw); + ep_ctrl_clr_ctr_rx(hw); } /* Release buffer */ usb_free_buffer(EP); @@ -854,20 +885,20 @@ static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable) } /* Get EP stall/unstall */ -static int USB_GetStallEP(int EP, bool *pStall) +static int usb_ep_get_stall(int EP, bool *pStall) { if (ep_cnfg[EP].hw == NULL) return -USB_NODEV_ERROR; *pStall = (EP & 0x01) ? - (EpCtrlGet_STAT_TX(ep_cnfg[EP].hw) == EP_STALL): /* IN EP */ - (EpCtrlGet_STAT_RX(ep_cnfg[EP].hw) == EP_STALL); /* OUT EP */ + (ep_ctrl_get_stat_tx(ep_cnfg[EP].hw) == EP_STALL): /* IN EP */ + (ep_ctrl_get_stat_rx(ep_cnfg[EP].hw) == EP_STALL); /* OUT EP */ return USB_OK; } /* Set EP stall/unstall */ -static int USB_SetStallEP(int EP, bool Stall) +static int usb_ep_set_stall(int EP, bool Stall) { if (ep_cnfg[EP].hw == NULL) return -USB_NODEV_ERROR; @@ -878,13 +909,13 @@ static int USB_SetStallEP(int EP, bool Stall) if (EP & 0x01) { /* IN EP */ - EpCtrlSet_STAT_TX(ep_cnfg[EP].hw, EP_STALL); + ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_STALL); ep_cnfg[EP].avail_data = 1; } else { /* OUT EP */ - EpCtrlSet_STAT_RX(ep_cnfg[EP].hw, EP_STALL); + ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_STALL); ep_cnfg[EP].avail_data = 0; } } @@ -896,31 +927,31 @@ static int USB_SetStallEP(int EP, bool Stall) /* IN EP */ ep_cnfg[EP].avail_data = 1; /* reset Data Toggle bit */ - EpCtrlSet_DTOG_TX(ep_cnfg[EP].hw, 0); - EpCtrlSet_STAT_TX(ep_cnfg[EP].hw, EP_NAK); + ep_ctrl_set_dtog_tx(ep_cnfg[EP].hw, 0); + ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_NAK); } else { /* OUT EP */ ep_cnfg[EP].avail_data = 0; /* reset Data Toggle bit */ - EpCtrlSet_DTOG_RX(ep_cnfg[EP].hw, 0); - EpCtrlSet_STAT_RX(ep_cnfg[EP].hw, EP_VALID); + ep_ctrl_set_dtog_rx(ep_cnfg[EP].hw, 0); + ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_VALID); } } return USB_OK; } /* Stall both directions of the control EP */ -static void USB_StallCtrlEP(void) +static void usb_ep_set_stall_ctrl(void) { ep_cnfg[CTRL_ENP_IN].avail_data = 1; ep_cnfg[CTRL_ENP_IN].status = STALLED; ep_cnfg[CTRL_ENP_OUT].avail_data = 0; ep_cnfg[CTRL_ENP_OUT].status = STALLED; - USB_SetStallEP(CTRL_ENP_IN, true); - USB_SetStallEP(CTRL_ENP_OUT, true); + usb_ep_set_stall(CTRL_ENP_IN, true); + usb_ep_set_stall(CTRL_ENP_OUT, true); } /* @@ -1033,7 +1064,7 @@ static void usb_set_device_state(int state) } /* Setup packet: set address status phase end handler */ -static void USB_AddStatusEndHandler(UNUSED_ARG(int, EP)) +static void usb_add_status_handler_end(UNUSED_ARG(int, EP)) { uint16_t w_value; @@ -1051,123 +1082,141 @@ static void USB_AddStatusEndHandler(UNUSED_ARG(int, EP)) } /* Prepare status phase */ -static void USB_StatusPhase(bool in) +static void usb_status_phase(bool in) { if (in) __usb_ep_write(CTRL_ENP_IN, NULL, 0, NULL); } /* Setup packet: status phase end handler */ -static void USB_StatusEndHandler(UNUSED_ARG(int, EP)) +static void usb_status_handler_end(UNUSED_ARG(int, EP)) { __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL); __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL); } /* Address status handler */ -static void USB_StatusHandler(UNUSED_ARG(int, EP)) +static void usb_status_handler(UNUSED_ARG(int, EP)) { if (setup_packet.mRequestType & USB_DIR_IN) { - USB_StatusPhase(false); - ep_cnfg[CTRL_ENP_OUT].complete = USB_StatusEndHandler; + usb_status_phase(false); + ep_cnfg[CTRL_ENP_OUT].complete = usb_status_handler_end; } else { - USB_StatusPhase(true); + usb_status_phase(true); ep_cnfg[CTRL_ENP_IN].complete = (setup_packet.bRequest == USB_REQ_SET_ADDRESS) ? - USB_AddStatusEndHandler : - USB_StatusEndHandler; + usb_add_status_handler_end : + usb_status_handler_end; } } -static bool rx_done; -static size_t rx_size; - 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(ep_buffer[ep_num]); /* Non-blocking read for EP0 */ - if (ep_num == CTRL_ENP_OUT) + if (in_atomic && (ep_num == CTRL_ENP_OUT)) { size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength)); + if (UNLIKELY(size > max_size)) + { + LOG_ERR("%s: ep_buffer exceeded, try to enlarge CONFIG_USB_BUFSIZE\n", + __func__); + ASSERT(0); + return -USB_BUF_OVERFLOW; + } if (!size) - USB_StatusHandler(ep_num); + usb_status_handler(ep_num); else - __usb_ep_read(ep_num, buffer, size, - USB_StatusHandler); + { + __usb_ep_read(ep_num, ep_buffer[ep_num], size, + usb_status_handler); + memcpy(buffer, ep_buffer[ep_num], size); + } return size; } if (UNLIKELY(!size)) return 0; - size = MIN(size, USB_RX_MAX_SIZE); - rx_done = false; + size = MIN(size, max_size); + event_initGeneric(&usb_event_done[ep_num >> 1]); rx_size = 0; /* Blocking read */ - __usb_ep_read(ep_num, buffer, size, usb_endpointRead_complete); - while (!rx_done) - cpu_relax(); + __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 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(ep_buffer[ep_num]); /* Non-blocking write for EP0 */ - if (ep_num == CTRL_ENP_IN) + if (in_atomic && (ep_num == CTRL_ENP_IN)) { size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength)); + if (UNLIKELY(size > max_size)) + { + LOG_ERR("%s: ep_buffer exceeded, try to enlarge CONFIG_USB_BUFSIZE\n", + __func__); + ASSERT(0); + return -USB_BUF_OVERFLOW; + } if (!size) - USB_StatusHandler(ep_num); + usb_status_handler(ep_num); else - __usb_ep_write(ep_num, buffer, size, - USB_StatusHandler); + { + memcpy(ep_buffer[ep_num], buffer, size); + __usb_ep_write(ep_num, ep_buffer[ep_num], size, + usb_status_handler); + } return size; } if (UNLIKELY(!size)) return 0; - size = MIN(size, USB_TX_MAX_SIZE); - tx_done = false; + size = MIN(size, max_size); + event_initGeneric(&usb_event_done[ep_num >> 1]); tx_size = 0; /* Blocking write */ - __usb_ep_write(ep_num, 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; } @@ -1176,7 +1225,7 @@ ssize_t usb_endpointWrite(int ep, const void *buffer, ssize_t size) static uint32_t InData; /* Get device status */ -static int UsbDevStatus(uint16_t index) +static int usb_send_device_status(uint16_t index) { if (index) return -USB_NODEV_ERROR; @@ -1184,36 +1233,36 @@ static int UsbDevStatus(uint16_t index) InData = ((uint32_t)udc.feature) & 0xff; __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, sizeof(uint16_t), - USB_StatusHandler); + usb_status_handler); return 0; } /* Get interface status */ -static int UsbInterfaceStatus(UNUSED_ARG(uint16_t, index)) +static int usb_send_interface_status(UNUSED_ARG(uint16_t, index)) { InData = 0; __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, sizeof(uint16_t), - USB_StatusHandler); + usb_status_handler); return 0; } /* Get endpoint status */ -static int UsbEpStatus(uint16_t index) +static int usb_send_ep_status(uint16_t index) { if ((index & 0x7F) > 16) return -USB_NODEV_ERROR; InData = 0; - USB_GetStallEP(usb_ep_logical_to_hw(index), (bool *)&InData); + usb_ep_get_stall(usb_ep_logical_to_hw(index), (bool *)&InData); __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, sizeof(uint16_t), - USB_StatusHandler); + usb_status_handler); return 0; } /* USB setup packet: GET_STATUS request handler */ -static void USB_GetStatusHandler(void) +static void usb_get_status_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1253,7 +1302,7 @@ static void USB_GetStatusHandler(void) switch (setup_packet.mRequestType & USB_RECIP_MASK) { case USB_RECIP_DEVICE: - if (UsbDevStatus(w_index) < 0) + if (usb_send_device_status(w_index) < 0) { LOG_WARN("%s: GET_STATUS: invalid UsbRecipientDevice\n", __func__); @@ -1264,7 +1313,7 @@ static void USB_GetStatusHandler(void) __func__, setup_packet.mRequestType); break; case USB_RECIP_INTERFACE: - if (UsbInterfaceStatus(w_index) < 0) + if (usb_send_interface_status(w_index) < 0) { LOG_WARN("%s: GET_STATUS: invalid UsbRecipientInterface\n", __func__); @@ -1275,7 +1324,7 @@ static void USB_GetStatusHandler(void) __func__, setup_packet.mRequestType); break; case USB_RECIP_ENDPOINT: - if (UsbEpStatus(w_index) < 0) + if (usb_send_ep_status(w_index) < 0) { LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n", __func__); @@ -1302,10 +1351,16 @@ static int usb_get_device_descriptor(int id) __usb_ep_write(CTRL_ENP_IN, (const uint8_t *)usb_dev->device, usb_size(usb_dev->device->bLength, usb_le16_to_cpu(setup_packet.wLength)), - USB_StatusHandler); + usb_status_handler); return 0; } +/* + * TODO: refactor this part to remove this temporary buffer. + * + * It would be better to define all the USB descriptors in the right order and + * send them as a contiguous buffer directly from the flash / rodata memory. + */ #define USB_BUFSIZE (128) static uint8_t usb_cfg_buffer[USB_BUFSIZE]; STATIC_ASSERT(USB_BUFSIZE < (1 << (sizeof(uint16_t) * 8))); @@ -1338,7 +1393,7 @@ static int usb_get_configuration_descriptor(int id) usb_cfg_buffer, usb_size(p - usb_cfg_buffer, usb_le16_to_cpu(setup_packet.wLength)), - USB_StatusHandler); + usb_status_handler); return 0; } @@ -1379,11 +1434,11 @@ static int usb_get_string_descriptor(unsigned int id) lang_str, usb_size(lang_str->bLength, usb_le16_to_cpu(setup_packet.wLength)), - USB_StatusHandler); + usb_status_handler); return 0; } -static void UsbGetDescriptor(void) +static void usb_get_descriptor(void) { uint16_t w_value_lo = usb_le16_to_cpu(setup_packet.wValue) & 0x00ff; uint16_t w_value_hi = (usb_le16_to_cpu(setup_packet.wValue) & 0xff00) >> 8; @@ -1435,23 +1490,20 @@ static void usb_event_handler(UsbDevice *dev) } /* USB setup packet: GET_DESCRIPTOR handler */ -static void UBS_GetDescriptorHandler(void) +static void usb_get_descriptor_handler(void) { LOG_INFO("%s: GET_DESCRIPTOR: RECIP = %d\n", __func__, setup_packet.mRequestType & USB_RECIP_MASK); if ((setup_packet.mRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) - UsbGetDescriptor(); - /* 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); + usb_get_descriptor(); else - ep_cnfg[CTRL_ENP_OUT].status = STALLED; + usb_event_handler(usb_dev); } /* USB setup packet: SET_ADDRESS handler */ -static void USB_SetAddressHandler(void) +static void usb_set_address_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1463,13 +1515,13 @@ static void USB_SetAddressHandler(void) ((setup_packet.mRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) && (w_index == 0) && (w_length == 0) && (w_value < 128)) - USB_StatusHandler(CTRL_ENP_IN); + usb_status_handler(CTRL_ENP_IN); else ep_cnfg[CTRL_ENP_OUT].status = STALLED; } /* USB setup packet: GET_CONFIGURATION handler */ -static void USB_GetConfigurationHandler(void) +static void usb_get_config_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1479,7 +1531,7 @@ static void USB_GetConfigurationHandler(void) (w_value == 0) && (w_index == 0) && (w_value == 1)) { InData = udc.cfg_id; - __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 1, USB_StatusHandler); + __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 1, usb_status_handler); } else ep_cnfg[CTRL_ENP_OUT].status = STALLED; @@ -1503,15 +1555,15 @@ static const UsbConfigDesc *usb_find_configuration(int num) return NULL; } -static int UsbSetConfigurationState(uint32_t Configuration) +static int usb_set_config_state(uint32_t conf) { const UsbConfigDesc *pCnfg; unsigned int i; - if (Configuration) + if (conf) { /* Find configuration descriptor */ - pCnfg = usb_find_configuration(Configuration); + pCnfg = usb_find_configuration(conf); if (pCnfg == NULL) return -USB_NODEV_ERROR; @@ -1521,7 +1573,7 @@ static int UsbSetConfigurationState(uint32_t Configuration) udc.cfg = pCnfg; /* Set Interface and Alternative Setting */ - udc.cfg_id = Configuration; + udc.cfg_id = conf; /* Set self-powered state */ if (pCnfg->bmAttributes & USB_CONFIG_ATT_SELFPOWER) udc.feature |= STM32_UDC_FEATURE_SELFPOWERED; @@ -1533,6 +1585,8 @@ static int UsbSetConfigurationState(uint32_t Configuration) 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 { @@ -1543,7 +1597,7 @@ static int UsbSetConfigurationState(uint32_t Configuration) } /* USB setup packet: SET_CONFIGURATION handler */ -static void USB_SetConfigurationHandler(void) +static void usb_set_config_handler(void) { uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue); uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex); @@ -1553,19 +1607,19 @@ static void USB_SetConfigurationHandler(void) __func__, w_value); if ((udc.state >= USB_STATE_ADDRESS) && (w_index == 0) && (w_length == 0) && - (UsbSetConfigurationState(w_value & 0xff) == 0)) - USB_StatusHandler(CTRL_ENP_OUT); + (usb_set_config_state(w_value & 0xff) == 0)) + usb_status_handler(CTRL_ENP_OUT); else ep_cnfg[CTRL_ENP_OUT].status = STALLED; } /* USB setup packet: standard request handler */ -static void USB_StandardRequestHandler(void) +static void usb_standard_request_handler(void) { switch (setup_packet.bRequest) { case USB_REQ_GET_STATUS: - USB_GetStatusHandler(); + usb_get_status_handler(); break; case USB_REQ_CLEAR_FEATURE: LOG_INFO("%s: bRequest=%d (CLEAR_FEATURE)\n", @@ -1576,20 +1630,20 @@ static void USB_StandardRequestHandler(void) __func__, setup_packet.bRequest); break; case USB_REQ_SET_ADDRESS: - USB_SetAddressHandler(); + usb_set_address_handler(); break; case USB_REQ_GET_DESCRIPTOR: - UBS_GetDescriptorHandler(); + usb_get_descriptor_handler(); break; case USB_REQ_SET_DESCRIPTOR: LOG_INFO("%s: bRequest=%d (SET_DESCRIPTOR)\n", __func__, setup_packet.bRequest); break; case USB_REQ_GET_CONFIGURATION: - USB_GetConfigurationHandler(); + usb_get_config_handler(); break; case USB_REQ_SET_CONFIGURATION: - USB_SetConfigurationHandler(); + usb_set_config_handler(); break; case USB_REQ_GET_INTERFACE: LOG_INFO("%s: bRequest=%d (GET_INTERFACE)\n", @@ -1612,7 +1666,7 @@ static void USB_StandardRequestHandler(void) } /* USB setup packet handler */ -static void USB_SetupHandler(void) +static void usb_setup_handler(void) { switch (setup_packet.mRequestType & USB_TYPE_MASK) { @@ -1620,7 +1674,7 @@ static void USB_SetupHandler(void) case USB_TYPE_STANDARD: LOG_INFO("%s: bmRequestType=%02x (Standard)\n", __func__, setup_packet.mRequestType); - USB_StandardRequestHandler(); + usb_standard_request_handler(); break; /* Class */ case USB_TYPE_CLASS: @@ -1701,13 +1755,13 @@ static void usb_isr_correct_transfer(stm32_usb_irq_status_t interrupt) ASSERT(ep_cnfg[EP].hw); /* IN EP */ if (EP & 0x01) - EpCtrlClr_CTR_TX(ep_cnfg[EP].hw); + ep_ctrl_clr_ctr_tx(ep_cnfg[EP].hw); else - EpCtrlClr_CTR_RX(ep_cnfg[EP].hw); + ep_ctrl_clr_ctr_rx(ep_cnfg[EP].hw); if (EP == CTRL_ENP_OUT) { /* Determinate type of packet (only for control EP) */ - bool SetupPacket = EpCtrlGet_SETUP(ep_cnfg[CTRL_ENP_OUT].hw); + bool SetupPacket = ep_ctrl_get_setup(ep_cnfg[CTRL_ENP_OUT].hw); if (SetupPacket) { @@ -1719,10 +1773,10 @@ static void usb_isr_correct_transfer(stm32_usb_irq_status_t interrupt) /* reset EP IO ctrl */ if (setup_packet.mRequestType & USB_DIR_IN) - USB_StatusHandler(CTRL_ENP_OUT); - USB_SetupHandler(); + usb_status_handler(CTRL_ENP_OUT); + usb_setup_handler(); if (ep_cnfg[CTRL_ENP_OUT].status == STALLED) - USB_StallCtrlEP(); + usb_ep_set_stall_ctrl(); } else { @@ -1755,6 +1809,9 @@ static void usb_isr(void) interrupt.status = usb->ISTR; interrupt.status &= usb->CNTR | 0x1f; + /* Set the context as atomic */ + in_atomic = true; + if (interrupt.PMAOVR) { LOG_WARN("%s: DMA overrun / underrun\n", __func__); @@ -1804,6 +1861,7 @@ static void usb_isr(void) { usb_isr_correct_transfer(interrupt); } + in_atomic = false; } /* USB: hardware initialization */ @@ -1868,8 +1926,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; }