Qinheng CH32X035C8T6 supports USB PD function. This article mainly describes SNK as the power consumption end, which can be applied to products and equipment that require fast charging.
1. Hardware evaluation and USB PD protocol understanding
1. From the schematic diagram, we can see that there is a HT7533 voltage conversion LDO connected to the USB Type-C power supply. Check its manual as follows. It can be seen that the USB PD SNK (power consumption end) voltage should be less than its input upper limit voltage, that is, less than 28V .
Figure 1: USB PD SNK hardware limited voltage
2. About USB PD protocol
The working principle of the USB PD protocol is to use the CC line (configuration channel line, used for identification and control) of the Type-C interface as the data line to negotiate the voltage, current and power supply direction. The entire communication process needs to follow a specific data packet format and there is a mutual authentication process. The figure below is the USB Type C pin definition.
Figure 2: USB Type C pinout
The USB PD protocol communicates through packets in a specific format. The format of the packet is shown below.
Figure 3: USB PD protocol format
A complete USB PD data packet consists of a preamble, a usage scenario code (SOP* all PD transmission processes), a function code (MessageHeader), a data code (Byte0-n), a check code (CRC) and an end code (EOP data packet end flag). If the data part is empty, it means that the data packet is only used as a control instruction, which is called a control message. The data message with data content is called a data message, which usually contains information such as the voltage and current values to be changed. In the entire USB PD data packet, except for the preamble, which does not need to be 4b5b encoded, the other parts of the data packet need to be 4b5b encoded. After the specified data is 4b5b encoded, all data in the data packet needs to be encoded with BMC before it can be sent through CC.
Detailed information about the USB PD protocol can be found on the USB official website [1]. Here is a general overview.
2. Code preparation and testing
1. Code organization and preparation
WCH official SDK has integrated USBPD_SNK and USBPD_SRC. This article uses the former demo for debugging and testing.
PD_Init() performs hardware initialization configuration for USB PD (including D+, D-, CC1, CC2), PD_PHY_Reset() initializes PD SNK and PD_Rx_Mode() configures USB PD receiving mode. In the main function (as shown below), there are timer initialization, USB PD initialization configuration, PD_Det_Proc connection data receiving processing, PD_Main_Proc protocol status flow processing, and receiving data protocol parsing processing. The official SDK provides a complete solution code for the PD protocol. We make some small modifications in the adaptation area in combination with the application to achieve the required purpose.
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
printf( "SystemClk:%d\r\n", SystemCoreClock );
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
printf( "PD SNK TEST\r\n" );
PD_Init( );
TIM1_Init( 999, 48-1);
while(1)
{
/* Get the calculated timing interval value */
TIM_ITConfig( TIM1, TIM_IT_Update , DISABLE );
Tmr_Ms_Dlt = Tim_Ms_Cnt - Tmr_Ms_Cnt_Last;
Tmr_Ms_Cnt_Last = Tim_Ms_Cnt;
TIM_ITConfig( TIM1, TIM_IT_Update , ENABLE );
PD_Ctl.Det_Timer += Tmr_Ms_Dlt;
if( PD_Ctl.Det_Timer > 4 )
{
PD_Ctl.Det_Timer = 0;
PD_Det_Proc( );
}
PD_Main_Proc( );
}
}
USBPD_IRQHandler receives PD data and responds immediately. PD_Detect() is a CC data handshake to determine whether it is connected. In PD_Main_Proc, there is a very matching application, which is the voltage request function PDO_Request after confirming the connection. The reference data is initially understood to be 1--5V, 2--9V, 3--12V, 4--15V, 5--20V, for reference. In initialization and PD_Main_Proc, SrcCap_Ext_Tab, Status_Ext_Tab, and other protocol data configurations are used.
Figure 4: USB PD SNK voltage request
2. Testing
Select the charger to view the adapter voltage information on the charger. It is best to confirm that the charger is compatible with the USB PD protocol. After burning the program, turn off the power, connect the charger to the development board with a USB Type C cable, and measure the voltage between VBUS1 and GND. The results of the above configuration are as shown below. (When doing this experiment, I only had one brand-name charger on hand. I will test more groups later depending on the situation.)
Figure 5: USB PD SNK voltage measurement
Through this practice, I have a better understanding of the USB PD power transmission function. This function can be used in products such as fast charging. It is a good solution choice and is worth recommending.
【1】USB PD protocol
https://www.usb.org/document-library/usb-power-delivery
|