cancel
Showing results for 
Search instead for 
Did you mean: 

Has anyone ever managed to get a STM32F411CE Blackpill working as a USB Host?

Nello
Associate II

Hi. I've been trying to work this out for a couple of weeks now and am pretty well stumped. I can't even get VBUS power working, so suffice it to say I'm not getting very far.

All the examples seem to be for Nucleo boards, so not sure if it's even possible with a Blackpill. So far I've worked my way through these examples with no great luck:

https://www.youtube.com/watch?v=MlhUG4GsOT0

https://controllerstech.com/stm32-usb-host-hid

https://www.bennettnotes.com/notes/stm32-blackpill-with-stmcubeide-usb-serial

I have had a bit of success totally avoiding ST tooling and using the Arduino stm32duino libraries but they don't seem to have managed to get USB Host working either, so that's not particularly productive.

Any help would be very much appreciated!

5 REPLIES 5
Andrew Neil
Evangelist III

"All the examples seem to be for Nucleo boards"

Have you tried getting it to work on a Nucleo first - so you have a known-good starting point?

That's the route I'd take...

"not sure if it's even possible with a Blackpill"

So does this "Blackpill" have USB Host hardware?

Compare & contrast schematics.

Please post a link to the actual "Blackpill" you're using

Nello
Associate II

So have you tried asking Adafruit?

https://forums.adafruit.com/

Nello
Associate II

I admit I tried here first, but I won't do that next time.

TSrip.1
Associate

I don't know about the authentic Black Pill from Arduino. But I use one from WeAct and finally got it working with USB wireless keyboard, so I am sharing interesting things I found out here.

Blackpill's USB port was designed for USB device only. It probably has a diode connected to USB's VCC to prevent the device to overload the board's power. If you would like to connect a device, you have to provide power to the device yourself. In my case, I modify a USB cable to add power to the VCC line to feed the device with external 5V.

I connected the USB keyboard like this

Blackpill<==>USB-OTG cable (USB-C to USB-A) <==> my modified cable <==> USB keyboard.

Alternatively, you might build your own USB interface and connect that way so you can correctly feed 5V supply to the device.

==================================================

Additional notes.

==================================================

*You don't have to enable VBUS or SOF in STM32CubeIDE. However the IDE requires you to assign a pin to act as VBUS enable so you just assign a GPIO pin as Output (push-pull) and assign Drive_VBUS_FS as GPIO:Output (under USB_HOST/Platform Settings)

*USB host requires MX_USB_HOST_Process() to be called frequently to get data from USB. If your main loop takes a long time and just to get a USB keyboard working, you might consider adding polling commands into your own keyboard input function. For example:

unit8_t kBin,kBout,kBuffer[16];

In my case, I wrote a simple keyboard buffer input within USBD_HID_EventCallback()

void USBH_HID_EventCallback(USBH_HandleTypeDef *phost){

if(USBH_HID_GetDeviceType(phost) == HID_KEYBOARD){

HID_KEYBD_Info_TypeDef *Keyboard_Info;

Keyboard_Info = USBH_HID_GetKeybdInfo(phost);

uint8_t key = USBH_HID_GetASCIICode(Keyboard_Info);

if(key==0)return;

if(((kBin+1)&0xf)==kBout)return;

kBuffer[kBin]=key;

kBin=(kBin+1)&0xf;

}

}

and I wrote a simple getch() like this:

extern uint8_t getch(char *ch){

for(int i=0;i<10;i++){

  MX_USB_HOST_Process();

  HAL_Delay(2);

}

if(kBin==kBout) return 0;

*ch = kBuffer[kBout];

kBout = (kBout+1)&0xf;

return 1;

}

In the program, I used:

int ch;

if(getch(&ch)){

...to do...

}

So MX_USB_HOST_Process() will be called when needed, which in this case, waiting for a keystroke.