cancel
Showing results for 
Search instead for 
Did you mean: 

CDC_Host does not transmit data

Paulus
Associate II

Hi,

I am struggling for a while to get the CDC_Host to work. Steps that I have taken are:

  1. Create a MX Cube USB Host HS port project
  2. set correct frequencies 8Mhz for HSI, heap=1200 stack 800 (tried various values with no effect)
  3. Generate the code (eWarm and CubeIDE code - no difference when running)
  4. Flash to a Disco407 board
  5. connected the USB host connector to PB14,15 via a USB sniffer to a STM32F103 board

The results I see is that the device enumerates on the host successfully (goes thru all stages and ends with set_configuration1 and getlinecoding packets).

However when I send a USBH_CDC_Transmit API then that packet does not get send out. I did debug the code see below. I did find the point where the transmit fails to initiate the packet based on the CDC_Handle status (incorrect status code e.g. 0x81 instead of CDC_IDLE etc).

main.c routine contains...
  
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
#include "usb_host.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern uint8_t USBH_CDC_Transmit(uint8_t* Buff, uint16_t Lenn);
 
uint8_t buffer[1]= {0x0a};
uint32_t pos32Hs;
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_NVIC_Init(void);
void MX_USB_HOST_Process(void);
 
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USB_DEVICE_Init();
  MX_USB_HOST_Init();
 
  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */ the Systick. */
 
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 
/* I do set the GPIOD pin12 when the USB has been succesfully enumerated in usbh_core.c */
/*  in main.c I wait with sending any data until enumeration is complete
/* Appli=READY seems not indicate a complete enumeration since i still see packets*/
/* usbh_core.c */
/* ..... */
/*     case HOST_CLASS: */
/*       /* process class state machine */
/*       if (phost->pActiveClass != NULL)
/*       {
/*         HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
/*         phost->pActiveClass->BgndProcess(phost);
/*       }
/*      break; */
 
  while (HAL_GPIO_ReadPin(GPIOD,GPIO_PIN_12) != GPIO_PIN_SET) 
// test to ensure enumeration is complete
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
    HAL_Delay(1);
  }
  HAL_Delay(1000);
  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_RESET);
// --> my debug breakpoint is set here
 USBH_CDC_Transmit(buffer, sizeof (buffer));
// end breakpoint
  while(1)
  {
// just add a led to let me know where the code gets while running
    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_SET);
    MX_USB_HOST_Process();
    HAL_Delay(1);
  }
  /* USER CODE END 3 */
}......
 
I followed Breakpoint to
usbh_cdc.c routine
 
**
  * @brief  This function prepares the state before issuing the class specific commands
  * @param  None
  * @retval None
  */
USBH_StatusTypeDef  USBH_CDC_Transmit(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length)
{
  USBH_StatusTypeDef Status = USBH_BUSY;
  CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef *) phost->pActiveClass->pData;
 
  if ((CDC_Handle->state == CDC_IDLE_STATE) || (CDC_Handle->state == CDC_TRANSFER_DATA))
  // --->Here the if then statement breaks... CDC_Handle->state = 0x81 which is not a valid enum state at all e.g. CDC_IDLE_STATE etc.
  {
    CDC_Handle->pTxData = pbuff;
    CDC_Handle->TxDataLength = length;
    CDC_Handle->state = CDC_TRANSFER_DATA;
    CDC_Handle->data_tx_state = CDC_SEND_DATA;
    Status = USBH_OK;
 
#if (USBH_USE_OS == 1U)
    phost->os_msg = (uint32_t)USBH_CLASS_EVENT;
#if (osCMSIS < 0x20000U)
    (void)osMessagePut(phost->os_event, phost->os_msg, 0U);
#else
    (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL);
#endif
#endif
  }
  return Status;
}

0693W000005BR9WQAW.pngSo should CDC_Handle be a static variable? I could not find the assignment again but saw it before that a malloc statement was used. Do I have a memory leak here?

Any help or suggestions would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Paulus
Associate II

Finally found the issue... too much time behind the PC.

The error is in the definition-->extern uint8_t USBH_CDC_Transmit(uint8_t* Buff, uint16_t Lenn);

USBH_CDC_Transmit takes three arguments instead of two (phost is missing).

View solution in original post

1 REPLY 1
Paulus
Associate II

Finally found the issue... too much time behind the PC.

The error is in the definition-->extern uint8_t USBH_CDC_Transmit(uint8_t* Buff, uint16_t Lenn);

USBH_CDC_Transmit takes three arguments instead of two (phost is missing).