cancel
Showing results for 
Search instead for 
Did you mean: 

Connect USBCDC to stdin

ETone
Associate III

I have successfully connected USBCDC to stdout by redefining __io_putchar and can use printf to talk to a PC thru its virtual serial port but am not having any success with stdin (modifying __io_getchar). Anyone done this and can advise?

1 ACCEPTED SOLUTION
3 REPLIES 3
ETone
Associate III

Some success - the system will operate getc correctly once but further calls to putc will not call __io_getchar.

gbm
Lead III

To keep the things simple, you need to overwrite _write(), not __io_putchar. Then printf() could be called only from an interrupt service routine of the same priority as USB interrupt. To allow printf to be called from lower priority code, including main(), some more steps must be taken:

Select some unused interrupt as software interrupt. Set its priority to the same as USB interrupt

Define USB transmit busy flag . The flag should be set by CDC_Transmit and rest by CDC Transmit complete callback.

In _write(), wait unti CDC Tx is not busy,then issue the selected interrupt - NVIC_SetPending()

Call CDC_Transmit from the interrupt service routine (not from _write()).

That's a short and not quite complete outline of what needs to be done. Some time I will post a complete code on Github.

BTW, the stdin code from the link above doesn't work. CDC_Receive must be called when the input buffer is emptied, then again CDC_Receive must be called from the code running at the same priority as USB interrupt, similarly to what I described for Transmit above.

I also finally have a 99% working code of USB-UART bridge using USB HAL (would be much simpler without but I wanted to finally do something in STM-blessed way, at least for USB; UART HAL is not compatible with anything that must be simple and efficient). Going to make it public soon.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice