Arduino_Core_STM32 icon indicating copy to clipboard operation
Arduino_Core_STM32 copied to clipboard

USB Host + MSC

Open rhapsodyv opened this issue 5 years ago • 26 comments
trafficstars

Pull Request template

Summary

This PR fixes/implements the following bugs/features

  • [X] USB Host
  • [X] MSC for USB Host

It will allow STM users to use USB Host feature with MSC support.

I tried to follow the same development workflow from the usb device midleware. I created a folder cores/arduino/stm32/usb_host, and put the conf and the references to the middleware.

There's some work to do yet:

  • [x] HCD shares a few definitions with PCD. For example, the PinMap_USB_OTG_FS in the PeripheralPins.c for each variant. Do we need update all variants to include a "or" in the #ifdef? Can HCD and PCD be enabled at same time? If so, we need to join IRQ handler.
  • [ ] Interface for USB Host Config + MSC operations. Right now, I'm just exposing the MSC interface from STM32 middleware.
  • [x] Missing OTG HS
  • [x] I didn't organize the comments, style and options in the files, yet.

It works if USBCON is disabled, and HAL_HCD_MODULE_ENABLED and USBHOST are enabled.

Sorry if I'm missing something. It's my first PR in this project, and it may take sometime to get everything as it should be.

Related to: #687

rhapsodyv avatar Oct 04 '20 01:10 rhapsodyv

Usage:

Compile with: -DHAL_HCD_MODULE_ENABLED -DUSBHOST -DHAL_PCD_MODULE_ENABLED

//includes
#include "usbh_core.h"
#include "usbh_msc.h"

//handle
USBH_HandleTypeDef hUsbHostFS;

//callback
static void USBH_UserProcess  (USBH_HandleTypeDef *phost, uint8_t id)
{
  /* USER CODE BEGIN CALL_BACK_1 */
  switch(id)
  {
    case HOST_USER_SELECT_CONFIGURATION:
    
    break;

    case HOST_USER_DISCONNECTION:

    break;

    case HOST_USER_CLASS_ACTIVE:
      //ready!
    break;

    case HOST_USER_CONNECTION:

    break;

    default:
    break;
  }
}

//init calls
void my_init_routine() {
  ...
  USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS);
  USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS);
  USBH_Start(&hUsbHostFS);
  ..
}

//loop call
void loop() {
  ..
  USBH_Process(&hUsbHostFS);
  ..
}

//read sample: it will read (sectors_to_read) * (sector size) bytes
USBH_StatusTypeDef ret = USBH_MSC_Read(&hUsbHostFS, lun, sector_addr, buffer, sectors_to_read);

rhapsodyv avatar Oct 04 '20 03:10 rhapsodyv

Tested with success in: STM32F407VET6, STM32F407IGT6 and STM32F407ZGT6.

rhapsodyv avatar Oct 04 '20 18:10 rhapsodyv

Hi @rhapsodyv

thanks a lot for this PR. I will try to do a first review soon.

fpistm avatar Oct 05 '20 08:10 fpistm

@fpistm Can this PR be merged now?

MS1987 avatar Oct 27 '20 01:10 MS1987

I think the only important question to merge it is:

HCD shares a few definitions with PCD. For example, the PinMap_USB_OTG_FS in the PeripheralPins.c for each variant. Do we need update all variants to include a "or" in the #ifdef? Can HCD and PCD be enabled at same time? If so, we need to join IRQ handler.

I'm currently enabling PCD just to use the pins definitions. But maybe we need update the variants to handle HCD too.

And soon I will receive a board with two usb, so I will test it and join the pcd/hcs irq handler.

I can send all changes. I just need that @fpistm point the direction for me :-)

rhapsodyv avatar Oct 27 '20 01:10 rhapsodyv

For example, the PinMap_USB_OTG_FS in the PeripheralPins.c for each variant. Do we need update all variants to include a "or" in the #ifdef? Can HCD and PCD be enabled at same time? If so, we need to join IRQ handler.

Yes the "or" have to be added. I'm currently updating the files generation, I will add the HCD.

Can HCD and PCD be enabled at same time? If so, we need to join IRQ handler.

I don't know but I think as a first step it should be better to handle one at a time.

fpistm avatar Oct 30 '20 15:10 fpistm

Can HCD and PCD be enabled at same time? If so, we need to join IRQ handler.

I don't know but I think as a first step it should be better to handle one at a time.

I'm about to receive a board that have two USB. One in OTG HS and other in OTG FS. The central idea is to use one to read a thumb drive (USB Host) and the other to connect to the computer and share the pendrive as mass storage. I didn't tested it yet. But I can't see any reason for it not work.

But I really agree with you. For the first version, we must keep what is tested and working. As soon I test that dual mode on that board, I will let you know if it works.

Thanks.

rhapsodyv avatar Oct 30 '20 17:10 rhapsodyv

I'm about to receive a board that have two USB. One in OTG HS and other in OTG FS. The central idea is to use one to read a thumb drive (USB Host) and the other to connect to the computer and share the pendrive as mass storage. I didn't tested it yet. But I can't see any reason for it not work.

Main issue would be the USBD part as currently if we enable both FS and HS this will not work, I guess.

fpistm avatar Oct 30 '20 17:10 fpistm

Main issue would be the USBD part as currently if we enable both FS and HS this will not work, I guess.

Indeed. I don't know if it will work. I found some stm32 forum messages saying that it works. But only testing to confirm it or not. I know that the code will need to be updated, so we can choose what enable in each mode. Today we just have USB_OTG_FS and USB_OTG_HS. We may need a way to specify, for example, that we want FS in PCD and HS in HCD.

rhapsodyv avatar Oct 30 '20 17:10 rhapsodyv

Today we just have USB_OTG_FS and USB_OTG_HS. We may need a way to specify, for example, that we want FS in PCD and HS in HCD.

Right.

fpistm avatar Oct 30 '20 17:10 fpistm

GLad to see work in this area.

Does this replace PR #1088?

Bob-the-Kuhn avatar Dec 07 '20 02:12 Bob-the-Kuhn

GLad to see work in this area.

Does this replace PR #1088?

I think #1088 is for usb mode device (PCD). My work is for usb mode host (HCD). They are complementary.

BTW, @fpistm I just received a board to test PCD+HCD. Soon I will start to push new code here.

rhapsodyv avatar Dec 07 '20 03:12 rhapsodyv

@fpistm I pushed a version that will allow PCD and HCD working together. To avoid conflict and to not break compatibility, I added a new config to select HS on HCD (USE_USBHOST_HS).

It easier to just show the possible combinations:

To use HCD on FS:

#define HAL_HCD_MODULE_ENABLED
#define USBHOST

To use HCD on HS:

#define HAL_HCD_MODULE_ENABLED
#define USBHOST
#define USE_USBHOST_HS.  // <<<--- new macro, to select HS on HCD

To use PCD on FS: (current code, just to illustrate)

#define HAL_PCD_MODULE_ENABLED
#define USBCON

To use PCD on HS: (current code, just to illustrate)

#define HAL_PCD_MODULE_ENABLED
#define USBCON
#define USE_USB_HS

And we can to combine then:

HCD on FS + PCD on HS:

// HCD on FS
#define HAL_HCD_MODULE_ENABLED
#define USBHOST

// PCD on HS
#define HAL_PCD_MODULE_ENABLED
#define USBCON 
#define USE_USB_HS

Or: HCD on HS + PCD on FS:

// HCD on HS
#define HAL_HCD_MODULE_ENABLED
#define USBHOST
#define USE_USBHOST_HS

// PCD on FS
#define HAL_PCD_MODULE_ENABLED
#define USBCON 

rhapsodyv avatar Dec 10 '20 04:12 rhapsodyv

With this PR plus #1088 I'm able to use fully mix usb host / usb device mass storage.

Using OTG HS to host the thumb drive + using OTG FS to uses CDC + MSC, to share the thumb driver or the SD Card with the Computer. Everything working together. The mount/unmont and media select work without any power cycle needed.

rhapsodyv avatar Dec 11 '20 04:12 rhapsodyv

hi @rhapsodyv what is the status of this PR. I'm a bit lost when you said

With this PR plus #1088 I'm able to use fully mix usb host / usb device mass storage.

I though this one replace #1088 ?

Thanks

fpistm avatar May 03 '21 12:05 fpistm

This is exclusively to USB host: for example, enable a board to use a usb disk drive using OTG (hs or fs - hcd).

#1088 is to share the board media with a computer/using using usb OTG. For example: to share a board sd card with a windows computer as a mass storage.

I merged both PRs in a local branch, so we can, for example: allow the board access and share a usb disk drive with the computer.

We are actively using it on Marlin.

The only issue with 1088 is the name of the usb class... it should be renamed to something like "USBComposite", instead of only "USB".

rhapsodyv avatar May 03 '21 14:05 rhapsodyv

It's even allow a mouse to be attached to a board heheheh:

https://user-images.githubusercontent.com/81722/116918582-78c76400-ac26-11eb-82df-6edcccb0f1a6.MOV

rhapsodyv avatar May 03 '21 18:05 rhapsodyv

This is exclusively to USB host: for example, enable a board to use a usb disk drive using OTG (hs or fs - hcd).

#1088 is to share the board media with a computer/using using usb OTG. For example: to share a board sd card with a windows computer as a mass storage.

I merged both PRs in a local branch, so we can, for example: allow the board access and share a usb disk drive with the computer.

We are actively using it on Marlin.

The only issue with 1088 is the name of the usb class... it should be renamed to something like "USBComposite", instead of only "USB".

Well thats cool! I'm trying to implement this for the newer skr v2 board any tips? I've been looking through this thread and https://github.com/stm32duino/Arduino_Core_STM32/pull/1088 but I'm not sure how much has already been implemented since then. Thanks

gordo3di avatar Jul 19 '21 18:07 gordo3di

Well thats cool! I'm trying to implement this for the newer skr v2 board any tips? I've been looking through this thread and #1088 but I'm not sure how much has already been implemented since then. Thanks

It's already done on Marlin: https://github.com/MarlinFirmware/Marlin/pull/22354

rhapsodyv avatar Jul 19 '21 18:07 rhapsodyv

Well thats cool! I'm trying to implement this for the newer skr v2 board any tips? I've been looking through this thread and #1088 but I'm not sure how much has already been implemented since then. Thanks

It's already done on Marlin: MarlinFirmware/Marlin#22354

Interesting. Thank you. Now I have to figure out why it's not mounting for me.

gordo3di avatar Jul 19 '21 18:07 gordo3di

Hello,

Is USB HID Host (keyboard) working too?

Thank you.

rtek1000 avatar Jan 17 '22 10:01 rtek1000

Hello,

Is USB HID Host (keyboard) working too?

Thank you.

Mouse is working, keyboard should work too:

https://github.com/stm32duino/Arduino_Core_STM32/pull/1196#issuecomment-831456849

rhapsodyv avatar Jan 17 '22 14:01 rhapsodyv

Hello,

Is USB HID Host (keyboard) working too?

Thank you.

Very interesting, I'm just looking around to see how to build a USB keyboard to legacy machine adapter (Commodore 128D in this case), and this would be extremely useful - I don't think I have the energy to dive into using raw CubeMX for it.

So what's this waiting for in terms of being able to be merged, it looks like the PCD+HCD test was successful... I guess it's just down to time from @fpistm

BennehBoy avatar Jan 27 '22 16:01 BennehBoy

Hello, Is USB HID Host (keyboard) working too? Thank you.

Very interesting, I'm just looking around to see how to build a USB keyboard to legacy machine adapter (Commodore 128D in this case), and this would be extremely useful - I don't think I have the energy to dive into using raw CubeMX for it.

So what's this waiting for in terms of being able to be merged, it looks like the PCD+HCD test was successful... I guess it's just down to time from @fpistm

Hello,

The USB Host library of the STM32 line is still very precarious, just didactic.

If you want something more fluid, check out this other alternative, which can accept the touchpad keyboard. Because it has the HUB part:

https://github.com/felis/UHS30

rtek1000 avatar Jan 27 '22 16:01 rtek1000

https://community.st.com/s/question/0D53W00000GYsczSAD/is-it-possible-to-use-usb-hid-keyboard-and-mouse-at-the-same-time-on-a-stm32f469

rtek1000 avatar Jan 27 '22 16:01 rtek1000

I really wonder about the status of this PR since it has not been merged yet and there appear to be conflicts now. I would greatly appreciate any statement by participating parties. The features described in this PR seem amazing and worthwhile for the embedded world.

quiret avatar Feb 15 '23 21:02 quiret