2020-11-15 07:52 PM
Hi,
I am struggling for a while to get the CDC_Host to work. Steps that I have taken are:
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;
}
So 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.
Solved! Go to Solution.
2020-11-16 02:12 AM
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).
2020-11-16 02:12 AM
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).