USB In MQX

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

USB In MQX

Jump to solution
2,062 Views
asiad
Contributor I

Hi All,

 

I have a USB device class
3, subclass 0 and protocol 0. I need to communicate it with kit twrmcf52259.
I’m in trouble to do it, can you help me? I'm following the general steps from
manual MQXUSBHOSTAPIRM.pdf (page 8):

 

1) _usb_host_init

Status : USB_OK

 

2) _usb_host_open_pipe

PIPE_INIT_PARAM_STRUCT

pipe_s_struct.DEV_INSTANCE= &host_handle;

pipe_s_struct.INTERVAL=40;

pipe_s_struct.MAX_PACKET_SIZE=1024;

pipe_s_struct.NAK_COUNT=0xA;

pipe_s_struct.FIRST_FRAME=0x0;

pipe_s_struct.FIRST_UFRAME=0x0;

pipe_s_struct.FLAGS=0x0;

pipe_s_struct.DEVICE_ADDRESS=0x00;

pipe_s_struct.ENDPOINT_NUMBER=0;

pipe_s_struct.DIRECTION=USB_SEND;

pipe_s_struct.PIPETYPE=USB_INTERRUPT_PIPE;

pipe_s_struct.SPEED=2;

pipe_s_struct.TRS_PER_UFRAME=1;

Status: Open pipe OK!

 

3) _usb_host_send_data

TR_INIT_PARAM_STRUCT

tr_send.TR_INDEX= 0x1;

tr_send.TX_BUFFER= tx_buffer;

tr_send.RX_BUFFER= rx_buffer;

tr_send.TX_LENGTH= 0x4;

tr_send.RX_LENGTH= 0x4;

tr_send.CALLBACK= pipe_end_tx;

tr_send.CALLBACK_PARAM= 0x0;

tr_send.DEV_REQ_PTR= dev_req;

Status: USB_STATUS_TRANSFER_QUEUED

 

4) _usb_host_recv_data

status:
USB_STATUS_TRANSFER_QUEUED

pipe_end_tx (função do
TR_INIT_PARAM_STRUCT)

status=97

97: USBERR_TR_FAILED

pipe_end_rx

status=97

97: USBERR_TR_FAILED

 

 

Waiting for an answer,

 

Best regards!

 

Daísa de Lima

 

 

P.S. If it’s necessary, the
code is below:

 

/**HEADER********************************************************************

*

* Copyright
(c) 2008 Freescale Semiconductor;

* All Rights
Reserved

*

* Copyright
(c) 1989-2008 ARC International;

* All Rights
Reserved

*

***************************************************************************

*

* THIS
SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR

* IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES

* OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.

* IN NO EVENT
SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,

* INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES

* (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

* SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)

* HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,

* STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING

* IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF

* THE
POSSIBILITY OF SUCH DAMAGE.

*

*END************************************************************************/

#include "main.h"

#include "usb_classes.h"

static
_usb_host_handle host_handle; /* Global handle for
calling host */

static
_usb_pipe_handle pipe_s;

static
_usb_pipe_handle pipe_r;

static
USB_STATUS status=USB_OK;

PIPE_INIT_PARAM_STRUCT
pipe_s_struct;

PIPE_INIT_PARAM_STRUCT
pipe_r_struct;

TR_INIT_PARAM_STRUCT
tr_send;

TR_INIT_PARAM_STRUCT
tr_recv;

static
uchar tx_buffer[512];

static
uchar rx_buffer[512];

static
uchar dev_req[512];

extern void usb_host_cdc_acm_event(_usb_device_instance_handle,
_usb_interface_descriptor_handle intf_handle, uint_32);

extern void usb_host_cdc_data_event(_usb_device_instance_handle,
_usb_interface_descriptor_handle intf_handle, uint_32);

extern void usb_host_hid_event(_usb_device_instance_handle,
_usb_interface_descriptor_handle intf_handle, uint_32);

extern void usb_host_msd_event(_usb_device_instance_handle,
_usb_interface_descriptor_handle intf_handle, uint_32);

void usb_host_event(_usb_device_instance_handle,
_usb_interface_descriptor_handle intf_handle, uint_32);

void pipe_end_tx(_usb_pipe_handle,
pointer, uchar_ptr, uint_32, uint_32);

void pipe_end_rx(_usb_pipe_handle,
pointer, uchar_ptr, uint_32, uint_32);

/************************************************************************************

Table of
driver capabilities this application wants to use. See Host API document for

details on
How to define a driver info table.

************************************************************************************/

static
USB_HOST_DRIVER_INFO DriverInfoTable[] =

{

{

{0x00, 0x00}, /* Vendor ID per USB-IF */

{0x00, 0x00}, /* Product ID per manufacturer */

3, /*
Class code */

0, /*
Sub-Class code */

0, /*
Protocol */

0, /*
Reserved */

usb_host_event /* Application call back function */

},

{

{0x00, 0x00}, /* Vendor ID per USB-IF */

{0x00, 0x00}, /* Product ID per manufacturer */

0, /*
Class code */

0, /*
Sub-Class code */

0, /*
Protocol */

0, /*
Reserved */

usb_host_event /* Application call back function */

}

};

CLASS_MAP
class_interface_map[] =

{

{

NULL,

0,

0, 0, 0,

0, 0, 0

}

};

 

/*FUNCTION*----------------------------------------------------------------

*

* Function
Name : usbh_init

* Returned
Value : None

* Comments :

* Initializes
USB host

*END*--------------------------------------------------------------------*/

void usbh_init()

{

USB_STATUS status = USB_OK;

printf("Tamanho do host_handle:%d\n\r", sizeof(host_handle));

printf("Valor host_handle antes:%x\n\r",
(uint_32)host_handle);

_int_disable();

_usb_host_driver_install(0,
(pointer)&_bsp_usb_host_callback_table);

status = _usb_host_init

(HOST_CONTROLLER_NUMBER, /* Use value in header file */

MAX_FRAME_SIZE, /* Frame size per USB spec */

&host_handle); /* Returned pointer */

if (status != USB_OK)

{

printf("USB Host Initialization failed. STATUS: %x\n",
status);

fflush(stdout);

exit(1);

}

printf("Valor host_handle depois:%x\n\r",
(uint_32)host_handle);

/*

** Since we
are going to act as the host driver, register the driver

**
information for wanted class/subclass/protocols

*/

status =
_usb_host_driver_info_register(host_handle, DriverInfoTable);

if (status != USB_OK)

{

printf("Driver Registration failed. STATUS: %x\n",
status);

fflush(stdout);

exit(1);

}

_int_enable();

printf("\nMQX USB\nWaiting for USB device to be
attached...\n");

fflush(stdout);

/* If a
device will be attached, handler in usbh_<class>_handler.c file

** will catch
that event

*/

}

/*FUNCTION*----------------------------------------------------------------

*

* Function
Name : usb_host_event

* Returned
Value : None

* Comments :

* Called when
a device is attached / detached, but not recognized by

* any class
driver.

*END*--------------------------------------------------------------------*/

void usb_host_event

(

/* [IN]
pointer to device instance */

_usb_device_instance_handle
dev_handle,

/* [IN]
pointer to interface descriptor */

_usb_interface_descriptor_handle
intf_handle,

/* [IN] code
number for event causing callback */

uint_32 event_code

)

{

switch
(event_code) {

case
USB_CONFIG_EVENT:

case
USB_ATTACH_EVENT:

printf("----- Aparelho
ligado! -----\n");

fflush(stdout);

pipe_s_struct.DEV_INSTANCE=
&host_handle;

pipe_s_struct.INTERVAL=40;

pipe_s_struct.MAX_PACKET_SIZE=1024;

pipe_s_struct.NAK_COUNT=0xA;

pipe_s_struct.FIRST_FRAME=0x0;

pipe_s_struct.FIRST_UFRAME=0x0;

pipe_s_struct.FLAGS=0x0;

pipe_s_struct.DEVICE_ADDRESS=0x00;

pipe_s_struct.ENDPOINT_NUMBER=0;

pipe_s_struct.DIRECTION=USB_SEND;

pipe_s_struct.PIPETYPE=USB_INTERRUPT_PIPE;

pipe_s_struct.SPEED=2;

pipe_s_struct.TRS_PER_UFRAME=1;

printf("Pipe antes:%x\n\r",
(uint_32)pipe_s);

status=
_usb_host_open_pipe(host_handle,&pipe_s_struct,&pipe_s);

printf("Pipe depois:%x\n\r",
(uint_32)pipe_s);

if(status==USBERR_OPEN_PIPE_FAILED)

printf("\nErro:%x\n\r",
status);

else{

printf("Open pipe OK!!!%x\n\r", status);}

pipe_r_struct.DEV_INSTANCE=
&host_handle;

pipe_r_struct.INTERVAL=40;

pipe_r_struct.MAX_PACKET_SIZE=1024;

pipe_r_struct.NAK_COUNT=0xA;

pipe_r_struct.FIRST_FRAME=0x0;

pipe_r_struct.FIRST_UFRAME=0x0;

pipe_r_struct.FLAGS=0x0;

pipe_r_struct.DEVICE_ADDRESS=0x00;

pipe_r_struct.ENDPOINT_NUMBER=0;

pipe_r_struct.DIRECTION=USB_RECV;

pipe_r_struct.PIPETYPE=USB_INTERRUPT_PIPE;

pipe_r_struct.SPEED=2;

pipe_r_struct.TRS_PER_UFRAME=1;

printf("Pipe antes:%x\n\r",
(uint_32)pipe_r);

status=
_usb_host_open_pipe(host_handle,&pipe_r_struct,&pipe_r);

printf("Pipe depois:%x\n\r",
(uint_32)pipe_r);

if(status==USBERR_OPEN_PIPE_FAILED)

printf("\nErro:%x\n\r",
status);

else{

printf("Open pipe OK!!!%x\n\r", status);

}

tr_send.TR_INDEX= 0x1;

tr_send.TX_BUFFER=
tx_buffer;

tr_send.RX_BUFFER=
rx_buffer;

tr_send.TX_LENGTH= 0x4;

tr_send.RX_LENGTH= 0x4;

tr_send.CALLBACK=
pipe_end_tx;

tr_send.CALLBACK_PARAM=
0x0;

tr_send.DEV_REQ_PTR=
dev_req;

tx_buffer[0]=0x12;

tx_buffer[1]=0x16;

tx_buffer[2]=0x18;

tx_buffer[3]=0x20;

status=_usb_host_send_data(host_handle,pipe_s,&tr_send);

printf("Status send_data:%x\n\r", status);

status=_usb_host_get_transfer_status(pipe_s,
0x1);

printf("Status get_transfer_status:%x\n\r",
status);

// Retorno
get_transfer:

//
USB_STATUS_IDLE (no transfer is queued or completed)-> USB_STATUS_IDLE (0)

//
USB_STATUS_TRANSFER_QUEUED (transfer is queued, but is not in progress)->
USB_STATUS_TRANSFER_QUEUED(7)

//
USB_STATUS_TRANSFER_IN_PROGRESS (transfer is queued in the hardware and is in

//
progress)-> USB_STATUS_TRANSFER_IN_PROGRESS(3)

//
USBERR_INVALID_PIPE_HANDLE (error; pipe_handle is not
valid)->USBERR_INVALID_PIPE_HANDLE (0x8F) 143

_time_delay(8000);

tr_recv.TR_INDEX= 0x1;

tr_recv.TX_BUFFER=
tx_buffer;

tr_recv.RX_BUFFER=
rx_buffer;

tr_recv.TX_LENGTH= 0x4;

tr_recv.RX_LENGTH= 0x4;

tr_recv.CALLBACK=
pipe_end_rx;

tr_recv.CALLBACK_PARAM=
0x0;

tr_recv.DEV_REQ_PTR=
dev_req;

status=_usb_host_recv_data(host_handle,pipe_r,
&tr_recv);

printf("Status recv_data:%x\n\r", status);

_time_delay(3000);

break;

case
USB_DETACH_EVENT:

printf("----- Aparelho
desligado! -----\n");

fflush(stdout);

break;

default:

break;

}

}

//callback function para
fim de tx no pipe

void pipe_end_tx

(

/* [IN]
pointer to pipe */

_usb_pipe_handle
pipe_handle,

/* [IN]
user-defined parameter */

pointer user_parm,

/* [IN]
buffer address */

uchar_ptr buffer,

/* [IN]
length of data transferred */

uint_32 buflen,

/* [IN]
status, hopefully USB_OK or USB_DONE */

uint_32 status

)

{

printf("Pipe_end_tx\n\r");

printf("Status tx:%x\n\r", status);

printf("buflen
tx:%x\n\r", buflen);

printf("user_parm tx:%x\n\r",
user_parm);

}

//callback function para
fim de rx no pipe

void pipe_end_rx

(

/* [IN]
pointer to pipe */

_usb_pipe_handle
pipe_handle,

/* [IN]
user-defined parameter */

pointer user_parm,

/* [IN]
buffer address */

uchar_ptr buffer,

/* [IN]
length of data transferred */

uint_32 buflen,

/* [IN]
status, hopefully USB_OK or USB_DONE */

uint_32 status

)

{

printf("Pipe_end_rx\n\r");

printf("Status rx:%x\n\r", status);

printf("buflen rx:%x\n\r",
buflen);

printf("user_parm rx:%x\n\r",
user_parm);

printf("pipe_handle rx:%x\n\r",
pipe_handle);

printf("Dado
recebido 0:%x\n\r", *buffer);

printf("Dado
recebido 1:%x\n\r", *(buffer+1));

printf("Dado
recebido 2:%x\n\r", *(buffer+2));

printf("Dado
recebido 3:%x\n\r", *(buffer+3));

}

 

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
1,045 Views
JuroV
NXP Employee
NXP Employee

There is one issue...

You can get err. code 97 when you receive less bytes than requested.

To distinguish if there was really transfer error, the buffer pointer in the callback should be NULL. If it is not, then buffer contains valid data and the size contains number of bytes received.

View solution in original post

0 Kudos
Reply
1 Reply
1,046 Views
JuroV
NXP Employee
NXP Employee

There is one issue...

You can get err. code 97 when you receive less bytes than requested.

To distinguish if there was really transfer error, the buffer pointer in the callback should be NULL. If it is not, then buffer contains valid data and the size contains number of bytes received.

0 Kudos
Reply