ESP32-C3 CDC-ACM must printf "\n" after putchar

Archi.Xie
Posts: 7
Joined: Sat Oct 09, 2021 11:35 am

ESP32-C3 CDC-ACM must printf "\n" after putchar

Postby Archi.Xie » Fri Mar 04, 2022 3:30 am

Hello,

My project is using ESP32-C3, I'm working on USB CDC-ACM,My program wish use CDC-ACM to putchar and getchar,but not want putchar's next must follow printf("\n") to trigger output, How can I solve this?Thanks!

1. idf.py menuconfig -> Component config -> ESP System Setting -> Channel for console output -> USB Serial/JTAG Controlller

2. On Standard Unix Enviroment:
a. fflush(stdout)
b. setvbuf(stdout,NULL,_IONBF,0)

But ESP32C3 USB CDC-ACM seems to be hardware implementation, a/b methods could not work, Sincerely looking forward to answer。

Best Regards!

ESP_Sprite
Posts: 9749
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 CDC-ACM must printf "\n" after putchar

Postby ESP_Sprite » Fri Mar 04, 2022 4:43 am

That is odd... there indeed is a flush system in the usb-jtag-serial hardware that needs to be triggered, but from what I can tell that should be hooked up to the fflush() call. Can you try manually calling usb_serial_jtag_ll_txfifo_flush(), see if that fixes it?

Archi.Xie
Posts: 7
Joined: Sat Oct 09, 2021 11:35 am

Re: ESP32-C3 CDC-ACM must printf "\n" after putchar

Postby Archi.Xie » Fri Mar 04, 2022 5:48 am

ESP_Sprite wrote:
Fri Mar 04, 2022 4:43 am
That is odd... there indeed is a flush system in the usb-jtag-serial hardware that needs to be triggered, but from what I can tell that should be hooked up to the fflush() call. Can you try manually calling usb_serial_jtag_ll_txfifo_flush(), see if that fixes it?
Ok,Thanks for reply, but which header file should be contained for using "usb_serial_jtag_ll_txfifo_flush()"? I searched whole file under esp-idf-v4.4 but don't find "usb_serial_jtag_ll_txfifo_flush()".

Best regards!

Archi.Xie
Posts: 7
Joined: Sat Oct 09, 2021 11:35 am

Re: ESP32-C3 CDC-ACM must printf "\n" after putchar

Postby Archi.Xie » Fri Mar 04, 2022 6:43 am

I solve this issue by manual set register USB_SERIAL_JTAG_EP1_CONF_REG's USB_SERIAL_JTAG_WR_DONE bit, here is the source code and test picture, hope could help more people!

static void usb_cdc_transfer_task(void *arg)
{
while(1)
{
int USB_CDC_Recv_Byte = -1;

uint32_t EP1_Conf_Reg = 0x00;

uint32_t Send_Byte_Cnt = 0x00;

while((USB_CDC_Recv_Byte = getc(stdin)) != EOF)
{
putc((char)USB_CDC_Recv_Byte, stdout);

Send_Byte_Cnt++;
}

/* Replace (fflush(stdout) or printf("\n"))'s function!!! */
if(Send_Byte_Cnt > 0)
{
/* Read USB_SERIAL_JTAG_EP1_CONF_REG (BaseAddr: 0x60043000 Offset: 0x0004) */
EP1_Conf_Reg = *((uint32_t*)(0x60043004));

/* Set 'USB_SERIAL_JTAG_WR_DONE' bit*/
EP1_Conf_Reg |= 0x01;

/* Write USB_SERIAL_JTAG_EP1_CONF_REG Back */
*((uint32_t*)(0x60043004)) = EP1_Conf_Reg;

Send_Byte_Cnt = 0;
}

vTaskDelay(1);
}
}
Attachments
ESP32-C3使用USB CDC-ACM实现接收和发送.png
ESP32-C3使用USB CDC-ACM实现接收和发送.png (112.79 KiB) Viewed 2881 times

1321457900
Posts: 1
Joined: Wed Jan 18, 2023 4:54 pm

Re: ESP32-C3 CDC-ACM must printf "\n" after putchar

Postby 1321457900 » Wed Jan 18, 2023 4:57 pm

你可以使用

Code: Select all

printf("%s\033[s\033[1A\n\033[u", data);
来在打印换行符之后再回到上一行

Who is online

Users browsing this forum: No registered users and 132 guests