Split from: Unable to read more the 1 Byte using ATSAMD21 USB CDC

Go To Last Post
34 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hallo

i am still not able to fix my bug with data transfer from usb to my xml buffer my intention is to copy data from usb rx_buf to xml buf and after that i would like to do xml parsing

i am writing a bootloader for my firmware update my boot section is writing asw ell as my application section which is from 4600 is working but my firmware signature section is showing ff

void fw_update_main()
{
    uint8_t clearXmlBuffer = 1;
    volatile XMLparser_StatusExtTypeDef xmlReq;
    StatusTypeDef status = Status_UNKNWON;
    //delay_ms(10000);
    usb_init();
    boot_data_sector_update();
    for(uint8_t i = 0; i < sizeof(Boot_data_sector.HW_Serial); i++)
    hw_serial_string[i] = Boot_data_sector.HW_Serial[i];
    hw_serial_string[sizeof(hw_serial_string)-1] = '\0';
    uint8_t xml_start_index = xmlBuf;
    while(1)
  {
            
            if(usb_rx_buf != 0) //checking if data is available
            
            
            xml_start_index = memcpy(xmlBuf, usb_rx_buf,sizeof(usb_rx_buf));   //copying Usb data to xml Buffer
            //if(usb_rv_index >= sizeof(usb_rx_buf) )
            //usb_rv_index ++;
            
            xmlReq == XMLparser_parse_str((uint8_t*)xmlBuf, &XMLParameter);
            clearXmlBuffer = 1;

after this my switch statements for xml parsing starts

i really need help to move forwaerd

Thankyou

           

Attachment(s): 

Last Edited: Fri. Oct 25, 2019 - 02:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can't keep spamming random unrelated threads with this question. Make you own thread or continue on one you have with more information. For example, you can't really expect all readers to know what usb_rx_buf is. Attach the project maybe.

/Lars

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i am using samr21e19a

my purpose is to read data from usb and store it in  a new buffer

 

while(1)
  {
            
            if(usb_rx_buf != 0) //checking if data is available
            
            
            xml_start_index = memcpy(xmlBuf, usb_rx_buf,sizeof(usb_rx_buf));   //copying Usb data to xml Buffer
           xml_start_index++;

This is the way am trying to do it but am not able to get the usb data

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As mentioned I don't know what usb_rx_buf is. You have to show more code or the whole project. And use the "Add Code" button when you post code, it looks like "<>".

while(1)
{
    if(usb_rx_buf != 0) //checking if data is available
        xml_start_index = memcpy(xmlBuf, usb_rx_buf,sizeof(usb_rx_buf));   //copying Usb data to xml Buffer
    xml_start_index++;

You compare usb_rx_buf with 0 (NULL) which indicates it is a pointer to some data and a pointer that can be NULL. But when you memcpy you use sizeof(usb_rx_buf) which rather suggests it is an array. Something is wrong.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

there are so many other files also usb_rx_buf is the buffer we use to receive data from usb i have attached my main code myaim for now is to transfer content of usb buffer to xml buffer

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

moderator please delet post of mine on oct 25

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The indent is all over the place in that attachment, I sure hope that is not what it looks like in your editor.


    { 
        if(usb_rx_buf[USB_RX_BUFFER_SIZE_BYTE]!= 0) //checking if data is available

 

This is not any better than post #1, you are looking at a byte outside the array (the index goes from 0 to USB_RX_BUFFER_SIZE_BYTE-1). Seems you have problems with arrays and how to use them. Here is another example:

cdcdf_acm_write(xmlBuf,xmlBuf[5000]);

Don't you know how many bytes are in the buffers? For usb_rx_buf this maybe:

 //Number of bytes in receive buffer
volatile uint16_t usb_rx_count=0;

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

usb_rx_buf is 64 bytes

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The size yes, but how much is in the buffer? Is it always full?

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

xmlbuf size is 5000

i have to check whether usb data is available and is not empty if it is not empty i should copy it to xml buf

yes usb has data always

Last Edited: Mon. Oct 28, 2019 - 01:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You also need to know when there is enough data in xmlBuf that it makes sense to call XMLparser_parse_str.

yes usb has data always

I doubt that.

/Lars

 

Last Edited: Mon. Oct 28, 2019 - 01:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes exactly and i made a change

if(usb_rx_buf[USB_RX_BUFFER_SIZE_BYTE] != 0)

i hope since it is a array it will make sense

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No I already wrote about that in post #7, not ok.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so how can i check the receiving buffer is empty or not . Could you please help me with this

usb_rx_buf is the usb buffer and usb_rx_buffer_size_byte is 64 bytes

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Obviously no one can help with that unless you show the code that fills the buffer.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include "atmel_start.h"
#include "usb_start.h"
#include "usb.h"


#if CONF_USBD_HS_SP
static uint8_t single_desc_bytes[] = {
	/* Device descriptors and Configuration descriptors list. */
CDCD_ACM_HS_DESCES_LS_FS};
static uint8_t single_desc_bytes_hs[] = {
	/* Device descriptors and Configuration descriptors list. */
CDCD_ACM_HS_DESCES_HS};

#else



static uint8_t single_desc_bytes[] = {
    /* Device descriptors and Configuration descriptors list. */
    CDCD_ACM_DESCES_LS_FS};
#define CDCD_ECHO_BUF_SIZ CONF_USB_CDCD_ACM_DATA_BULKIN_MAXPKSZ
#endif

static struct usbd_descriptors single_desc[]
    = {{single_desc_bytes, single_desc_bytes + sizeof(single_desc_bytes)}
#if CONF_USBD_HS_SP
       ,
       {single_desc_bytes_hs, single_desc_bytes_hs + sizeof(single_desc_bytes_hs)}
#endif
};

/** Buffers to receive and echo the communication bytes. */


 uint32_t usbd_cdc_buffer[CDCD_ECHO_BUF_SIZ / 4];

/** Ctrl endpoint buffer */
static uint8_t ctrl_buffer[64];

/**
 * \brief Callback invoked when bulk OUT data received
 */
 static bool usb_device_cb_bulk_out(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count)
{
	
	 uint32_t rx_cnt = count;
	 uint8_t *data = (uint8_t *)usbd_cdc_buffer;
	//cdcdf_acm_write((uint8_t *)usbd_cdc_buffer, 0);

	while(rx_cnt-- != 0)
	{
		usb_rx_buf[usb_rx_index++] = *data++;
		if(usb_rx_index >= sizeof(usb_rx_buf) )
			usb_rx_index = 0;
	}
	
	usb_rx_cnt += count;	
    
	/* No error. */
	return false;
}

/**
 * \brief Callback invoked when bulk IN data received
 */
static bool usb_device_cb_bulk_in(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count)
{
	/* Echo data. */
	cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer));

	/* No error. */
	return false;
}

/**
 * \brief Callback invoked when Line State Change
 */
static bool usb_device_cb_state_c(usb_cdc_control_signal_t state)
{
	//if (state.rs232.DTR )		// Note: not every terminal / connection sends DTR
	{
		/* Callbacks must be registered after endpoint allocation */
		cdcdf_acm_register_callback(CDCDF_ACM_CB_READ, (FUNC_PTR)usb_device_cb_bulk_out);
		cdcdf_acm_register_callback(CDCDF_ACM_CB_WRITE, (FUNC_PTR)usb_device_cb_bulk_in);
		/* Start Rx */
		cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer));
	}	

	

	/* No error. */
	return false;
}

/**
 * \brief CDC ACM Init
 */
void cdc_device_acm_init(void)
{
	/* usb stack init */
	usbdc_init(ctrl_buffer);

	/* usbdc_register_funcion inside */
	cdcdf_acm_init();

	usbdc_start(single_desc);
	usbdc_attach();
}

/**
 * Example of using CDC ACM Function.
 * \note
 * In this example, we will use a PC as a USB host:
 * - Connect the DEBUG USB on XPLAINED board to PC for program download.
 * - Connect the TARGET USB on XPLAINED board to PC for running program.
 * The application will behave as a virtual COM.
 * - Open a HyperTerminal or other COM tools in PC side.
 * - Send out a character or string and it will echo the content received.
 */
void cdcd_acm_example(void)
{
	while (!cdcdf_acm_is_enabled()) {
		// wait cdc acm to be installed
	};

	cdcdf_acm_register_callback(CDCDF_ACM_CB_STATE_C, (FUNC_PTR)usb_device_cb_state_c);

	//while (1) {
	//}
}

void usb_init(void)
{

	cdc_device_acm_init();
}

 

this is the cdc_acm.c file am using the bulk out function to read the usb data i made modifications  to the bulk out function to get the usb data and i copied to xml buffer

Am completely new to this your help and guidance will be really appreciated

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes you are right i was not receiving nay usb data

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Possibly you can read directly into your buffer

        /* Start Rx */
        cdcdf_acm_read(usb_rx_buf, sizeof(usb_rx_buf));

And in the out callback you just save the count

static bool usb_device_cb_bulk_out(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count)
{
    usb_rx_cnt = count;	

    /* No error. */
    return false;
}

Check for data

        if (usb_rx_cnt != 0) { //checking if data is available
           // copy to xml buffer etc
           .
           .
           .
           usb_rx_cnt = 0;
           // Read more
           cdcdf_acm_read(usb_rx_buf, sizeof(usb_rx_buf));
        }

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you for your solution

but i am getting  conflicting type error for usb_device_cb_bulk_out

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What? Where exactly?

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
static bool usb_device_cb_bulk_out(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count)

This is throwing error of conflicting types

Also my application adress is from 00004600-0003ffff

but from 4600 till  0000A31C i am getting data after that its ff ff till 3ffef after that ?? ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is throwing error of conflicting types

I can't guess what this is, you should show the actual error text.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Severity    Code    Description    Project    File    Line
Error        invalid storage class for function 'usb_device_cb_bulk_out'

 

 

this is the error message

static  bool usb_device_cb_bulk_out(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t usb_rx_cnt)

this is a function used in another file so when i call it here it showing error with ep and rc as well

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it is also showing expected expression before const

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
usb_device_cb_bulk_out

is a function from post #16, edit that file, don't add the function elsewhere.

it is also showing expected expression before const

Again not enough information, where is this? What line?

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

void fw_update_main()
{
    
    uint8_t clearXmlBuffer = 1;
    volatile XMLparser_StatusExtTypeDef xmlReq;
    StatusTypeDef status = Status_UNKNWON;
    //delay_ms(10000);
    usb_init();
    boot_data_sector_update();
    
    for(uint8_t i = 0; i < sizeof(Boot_data_sector.HW_Serial); i++)
    hw_serial_string[i] = Boot_data_sector.HW_Serial[i];
    hw_serial_string[sizeof(hw_serial_string)-1] = '\0';
    
    uint8_t xmlBufInd = xmlBuf;
    while(1)
  {
            //delay_ms(10);/* wait until new data is received*/
            cdcdf_acm_read(usb_rx_buf, sizeof(usb_rx_buf)); /*Start Receiving*/
           
            
            if(usb_rx_cnt != 0) //checking if data is available
            
            xmlBufInd = memcpy(xmlBuf, usb_rx_buf,sizeof(usb_rx_buf));   //copying Usb data to xml Buffer
            xmlBufInd +=sizeof(usb_rx_buf);
            usb_rx_cnt = 0;
            //cdcdf_acm_write();
            xmlBuf[xmlBufInd] = '\0';    // Terminate String
            xmlReq = XMLparser_parse_str((uint8_t*)xmlBuf, &XMLParameter);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       xmlReq == XMLparser_parse_str((uint8_t*)xmlBuf, &XMLParameter);
            clearXmlBuffer = 1;
            
            
    switch(xmlReq)

this are the changes but error is now

Severity    Code    Description    Project    File    Line
Error        undefined reference to `usb_rx_cnt'    READ41107A00    C:\Program Files (x86)\Atmel\Studio\7.0\Packs\arm\cmsis\5.4.0\CMSIS\Core\Include\core_cm0plus.h    993

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

After checking data availabilioty   if(usb_rx_cnt != 0) //checking if data is available

it is jumping to

 xmlBuf[xmlBufInd] = '\0';    // Terminate String
  xmlReq = XMLparser_parse_str((uint8_t*)xmlBuf,

Please tell me why it is not copying data

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

usb_rx_count then, it's in your posted file, change it to uint32_t, (btw, this is not really a problem you should need to post a question about).

 

        /* Start Rx */
        cdcdf_acm_read(usb_rx_buf, sizeof(usb_rx_buf));

this start is not for your while loop, it should replace that call in code from post #16

The while loop needs to have this structure and you should use the usb_rx_count not sizeof(usb_rx_buf) 

        if (usb_rx_count != 0) { <---------------------- notice the '{', it's not optional
           // copy usb_rx_count bytes to xml buffer etc
           .
           .
           .
           usb_rx_count = 0;
           // Read more
           cdcdf_acm_read(usb_rx_buf, sizeof(usb_rx_buf));
        }

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your reply

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But now it is not checking usb_rx_cnt ! = 0

if(usb_rx_cnt != 0) //checking if data is available
            {
            xmlBufInd = memcpy(xmlBuf, usb_rx_buf,usb_rx_cnt);   //copying Usb data to xml Buffer
            xmlBufInd += usb_rx_cnt;
            usb_rx_cnt = 0;
            cdcdf_acm_read(usb_rx_buf, sizeof(usb_rx_buf));
            }
            //cdcdf_acm_write();
            xmlBuf[xmlBufInd] = '\0';    // Terminate String
            xmlReq = XMLparser_parse_str((uint8_t*)xmlBuf,    &XMLParameter);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       xmlReq == XMLparser_parse_str((uint8_t*)xmlBuf, &XMLParameter);
            clearXmlBuffer = 1;

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you make a new variable usb_rx_cnt? Is it volatile?

Do you really think you can call XMLparser_parse_str all the time in the while loop? There is at least no need to call it if there is no new data.

BTW, Are you happy with that indent? And please use the code button "<>"

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes usb_rx_cnt i have defined as volatile uint32_t

In memcpy if i change Usb_rx_buf_size to usb_rx_cnt

it is not making any changes

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

usb_rx_cnt is getting 0

I dont know what makes it so

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thankyou for your suggestions

but do you think it is good if i place the xmlparser_parse_str outside the while loop