cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072 DISCO USB cdc device sending data to PC

aydnouz
Associate II
Posted on February 24, 2014 at 15:51

Hi,

I'm working onSTM32F072-DISCO to send data over USB using cdc device. At this point, i am thinking that i achieved description part. So that i can see the device as a VCP when i plug it. But i m not able to send data over USB yet. These are my config files: usb_conf.h:

#define USB_DEVICE_LOW_PWR_MGMT_SUPPORT 
/* Configure the USB clock source as HSI48 with Clock Recovery System(CRS)*/
#define USB_CLOCK_SOURCE_CRS
/* Endpoints used by the device */
#define EP_NUM (4) 
/* buffer table base address */
#define BTABLE_ADDRESS (0x000)
/* EP0, RX/TX buffers base address */
#define ENDP0_RX_ADDRESS (0x40)
#define ENDP0_TX_ADDRESS (0x80)
#define INT_IN_TX_ADDRESS (0xC0)
#define BULK_IN_TX_ADDRESS (0x100)
#define BULK_OUT_RX_ADDRESS (0x110)

usbd_conf.h:

#define USBD_CFG_MAX_NUM 1
#define USBD_ITF_MAX_NUM 1
//#define USBD_SELF_POWERED
#define USB_MAX_STR_DESC_SIZ 255
/** @defgroup USB_VCP_Class_Layer_Parameter
* @{
*/
#define CDC_IN_EP 0x81 /* EP1 for data IN */
#define CDC_OUT_EP 0x01 /* EP1 for data OUT */
#define CDC_CMD_EP 0x82 /* EP2 for CDC commands */
#define CDC_DATA_MAX_PACKET_SIZE 64 /* Endpoint IN & OUT Packet size */
#define CDC_CMD_PACKET_SZE 8 /* Control Endpoint Packet size */
#define CDC_IN_FRAME_INTERVAL 5 /* Number of frames between IN transfers */
#define APP_RX_DATA_SIZE 2048 /* Total size of IN buffer:
#define APP_FOPS VCP_fops

-------------------------------------------------------------------------------------------------------------------------------------------------- And i'm trying to send data with this routine

VCP_DataTx

in my usbd_cdc_vcp.c file:

extern
uint8_t APP_Rx_Buffer []; 
/* Write CDC received data in this buffer.
These data will be sent over USB IN endpoint
in the CDC core functions. */
extern
uint32_t APP_Rx_ptr_in; 
/* Increment this pointer or roll it back to
start address when writing received data
in the buffer APP_Rx_Buffer. */
/* Private function prototypes -----------------------------------------------*/
static
uint16_t VCP_Init (
void
);
static
uint16_t VCP_DeInit (
void
);
static
uint16_t VCP_Ctrl (uint32_t Cmd, uint8_t* Buf, uint32_t Len);
static
uint16_t VCP_DataTx (uint8_t* Buf, uint32_t Len);
static
uint16_t VCP_DataRx (uint8_t* Buf, uint32_t Len);
//static uint16_t VCP_COMConfig(uint8_t Conf);
CDC_IF_Prop_TypeDef VCP_fops = 
{
VCP_Init,
VCP_DeInit,
VCP_Ctrl,
VCP_DataTx,
VCP_DataRx
};
/* Private functions ---------------------------------------------------------*/
/**
* @brief VCP_Init
* Initializes the Media on the STM32
* @param None
* @retval Result of the opeartion (USBD_OK in all cases)
*/
static
uint16_t VCP_Init(
void
)
{
return
USBD_OK;
}
/**
* @brief VCP_DeInit
* DeInitializes the Media on the STM32
* @param None
* @retval Result of the opeartion (USBD_OK in all cases)
*/
static
uint16_t VCP_DeInit(
void
)
{
return
USBD_OK;
}
/**
* @brief VCP_Ctrl
* Manage the CDC class requests
* @param Cmd: Command code 
* @param Buf: Buffer containing command data (request parameters)
* @param Len: Number of data to be sent (in bytes)
* @retval Result of the opeartion (USBD_OK in all cases)
*/
static
uint16_t VCP_Ctrl (uint32_t Cmd, uint8_t* Buf, uint32_t Len)
{ 
switch
(Cmd)
{
case
SEND_ENCAPSULATED_COMMAND:
break
;
case
GET_ENCAPSULATED_RESPONSE:
break
;
case
SET_COMM_FEATURE:
break
;
case
GET_COMM_FEATURE:
break
;
case
CLEAR_COMM_FEATURE:
break
;
case
SET_LINE_CODING:
break
;
case
GET_LINE_CODING:
break
;
case
SET_CONTROL_LINE_STATE:
/* Not needed for this driver */
break
;
case
SEND_BREAK:
/* Not needed for this driver */
break
; 
default
:
break
;
}
return
USBD_OK;
}
/**
* @brief VCP_DataTx
* CDC received data to be send over USB IN endpoint are managed in 
* this function.
* @param Buf: Buffer of data to be sent
* @param Len: Number of data to be sent (in bytes)
* @retval Result of the opeartion: USBD_OK if all operations are OK else VCP_FAIL
*/
static
uint16_t VCP_DataTx (uint8_t* Buf, uint32_t Len)
{
uint32_t i=0;
GPIO_SetBits(GPIOC, GPIO_Pin_9);
GPIO_SetBits(GPIOC, GPIO_Pin_7);
GPIO_SetBits(GPIOC, GPIO_Pin_8);
GPIO_SetBits(GPIOC, GPIO_Pin_6);
while
(i < Len)
{
APP_Rx_Buffer[APP_Rx_ptr_in] = *(Buf + i);
APP_Rx_ptr_in++;
i++;
/* To avoid buffer overflow */
if
(APP_Rx_ptr_in == APP_RX_DATA_SIZE)
{
APP_Rx_ptr_in = 0;
} 
}
return
USBD_OK;
}
/**
* @brief VCP_DataRx
* Data received over USB OUT endpoint are sent over CDC interface 
* through this function.
* 
* @note
* This function will block any OUT packet reception on USB endpoint 
* untill exiting this function. If you exit this function before transfer
* is complete on CDC interface (ie. using DMA controller) it will result 
* in receiving more data while previous ones are still not sent.
* 
* @param Buf: Buffer of data to be received
* @param Len: Number of data received (in bytes)
* @retval Result of the opeartion: USBD_OK if all operations are OK else VCP_FAIL
*/
static
uint16_t VCP_DataRx (uint8_t* Buf, uint32_t Len)
{
return
USBD_OK;
}
void
oz(
void
){
uint8_t oz[4] = {0x4f,0x67,0x75,0x7a};
VCP_DataTx (oz,4);
}

Unfortunately, these configs didnt work for me or i m missing another thing. I'm really stuck here.

Any help will be highly appreciated.

Best Regards,

Oguz.

#stm32f072 #usb-lib-cdc #usb-cdc-vcp
8 REPLIES 8
chen
Associate II
Posted on February 24, 2014 at 17:15

Hi

''

Unfortunately,  these configs didnt work for me  or  i m missing another thing. I'm really  stuck here. 

Any help will be highly appreciated.

''

Start with the working USB CDC example!

You can download the example code for your board from here :

http://www.st.com/web/en/catalog/tools/PF259446#

There is a USB example. Unfortunately, it is a HID (Human interface device).

Start with this and understand this first!

aydnouz
Associate II
Posted on February 24, 2014 at 17:41

Thanks for reply.Actually i started there, because of that i could manage power, clock and descriptions. And I researched on STM32 USB-FS-Device Library example codes too. Nevertheless, I'm keep looking to it. But this point, I really dont know what is the problem. What should i look into? Interrupt routine, Buffer size, endpoints addresses?

Thanks in Advance,

Oguz.

 

chen
Associate II
Posted on February 25, 2014 at 16:11

''But this point, I really dont know what is the problem. What should i look into? ''

Does your device enumerate correctly when plugged into a PC?

aydnouz
Associate II
Posted on February 25, 2014 at 17:25

i guess it does, im not sure how can i understand that. But here it looks okay:

Device Manager: 0690X00000605XnQAI.png Additionally, i add some piece of code in main.c:

UserToPMABufferCopy((unsigned 
char
*)ozee, INT_IN_TX_ADDRESS, Send_length);
SetEPTxCount(ENDP1, Send_length);
SetEPTxValid(ENDP1);

When i did that it sends data over USB, but after some data sent to PC it sending bunch of zeros like this: 0690X00000605RqQAI.png I'm guessing this is cuz of buffer size. But still im unable to use usbd_cdc_vcp.c andusbd_cdc_core.c to send data.
chen
Associate II
Posted on February 26, 2014 at 15:03

Hi

OK, so the USB stack for the most part is working OK.

It enumerates and data can be transferred.

From the symptoms you describe, it sounds like the code in

usb_cdc_vcp.c is not doing what it is suppose to do.

The buffer 'APP_Rx_Buffer[]' should contain the data to send to the host.

It uses a pointer 'APP_Rx_ptr_in' should point to the end of the buffer when you have inserted what you want to transmit.

How does it work - go search for it in the code and figure it out!

Incidentally, the code as provided uses the same buffer to get data from the host.

I did not like that, so I created a separate buffer for that in my code.

Muhammad Moiz khan
Associate II
Posted on November 07, 2016 at 09:30

Hello,

I am using STM32F070 and using SPL based USB library on my hardware it is enumerating however when I send the data to the PC via USB it is not sending, I have used VCP_DataTx function declared in usbd_cdc_vcp.h.

Declared in usbd_cdc_vcp.h

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

static uint16_t VCP_DataTx (uint8_t* Buf, uint32_t Len)

{

  if (linecoding.datatype == 7)

  {

    APP_Rx_Buffer[APP_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1) & 0x7F;

  }

  else if (linecoding.datatype == 😎

  {

    APP_Rx_Buffer[APP_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1);

  }

  

  APP_Rx_ptr_in++;

  

  /* To avoid buffer overflow */

  if(APP_Rx_ptr_in == APP_RX_DATA_SIZE)

  {

    APP_Rx_ptr_in = 0;

  }  

  

  return USBD_OK;

}

void VCP_put_char(uint8_t buf)

{

VCP_DataTx(&buf,1);

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CODE:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include ''usbd_cdc_core.h''

#include ''usbd_usr.h''

#include ''stm32f0xx_gpio.h''

#include ''usbd_cdc_vcp.h''

#include ''string.h''

void Delay(__IO uint32_t time);

extern __IO uint32_t TimmingDelay;

USB_CORE_HANDLE  USB_Device_dev ;

void GPIO_config(void);

int main(void)

  SystemInit();

  SysTick_Config(SystemCoreClock/1000);

  GPIO_config();

  Delay(300);

  USBD_Init(&USB_Device_dev,&USR_desc,&USBD_CDC_cb,&USR_cb);

  while (1)

  {

 if (USB_Device_dev.dev.device_state==USB_CONFIGURED)

 {

 VCP_put_char('A');

 GPIO_SetBits(GPIOB, GPIO_Pin_2);

 }

 else

 {

 GPIO_SetBits(GPIOB,GPIO_Pin_2);

 Delay(100);

 GPIO_ResetBits(GPIOB,GPIO_Pin_2);

 Delay(100);

 }

  }

}

void GPIO_config(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure;

  /* Enable the GPIO_LED Clock */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  /* Configure the GPIO_LED pin */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

}

__IO uint32_t TimmingDelay;

void SysTick_Handler(void)

{

  if(TimmingDelay !=0)

  {

    TimmingDelay --;

  }

}

void Delay(__IO uint32_t time)

{

TimmingDelay = time;

while(TimmingDelay !=0);

}

Above is the function of VCP_DataTx and my code, I am transmitting using this function, Any help in this regard would be highly appreciated.

Regards,

Muhammad Moiz khan.

Posted on November 07, 2016 at 09:53

That doesn't look like it will send your A character. You'd perhaps want it to send the data from the buffer passed?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Muhammad Moiz khan
Associate II
Posted on November 08, 2016 at 12:38

Issue resolved I forgot to add usbd_cdc_if_template.h in my project. Thanks for the support.

Regards,

Muhammad Moiz khan