STM32 gotchas
29.Processor (and debugger) hangs while debugging OTG USB Host

While debugging OTG USB host on 'F407, debugger hung, ST-Link ceasing to communicate (and blink the two-color LED). Simultaneously, the processor hung, too, as the "heartbeat" LED toggled in main loop ceased to blink.

This is the sort of hang caused by the SoC nature of the chip - if one of the peripheral modules fail to correctly end a read or write transaction, and hold WAIT infinitely, processor will wait infinitely too. As the debug module is part of the processor and uses the processor's "connection to outer world" (i.e. it's bus system), when it's the debugger which attempted read/write to such an offending peripheral, both the debugger and processor will hang waiting infinitely.

Upon investigation it turned out, that hang occured due to read to one of the USB OTG modules at offset 0x51C, but only when the device was connected.

This offset belongs to a group of registers related to Host Channels. While this particular offset is not documented by ST and in the headers is marked as Reserved, it is a register which is implemented in only those versions of the Synopsys OTG module which have enabled the Scatter-Gather-DMA feature (at implementation time, i.e. when the chip is designed). This feature is not implemented in the ST's incarnations of this module, so the register is rightly marked as reserved. However, it would be prudent to implement the register so, that reading it won't cause hang. Reading the "neighbouring" offset 0x518 (which is Reserved, too) won't result in hang. The Synopsys module is not famous for being well designed, to put it mildly.

Why did the debugger read a reserved position anyway? Using gdb, when commanded to read a struct, it reads all of it, as it does not know, which parts of it are reserved. Here's the definition of said struct in the respective device header:

typedef struct {
  __IO uint32_t HCCHAR;           /*!< Host Channel Characteristics Register    500h */
  __IO uint32_t HCSPLT;           /*!< Host Channel Split Control Register      504h */
  __IO uint32_t HCINT;            /*!< Host Channel Interrupt Register          508h */
  __IO uint32_t HCINTMSK;         /*!< Host Channel Interrupt Mask Register     50Ch */
  __IO uint32_t HCTSIZ;           /*!< Host Channel Transfer Size Register      510h */
  __IO uint32_t HCDMA;            /*!< Host Channel DMA Address Register        514h */
  uint32_t Reserved[2];           /*!< Reserved                                      */
} USB_OTG_HostChannelTypeDef;
The offending register is here .Reserved[2].

It would appear, that it's enough to remove the Reserved[2] field from the struct. In ST's "libraries" code, as they use macros to access these registers, calculating the offsets ad-hoc, it might work; however, I've written a proper struct encompassing all OTG registers; that uses this particular struct in an array (as there are several Host Channels), so removing items from struct would make it's length different, rendering such array unusable. The only solution for me thus is to exercise care and avoid reading the whole struct, read only its individual named items.

"Clicky" debuggers built into IDEs may not suffer from this problem, as they for registers layout don't use information propagated from headers through .elf/.dwarf.