AnsweredAssumed Answered

Lowlevel USB programming

Question asked by kolesnikov.ivan on Nov 17, 2015
Latest reply on Nov 20, 2015 by kolesnikov.ivan
I'm trying to understand low-level (without third-party libraries) programming of USB module of STM32F103 MCU and have encountered a problem.
My sequence of actions:
1) I have an external crystal of 8 MHz. PLL setup to output 72 MHz (multiplication factor of 9, divider 3 USB). Of course, this process consists of several stages, however, the result in register RCC-> CFGR is 0x001DC40A (which among other things means that the PLL is successfully selected as the system frequency source). USART and SysTick works normally (frequency exactly true).
2) Enable USB clock via RCC. Clear PWDN bit in USB->CNTR. Waiting 1 microsecond (as datasheet says it need to stabilize the reference voltage), write zeros to CNTR, BTABLE and ISTR registers. Enable USB_FS_WKUP and CAN1_RX0 IRQs. Then write USB_CNTR_RESETM | USB_CNTR_SUSPM | USB_CNTR_WKUPM | USB_CNTR_CTRM value to the USB->CNTR.
3) USB initialization is done. Next comes first USB interrupt. Interrupt reason from USB->ISTR is RESET. Yes, this is what should happen, everything is ok. I cleared this flag.
4) Setup endpoint 0. As a result, USB-> EP0R value is 0x3220. At address USB_PMA_BASE + 0 placed value 64 (TXADDR for EP0 == 64), at the address USB_PMA_BASE + 8 placed value 128 (RXADDR for EP0 == 128), at the address USB_PMA_BASE + 12 placed value 0x8400 (receiving buffer size for EP0 - 64 bytes).
5) Enable USB operation - write in the USB->DADDR value 0x80.
6) Return from the interrupt handler.
7) I expect that I will have to received the first SETUP-packet, what will be indicated by flag CTR in register USB->ISTR. And it does not come. But comes a bunch of interrupts due WKUP (I reset this the flag, but it comes again). Sometimes I have receive USB RESET interrupt (device address setting timeout exceed and host perfromed bus reset), but after that I receiving WKUP events again. In the end, the host give up and no more interrupts occurs. In addition ESOF condition is happens before WKUP.
My theory: USB-module does not run properly and is not able to understand what is coming on the bus except RESET. Data transfer is seen as an WKUP event (which is in fact caused by any activity on the line).
Maybe I forgot to initialize something or do it wrong.
The hardware part is OK - USB works properly with ChibiOS or libopencm3 on same board.
Any ideas?