4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief STM32 USB driver
35 * \author Andrea Righi <arighi@develer.com>
38 #include "cfg/cfg_usb.h"
40 #define LOG_LEVEL USB_LOG_LEVEL
41 #define LOG_FORMAT USB_LOG_FORMAT
44 #include <cfg/debug.h>
45 #include <cfg/macros.h>
46 #include <cfg/module.h>
49 #include <cpu/power.h>
51 #include <drv/irq_cm3.h>
52 #include <drv/gpio_stm32.h>
53 #include <drv/clock_stm32.h>
54 #include <drv/timer.h>
57 #include <string.h> /* memcpy() */
59 #include "usb_stm32.h"
61 /* XXX: consider to move this to cfg/compiler.h */
62 #define ALIGNED(x) __attribute__ ((__aligned__(x)))
64 /* XXX: consider to move this to cfg/macros.h */
65 #define ALIGN_UP(value, align) (((value) & ((align) - 1)) ? \
66 (((value) + ((align) - 1)) & ~((align) - 1)) : \
69 /* XXX: redefine this to make it usable within C expression */
70 #define _MIN(a,b) (((a) < (b)) ? (a) : (b))
72 /* STM32 USB registers */
83 reg32_t __reserved[8];
91 /* Hardware registers */
92 static struct stm32_usb *usb = (struct stm32_usb *)USB_BASE_ADDR;
94 /* Endpoint descriptors: used for handling requests to use with endpoints */
95 static stm32_UsbEp ep_cnfg[ENP_MAX_NUMB];
97 /* USB EP0 control descriptor */
98 static const UsbEndpointDesc USB_CtrlEpDescr0 =
100 .bLength = sizeof(USB_CtrlEpDescr0),
101 .bDescriptorType = USB_DT_ENDPOINT,
102 .bEndpointAddress = USB_DIR_OUT | 0,
103 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
104 .wMaxPacketSize = USB_EP0_MAX_SIZE,
108 /* USB EP1 control descriptor */
109 static const UsbEndpointDesc USB_CtrlEpDescr1 =
111 .bLength = sizeof(USB_CtrlEpDescr1),
112 .bDescriptorType = USB_DT_ENDPOINT,
113 .bEndpointAddress = USB_DIR_IN | 0,
114 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
115 .wMaxPacketSize = USB_EP0_MAX_SIZE,
119 /* USB setup packet */
120 static UsbCtrlRequest setup_packet;
122 /* USB device controller: max supported interfaces */
123 #define USB_MAX_INTERFACE 1
125 /* USB device controller features */
126 #define STM32_UDC_FEATURE_SELFPOWERED BV(0)
127 #define STM32_UDC_FEATURE_REMOTE_WAKEUP BV(1)
129 /* Hardware-specific USB device controller structure */
130 typedef struct stm32_udc
134 const UsbConfigDesc *cfg;
136 uint32_t alt[USB_MAX_INTERFACE];
139 } PACKED stm32_udc_t;
141 /* Hardware-specific USB Device Controller */
142 static stm32_udc_t udc;
144 /* Generic USB Device Controller structure */
145 static UsbDevice *usb_dev;
147 /* USB packet memory management: list of allocated chunks */
148 static stm32_UsbMemSlot *mem_use;
150 /* USB packet memory management: memory buffer metadata */
151 #define EP_MAX_SLOTS 16
152 static stm32_UsbMemSlot memory_buffer[EP_MAX_SLOTS];
154 /* Endpoint TX and RX buffers */
156 /* XXX: use the empty cond section to silent a buggy doxygen warning */
157 static bool rx_done, tx_done;
158 static size_t rx_size, tx_size;
159 static uint8_t ep_buffer[_MIN(CONFIG_USB_BUFSIZE, USB_XFER_MAX_SIZE)] ALIGNED(4);
162 /* Allocate a free block of the packet memory */
163 static stm32_UsbMemSlot *usb_malloc(void)
167 for (i = 0; i < countof(memory_buffer); i++)
168 if (memory_buffer[i].Size == 0)
169 return &memory_buffer[i];
173 /* Release a block of the packet memory */
174 static void usb_free(stm32_UsbMemSlot *pPntr)
179 /* Allocate a free chunk of the packet memory (inside a block) */
180 static bool usb_alloc_buffer(uint16_t *pOffset, uint32_t *size,
183 stm32_UsbMemSlot *mem = mem_use,
184 *memNext, *mem_useNew;
185 uint32_t max_size = *size;
188 * Packet size alignment:
189 * - fine-granularity allocation: size alignment by 2;
190 * - coarse-granularity allocation: size alignment by 32.
193 max_size = ALIGN_UP(max_size, 2);
195 max_size = ALIGN_UP(max_size, 32);
197 * Finding free memory chunks from the allocated blocks of the USB
203 /* Offset alignment by 4 */
204 *pOffset = ALIGN_UP(mem->Start + mem->Size, 4);
206 if ((mem->next == NULL) ||
208 *pOffset + max_size))
212 /* Check for out-of-memory condition */
213 if ((*pOffset + max_size) >= USB_BDT_OFFSET)
216 * Allocate a new memory block, next to the last allocated block.
218 mem_useNew = usb_malloc();
219 if (mem_useNew == NULL)
221 /* Insert the block to the list of allocated blocks */
224 mem_use = mem_useNew;
225 mem_use->next = NULL;
229 mem_useNew->next = mem->next;
230 mem->next = mem_useNew;
232 /* Update block's metadata */
233 mem_useNew->ep_addr = EndPoint;
234 mem_useNew->Start = *pOffset;
235 mem_useNew->Size = max_size;
242 /* Release a chunk of the packet memory (inside a block) */
243 static void usb_free_buffer(int EndPoint)
245 stm32_UsbMemSlot *mem, *memPrev = NULL;
250 if (mem->ep_addr == EndPoint)
252 if (UNLIKELY(memPrev == NULL))
254 /* Free the first element of the list */
255 mem_use = mem_use->next;
260 memPrev->next = mem->next;
269 /*-------------------------------------------------------------------------*/
271 /* Connect USB controller */
272 static void usb_connect(void)
274 stm32_gpioPinWrite((struct stm32_gpio *)GPIOC_BASE, 1 << 11, 0);
277 /* Set USB device address */
278 static void usb_set_address(uint32_t addr)
280 usb->DADDR = addr | 0x80;
283 /* Suspend USB controller */
284 static void usb_suspend(void)
286 usb->CNTR |= bmFSUSP | bmLPMODE;
289 /* Resume USB controller */
290 static void usb_resume(void)
292 uint32_t line_status;
294 line_status = usb->FNR & 0xc000;
297 /* check for noise and eventually return to sleep */
298 if (line_status == 0xc000)
301 usb->CNTR &= ~(bmFSUSP | bmLPMODE);
304 /* Convert logical EP address to physical EP address */
305 static int usb_ep_logical_to_hw(uint8_t ep_addr)
307 int addr = (ep_addr & 0x0f) << 1;
308 return (ep_addr & 0x80) ? addr + 1 : addr;
312 static void ep_ctrl_set_ea(reg32_t *reg, uint32_t val)
315 val |= *reg & 0x0700;
316 val |= USB_CTRL_CLEAR_ONLY_MASK;
320 /* Get EP IN status */
321 static uint32_t ep_ctrl_get_stat_tx(reg32_t *reg)
323 return (*reg & (0x3UL << 4)) >> 4;
326 /* Set EP IN state */
327 static void ep_ctrl_set_stat_tx(reg32_t *reg, stm32_UsbEpState val)
333 * The EP can change state between read and write operations from VALID
334 * to NAK and result of set operation will be invalid.
336 for (i = 0; i < 2; i++)
338 if (ep_ctrl_get_stat_tx(reg) == val)
343 state |= USB_CTRL_CLEAR_ONLY_MASK;
344 /* Clear the toggle bits without STAT_TX (4,5) */
350 /* Set EP DTOG_TX bit (IN) */
351 static void ep_ctrl_set_dtog_tx(reg32_t *reg, uint32_t val)
353 val = val ? (*reg ^ (1UL << 6)) : *reg;
354 /* Clear the toggle bits without DTOG_TX (6) */
356 val |= USB_CTRL_CLEAR_ONLY_MASK;
360 /* Clear EP CTR_TX bit (IN) */
361 static void ep_ctrl_clr_ctr_tx(reg32_t *reg)
365 val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 7);
371 /* Clear EP CTR_RX bit (OUT) */
372 static void ep_ctrl_clr_ctr_rx(reg32_t *reg)
375 val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 15);
381 /* Set EP KIND bit */
382 static void ep_ctrl_set_ep_kind(reg32_t *reg, uint32_t val)
384 val = val ? (1UL << 8) : 0;
385 val |= *reg & ~(USB_CTRL_TOGGLE_MASK | (1UL << 8));
386 val |= USB_CTRL_CLEAR_ONLY_MASK;
391 static int ep_ctrl_set_ep_type(reg32_t *reg, uint8_t val)
395 if (UNLIKELY(val >= EP_TYPE_MAX))
398 return USB_INVAL_ERROR;
402 type |= *reg & ~(USB_CTRL_TOGGLE_MASK | (0x3UL << 9));
403 type |= USB_CTRL_CLEAR_ONLY_MASK;
409 /* Get EP STAT_RX (OUT) */
410 static uint32_t ep_ctrl_get_stat_rx(reg32_t *reg)
412 uint32_t val = *reg & (0x3UL << 12);
416 /* Set EP STAT_RX (OUT) */
417 static void ep_ctrl_set_stat_rx(reg32_t *reg, stm32_UsbEpState val)
423 * The EP can change state between read and write operations from VALID
424 * to NAK and result of set operation will be invalid.
426 for (i = 0; i < 2; i++)
428 if (ep_ctrl_get_stat_rx(reg) == val)
433 state |= USB_CTRL_CLEAR_ONLY_MASK;
434 /* Clear the toggle bits without STAT_RX (12,13) */
440 /* Set DTOG_RX bit */
441 static void ep_ctrl_set_dtog_rx(reg32_t *reg, uint32_t val)
443 val = val ? (*reg ^ (1UL << 14)) : *reg;
444 /* Clear the toggle bits without DTOG_RX (14) */
446 val |= USB_CTRL_CLEAR_ONLY_MASK;
450 /* Get EP SETUP bit */
451 static uint32_t ep_ctrl_get_setup(reg32_t *reg)
453 uint32_t val = *reg & (1UL << 11);
457 /* Core endpoint I/O function */
458 static void __usb_ep_io(int EP)
460 ssize_t Count, CountHold, Offset;
461 uint32_t *pDst, *pSrc, Data;
462 stm32_UsbEp *epd = &ep_cnfg[EP];
464 if (UNLIKELY(epd->hw == NULL))
466 LOG_ERR("%s: invalid endpoint (EP%d-%s)\n",
469 (EP & 0x01) ? "IN" : "OUT");
473 if (epd->status != BEGIN_SERVICED && epd->status != NO_SERVICED)
479 Count = epd->size - epd->offset;
480 while (epd->avail_data)
482 if (!Count && !(epd->flags & STM32_USB_EP_ZERO_PACKET))
486 epd->status = BEGIN_SERVICED;
488 if ((epd->flags & STM32_USB_EP_ZERO_PACKET) &&
489 (Count == epd->max_size))
490 epd->flags |= STM32_USB_EP_ZERO_PACKET |
491 STM32_USB_EP_ZERO_POSSIBLE;
493 CountHold = Count = MIN(Count, epd->max_size);
495 epd->flags |= STM32_USB_EP_ZERO_PACKET;
496 Offset = epd->offset;
497 epd->offset += Count;
500 case USB_ENDPOINT_XFER_CONTROL:
501 case USB_ENDPOINT_XFER_INT:
502 pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET));
504 case USB_ENDPOINT_XFER_BULK:
505 pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET));
507 case USB_ENDPOINT_XFER_ISOC:
508 LOG_ERR("%s: isochronous transfer not supported\n",
510 /* Fallback to default */
516 /* Write data to packet memory buffer */
519 Data = *(epd->write_buffer + Offset++);
522 Data |= (uint32_t)(*(epd->write_buffer + Offset++)) << 8;
528 EP_DTB_WRITE(EP >> 1, COUNT_TX_OFFSET, CountHold);
529 ep_ctrl_set_stat_tx(epd->hw, EP_VALID);
531 --ep_cnfg[EP].avail_data;
532 Count = epd->size - epd->offset;
534 if (!Count && !(epd->flags & STM32_USB_EP_ZERO_PACKET))
536 epd->status = COMPLETE;
537 /* call callback function */
545 while (epd->avail_data)
547 /* Get data size and buffer pointer */
550 case USB_ENDPOINT_XFER_CONTROL:
551 case USB_ENDPOINT_XFER_INT:
552 /* Get received bytes number */
553 Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF;
554 /* Get address of the USB packet buffer for corresponding EP */
555 pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET));
557 case USB_ENDPOINT_XFER_BULK:
558 /* Get received bytes number */
559 Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF;
560 /* Get address of the USB packet buffer for corresponding EP */
561 pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET));
563 case USB_ENDPOINT_XFER_ISOC:
564 LOG_ERR("%s: isochronous transfer not supported\n",
566 /* Fallback to default */
572 if (Count > (epd->size - epd->offset))
574 epd->status = BUFFER_OVERRUN;
575 epd->size = ep_cnfg[EP].offset;
578 else if (Count < ep_cnfg[EP].max_size)
580 epd->status = BUFFER_UNDERRUN;
581 epd->size = ep_cnfg[EP].offset + Count;
584 epd->status = BEGIN_SERVICED;
586 Offset = epd->offset;
587 epd->offset += Count;
589 /* Read data from packet memory buffer */
593 *(epd->read_buffer + Offset++) = Data;
597 *(epd->read_buffer + Offset++) = Data;
602 ep_ctrl_set_stat_rx(epd->hw, EP_VALID);
604 --ep_cnfg[EP].avail_data;
606 if (*epd->hw & (1UL << 11))
608 ep_cnfg[EP].status = SETUP_OVERWRITE;
611 if (!(Count = (epd->size - epd->offset)))
613 epd->status = COMPLETE;
617 if (epd->status != BEGIN_SERVICED && epd->status != NO_SERVICED)
619 /* call callback function */
627 * Return the lower value from Host expected size and size and set a flag
628 * STM32_USB_EP_ZERO_POSSIBLE when size is lower that host expected size.
630 static size_t usb_size(size_t size, size_t host_size)
632 if (size < host_size)
634 ep_cnfg[CTRL_ENP_IN].flags |= STM32_USB_EP_ZERO_POSSIBLE;
640 /* Configure an EP descriptor before performing a I/O operation */
641 #define USB_EP_IO(__EP, __op, __buf, __size, __complete) \
644 stm32_UsbIoStatus ret; \
646 /* Fill EP descriptor */ \
647 IRQ_SAVE_DISABLE(flags); \
650 ep_cnfg[__EP].status = NOT_READY; \
651 ep_cnfg[__EP].complete = NULL; \
655 if (ep_cnfg[__EP].status == BEGIN_SERVICED) \
661 * NOTE: the write_buffer and read_buffer are actually the \
662 * same location in memory (it's a union). \
664 * We have to do this trick to silent a build warning by \
665 * casting the I/O buffer to (void *) or (const void *). \
667 ep_cnfg[__EP].__op ## _buffer = __buf; \
668 ep_cnfg[__EP].offset = 0; \
669 ep_cnfg[__EP].size = __size; \
670 ep_cnfg[__EP].complete = __complete; \
672 ep_cnfg[__EP].flags = STM32_USB_EP_ZERO_PACKET; \
674 ep_cnfg[__EP].flags = 0; \
675 ep_cnfg[__EP].status = NO_SERVICED; \
677 /* Perform the I/O operation */ \
680 ret = ep_cnfg[__EP].status; \
682 IRQ_RESTORE(flags); \
686 /* Configure and endponint and perform a read operation */
687 static stm32_UsbIoStatus
688 __usb_ep_read(int ep, void *buffer, ssize_t size, void (*complete)(int))
690 if (UNLIKELY((ep >= ENP_MAX_NUMB) || (ep & 0x01)))
692 LOG_ERR("%s: invalid EP number %d\n", __func__, ep);
696 if (UNLIKELY((size_t)buffer & 0x03))
698 LOG_ERR("%s: unaligned buffer @ %p\n", __func__, buffer);
702 return USB_EP_IO(ep, read, buffer, size, complete);
705 /* Configure and endponint and perform a write operation */
706 static stm32_UsbIoStatus
707 __usb_ep_write(int ep, const void *buffer, ssize_t size, void (*complete)(int))
709 if (UNLIKELY((ep >= ENP_MAX_NUMB) || !(ep & 0x01)))
711 LOG_ERR("%s: invalid EP number %d\n", __func__, ep);
715 if (UNLIKELY((size_t)buffer & 0x03))
717 LOG_ERR("%s: unaligned buffer @ %p\n", __func__, buffer);
721 return USB_EP_IO(ep, write, buffer, size, complete);
724 static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size)
726 stm32_UsbEp *epc = &ep_cnfg[ep];
732 ep_ctrl_set_stat_tx(epc->hw, EP_DISABLED);
733 /* Clear Tx toggle */
734 ep_ctrl_set_dtog_tx(epc->hw, 0);
735 /* Clear Correct Transfer for transmission flag */
736 ep_ctrl_clr_ctr_tx(epc->hw);
738 /* Update EP description table */
739 EP_DTB_WRITE(ep >> 1, ADDR_TX_OFFSET, offset);
740 EP_DTB_WRITE(ep >> 1, COUNT_TX_OFFSET, 0);
745 uint16_t rx_count = 0;
748 ep_ctrl_set_stat_rx(epc->hw, EP_DISABLED);
749 /* Clear Rx toggle */
750 ep_ctrl_set_dtog_rx(epc->hw, 0);
751 /* Clear Correct Transfer for reception flag */
752 ep_ctrl_clr_ctr_rx(epc->hw);
753 /* Descriptor block size field */
754 rx_count |= (size > 62) << 15;
755 /* Descriptor number of blocks field */
756 rx_count |= (((size > 62) ? (size >> 5) - 1 : size >> 1) &
758 /* Update EP description table */
759 EP_DTB_WRITE(ep >> 1, ADDR_RX_OFFSET, offset);
760 EP_DTB_WRITE(ep >> 1, COUNT_RX_OFFSET, rx_count);
764 /* Enable/Disable an endpoint */
765 static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable)
773 EP = usb_ep_logical_to_hw(epd->bEndpointAddress);
774 ep_hw = &ep_cnfg[EP];
779 * Allocate packet memory for EP buffer/s calculate actual size
780 * only for the OUT EPs.
782 size = epd->wMaxPacketSize;
783 if (!usb_alloc_buffer(&Offset, &size, EP))
784 return -USB_MEMORY_FULL;
787 ep_hw->status = NOT_READY;
791 /* Set endpoint type */
792 ep_hw->type = usb_endpointType(epd);
793 /* Init EP max packet size */
794 ep_hw->max_size = epd->wMaxPacketSize;
797 ep_hw->avail_data = 1;
799 ep_hw->avail_data = 0;
800 hw = (reg32_t *)&usb->EP0R;
804 ep_ctrl_set_ea(hw, EP >> 1);
806 LOG_INFO("%s: EP%d-%s configured\n",
807 __func__, EP >> 1, EP & 1 ? "IN" : "OUT");
809 /* Low-level endpoint configuration */
810 usb_ep_low_level_config(EP, Offset, size);
812 /* Set EP Kind & enable */
815 case USB_ENDPOINT_XFER_CONTROL:
816 LOG_INFO("EP%d: CONTROL IN\n", EP >> 1);
817 ep_ctrl_set_ep_type(hw, EP_CTRL);
818 ep_ctrl_set_ep_kind(hw, 0);
820 case USB_ENDPOINT_XFER_INT:
821 LOG_INFO("EP%d: INTERRUPT IN\n", EP >> 1);
822 ep_ctrl_set_ep_type(hw, EP_INTERRUPT);
823 ep_ctrl_set_ep_kind(hw, 0);
825 case USB_ENDPOINT_XFER_BULK:
826 LOG_INFO("EP%d: BULK IN\n", EP >> 1);
827 ep_ctrl_set_ep_type(hw, EP_BULK);
828 ep_ctrl_set_ep_kind(hw, 0);
830 case USB_ENDPOINT_XFER_ISOC:
831 LOG_ERR("EP%d: ISOCHRONOUS IN: not supported\n", EP >> 1);
832 /* Fallback to default */
835 return -USB_NODEV_ERROR;
840 ep_ctrl_set_stat_tx(hw, EP_NAK);
841 /* Clear Correct Transfer for transmission flag */
842 ep_ctrl_clr_ctr_tx(hw);
847 ep_ctrl_set_stat_rx(hw, EP_VALID);
850 else if (ep_cnfg[EP].hw)
852 hw = (reg32_t *)&usb->EP0R;
859 ep_ctrl_set_stat_tx(hw, EP_DISABLED);
860 /* Clear Correct Transfer for reception flag */
861 ep_ctrl_clr_ctr_tx(hw);
867 ep_ctrl_set_stat_rx(hw, EP_DISABLED);
868 /* Clear Correct Transfer for reception flag */
869 ep_ctrl_clr_ctr_rx(hw);
873 ep_cnfg[EP].hw = NULL;
878 /* Get EP stall/unstall */
879 static int usb_ep_get_stall(int EP, bool *pStall)
881 if (ep_cnfg[EP].hw == NULL)
882 return -USB_NODEV_ERROR;
884 *pStall = (EP & 0x01) ?
885 (ep_ctrl_get_stat_tx(ep_cnfg[EP].hw) == EP_STALL): /* IN EP */
886 (ep_ctrl_get_stat_rx(ep_cnfg[EP].hw) == EP_STALL); /* OUT EP */
891 /* Set EP stall/unstall */
892 static int usb_ep_set_stall(int EP, bool Stall)
894 if (ep_cnfg[EP].hw == NULL)
895 return -USB_NODEV_ERROR;
899 ep_cnfg[EP].status = STALLED;
903 ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_STALL);
904 ep_cnfg[EP].avail_data = 1;
909 ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_STALL);
910 ep_cnfg[EP].avail_data = 0;
915 ep_cnfg[EP].status = NOT_READY;
919 ep_cnfg[EP].avail_data = 1;
920 /* reset Data Toggle bit */
921 ep_ctrl_set_dtog_tx(ep_cnfg[EP].hw, 0);
922 ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_NAK);
927 ep_cnfg[EP].avail_data = 0;
928 /* reset Data Toggle bit */
929 ep_ctrl_set_dtog_rx(ep_cnfg[EP].hw, 0);
930 ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_VALID);
936 /* Stall both directions of the control EP */
937 static void usb_ep_set_stall_ctrl(void)
939 ep_cnfg[CTRL_ENP_IN].avail_data = 1;
940 ep_cnfg[CTRL_ENP_IN].status = STALLED;
941 ep_cnfg[CTRL_ENP_OUT].avail_data = 0;
942 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
944 usb_ep_set_stall(CTRL_ENP_IN, true);
945 usb_ep_set_stall(CTRL_ENP_OUT, true);
949 * Find the position of an interface descriptor inside the configuration
952 static int usb_find_interface(uint32_t num, uint32_t alt)
954 const UsbInterfaceDesc *id;
959 /* TODO: support more than one configuration per device */
960 id = (const UsbInterfaceDesc *)usb_dev->config[i];
963 if (id->bDescriptorType != USB_DT_INTERFACE)
965 if ((id->bInterfaceNumber == num) &&
966 (id->bAlternateSetting == alt))
969 return -USB_NODEV_ERROR;
973 * Configure/deconfigure EPs of a certain interface.
976 usb_configure_ep_interface(unsigned int num, unsigned int alt, bool enable)
978 const UsbEndpointDesc *epd;
982 * Find the position of the interface descriptor (inside the
983 * configuration descriptor).
985 start = usb_find_interface(num, alt);
988 LOG_ERR("%s: interface (%u,%u) not found\n",
993 * Cycle over endpoint descriptors.
995 * NOTE: the first endpoint descriptor is placed next to the interface
996 * descriptor, so we need to add +1 to the position of the interface
997 * descriptor to find it.
999 for (i = start + 1; ; i++)
1001 epd = (const UsbEndpointDesc *)usb_dev->config[i];
1002 if ((epd == NULL) || (epd->bDescriptorType == USB_DT_INTERFACE))
1004 if (epd->bDescriptorType != USB_DT_ENDPOINT)
1006 if (UNLIKELY(usb_ep_configure(epd, enable) < 0))
1008 LOG_ERR("%s: out of memory, can't initialize EP\n",
1015 /* Set device state */
1016 static void usb_set_device_state(int state)
1020 LOG_INFO("%s: new state %d\n", __func__, state);
1022 if (udc.state == USB_STATE_CONFIGURED)
1024 /* Deconfigure device */
1025 for (i = 0; i < udc.interfaces; ++i)
1026 usb_configure_ep_interface(i,
1031 case USB_STATE_ATTACHED:
1032 case USB_STATE_POWERED:
1033 case USB_STATE_DEFAULT:
1035 usb_dev->configured = false;
1036 udc.address = udc.cfg_id = 0;
1038 case USB_STATE_ADDRESS:
1041 case USB_STATE_CONFIGURED:
1042 /* Configure device */
1043 for (i = 0; i < udc.interfaces; ++i)
1044 usb_configure_ep_interface(i,
1048 /* Unknown state: disconnected or connection in progress */
1049 usb_dev->configured = false;
1057 /* Setup packet: set address status phase end handler */
1058 static void usb_add_status_handler_end(UNUSED_ARG(int, EP))
1062 w_value = usb_le16_to_cpu(setup_packet.wValue);
1063 udc.address = w_value & 0xff;
1064 usb_set_address(udc.address);
1067 usb_set_device_state(USB_STATE_ADDRESS);
1069 usb_set_device_state(USB_STATE_DEFAULT);
1071 __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1072 __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL);
1075 /* Prepare status phase */
1076 static void usb_status_phase(bool in)
1079 __usb_ep_write(CTRL_ENP_IN, NULL, 0, NULL);
1082 /* Setup packet: status phase end handler */
1083 static void usb_status_handler_end(UNUSED_ARG(int, EP))
1085 __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1086 __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL);
1089 /* Address status handler */
1090 static void usb_status_handler(UNUSED_ARG(int, EP))
1092 if (setup_packet.mRequestType & USB_DIR_IN)
1094 usb_status_phase(false);
1095 ep_cnfg[CTRL_ENP_OUT].complete = usb_status_handler_end;
1099 usb_status_phase(true);
1100 ep_cnfg[CTRL_ENP_IN].complete =
1101 (setup_packet.bRequest == USB_REQ_SET_ADDRESS) ?
1102 usb_add_status_handler_end :
1103 usb_status_handler_end;
1107 static void usb_endpointRead_complete(int ep)
1109 if (UNLIKELY(ep >= ENP_MAX_NUMB))
1114 ASSERT(!(ep & 0x01));
1117 rx_size = ep_cnfg[ep].size;
1120 ssize_t usb_endpointRead(int ep, void *buffer, ssize_t size)
1122 int ep_num = usb_ep_logical_to_hw(ep);
1123 ssize_t max_size = sizeof(ep_buffer);
1125 /* Non-blocking read for EP0 */
1126 if (ep_num == CTRL_ENP_OUT)
1128 size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
1129 if (UNLIKELY(size > max_size))
1131 LOG_ERR("%s: ep_buffer exceeded, try to enlarge CONFIG_USB_BUFSIZE\n",
1134 return -USB_BUF_OVERFLOW;
1137 usb_status_handler(ep_num);
1140 __usb_ep_read(ep_num, ep_buffer, size,
1141 usb_status_handler);
1142 memcpy(buffer, ep_buffer, size);
1146 if (UNLIKELY(!size))
1148 size = MIN(size, max_size);
1153 __usb_ep_read(ep_num, ep_buffer, size, usb_endpointRead_complete);
1156 memcpy(buffer, ep_buffer, rx_size);
1161 static void usb_endpointWrite_complete(int ep)
1163 if (UNLIKELY(ep >= ENP_MAX_NUMB))
1171 tx_size = ep_cnfg[ep].size;
1174 ssize_t usb_endpointWrite(int ep, const void *buffer, ssize_t size)
1176 int ep_num = usb_ep_logical_to_hw(ep);
1177 ssize_t max_size = sizeof(ep_buffer);
1179 /* Non-blocking write for EP0 */
1180 if (ep_num == CTRL_ENP_IN)
1182 size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
1183 if (UNLIKELY(size > max_size))
1185 LOG_ERR("%s: ep_buffer exceeded, try to enlarge CONFIG_USB_BUFSIZE\n",
1188 return -USB_BUF_OVERFLOW;
1191 usb_status_handler(ep_num);
1194 memcpy(ep_buffer, buffer, size);
1195 __usb_ep_write(ep_num, ep_buffer, size,
1196 usb_status_handler);
1200 if (UNLIKELY(!size))
1202 size = MIN(size, max_size);
1206 /* Blocking write */
1207 memcpy(ep_buffer, buffer, size);
1208 __usb_ep_write(ep_num, ep_buffer, size, usb_endpointWrite_complete);
1215 /* Global variable to handle the following non-blocking I/O operations */
1216 static uint32_t InData;
1218 /* Get device status */
1219 static int usb_send_device_status(uint16_t index)
1222 return -USB_NODEV_ERROR;
1224 InData = ((uint32_t)udc.feature) & 0xff;
1225 __usb_ep_write(CTRL_ENP_IN,
1226 (uint8_t *)&InData, sizeof(uint16_t),
1227 usb_status_handler);
1231 /* Get interface status */
1232 static int usb_send_interface_status(UNUSED_ARG(uint16_t, index))
1235 __usb_ep_write(CTRL_ENP_IN,
1236 (uint8_t *)&InData, sizeof(uint16_t),
1237 usb_status_handler);
1241 /* Get endpoint status */
1242 static int usb_send_ep_status(uint16_t index)
1244 if ((index & 0x7F) > 16)
1245 return -USB_NODEV_ERROR;
1248 usb_ep_get_stall(usb_ep_logical_to_hw(index), (bool *)&InData);
1249 __usb_ep_write(CTRL_ENP_IN,
1250 (uint8_t *)&InData, sizeof(uint16_t),
1251 usb_status_handler);
1255 /* USB setup packet: GET_STATUS request handler */
1256 static void usb_get_status_handler(void)
1258 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1259 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1260 uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1262 /* GET_STATUS sanity checks */
1263 if (udc.state < USB_STATE_ADDRESS)
1265 LOG_WARN("%s: bad GET_STATUS request (State=%02x)\n",
1266 __func__, udc.state);
1267 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1272 LOG_WARN("%s: bad GET_STATUS request (wLength.Word=%02x)\n",
1273 __func__, w_length);
1274 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1277 if (!(setup_packet.mRequestType & USB_DIR_IN))
1279 LOG_WARN("%s: bad GET_STATUS request (mRequestType=%02x)\n",
1280 __func__, setup_packet.mRequestType);
1281 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1286 LOG_WARN("%s: bad GET_STATUS request (wValue=%02x)\n",
1288 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1292 /* Process GET_STATUS request */
1293 switch (setup_packet.mRequestType & USB_RECIP_MASK)
1295 case USB_RECIP_DEVICE:
1296 if (usb_send_device_status(w_index) < 0)
1298 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientDevice\n",
1300 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1303 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientDevice)\n",
1304 __func__, setup_packet.mRequestType);
1306 case USB_RECIP_INTERFACE:
1307 if (usb_send_interface_status(w_index) < 0)
1309 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientInterface\n",
1311 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1314 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientInterface)\n",
1315 __func__, setup_packet.mRequestType);
1317 case USB_RECIP_ENDPOINT:
1318 if (usb_send_ep_status(w_index) < 0)
1320 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n",
1322 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1325 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientEndpoint)\n",
1326 __func__, setup_packet.mRequestType);
1329 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n",
1331 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1336 static int usb_get_device_descriptor(int id)
1339 return -USB_NODEV_ERROR;
1341 usb_dev->device->bMaxPacketSize0 = USB_EP0_MAX_SIZE;
1342 __usb_ep_write(CTRL_ENP_IN, (const uint8_t *)usb_dev->device,
1343 usb_size(usb_dev->device->bLength,
1344 usb_le16_to_cpu(setup_packet.wLength)),
1345 usb_status_handler);
1350 * TODO: refactor this part to remove this temporary buffer.
1352 * It would be better to define all the USB descriptors in the right order and
1353 * send them as a contiguous buffer directly from the flash / rodata memory.
1355 #define USB_BUFSIZE (128)
1356 static uint8_t usb_cfg_buffer[USB_BUFSIZE];
1357 STATIC_ASSERT(USB_BUFSIZE < (1 << (sizeof(uint16_t) * 8)));
1359 static int usb_get_configuration_descriptor(int id)
1361 const UsbConfigDesc **config =
1362 (const UsbConfigDesc **)usb_dev->config;
1363 uint8_t *p = usb_cfg_buffer;
1366 /* TODO: support more than one configuration per device */
1367 if (UNLIKELY(id > 0))
1368 return -USB_NODEV_ERROR;
1370 for (i = 0; config[i]; i++)
1372 memcpy(p, config[i], config[i]->bLength);
1373 p += config[i]->bLength;
1375 if (UNLIKELY((p - usb_cfg_buffer) > USB_BUFSIZE))
1378 return -USB_BUF_OVERFLOW;
1381 ((UsbConfigDesc *)usb_cfg_buffer)->wTotalLength =
1382 usb_cpu_to_le16((uint16_t)(p - usb_cfg_buffer));
1383 __usb_ep_write(CTRL_ENP_IN,
1385 usb_size(p - usb_cfg_buffer,
1386 usb_le16_to_cpu(setup_packet.wLength)),
1387 usb_status_handler);
1391 static int usb_get_string_descriptor(unsigned int id)
1393 const UsbStringDesc *lang_str;
1394 unsigned int lang_id, str_id;
1395 uint16_t w_index_lo = usb_le16_to_cpu(setup_packet.wIndex) & 0x00ff;
1396 uint16_t w_index_hi = (usb_le16_to_cpu(setup_packet.wIndex) &
1399 ASSERT(usb_dev->strings != NULL);
1400 ASSERT(usb_dev->strings[0] != NULL);
1402 lang_str = usb_dev->strings[0];
1405 /* Find Language index */
1406 for (lang_id = 0; ; lang_id++)
1408 const UsbStringDesc *str =
1409 usb_dev->strings[lang_id];
1410 if (UNLIKELY(str == NULL))
1411 return -USB_NODEV_ERROR;
1412 if ((str->data[0] == w_index_lo) &&
1413 (str->data[1] == w_index_hi))
1416 /* Check buffer overflow to find string index */
1417 for (str_id = 0; str_id < id; str_id++)
1419 lang_str = usb_dev->strings[lang_id + 1 + str_id];
1420 if (lang_str == NULL)
1421 return -USB_NODEV_ERROR;
1424 __usb_ep_write(CTRL_ENP_IN,
1426 usb_size(lang_str->bLength,
1427 usb_le16_to_cpu(setup_packet.wLength)),
1428 usb_status_handler);
1432 static void usb_get_descriptor(void)
1434 uint16_t w_value_lo = usb_le16_to_cpu(setup_packet.wValue) & 0x00ff;
1435 uint16_t w_value_hi = (usb_le16_to_cpu(setup_packet.wValue) & 0xff00) >> 8;
1437 if (udc.state < USB_STATE_DEFAULT)
1439 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1445 LOG_INFO("%s: GET_DEVICE_DESCRIPTOR: id=%d, state=%d\n",
1449 if (usb_get_device_descriptor(w_value_lo) < 0)
1450 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1453 LOG_INFO("%s: GET_CONFIG_DESCRIPTOR: id=%d, state=%d\n",
1454 __func__, w_value_lo, udc.state);
1455 if (usb_get_configuration_descriptor(w_value_lo) < 0)
1456 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1459 LOG_INFO("%s: GET_STRING_DESCRIPTOR: id=%d, state=%d\n",
1460 __func__, w_value_lo, udc.state);
1461 if (usb_get_string_descriptor(w_value_lo) < 0)
1462 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1465 LOG_WARN("%s: GET_UNKNOWN_DESCRIPTOR: id=%d, state=%d\n",
1466 __func__, w_value_lo, udc.state);
1467 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1472 /* USB setup packet: class/vendor request handler */
1473 static void usb_event_handler(UsbDevice *dev)
1476 * TODO: get the appropriate usb_dev in function of the endpoint
1480 dev->event_cb(&setup_packet);
1483 /* USB setup packet: GET_DESCRIPTOR handler */
1484 static void usb_get_descriptor_handler(void)
1486 LOG_INFO("%s: GET_DESCRIPTOR: RECIP = %d\n",
1488 setup_packet.mRequestType & USB_RECIP_MASK);
1489 if ((setup_packet.mRequestType & USB_RECIP_MASK) ==
1491 usb_get_descriptor();
1492 /* Getting descriptor for a device is a standard request */
1493 else if ((setup_packet.mRequestType & USB_DIR_MASK) == USB_DIR_IN)
1494 usb_event_handler(usb_dev);
1496 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1499 /* USB setup packet: SET_ADDRESS handler */
1500 static void usb_set_address_handler(void)
1502 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1503 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1504 uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1506 LOG_INFO("%s: SET_ADDRESS: %d\n",
1507 __func__, usb_le16_to_cpu(setup_packet.wValue));
1508 if ((udc.state >= USB_STATE_DEFAULT) &&
1509 ((setup_packet.mRequestType & USB_RECIP_MASK) ==
1510 USB_RECIP_DEVICE) &&
1511 (w_index == 0) && (w_length == 0) && (w_value < 128))
1512 usb_status_handler(CTRL_ENP_IN);
1514 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1517 /* USB setup packet: GET_CONFIGURATION handler */
1518 static void usb_get_config_handler(void)
1520 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1521 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1523 LOG_INFO("%s: GET_CONFIGURATION\n", __func__);
1524 if ((udc.state >= USB_STATE_ADDRESS) &&
1525 (w_value == 0) && (w_index == 0) && (w_value == 1))
1527 InData = udc.cfg_id;
1528 __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 1, usb_status_handler);
1531 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1534 static const UsbConfigDesc *usb_find_configuration(int num)
1536 const UsbConfigDesc *cfg;
1541 cfg = (const UsbConfigDesc *)usb_dev->config[i];
1544 if (cfg->bDescriptorType != USB_DT_CONFIG)
1546 if (cfg->bConfigurationValue == num)
1552 static int usb_set_config_state(uint32_t conf)
1554 const UsbConfigDesc *pCnfg;
1559 /* Find configuration descriptor */
1560 pCnfg = usb_find_configuration(conf);
1562 return -USB_NODEV_ERROR;
1564 /* Reset current configuration */
1565 usb_set_device_state(USB_STATE_ADDRESS);
1566 usb_dev->configured = false;
1569 /* Set Interface and Alternative Setting */
1571 /* Set self-powered state */
1572 if (pCnfg->bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1573 udc.feature |= STM32_UDC_FEATURE_SELFPOWERED;
1575 /* Configure all existing interfaces to alternative setting 0 */
1576 ASSERT(pCnfg->bNumInterfaces <= USB_MAX_INTERFACE);
1577 udc.interfaces = pCnfg->bNumInterfaces;
1578 for (i = 0; i < udc.interfaces; i++)
1580 usb_set_device_state(USB_STATE_CONFIGURED);
1581 usb_dev->configured = true;
1582 LOG_INFO("%s: device configured\n", __func__);
1586 usb_dev->configured = false;
1587 usb_set_device_state(USB_STATE_ADDRESS);
1592 /* USB setup packet: SET_CONFIGURATION handler */
1593 static void usb_set_config_handler(void)
1595 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1596 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1597 uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1599 LOG_INFO("%s: SET_CONFIGURATION: %d\n",
1601 if ((udc.state >= USB_STATE_ADDRESS) &&
1602 (w_index == 0) && (w_length == 0) &&
1603 (usb_set_config_state(w_value & 0xff) == 0))
1604 usb_status_handler(CTRL_ENP_OUT);
1606 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1609 /* USB setup packet: standard request handler */
1610 static void usb_standard_request_handler(void)
1612 switch (setup_packet.bRequest)
1614 case USB_REQ_GET_STATUS:
1615 usb_get_status_handler();
1617 case USB_REQ_CLEAR_FEATURE:
1618 LOG_INFO("%s: bRequest=%d (CLEAR_FEATURE)\n",
1619 __func__, setup_packet.bRequest);
1621 case USB_REQ_SET_FEATURE:
1622 LOG_INFO("%s: bRequest=%d (SET_FEATURE)\n",
1623 __func__, setup_packet.bRequest);
1625 case USB_REQ_SET_ADDRESS:
1626 usb_set_address_handler();
1628 case USB_REQ_GET_DESCRIPTOR:
1629 usb_get_descriptor_handler();
1631 case USB_REQ_SET_DESCRIPTOR:
1632 LOG_INFO("%s: bRequest=%d (SET_DESCRIPTOR)\n",
1633 __func__, setup_packet.bRequest);
1635 case USB_REQ_GET_CONFIGURATION:
1636 usb_get_config_handler();
1638 case USB_REQ_SET_CONFIGURATION:
1639 usb_set_config_handler();
1641 case USB_REQ_GET_INTERFACE:
1642 LOG_INFO("%s: bRequest=%d (GET_INTERFACE)\n",
1643 __func__, setup_packet.bRequest);
1645 case USB_REQ_SET_INTERFACE:
1646 LOG_INFO("%s: bRequest=%d (SET_INTERFACE)\n",
1647 __func__, setup_packet.bRequest);
1649 case USB_REQ_SYNCH_FRAME:
1650 LOG_INFO("%s: bRequest=%d (SYNCH_FRAME)\n",
1651 __func__, setup_packet.bRequest);
1654 LOG_WARN("%s: bRequest=%d (Unknown)\n",
1655 __func__, setup_packet.bRequest);
1656 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1661 /* USB setup packet handler */
1662 static void usb_setup_handler(void)
1664 switch (setup_packet.mRequestType & USB_TYPE_MASK)
1667 case USB_TYPE_STANDARD:
1668 LOG_INFO("%s: bmRequestType=%02x (Standard)\n",
1669 __func__, setup_packet.mRequestType);
1670 usb_standard_request_handler();
1673 case USB_TYPE_CLASS:
1674 LOG_INFO("%s: bmRequestType=%02x (Class)\n",
1675 __func__, setup_packet.mRequestType);
1676 usb_event_handler(usb_dev);
1679 case USB_TYPE_VENDOR:
1680 LOG_INFO("%s: bmRequestType=%02x (Vendor)\n",
1681 __func__, setup_packet.mRequestType);
1682 usb_event_handler(usb_dev);
1684 case USB_TYPE_RESERVED:
1685 LOG_INFO("%s: bmRequestType=%02x (Reserved)\n",
1686 __func__, setup_packet.mRequestType);
1690 LOG_WARN("%s: bmRequestType=%02x (Unknown)\n",
1691 __func__, setup_packet.mRequestType);
1692 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1697 /* USB: low-level hardware initialization */
1698 static void usb_hw_reset(void)
1703 /* Initialize endpoint descriptors */
1704 for (i = 0; i < countof(ep_cnfg); i++)
1705 ep_cnfg[i].hw = NULL;
1707 /* Initialize USB memory */
1708 for (i = 0; i < countof(memory_buffer); i++)
1709 memory_buffer[i].Size = 0;
1710 usb->BTABLE = USB_BDT_OFFSET;
1713 /* Endpoint initialization */
1714 ret = usb_ep_configure(&USB_CtrlEpDescr0, true);
1715 if (UNLIKELY(ret < 0))
1717 LOG_WARN("%s: out of memory, cannot initialize EP0\n",
1721 ret = usb_ep_configure(&USB_CtrlEpDescr1, true);
1722 if (UNLIKELY(ret < 0))
1724 LOG_WARN("%s: out of memory, cannot initialize EP1\n",
1729 /* Set default address */
1732 /* Enable all the device interrupts */
1733 usb->CNTR = bmCTRM | bmRESETM | bmSOFM | bmERRM | bmPMAOVRM |
1737 /* Handle a correct transfer under ISR */
1738 static void usb_isr_correct_transfer(stm32_usb_irq_status_t interrupt)
1741 reg32_t *pReg = (reg32_t *)&usb->EP0R;
1743 /* Find corresponding EP */
1744 pReg += interrupt.EP_ID;
1745 EP = (int)(((*pReg & 0x0f) << 1) + (interrupt.DIR ? 0 : 1));
1746 ep_cnfg[EP].avail_data = 1;
1748 ASSERT(ep_cnfg[EP].hw);
1751 ep_ctrl_clr_ctr_tx(ep_cnfg[EP].hw);
1753 ep_ctrl_clr_ctr_rx(ep_cnfg[EP].hw);
1754 if (EP == CTRL_ENP_OUT)
1756 /* Determinate type of packet (only for control EP) */
1757 bool SetupPacket = ep_ctrl_get_setup(ep_cnfg[CTRL_ENP_OUT].hw);
1761 ep_cnfg[CTRL_ENP_IN].avail_data = 1;
1762 /* init IO to receive Setup packet */
1763 __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1764 __usb_ep_read(CTRL_ENP_OUT, &setup_packet,
1765 sizeof(setup_packet), NULL);
1767 /* reset EP IO ctrl */
1768 if (setup_packet.mRequestType & USB_DIR_IN)
1769 usb_status_handler(CTRL_ENP_OUT);
1770 usb_setup_handler();
1771 if (ep_cnfg[CTRL_ENP_OUT].status == STALLED)
1772 usb_ep_set_stall_ctrl();
1776 if (ep_cnfg[CTRL_ENP_OUT].complete &&
1777 setup_packet.mRequestType & USB_DIR_IN)
1778 ep_cnfg[CTRL_ENP_OUT].complete(CTRL_ENP_OUT);
1783 else if (EP == CTRL_ENP_IN)
1785 if (ep_cnfg[CTRL_ENP_IN].complete &&
1786 !(setup_packet.mRequestType & USB_DIR_IN))
1787 ep_cnfg[CTRL_ENP_IN].complete(CTRL_ENP_IN);
1796 /* USB: interrupt service routine */
1797 static void usb_isr(void)
1799 stm32_usb_irq_status_t interrupt;
1801 /* Get masked interrupt flags */
1802 interrupt.status = usb->ISTR;
1803 interrupt.status &= usb->CNTR | 0x1f;
1805 if (interrupt.PMAOVR)
1807 LOG_WARN("%s: DMA overrun / underrun\n", __func__);
1808 usb->ISTR = ~bmPMAOVRM;
1812 LOG_WARN("%s: engine error\n", __func__);
1813 usb->ISTR = ~bmERRM;
1815 if (interrupt.RESET)
1817 LOG_INFO("%s: device reset\n", __func__);
1818 usb->ISTR = ~bmRESETM;
1820 usb_set_device_state(USB_STATE_DEFAULT);
1826 * XXX: disable logging of frame interrupts (too much noise!)
1828 uint16_t frame_nr = usb->FNR & 0x0fff;
1829 LOG_INFO("%s: frame %#x\n", __func__, frame_nr);
1831 usb->ISTR = ~bmSOFM;
1835 LOG_INFO("%s: wake-up\n", __func__);
1836 usb->ISTR = ~(bmSUSPM | bmWKUPM);
1841 LOG_INFO("%s: suspend\n", __func__);
1843 usb->ISTR = ~(bmSUSPM | bmWKUPM);
1847 LOG_INFO("%s: expected frame\n", __func__);
1848 usb->ISTR = ~bmESOFM;
1852 usb_isr_correct_transfer(interrupt);
1856 /* USB: hardware initialization */
1857 static void usb_hw_init(void)
1859 /* Enable clocking on the required GPIO pins */
1860 RCC->APB2ENR |= RCC_APB2_GPIOA | RCC_APB2_GPIOC;
1862 /* Make sure that the CAN controller is disabled and held in reset */
1863 RCC->APB1ENR &= ~RCC_APB1_CAN;
1865 /* Configure USB_DM and USB_DP to work as USB lines */
1866 stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE,
1867 USB_DM_PIN | USB_DP_PIN,
1868 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
1869 /* Configure USB_DISC to work as USB disconnect */
1870 stm32_gpioPinConfig((struct stm32_gpio *)GPIOC_BASE,
1872 GPIO_MODE_OUT_PP, GPIO_SPEED_50MHZ);
1873 stm32_gpioPinWrite((struct stm32_gpio *)GPIOC_BASE,
1876 /* Ensure the USB clock is disabled before setting the prescaler */
1877 RCC->APB1ENR &= ~RCC_APB1_USB;
1879 /* Configure USB clock (48MHz) */
1880 *CFGR_USBPRE_BB &= ~RCC_USBCLK_PLLCLK_1DIV5;
1882 /* Activate USB clock */
1883 RCC->APB1ENR |= RCC_APB1_USB;
1885 /* Force USB reset and disable USB interrupts */
1889 /* Issue a USB reset */
1892 /* Clear spurious pending interrupt */
1895 /* Register interrupt handler */
1896 sysirq_setHandler(USB_LP_CAN_RX0_IRQHANDLER, usb_isr);
1898 /* Software connection enable */
1902 /* Initialize the USB controller */
1903 static void usb_init(void)
1905 udc.state = USB_STATE_NOTATTACHED;
1911 /* Register an upper layer USB device into the driver */
1912 int usb_deviceRegister(UsbDevice *dev)
1919 while (!usb_dev->configured)