AnsweredAssumed Answered

USB Host CDC

Question asked by berland.guillaume on Jul 25, 2016
Latest reply on Jul 26, 2016 by berland.guillaume
Hi,

I want to use the USB Host CDC.
For this I use the HAL library and the middleware layer

Here is my code:

01.#include "uart_USB.h"
02.#include "usb_host.h"
03.#include "usbh_core.h"
04.#include "usbh_cdc.h"
05. 
06.USBH_HandleTypeDef hUsbHostFS;
07. 
08.static USBH_StatusTypeDef USB_SetLineCoding(USB_CommunicationParam_t param);
09. 
10.static void USB_Process(USBH_HandleTypeDef *phost, uint8_t id);
11. 
12.USBH_StatusTypeDef USB_Init(USB_CommunicationParam_t param)
13.{
14.    USBH_StatusTypeDef returnCode = USBH_OK;
15.    if((returnCode = USBH_Init(&hUsbHostFS, USB_Process, HOST_FS)) != USBH_OK){
16.        return returnCode;
17.    }
18.    if((returnCode = USBH_RegisterClass(&hUsbHostFS, USBH_CDC_CLASS)) != USBH_OK){
19.        return returnCode;
20.    }
21.    if((returnCode = USBH_Start(&hUsbHostFS)) != USBH_OK){
22.        return returnCode;
23.    }
24.    if((returnCode = USB_SetLineCoding(param)) != USBH_OK){
25.        return returnCode;
26.    }
27.    return returnCode;
28.}
29. 
30.static void USB_Process(USBH_HandleTypeDef *phost, uint8_t id)
31.{
32.    switch(id){
33.        case HOST_USER_SELECT_CONFIGURATION:
34.        break;
35. 
36.        case HOST_USER_DISCONNECTION:
37.        break;
38. 
39.        case HOST_USER_CLASS_ACTIVE:
40.        break;
41. 
42.        case HOST_USER_CONNECTION:
43.        break;
44. 
45.        default:
46.        break;
47.    }
48.}
49. 
50.static USBH_StatusTypeDef USB_SetLineCoding(USB_CommunicationParam_t param){
51.    CDC_LineCodingTypeDef lineCoding;
52.    lineCoding.b.dwDTERate = param.baudrate;
53.    lineCoding.b.bCharFormat = param.stopBits;
54.    lineCoding.b.bParityType = param.parity;
55.    lineCoding.b.bDataBits = param.dataBits;
56.    return USBH_CDC_SetLineCoding(&hUsbHostFS, &lineCoding);
57.}

01.#ifndef UART_USB
02.#define UART_USB
03. 
04.    #include "stdint.h"
05. 
06.    typedef enum{
07.        STOP_BIT_1 = 0,         /*!< 1 Stop bit */
08.        STOP_BIT_1_HALF = 1,    /*!< 1.5 Stop bits */
09.        STOP_BIT_2 = 2          /*!< 2 Stop bits */
10.    }USB_StopBits_e;
11. 
12.    typedef enum{
13.        NONE = 0,
14.        ODD = 1,
15.        EVEN = 2,
16.        MARK = 3,
17.        SPACE = 4
18.    }USB_Parity_e;
19. 
20.    typedef enum{
21.        DATABITS_5 = 5,
22.        DATABITS_6 = 6,
23.        DATABITS_7 = 7,
24.        DATABITS_8 = 8,
25.        DATABITS_16 = 16
26.    }USB_DataBits_e;
27. 
28.    typedef struct{
29.        uint32_t baudrate;
30.        USB_StopBits_e stopBits;
31.        USB_Parity_e parity;
32.        USB_DataBits_e dataBits;
33.    }USB_CommunicationParam_t;
34. 
35.#endif /* UART_USB */

I'm looking for the right way to use functions :
    - USBH_StatusTypeDef USBH_CDC_Transmit(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length);
    - USBH_StatusTypeDef USBH_CDC_Receive(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length);
    - uint16_t USBH_CDC_GetLastReceivedDataSize(USBH_HandleTypeDef *phost);
    - USBH_StatusTypeDef  USBH_CDC_Stop(USBH_HandleTypeDef *phost);
    - void USBH_CDC_LineCodingChanged(USBH_HandleTypeDef *phost);
    - void USBH_CDC_TransmitCallback(USBH_HandleTypeDef *phost);
    - void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost);
    
If you have a code sample that would be perfect.

Thanks

Outcomes