Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB Host Implementation #817

Closed
senceryazici opened this issue Dec 4, 2019 · 2 comments
Closed

USB Host Implementation #817

senceryazici opened this issue Dec 4, 2019 · 2 comments
Labels
enhancement New feature or request help wanted 🙏 Extra attention is needed

Comments

@senceryazici
Copy link

senceryazici commented Dec 4, 2019

Hi there,
I have an XSens MTi-G-710 device in my hand, and I use a NUCLEO-F429ZI board.
I basically want to connect the Xsens device to STM32 via USB Connection, and want to set the stm as host device. The Xsens device communicates with ACM, so I want to be able to receive & send serial connection bytes through USB, just like I do with a PC on /dev/ttyACM0.

I have no clue where to start, because no example is based on stm32duino but I need to use stm32duino.

How can I implement such structure ?
I've tried the following:

#define HAL_HCD_MODULE_ENABLED

#include "Arduino.h"
#include <usbh_lib/usbh_cdc.h>
// #include "stm32f4xx_hal.h"
// #include "stm32f4xx_hal_def.h"

typedef enum {
  APPLICATION_IDLE = 0,  
  APPLICATION_START,
  APPLICATION_READY,    
  APPLICATION_RUNNING,
  APPLICATION_DISCONNECT,
} CDC_ApplicationTypeDef;

class XSensUSBDriver
{
private:
    CDC_ApplicationTypeDef Appli_state = APPLICATION_IDLE;
    USBH_HandleTypeDef hUSBHost;
    static CDC_LineCodingTypeDef LineCoding;
    static CDC_LineCodingTypeDef DefaultLineCoding;
    void receive();

public:
    // void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
    void process();
    void init();
    XSensUSBDriver();
    ~XSensUSBDriver();
};
void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
{
  switch(id)
  { 
  case HOST_USER_SELECT_CONFIGURATION:
    break;
    
  case HOST_USER_DISCONNECTION:
    // Appli_state = APPLICATION_DISCONNECT;
    break;
    
  case HOST_USER_CLASS_ACTIVE:
    // Appli_state = APPLICATION_READY;
    break;
    
  case HOST_USER_CONNECTION:
    // Appli_state = APPLICATION_START;
    break;

  default:
    break; 
  }
}


XSensUSBDriver::XSensUSBDriver()
{
    
}


void XSensUSBDriver::init()
{
    USBH_Init(&hUSBHost, USBH_UserProcess, 0);
    // USBH_RegisterClass(&hUSBHost, USBH_CDC_CLASS);
    

    // ?
    // USBH_CDC_GetLineCoding(&hUSBHost, &DefaultLineCoding);
    // LineCoding = DefaultLineCoding;
    // LineCoding.b.dwDTERate = 115200; // baudrate
    // LineCoding.b.bParityType = 0; // 0, 1, 2
    // LineCoding.b.bDataBits = 8; // data bits
    // LineCoding.b.bCharFormat = 1; // stop bits
    // USBH_CDC_SetLineCoding(&hUSBHost, &LineCoding);
}

void XSensUSBDriver::process()
{
    USBH_Process(&hUSBHost); 
}

void XSensUSBDriver::receive()
{
    uint8_t recv[16];
    USBH_CDC_Receive(&hUSBHost, recv, 16);
}

XSensUSBDriver::~XSensUSBDriver()
{
}

And my folder structure looks like this:

├── include
│   └── README
├── lib
│   ├── README
│   └── XSensUSBDriver
│   | ├── usbh_lib
│   | │   ├── usb_conf.c
│   | │   ├── usbh_cdc.c
│   | │   ├── usbh_cdc.h
│   | │   ├── usbh_conf.h
│   | │   ├── usbh_conf_template.c
│   | │   ├── usbh_conf_template.h
│   | │   ├── usbh_core.c
│   | │   ├── usbh_core.h
│   | │   ├── usbh_ctlreq.c
│   | │   ├── usbh_ctlreq.h
│   | │   ├── usbh_def.h
│   | │   ├── usbh_ioreq.c
│   | │   ├── usbh_ioreq.h
│   | │   ├── usbh_pipes.c
│   | │   └── usbh_pipes.h
│   | ├── XSensUSBDriver.cpp
│   | └── XSensUSBDriver.h
├── platformio.ini
├── src
│   └── main.cpp
└── test
└── README

Any help would be appriciated.

Sorry for my english though

And also I've checked #687 , are there any estimations when the USB Host library will be supported on stm32duino core ?

@fpistm
Copy link
Member

fpistm commented Dec 5, 2019

Hi @senceryazici
First you will have to add the USB Host library in the path like for the Device one:

compiler.stm.extra_include="-I{build.source.path}" "-I{build.core.path}/avr" "-I{build.core.path}/stm32" "-I{build.core.path}/stm32/LL" "-I{build.core.path}/stm32/usb" "-I{build.core.path}/stm32/usb/hid" "-I{build.core.path}/stm32/usb/cdc" "-I{build.system.path}/Drivers/{build.series}_HAL_Driver/Inc" "-I{build.system.path}/Drivers/{build.series}_HAL_Driver/Src" "-I{build.system.path}/{build.series}" "-I{build.system.path}/Middlewares/ST/STM32_USB_Device_Library/Core/Inc" "-I{build.system.path}/Middlewares/ST/STM32_USB_Device_Library/Core/Src"

Then to define HAL_HCD_MODULE_ENABLED you have to use the hal_conf_extra.h:
HAL-configuration

Then, you will have to wrap source files from USB Host library like it is for Device one:
https://github.com/stm32duino/Arduino_Core_STM32/blob/master/cores/arduino/stm32/usb/usb_device_core.c

Then add all files required for USB host (like it has been done for USB device).

@fpistm fpistm added help wanted 🙏 Extra attention is needed New feature labels Dec 5, 2019
@fpistm
Copy link
Member

fpistm commented Dec 5, 2019

And also I've checked #687 , are there any estimations when the USB Host library will be supported on stm32duino core ?

Currently not. Time is always missing ingredients.
Any contributions are welcome.

I close this one as now added to #687 list

@fpistm fpistm closed this as completed Dec 5, 2019
@fpistm fpistm added enhancement New feature or request and removed Request labels Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted 🙏 Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants