Controlling toggle-only bits in STM32F10Xxx USB registers
STM32F10Xxx USB endpoint registers contain several bits that can only be controlled from software by toggling their value. For example, from RM0008 section 23.5.2, Endpoint-specific registers, USB endpoint n register (USB_EPnR), n=[0..7]:
Bits 13:12 STAT_RX [1:0]: Status bits, for reception transfers
...
These bits can be toggled by software to initialize their value. When the application software writes ‘0, the value remains unchanged, while writing ‘1 makes the bit value toggle.
...
These bits are read/write but they can be only toggled by writing ‘1.
Prior to this, the same section states:
Read-modify-write cycles on these registers should be avoided because between the read and the write operations some bits could be set by the hardware and the next write would modify them before the CPU has the time to detect the change.
This is logically inconsistent. To set bits to some required state necessitates reading them before deciding whether to toggle or not. And setting them is required, for example as per "the application software should set the STAT_RX bits to ‘11 (Valid) in the USB_EPnR" in section 23.4.2, System and power-on reset.
My particular use-case is when replying to the USB host's first "Setup Packet" request by sending the required USB Device Descriptor data. I have written the descriptor data to the correct USB_PMAADDR packet memory buffer location and believe I have to set the USB_EP0R register's STAT_TX bits (which are toggle-only) to 0b11 (VALID) to inform the peripheral to send the data at the next USB host poll. I have observed the bits dynamically changing at the point in the protocol where this is necessary, and there doesn't seem to be a definitive "quiescent" state at which the read-modify-write/toggling is safe.
Any corrections to my assumptions about how to send descriptors welcome, but mainly to the generic question of how to control toggle-only/no-read-modify-write bits.
