eeucalyptus - USB PD on the CH32V003
In a recent post I wrote about USB PD Coding and how to encode and decode the signals in software. In this post I will talk about how to do this on the CH32V003. The bitrate of USB PD is 300 kHz, with a tolerance of 10%. For transmission, this implies quite relaxed timing requirements. But for receiving, the constraints are quite tight that way. The sampling (for both input and output) will be done in an assembler routine with busy-wait loops and synchronization with the systick timer. I'll run the '003 and the systick at 48 MHz. For 600 kHz sampling rate (twice the bitrate), half a bit is 80 clock cycles. This leaves us with enough time to read the data from the ram and write it to the GPIOs. For transmission, I'm not going to use a timer interrupt, but a simple busy-wait loop. It might result in some lost cpu time but will be much easier to implement. The systick will be saved at the beginning and every bit waits for the next systick period of 80 clock cycles. The receiving part is a