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 #define ALIGN_UP(value, align) (((value) & ((align) - 1)) ? \
62 (((value) + ((align) - 1)) & ~((align) - 1)) : \
64 /* STM32 USB registers */
75 reg32_t __reserved[8];
83 /* Hardware registers */
84 static struct stm32_usb *usb = (struct stm32_usb *)USB_BASE_ADDR;
86 /* Endpoint descriptors: used for handling requests to use with endpoints */
87 static stm32_UsbEp ep_cnfg[ENP_MAX_NUMB];
89 /* USB EP0 control descriptor */
90 static const UsbEndpointDesc USB_CtrlEpDescr0 =
92 .bLength = sizeof(USB_CtrlEpDescr0),
93 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_OUT | 0,
95 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
96 .wMaxPacketSize = USB_EP0_MAX_SIZE,
100 /* USB EP1 control descriptor */
101 static const UsbEndpointDesc USB_CtrlEpDescr1 =
103 .bLength = sizeof(USB_CtrlEpDescr1),
104 .bDescriptorType = USB_DT_ENDPOINT,
105 .bEndpointAddress = USB_DIR_IN | 0,
106 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
107 .wMaxPacketSize = USB_EP0_MAX_SIZE,
111 /* USB setup packet */
112 static UsbCtrlRequest setup_packet;
114 /* USB device controller: max supported interfaces */
115 #define USB_MAX_INTERFACE 1
117 /* USB device controller features */
118 #define STM32_UDC_FEATURE_SELFPOWERED BV(0)
119 #define STM32_UDC_FEATURE_REMOTE_WAKEUP BV(1)
121 /* Hardware-specific USB device controller structure */
122 typedef struct stm32_udc
126 const UsbConfigDesc *cfg;
128 uint32_t alt[USB_MAX_INTERFACE];
131 } PACKED stm32_udc_t;
133 /* Hardware-specific USB Device Controller */
134 static stm32_udc_t udc;
136 /* Generic USB Device Controller structure */
137 static UsbDevice *usb_dev;
139 /* USB packet memory management: list of allocated chunks */
140 static stm32_UsbMemSlot *mem_use;
142 /* USB packet memory management: memory buffer metadata */
143 #define EP_MAX_SLOTS 16
144 static stm32_UsbMemSlot memory_buffer[EP_MAX_SLOTS];
146 /* Allocate a free block of the packet memory */
147 static stm32_UsbMemSlot *usb_malloc(void)
151 for (i = 0; i < countof(memory_buffer); i++)
152 if (memory_buffer[i].Size == 0)
153 return &memory_buffer[i];
157 /* Release a block of the packet memory */
158 static void usb_free(stm32_UsbMemSlot *pPntr)
163 /* Allocate a free chunk of the packet memory (inside a block) */
164 static bool usb_alloc_buffer(uint16_t *pOffset, uint32_t *size,
167 stm32_UsbMemSlot *mem = mem_use,
168 *memNext, *mem_useNew;
169 uint32_t max_size = *size;
172 * Packet size alignment:
173 * - fine-granularity allocation: size alignment by 2;
174 * - coarse-granularity allocation: size alignment by 32.
177 max_size = ALIGN_UP(max_size, 2);
179 max_size = ALIGN_UP(max_size, 32);
181 * Finding free memory chunks from the allocated blocks of the USB
187 /* Offset alignment by 4 */
188 *pOffset = ALIGN_UP(mem->Start + mem->Size, 4);
190 if ((mem->next == NULL) ||
192 *pOffset + max_size))
196 /* Check for out-of-memory condition */
197 if ((*pOffset + max_size) >= USB_BDT_OFFSET)
200 * Allocate a new memory block, next to the last allocated block.
202 mem_useNew = usb_malloc();
203 if (mem_useNew == NULL)
205 /* Insert the block to the list of allocated blocks */
208 mem_use = mem_useNew;
209 mem_use->next = NULL;
213 mem_useNew->next = mem->next;
214 mem->next = mem_useNew;
216 /* Update block's metadata */
217 mem_useNew->ep_addr = EndPoint;
218 mem_useNew->Start = *pOffset;
219 mem_useNew->Size = max_size;
226 /* Release a chunk of the packet memory (inside a block) */
227 static void usb_free_buffer(int EndPoint)
229 stm32_UsbMemSlot *mem, *memPrev = NULL;
234 if (mem->ep_addr == EndPoint)
236 if (UNLIKELY(memPrev == NULL))
238 /* Free the first element of the list */
239 mem_use = mem_use->next;
244 memPrev->next = mem->next;
253 /*-------------------------------------------------------------------------*/
255 /* Connect USB controller */
256 static void usb_connect(void)
258 stm32_gpioPinWrite((struct stm32_gpio *)GPIOC_BASE, 1 << 11, 0);
261 /* Set USB device address */
262 static void usb_set_address(uint32_t addr)
264 usb->DADDR = addr | 0x80;
267 /* Suspend USB controller */
268 static void usb_suspend(void)
270 usb->CNTR |= bmFSUSP | bmLPMODE;
273 /* Resume USB controller */
274 static void usb_resume(void)
276 uint32_t line_status;
278 line_status = usb->FNR & 0xc000;
281 /* check for noise and eventually return to sleep */
282 if (line_status == 0xc000)
285 usb->CNTR &= ~(bmFSUSP | bmLPMODE);
288 /* Convert logical EP address to physical EP address */
289 static int usb_ep_logical_to_hw(uint8_t ep_addr)
291 int addr = (ep_addr & 0x0f) << 1;
292 return (ep_addr & 0x80) ? addr + 1 : addr;
296 static void ep_ctrl_set_ea(reg32_t *reg, uint32_t val)
299 val |= *reg & 0x0700;
300 val |= USB_CTRL_CLEAR_ONLY_MASK;
304 /* Get EP IN status */
305 static uint32_t ep_ctrl_get_stat_tx(reg32_t *reg)
307 return (*reg & (0x3UL << 4)) >> 4;
310 /* Set EP IN state */
311 static void ep_ctrl_set_stat_tx(reg32_t *reg, stm32_UsbEpState val)
317 * The EP can change state between read and write operations from VALID
318 * to NAK and result of set operation will be invalid.
320 for (i = 0; i < 2; i++)
322 if (ep_ctrl_get_stat_tx(reg) == val)
327 state |= USB_CTRL_CLEAR_ONLY_MASK;
328 /* Clear the toggle bits without STAT_TX (4,5) */
334 /* Set EP DTOG_TX bit (IN) */
335 static void ep_ctrl_set_dtog_tx(reg32_t *reg, uint32_t val)
337 val = val ? (*reg ^ (1UL << 6)) : *reg;
338 /* Clear the toggle bits without DTOG_TX (6) */
340 val |= USB_CTRL_CLEAR_ONLY_MASK;
344 /* Clear EP CTR_TX bit (IN) */
345 static void ep_ctrl_clr_ctr_tx(reg32_t *reg)
349 val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 7);
355 /* Clear EP CTR_RX bit (OUT) */
356 static void ep_ctrl_clr_ctr_rx(reg32_t *reg)
359 val &= ~(USB_CTRL_TOGGLE_MASK | 1UL << 15);
365 /* Set EP KIND bit */
366 static void ep_ctrl_set_ep_kind(reg32_t *reg, uint32_t val)
368 val = val ? (1UL << 8) : 0;
369 val |= *reg & ~(USB_CTRL_TOGGLE_MASK | (1UL << 8));
370 val |= USB_CTRL_CLEAR_ONLY_MASK;
375 static int ep_ctrl_set_ep_type(reg32_t *reg, uint8_t val)
379 if (UNLIKELY(val >= EP_TYPE_MAX))
382 return USB_INVAL_ERROR;
386 type |= *reg & ~(USB_CTRL_TOGGLE_MASK | (0x3UL << 9));
387 type |= USB_CTRL_CLEAR_ONLY_MASK;
393 /* Get EP STAT_RX (OUT) */
394 static uint32_t ep_ctrl_get_stat_rx(reg32_t *reg)
396 uint32_t val = *reg & (0x3UL << 12);
400 /* Set EP STAT_RX (OUT) */
401 static void ep_ctrl_set_stat_rx(reg32_t *reg, stm32_UsbEpState val)
407 * The EP can change state between read and write operations from VALID
408 * to NAK and result of set operation will be invalid.
410 for (i = 0; i < 2; i++)
412 if (ep_ctrl_get_stat_rx(reg) == val)
417 state |= USB_CTRL_CLEAR_ONLY_MASK;
418 /* Clear the toggle bits without STAT_RX (12,13) */
424 /* Set DTOG_RX bit */
425 static void ep_ctrl_set_dtog_rx(reg32_t *reg, uint32_t val)
427 val = val ? (*reg ^ (1UL << 14)) : *reg;
428 /* Clear the toggle bits without DTOG_RX (14) */
430 val |= USB_CTRL_CLEAR_ONLY_MASK;
434 /* Get EP SETUP bit */
435 static uint32_t ep_ctrl_get_setup(reg32_t *reg)
437 uint32_t val = *reg & (1UL << 11);
441 /* Core endpoint I/O function */
442 static void __usb_ep_io(int EP)
444 ssize_t Count, CountHold, Offset;
445 uint32_t *pDst, *pSrc, Data;
446 stm32_UsbEp *epd = &ep_cnfg[EP];
448 if (UNLIKELY(epd->hw == NULL))
450 LOG_ERR("%s: invalid endpoint (EP%d-%s)\n",
453 (EP & 0x01) ? "IN" : "OUT");
457 if (epd->status != BEGIN_SERVICED && epd->status != NO_SERVICED)
463 Count = epd->size - epd->offset;
464 while (epd->avail_data)
466 if (!Count && !(epd->flags & STM32_USB_EP_ZERO_PACKET))
470 epd->status = BEGIN_SERVICED;
472 if ((epd->flags & STM32_USB_EP_ZERO_PACKET) &&
473 (Count == epd->max_size))
474 epd->flags |= STM32_USB_EP_ZERO_PACKET |
475 STM32_USB_EP_ZERO_POSSIBLE;
477 CountHold = Count = MIN(Count, epd->max_size);
479 epd->flags |= STM32_USB_EP_ZERO_PACKET;
480 Offset = epd->offset;
481 epd->offset += Count;
484 case USB_ENDPOINT_XFER_CONTROL:
485 case USB_ENDPOINT_XFER_INT:
486 pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET));
488 case USB_ENDPOINT_XFER_BULK:
489 pDst = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_TX_OFFSET));
491 case USB_ENDPOINT_XFER_ISOC:
492 LOG_ERR("%s: isochronous transfer not supported\n",
494 /* Fallback to default */
500 /* Write data to packet memory buffer */
503 Data = *(epd->write_buffer + Offset++);
506 Data |= (uint32_t)(*(epd->write_buffer + Offset++)) << 8;
512 EP_DTB_WRITE(EP >> 1, COUNT_TX_OFFSET, CountHold);
513 ep_ctrl_set_stat_tx(epd->hw, EP_VALID);
515 --ep_cnfg[EP].avail_data;
516 Count = epd->size - epd->offset;
518 if (!Count && !(epd->flags & STM32_USB_EP_ZERO_PACKET))
520 epd->status = COMPLETE;
521 /* call callback function */
529 while (epd->avail_data)
531 /* Get data size and buffer pointer */
534 case USB_ENDPOINT_XFER_CONTROL:
535 case USB_ENDPOINT_XFER_INT:
536 /* Get received bytes number */
537 Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF;
538 /* Get address of the USB packet buffer for corresponding EP */
539 pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET));
541 case USB_ENDPOINT_XFER_BULK:
542 /* Get received bytes number */
543 Count = EP_DTB_READ(EP >> 1, COUNT_RX_OFFSET) & 0x3FF;
544 /* Get address of the USB packet buffer for corresponding EP */
545 pSrc = (uint32_t *)USB_MEM_ADDR(EP_DTB_READ(EP >> 1, ADDR_RX_OFFSET));
547 case USB_ENDPOINT_XFER_ISOC:
548 LOG_ERR("%s: isochronous transfer not supported\n",
550 /* Fallback to default */
556 if (Count > (epd->size - epd->offset))
558 epd->status = BUFFER_OVERRUN;
559 epd->size = ep_cnfg[EP].offset;
562 else if (Count < ep_cnfg[EP].max_size)
564 epd->status = BUFFER_UNDERRUN;
565 epd->size = ep_cnfg[EP].offset + Count;
568 epd->status = BEGIN_SERVICED;
570 Offset = epd->offset;
571 epd->offset += Count;
573 /* Read data from packet memory buffer */
577 *(epd->read_buffer + Offset++) = Data;
581 *(epd->read_buffer + Offset++) = Data;
586 ep_ctrl_set_stat_rx(epd->hw, EP_VALID);
588 --ep_cnfg[EP].avail_data;
590 if (*epd->hw & (1UL << 11))
592 ep_cnfg[EP].status = SETUP_OVERWRITE;
595 if (!(Count = (epd->size - epd->offset)))
597 epd->status = COMPLETE;
601 if (epd->status != BEGIN_SERVICED && epd->status != NO_SERVICED)
603 /* call callback function */
611 * Return the lower value from Host expected size and size and set a flag
612 * STM32_USB_EP_ZERO_POSSIBLE when size is lower that host expected size.
614 static size_t usb_size(size_t size, size_t host_size)
616 if (size < host_size)
618 ep_cnfg[CTRL_ENP_IN].flags |= STM32_USB_EP_ZERO_POSSIBLE;
624 /* Configure an EP descriptor before performing a I/O operation */
625 #define USB_EP_IO(__EP, __op, __buf, __size, __complete) \
628 stm32_UsbIoStatus ret; \
630 /* Fill EP descriptor */ \
631 IRQ_SAVE_DISABLE(flags); \
634 ep_cnfg[__EP].status = NOT_READY; \
635 ep_cnfg[__EP].complete = NULL; \
639 if (ep_cnfg[__EP].status == BEGIN_SERVICED) \
645 * NOTE: the write_buffer and read_buffer are actually the \
646 * same location in memory (it's a union). \
648 * We have to do this trick to silent a build warning by \
649 * casting the I/O buffer to (void *) or (const void *). \
651 ep_cnfg[__EP].__op ## _buffer = __buf; \
652 ep_cnfg[__EP].offset = 0; \
653 ep_cnfg[__EP].size = __size; \
654 ep_cnfg[__EP].complete = __complete; \
656 ep_cnfg[__EP].flags = STM32_USB_EP_ZERO_PACKET; \
658 ep_cnfg[__EP].flags = 0; \
659 ep_cnfg[__EP].status = NO_SERVICED; \
661 /* Perform the I/O operation */ \
664 ret = ep_cnfg[__EP].status; \
666 IRQ_RESTORE(flags); \
670 /* Configure and endponint and perform a read operation */
671 static stm32_UsbIoStatus
672 __usb_ep_read(int ep, void *buffer, ssize_t size, void (*complete)(int))
674 if (UNLIKELY(ep >= ENP_MAX_NUMB))
679 ASSERT(!(ep & 0x01));
680 return USB_EP_IO(ep, read, buffer, size, complete);
683 /* Configure and endponint and perform a write operation */
684 static stm32_UsbIoStatus
685 __usb_ep_write(int ep, const void *buffer, ssize_t size, void (*complete)(int))
687 if (UNLIKELY(ep >= ENP_MAX_NUMB))
693 return USB_EP_IO(ep, write, buffer, size, complete);
696 static void usb_ep_low_level_config(int ep, uint16_t offset, uint16_t size)
698 stm32_UsbEp *epc = &ep_cnfg[ep];
704 ep_ctrl_set_stat_tx(epc->hw, EP_DISABLED);
705 /* Clear Tx toggle */
706 ep_ctrl_set_dtog_tx(epc->hw, 0);
707 /* Clear Correct Transfer for transmission flag */
708 ep_ctrl_clr_ctr_tx(epc->hw);
710 /* Update EP description table */
711 EP_DTB_WRITE(ep >> 1, ADDR_TX_OFFSET, offset);
712 EP_DTB_WRITE(ep >> 1, COUNT_TX_OFFSET, 0);
717 uint16_t rx_count = 0;
720 ep_ctrl_set_stat_rx(epc->hw, EP_DISABLED);
721 /* Clear Rx toggle */
722 ep_ctrl_set_dtog_rx(epc->hw, 0);
723 /* Clear Correct Transfer for reception flag */
724 ep_ctrl_clr_ctr_rx(epc->hw);
725 /* Descriptor block size field */
726 rx_count |= (size > 62) << 15;
727 /* Descriptor number of blocks field */
728 rx_count |= (((size > 62) ? (size >> 5) - 1 : size >> 1) &
730 /* Update EP description table */
731 EP_DTB_WRITE(ep >> 1, ADDR_RX_OFFSET, offset);
732 EP_DTB_WRITE(ep >> 1, COUNT_RX_OFFSET, rx_count);
736 /* Enable/Disable an endpoint */
737 static int usb_ep_configure(const UsbEndpointDesc *epd, bool enable)
745 EP = usb_ep_logical_to_hw(epd->bEndpointAddress);
746 ep_hw = &ep_cnfg[EP];
751 * Allocate packet memory for EP buffer/s calculate actual size
752 * only for the OUT EPs.
754 size = epd->wMaxPacketSize;
755 if (!usb_alloc_buffer(&Offset, &size, EP))
756 return -USB_MEMORY_FULL;
759 ep_hw->status = NOT_READY;
763 /* Set endpoint type */
764 ep_hw->type = usb_endpointType(epd);
765 /* Init EP max packet size */
766 ep_hw->max_size = epd->wMaxPacketSize;
769 ep_hw->avail_data = 1;
771 ep_hw->avail_data = 0;
772 hw = (reg32_t *)&usb->EP0R;
776 ep_ctrl_set_ea(hw, EP >> 1);
778 LOG_INFO("%s: EP%d-%s configured\n",
779 __func__, EP >> 1, EP & 1 ? "IN" : "OUT");
781 /* Low-level endpoint configuration */
782 usb_ep_low_level_config(EP, Offset, size);
784 /* Set EP Kind & enable */
787 case USB_ENDPOINT_XFER_CONTROL:
788 LOG_INFO("EP%d: CONTROL IN\n", EP >> 1);
789 ep_ctrl_set_ep_type(hw, EP_CTRL);
790 ep_ctrl_set_ep_kind(hw, 0);
792 case USB_ENDPOINT_XFER_INT:
793 LOG_INFO("EP%d: INTERRUPT IN\n", EP >> 1);
794 ep_ctrl_set_ep_type(hw, EP_INTERRUPT);
795 ep_ctrl_set_ep_kind(hw, 0);
797 case USB_ENDPOINT_XFER_BULK:
798 LOG_INFO("EP%d: BULK IN\n", EP >> 1);
799 ep_ctrl_set_ep_type(hw, EP_BULK);
800 ep_ctrl_set_ep_kind(hw, 0);
802 case USB_ENDPOINT_XFER_ISOC:
803 LOG_ERR("EP%d: ISOCHRONOUS IN: not supported\n", EP >> 1);
804 /* Fallback to default */
807 return -USB_NODEV_ERROR;
812 ep_ctrl_set_stat_tx(hw, EP_NAK);
813 /* Clear Correct Transfer for transmission flag */
814 ep_ctrl_clr_ctr_tx(hw);
819 ep_ctrl_set_stat_rx(hw, EP_VALID);
822 else if (ep_cnfg[EP].hw)
824 hw = (reg32_t *)&usb->EP0R;
831 ep_ctrl_set_stat_tx(hw, EP_DISABLED);
832 /* Clear Correct Transfer for reception flag */
833 ep_ctrl_clr_ctr_tx(hw);
839 ep_ctrl_set_stat_rx(hw, EP_DISABLED);
840 /* Clear Correct Transfer for reception flag */
841 ep_ctrl_clr_ctr_rx(hw);
845 ep_cnfg[EP].hw = NULL;
850 /* Get EP stall/unstall */
851 static int usb_ep_get_stall(int EP, bool *pStall)
853 if (ep_cnfg[EP].hw == NULL)
854 return -USB_NODEV_ERROR;
856 *pStall = (EP & 0x01) ?
857 (ep_ctrl_get_stat_tx(ep_cnfg[EP].hw) == EP_STALL): /* IN EP */
858 (ep_ctrl_get_stat_rx(ep_cnfg[EP].hw) == EP_STALL); /* OUT EP */
863 /* Set EP stall/unstall */
864 static int usb_ep_set_stall(int EP, bool Stall)
866 if (ep_cnfg[EP].hw == NULL)
867 return -USB_NODEV_ERROR;
871 ep_cnfg[EP].status = STALLED;
875 ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_STALL);
876 ep_cnfg[EP].avail_data = 1;
881 ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_STALL);
882 ep_cnfg[EP].avail_data = 0;
887 ep_cnfg[EP].status = NOT_READY;
891 ep_cnfg[EP].avail_data = 1;
892 /* reset Data Toggle bit */
893 ep_ctrl_set_dtog_tx(ep_cnfg[EP].hw, 0);
894 ep_ctrl_set_stat_tx(ep_cnfg[EP].hw, EP_NAK);
899 ep_cnfg[EP].avail_data = 0;
900 /* reset Data Toggle bit */
901 ep_ctrl_set_dtog_rx(ep_cnfg[EP].hw, 0);
902 ep_ctrl_set_stat_rx(ep_cnfg[EP].hw, EP_VALID);
908 /* Stall both directions of the control EP */
909 static void usb_ep_set_stall_ctrl(void)
911 ep_cnfg[CTRL_ENP_IN].avail_data = 1;
912 ep_cnfg[CTRL_ENP_IN].status = STALLED;
913 ep_cnfg[CTRL_ENP_OUT].avail_data = 0;
914 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
916 usb_ep_set_stall(CTRL_ENP_IN, true);
917 usb_ep_set_stall(CTRL_ENP_OUT, true);
921 * Find the position of an interface descriptor inside the configuration
924 static int usb_find_interface(uint32_t num, uint32_t alt)
926 const UsbInterfaceDesc *id;
931 /* TODO: support more than one configuration per device */
932 id = (const UsbInterfaceDesc *)usb_dev->config[i];
935 if (id->bDescriptorType != USB_DT_INTERFACE)
937 if ((id->bInterfaceNumber == num) &&
938 (id->bAlternateSetting == alt))
941 return -USB_NODEV_ERROR;
945 * Configure/deconfigure EPs of a certain interface.
948 usb_configure_ep_interface(unsigned int num, unsigned int alt, bool enable)
950 const UsbEndpointDesc *epd;
954 * Find the position of the interface descriptor (inside the
955 * configuration descriptor).
957 start = usb_find_interface(num, alt);
960 LOG_ERR("%s: interface (%u,%u) not found\n",
965 * Cycle over endpoint descriptors.
967 * NOTE: the first endpoint descriptor is placed next to the interface
968 * descriptor, so we need to add +1 to the position of the interface
969 * descriptor to find it.
971 for (i = start + 1; ; i++)
973 epd = (const UsbEndpointDesc *)usb_dev->config[i];
974 if ((epd == NULL) || (epd->bDescriptorType == USB_DT_INTERFACE))
976 if (epd->bDescriptorType != USB_DT_ENDPOINT)
978 if (UNLIKELY(usb_ep_configure(epd, enable) < 0))
980 LOG_ERR("%s: out of memory, can't initialize EP\n",
987 /* Set device state */
988 static void usb_set_device_state(int state)
992 LOG_INFO("%s: new state %d\n", __func__, state);
994 if (udc.state == USB_STATE_CONFIGURED)
996 /* Deconfigure device */
997 for (i = 0; i < udc.interfaces; ++i)
998 usb_configure_ep_interface(i,
1003 case USB_STATE_ATTACHED:
1004 case USB_STATE_POWERED:
1005 case USB_STATE_DEFAULT:
1007 usb_dev->configured = false;
1008 udc.address = udc.cfg_id = 0;
1010 case USB_STATE_ADDRESS:
1013 case USB_STATE_CONFIGURED:
1014 /* Configure device */
1015 for (i = 0; i < udc.interfaces; ++i)
1016 usb_configure_ep_interface(i,
1020 /* Unknown state: disconnected or connection in progress */
1021 usb_dev->configured = false;
1029 /* Setup packet: set address status phase end handler */
1030 static void usb_add_status_handler_end(UNUSED_ARG(int, EP))
1034 w_value = usb_le16_to_cpu(setup_packet.wValue);
1035 udc.address = w_value & 0xff;
1036 usb_set_address(udc.address);
1039 usb_set_device_state(USB_STATE_ADDRESS);
1041 usb_set_device_state(USB_STATE_DEFAULT);
1043 __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1044 __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL);
1047 /* Prepare status phase */
1048 static void usb_status_phase(bool in)
1051 __usb_ep_write(CTRL_ENP_IN, NULL, 0, NULL);
1054 /* Setup packet: status phase end handler */
1055 static void usb_status_handler_end(UNUSED_ARG(int, EP))
1057 __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1058 __usb_ep_read(CTRL_ENP_OUT, NULL, -1, NULL);
1061 /* Address status handler */
1062 static void usb_status_handler(UNUSED_ARG(int, EP))
1064 if (setup_packet.mRequestType & USB_DIR_IN)
1066 usb_status_phase(false);
1067 ep_cnfg[CTRL_ENP_OUT].complete = usb_status_handler_end;
1071 usb_status_phase(true);
1072 ep_cnfg[CTRL_ENP_IN].complete =
1073 (setup_packet.bRequest == USB_REQ_SET_ADDRESS) ?
1074 usb_add_status_handler_end :
1075 usb_status_handler_end;
1079 static bool rx_done;
1080 static size_t rx_size;
1082 static void usb_endpointRead_complete(int ep)
1084 if (UNLIKELY(ep >= ENP_MAX_NUMB))
1089 ASSERT(!(ep & 0x01));
1092 rx_size = ep_cnfg[ep].size;
1095 ssize_t usb_endpointRead(int ep, void *buffer, ssize_t size)
1097 int ep_num = usb_ep_logical_to_hw(ep);
1099 if (UNLIKELY((size_t)buffer & 0x03))
1101 LOG_ERR("unaligned buffer @ %p\n", buffer);
1105 /* Non-blocking read for EP0 */
1106 if (ep_num == CTRL_ENP_OUT)
1108 size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
1110 usb_status_handler(ep_num);
1112 __usb_ep_read(ep_num, buffer, size,
1113 usb_status_handler);
1116 if (UNLIKELY(!size))
1118 size = MIN(size, USB_RX_MAX_SIZE);
1123 __usb_ep_read(ep_num, buffer, size, usb_endpointRead_complete);
1130 static bool tx_done;
1131 static size_t tx_size;
1133 static void usb_endpointWrite_complete(int ep)
1135 if (UNLIKELY(ep >= ENP_MAX_NUMB))
1143 tx_size = ep_cnfg[ep].size;
1146 ssize_t usb_endpointWrite(int ep, const void *buffer, ssize_t size)
1148 int ep_num = usb_ep_logical_to_hw(ep);
1150 if (UNLIKELY((size_t)buffer & 0x03))
1152 LOG_ERR("unaligned buffer @ %p\n", buffer);
1156 /* Non-blocking write for EP0 */
1157 if (ep_num == CTRL_ENP_IN)
1159 size = usb_size(size, usb_le16_to_cpu(setup_packet.wLength));
1161 usb_status_handler(ep_num);
1163 __usb_ep_write(ep_num, buffer, size,
1164 usb_status_handler);
1167 if (UNLIKELY(!size))
1169 size = MIN(size, USB_TX_MAX_SIZE);
1173 /* Blocking write */
1174 __usb_ep_write(ep_num, buffer, size, usb_endpointWrite_complete);
1181 /* Global variable to handle the following non-blocking I/O operations */
1182 static uint32_t InData;
1184 /* Get device status */
1185 static int usb_send_device_status(uint16_t index)
1188 return -USB_NODEV_ERROR;
1190 InData = ((uint32_t)udc.feature) & 0xff;
1191 __usb_ep_write(CTRL_ENP_IN,
1192 (uint8_t *)&InData, sizeof(uint16_t),
1193 usb_status_handler);
1197 /* Get interface status */
1198 static int usb_send_interface_status(UNUSED_ARG(uint16_t, index))
1201 __usb_ep_write(CTRL_ENP_IN,
1202 (uint8_t *)&InData, sizeof(uint16_t),
1203 usb_status_handler);
1207 /* Get endpoint status */
1208 static int usb_send_ep_status(uint16_t index)
1210 if ((index & 0x7F) > 16)
1211 return -USB_NODEV_ERROR;
1214 usb_ep_get_stall(usb_ep_logical_to_hw(index), (bool *)&InData);
1215 __usb_ep_write(CTRL_ENP_IN,
1216 (uint8_t *)&InData, sizeof(uint16_t),
1217 usb_status_handler);
1221 /* USB setup packet: GET_STATUS request handler */
1222 static void usb_get_status_handler(void)
1224 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1225 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1226 uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1228 /* GET_STATUS sanity checks */
1229 if (udc.state < USB_STATE_ADDRESS)
1231 LOG_WARN("%s: bad GET_STATUS request (State=%02x)\n",
1232 __func__, udc.state);
1233 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1238 LOG_WARN("%s: bad GET_STATUS request (wLength.Word=%02x)\n",
1239 __func__, w_length);
1240 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1243 if (!(setup_packet.mRequestType & USB_DIR_IN))
1245 LOG_WARN("%s: bad GET_STATUS request (mRequestType=%02x)\n",
1246 __func__, setup_packet.mRequestType);
1247 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1252 LOG_WARN("%s: bad GET_STATUS request (wValue=%02x)\n",
1254 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1258 /* Process GET_STATUS request */
1259 switch (setup_packet.mRequestType & USB_RECIP_MASK)
1261 case USB_RECIP_DEVICE:
1262 if (usb_send_device_status(w_index) < 0)
1264 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientDevice\n",
1266 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1269 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientDevice)\n",
1270 __func__, setup_packet.mRequestType);
1272 case USB_RECIP_INTERFACE:
1273 if (usb_send_interface_status(w_index) < 0)
1275 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientInterface\n",
1277 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1280 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientInterface)\n",
1281 __func__, setup_packet.mRequestType);
1283 case USB_RECIP_ENDPOINT:
1284 if (usb_send_ep_status(w_index) < 0)
1286 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n",
1288 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1291 LOG_INFO("%s: GET_STATUS: mRequestType=%02x (UsbRecipientEndpoint)\n",
1292 __func__, setup_packet.mRequestType);
1295 LOG_WARN("%s: GET_STATUS: invalid UsbRecipientEndpoint\n",
1297 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1302 static int usb_get_device_descriptor(int id)
1305 return -USB_NODEV_ERROR;
1307 usb_dev->device->bMaxPacketSize0 = USB_EP0_MAX_SIZE;
1308 __usb_ep_write(CTRL_ENP_IN, (const uint8_t *)usb_dev->device,
1309 usb_size(usb_dev->device->bLength,
1310 usb_le16_to_cpu(setup_packet.wLength)),
1311 usb_status_handler);
1316 * TODO: refactor this part to remove this temporary buffer.
1318 * It would be better to define all the USB descriptors in the right order and
1319 * send them as a contiguous buffer directly from the flash / rodata memory.
1321 #define USB_BUFSIZE (128)
1322 static uint8_t usb_cfg_buffer[USB_BUFSIZE];
1323 STATIC_ASSERT(USB_BUFSIZE < (1 << (sizeof(uint16_t) * 8)));
1325 static int usb_get_configuration_descriptor(int id)
1327 const UsbConfigDesc **config =
1328 (const UsbConfigDesc **)usb_dev->config;
1329 uint8_t *p = usb_cfg_buffer;
1332 /* TODO: support more than one configuration per device */
1333 if (UNLIKELY(id > 0))
1334 return -USB_NODEV_ERROR;
1336 for (i = 0; config[i]; i++)
1338 memcpy(p, config[i], config[i]->bLength);
1339 p += config[i]->bLength;
1341 if (UNLIKELY((p - usb_cfg_buffer) > USB_BUFSIZE))
1344 return -USB_BUF_OVERFLOW;
1347 ((UsbConfigDesc *)usb_cfg_buffer)->wTotalLength =
1348 usb_cpu_to_le16((uint16_t)(p - usb_cfg_buffer));
1349 __usb_ep_write(CTRL_ENP_IN,
1351 usb_size(p - usb_cfg_buffer,
1352 usb_le16_to_cpu(setup_packet.wLength)),
1353 usb_status_handler);
1357 static int usb_get_string_descriptor(unsigned int id)
1359 const UsbStringDesc *lang_str;
1360 unsigned int lang_id, str_id;
1361 uint16_t w_index_lo = usb_le16_to_cpu(setup_packet.wIndex) & 0x00ff;
1362 uint16_t w_index_hi = (usb_le16_to_cpu(setup_packet.wIndex) &
1365 ASSERT(usb_dev->strings != NULL);
1366 ASSERT(usb_dev->strings[0] != NULL);
1368 lang_str = usb_dev->strings[0];
1371 /* Find Language index */
1372 for (lang_id = 0; ; lang_id++)
1374 const UsbStringDesc *str =
1375 usb_dev->strings[lang_id];
1376 if (UNLIKELY(str == NULL))
1377 return -USB_NODEV_ERROR;
1378 if ((str->data[0] == w_index_lo) &&
1379 (str->data[1] == w_index_hi))
1382 /* Check buffer overflow to find string index */
1383 for (str_id = 0; str_id < id; str_id++)
1385 lang_str = usb_dev->strings[lang_id + 1 + str_id];
1386 if (lang_str == NULL)
1387 return -USB_NODEV_ERROR;
1390 __usb_ep_write(CTRL_ENP_IN,
1392 usb_size(lang_str->bLength,
1393 usb_le16_to_cpu(setup_packet.wLength)),
1394 usb_status_handler);
1398 static void usb_get_descriptor(void)
1400 uint16_t w_value_lo = usb_le16_to_cpu(setup_packet.wValue) & 0x00ff;
1401 uint16_t w_value_hi = (usb_le16_to_cpu(setup_packet.wValue) & 0xff00) >> 8;
1403 if (udc.state < USB_STATE_DEFAULT)
1405 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1411 LOG_INFO("%s: GET_DEVICE_DESCRIPTOR: id=%d, state=%d\n",
1415 if (usb_get_device_descriptor(w_value_lo) < 0)
1416 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1419 LOG_INFO("%s: GET_CONFIG_DESCRIPTOR: id=%d, state=%d\n",
1420 __func__, w_value_lo, udc.state);
1421 if (usb_get_configuration_descriptor(w_value_lo) < 0)
1422 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1425 LOG_INFO("%s: GET_STRING_DESCRIPTOR: id=%d, state=%d\n",
1426 __func__, w_value_lo, udc.state);
1427 if (usb_get_string_descriptor(w_value_lo) < 0)
1428 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1431 LOG_WARN("%s: GET_UNKNOWN_DESCRIPTOR: id=%d, state=%d\n",
1432 __func__, w_value_lo, udc.state);
1433 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1438 /* USB setup packet: class/vendor request handler */
1439 static void usb_event_handler(UsbDevice *dev)
1442 * TODO: get the appropriate usb_dev in function of the endpoint
1446 dev->event_cb(&setup_packet);
1449 /* USB setup packet: GET_DESCRIPTOR handler */
1450 static void usb_get_descriptor_handler(void)
1452 LOG_INFO("%s: GET_DESCRIPTOR: RECIP = %d\n",
1454 setup_packet.mRequestType & USB_RECIP_MASK);
1455 if ((setup_packet.mRequestType & USB_RECIP_MASK) ==
1457 usb_get_descriptor();
1458 /* Getting descriptor for a device is a standard request */
1459 else if ((setup_packet.mRequestType & USB_DIR_MASK) == USB_DIR_IN)
1460 usb_event_handler(usb_dev);
1462 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1465 /* USB setup packet: SET_ADDRESS handler */
1466 static void usb_set_address_handler(void)
1468 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1469 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1470 uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1472 LOG_INFO("%s: SET_ADDRESS: %d\n",
1473 __func__, usb_le16_to_cpu(setup_packet.wValue));
1474 if ((udc.state >= USB_STATE_DEFAULT) &&
1475 ((setup_packet.mRequestType & USB_RECIP_MASK) ==
1476 USB_RECIP_DEVICE) &&
1477 (w_index == 0) && (w_length == 0) && (w_value < 128))
1478 usb_status_handler(CTRL_ENP_IN);
1480 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1483 /* USB setup packet: GET_CONFIGURATION handler */
1484 static void usb_get_config_handler(void)
1486 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1487 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1489 LOG_INFO("%s: GET_CONFIGURATION\n", __func__);
1490 if ((udc.state >= USB_STATE_ADDRESS) &&
1491 (w_value == 0) && (w_index == 0) && (w_value == 1))
1493 InData = udc.cfg_id;
1494 __usb_ep_write(CTRL_ENP_IN, (uint8_t *)&InData, 1, usb_status_handler);
1497 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1500 static const UsbConfigDesc *usb_find_configuration(int num)
1502 const UsbConfigDesc *cfg;
1507 cfg = (const UsbConfigDesc *)usb_dev->config[i];
1510 if (cfg->bDescriptorType != USB_DT_CONFIG)
1512 if (cfg->bConfigurationValue == num)
1518 static int usb_set_config_state(uint32_t conf)
1520 const UsbConfigDesc *pCnfg;
1525 /* Find configuration descriptor */
1526 pCnfg = usb_find_configuration(conf);
1528 return -USB_NODEV_ERROR;
1530 /* Reset current configuration */
1531 usb_set_device_state(USB_STATE_ADDRESS);
1532 usb_dev->configured = false;
1535 /* Set Interface and Alternative Setting */
1537 /* Set self-powered state */
1538 if (pCnfg->bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1539 udc.feature |= STM32_UDC_FEATURE_SELFPOWERED;
1541 /* Configure all existing interfaces to alternative setting 0 */
1542 ASSERT(pCnfg->bNumInterfaces <= USB_MAX_INTERFACE);
1543 udc.interfaces = pCnfg->bNumInterfaces;
1544 for (i = 0; i < udc.interfaces; i++)
1546 usb_set_device_state(USB_STATE_CONFIGURED);
1547 usb_dev->configured = true;
1551 usb_dev->configured = false;
1552 usb_set_device_state(USB_STATE_ADDRESS);
1557 /* USB setup packet: SET_CONFIGURATION handler */
1558 static void usb_set_config_handler(void)
1560 uint16_t w_value = usb_le16_to_cpu(setup_packet.wValue);
1561 uint16_t w_index = usb_le16_to_cpu(setup_packet.wIndex);
1562 uint16_t w_length = usb_le16_to_cpu(setup_packet.wLength);
1564 LOG_INFO("%s: SET_CONFIGURATION: %d\n",
1566 if ((udc.state >= USB_STATE_ADDRESS) &&
1567 (w_index == 0) && (w_length == 0) &&
1568 (usb_set_config_state(w_value & 0xff) == 0))
1569 usb_status_handler(CTRL_ENP_OUT);
1571 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1574 /* USB setup packet: standard request handler */
1575 static void usb_standard_request_handler(void)
1577 switch (setup_packet.bRequest)
1579 case USB_REQ_GET_STATUS:
1580 usb_get_status_handler();
1582 case USB_REQ_CLEAR_FEATURE:
1583 LOG_INFO("%s: bRequest=%d (CLEAR_FEATURE)\n",
1584 __func__, setup_packet.bRequest);
1586 case USB_REQ_SET_FEATURE:
1587 LOG_INFO("%s: bRequest=%d (SET_FEATURE)\n",
1588 __func__, setup_packet.bRequest);
1590 case USB_REQ_SET_ADDRESS:
1591 usb_set_address_handler();
1593 case USB_REQ_GET_DESCRIPTOR:
1594 usb_get_descriptor_handler();
1596 case USB_REQ_SET_DESCRIPTOR:
1597 LOG_INFO("%s: bRequest=%d (SET_DESCRIPTOR)\n",
1598 __func__, setup_packet.bRequest);
1600 case USB_REQ_GET_CONFIGURATION:
1601 usb_get_config_handler();
1603 case USB_REQ_SET_CONFIGURATION:
1604 usb_set_config_handler();
1606 case USB_REQ_GET_INTERFACE:
1607 LOG_INFO("%s: bRequest=%d (GET_INTERFACE)\n",
1608 __func__, setup_packet.bRequest);
1610 case USB_REQ_SET_INTERFACE:
1611 LOG_INFO("%s: bRequest=%d (SET_INTERFACE)\n",
1612 __func__, setup_packet.bRequest);
1614 case USB_REQ_SYNCH_FRAME:
1615 LOG_INFO("%s: bRequest=%d (SYNCH_FRAME)\n",
1616 __func__, setup_packet.bRequest);
1619 LOG_WARN("%s: bRequest=%d (Unknown)\n",
1620 __func__, setup_packet.bRequest);
1621 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1626 /* USB setup packet handler */
1627 static void usb_setup_handler(void)
1629 switch (setup_packet.mRequestType & USB_TYPE_MASK)
1632 case USB_TYPE_STANDARD:
1633 LOG_INFO("%s: bmRequestType=%02x (Standard)\n",
1634 __func__, setup_packet.mRequestType);
1635 usb_standard_request_handler();
1638 case USB_TYPE_CLASS:
1639 LOG_INFO("%s: bmRequestType=%02x (Class)\n",
1640 __func__, setup_packet.mRequestType);
1641 usb_event_handler(usb_dev);
1644 case USB_TYPE_VENDOR:
1645 LOG_INFO("%s: bmRequestType=%02x (Vendor)\n",
1646 __func__, setup_packet.mRequestType);
1647 usb_event_handler(usb_dev);
1649 case USB_TYPE_RESERVED:
1650 LOG_INFO("%s: bmRequestType=%02x (Reserved)\n",
1651 __func__, setup_packet.mRequestType);
1655 LOG_WARN("%s: bmRequestType=%02x (Unknown)\n",
1656 __func__, setup_packet.mRequestType);
1657 ep_cnfg[CTRL_ENP_OUT].status = STALLED;
1662 /* USB: low-level hardware initialization */
1663 static void usb_hw_reset(void)
1668 /* Initialize endpoint descriptors */
1669 for (i = 0; i < countof(ep_cnfg); i++)
1670 ep_cnfg[i].hw = NULL;
1672 /* Initialize USB memory */
1673 for (i = 0; i < countof(memory_buffer); i++)
1674 memory_buffer[i].Size = 0;
1675 usb->BTABLE = USB_BDT_OFFSET;
1678 /* Endpoint initialization */
1679 ret = usb_ep_configure(&USB_CtrlEpDescr0, true);
1680 if (UNLIKELY(ret < 0))
1682 LOG_WARN("%s: out of memory, cannot initialize EP0\n",
1686 ret = usb_ep_configure(&USB_CtrlEpDescr1, true);
1687 if (UNLIKELY(ret < 0))
1689 LOG_WARN("%s: out of memory, cannot initialize EP1\n",
1694 /* Set default address */
1697 /* Enable all the device interrupts */
1698 usb->CNTR = bmCTRM | bmRESETM | bmSOFM | bmERRM | bmPMAOVRM |
1702 /* Handle a correct transfer under ISR */
1703 static void usb_isr_correct_transfer(stm32_usb_irq_status_t interrupt)
1706 reg32_t *pReg = (reg32_t *)&usb->EP0R;
1708 /* Find corresponding EP */
1709 pReg += interrupt.EP_ID;
1710 EP = (int)(((*pReg & 0x0f) << 1) + (interrupt.DIR ? 0 : 1));
1711 ep_cnfg[EP].avail_data = 1;
1713 ASSERT(ep_cnfg[EP].hw);
1716 ep_ctrl_clr_ctr_tx(ep_cnfg[EP].hw);
1718 ep_ctrl_clr_ctr_rx(ep_cnfg[EP].hw);
1719 if (EP == CTRL_ENP_OUT)
1721 /* Determinate type of packet (only for control EP) */
1722 bool SetupPacket = ep_ctrl_get_setup(ep_cnfg[CTRL_ENP_OUT].hw);
1726 ep_cnfg[CTRL_ENP_IN].avail_data = 1;
1727 /* init IO to receive Setup packet */
1728 __usb_ep_write(CTRL_ENP_IN, NULL, -1, NULL);
1729 __usb_ep_read(CTRL_ENP_OUT, &setup_packet,
1730 sizeof(setup_packet), NULL);
1732 /* reset EP IO ctrl */
1733 if (setup_packet.mRequestType & USB_DIR_IN)
1734 usb_status_handler(CTRL_ENP_OUT);
1735 usb_setup_handler();
1736 if (ep_cnfg[CTRL_ENP_OUT].status == STALLED)
1737 usb_ep_set_stall_ctrl();
1741 if (ep_cnfg[CTRL_ENP_OUT].complete &&
1742 setup_packet.mRequestType & USB_DIR_IN)
1743 ep_cnfg[CTRL_ENP_OUT].complete(CTRL_ENP_OUT);
1748 else if (EP == CTRL_ENP_IN)
1750 if (ep_cnfg[CTRL_ENP_IN].complete &&
1751 !(setup_packet.mRequestType & USB_DIR_IN))
1752 ep_cnfg[CTRL_ENP_IN].complete(CTRL_ENP_IN);
1761 /* USB: interrupt service routine */
1762 static void usb_isr(void)
1764 stm32_usb_irq_status_t interrupt;
1766 /* Get masked interrupt flags */
1767 interrupt.status = usb->ISTR;
1768 interrupt.status &= usb->CNTR | 0x1f;
1770 if (interrupt.PMAOVR)
1772 LOG_WARN("%s: DMA overrun / underrun\n", __func__);
1773 usb->ISTR = ~bmPMAOVRM;
1777 LOG_WARN("%s: engine error\n", __func__);
1778 usb->ISTR = ~bmERRM;
1780 if (interrupt.RESET)
1782 LOG_INFO("%s: device reset\n", __func__);
1783 usb->ISTR = ~bmRESETM;
1785 usb_set_device_state(USB_STATE_DEFAULT);
1791 * XXX: disable logging of frame interrupts (too much noise!)
1793 uint16_t frame_nr = usb->FNR & 0x0fff;
1794 LOG_INFO("%s: frame %#x\n", __func__, frame_nr);
1796 usb->ISTR = ~bmSOFM;
1800 LOG_INFO("%s: wake-up\n", __func__);
1801 usb->ISTR = ~(bmSUSPM | bmWKUPM);
1806 LOG_INFO("%s: suspend\n", __func__);
1808 usb->ISTR = ~(bmSUSPM | bmWKUPM);
1812 LOG_INFO("%s: expected frame\n", __func__);
1813 usb->ISTR = ~bmESOFM;
1817 usb_isr_correct_transfer(interrupt);
1821 /* USB: hardware initialization */
1822 static void usb_hw_init(void)
1824 /* Enable clocking on the required GPIO pins */
1825 RCC->APB2ENR |= RCC_APB2_GPIOA | RCC_APB2_GPIOC;
1827 /* Make sure that the CAN controller is disabled and held in reset */
1828 RCC->APB1ENR &= ~RCC_APB1_CAN;
1830 /* Configure USB_DM and USB_DP to work as USB lines */
1831 stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE,
1832 USB_DM_PIN | USB_DP_PIN,
1833 GPIO_MODE_AF_PP, GPIO_SPEED_50MHZ);
1834 /* Configure USB_DISC to work as USB disconnect */
1835 stm32_gpioPinConfig((struct stm32_gpio *)GPIOC_BASE,
1837 GPIO_MODE_OUT_PP, GPIO_SPEED_50MHZ);
1838 stm32_gpioPinWrite((struct stm32_gpio *)GPIOC_BASE,
1841 /* Ensure the USB clock is disabled before setting the prescaler */
1842 RCC->APB1ENR &= ~RCC_APB1_USB;
1844 /* Configure USB clock (48MHz) */
1845 *CFGR_USBPRE_BB &= ~RCC_USBCLK_PLLCLK_1DIV5;
1847 /* Activate USB clock */
1848 RCC->APB1ENR |= RCC_APB1_USB;
1850 /* Force USB reset and disable USB interrupts */
1854 /* Issue a USB reset */
1857 /* Clear spurious pending interrupt */
1860 /* Register interrupt handler */
1861 sysirq_setHandler(USB_LP_CAN_RX0_IRQHANDLER, usb_isr);
1863 /* Software connection enable */
1867 /* Initialize the USB controller */
1868 static void usb_init(void)
1870 udc.state = USB_STATE_NOTATTACHED;
1876 /* Register an upper layer USB device into the driver */
1877 int usb_deviceRegister(UsbDevice *dev)
1884 while (!usb_dev->configured)