openwch / ch32v20x

CH32V203 is an industrial-grade enhanced low-power,small-medium capacity general-purpose MCU based on 32-bit RISC-V core
67 stars 15 forks source link

Simple USB-CDC Example #7

Open greenscreenflicker opened 5 months ago

greenscreenflicker commented 5 months ago

Hello, is there a simple USB-CDC Example outputing "hello world" and returning the recieved string? That would be very handy! Michael

anushudupi commented 5 months ago

I have used this EVT USB CDC example and it works https://github.com/openwch/ch32v20x/tree/main/EVT/EXAM/USB/USBFS/DEVICE/SimulateCDC

here you have to connect USART1(default printf) and USART2 (used for spoofing USB CDC I guess) USART1TX (PA9) <---> USART2RX (PA3) USART1RX (PA10) <---> USART2TX (PA2)

then you can use normal printf debug through USB CDC port

test code i used with CH32V203C8T6

/*
 *Example routine to emulate a simulate USB-CDC Device, USE USART2(PA2/PA3);
 *Please note: This code uses the default serial port 1 for debugging,
 *if you need to modify the debugging serial port, please do not use USART2
*/

#include "ch32v20x_usbfs_device.h"
#include "debug.h"

int main(void)
{
    Delay_Init( );
    USART_Printf_Init( 115200 );
    printf("SystemClk:%d\r\n",SystemCoreClock);
    RCC_Configuration( );

    /* Tim2 init */
       TIM2_Init( );

    /* Usart1 init */
     UART2_Init( 1, DEF_UARTx_BAUDRATE, DEF_UARTx_STOPBIT, DEF_UARTx_PARITY );

    /* USB20 device init */
    USBFS_RCC_Init( );
    USBFS_Device_Init( ENABLE );

    Delay_Ms(2000);
    printf("SystemClk:%d\r\n",SystemCoreClock);
    UART2_DataRx_Deal( );

    while(1)
    {
     printf("hello world\n");
        UART2_DataRx_Deal( );
        //UART2_DataTx_Deal( );
        Delay_Ms(500);
    }
}

work fine but there is delay in response and dumps data together

hopefully this is helpful ! (it would be great if there is native USB serial without doing external wiring)

ldab commented 1 month ago

Something like this will print on CDC

void dbg_print(char *buf, int size)
{
#ifdef CH32V20X
  while (USBFS_Endp_Busy[DEF_UEP3]) {
    ;
  }

  USBFS_Endp_DataUp(DEF_UEP3, (uint8_t *)buf, size, DEF_UEP_CPY_LOAD);
#endif
}

If you want to replace printf than do so on https://github.com/openwch/ch32v20x/blob/c4c38f507e258a4e69b059ccc2dc27dde33cea1b/EVT/EXAM/SRC/Debug/debug.c#L173

greenscreenflicker commented 1 month ago

@ldab which init did you use?

ldab commented 1 month ago

@ldab which init did you use?

This should do, copied on my phone so could have missed something

#include "ch32v20x_usbfs_device.h"
#include "debug.h"
#include "string.h"

void dbg_print(char *buf, int size)
{
#ifdef CH32V20X
  while (USBFS_Endp_Busy[DEF_UEP3]) {
    ;
  }

  USBFS_Endp_DataUp(DEF_UEP3, (uint8_t *)buf, size, DEF_UEP_CPY_LOAD);
#endif
}

int main(void)
{
  SystemCoreClockUpdate();
  Delay_Init();
  USART_Printf_Init(115200);
  printf("SystemClk:%ld\r\n", SystemCoreClock);

#ifdef CH32V20X
  /* USB20 device init */
  USBFS_RCC_Init();
  USBFS_Device_Init(ENABLE);
#endif

  printf("main\r\n");

  while (1) {
    char message[64] = “something”;
    dbg_print(message, strlen(message));
    Delay_Ms(200);
  }
}