Allocating a receiver buffer for variable size of data from host in USB communication.

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

I'm working USB CDC on SAM D21 Xplained Pro. My application is to read the string of data send by the host to the device and send back the same  data to the host.

-I'm working using atmel studio 7.0, the code which i'm using is USB  Communication Device Class (CDC)  for ATSAMD21.

 

I was tracking down the read call iram_size_t udi_cdc_multi_read_buf(uint8_t port, void* buf, iram_size_t size) in this function i'm getting complete string from the host in "udi_cdc_rx_buf[port][buf_sel][pos] "   in this call--> memcpy(ptr_buf, &udi_cdc_rx_buf[port][buf_sel][pos], copy_nb);  

 

-->It will only copy the allocate size in the udi_cdc_read_buf(buf,size) to the "ptr_buf".

please check the code flow
udi_cdc_read_buf((void *)usb_data,len); ---->Code flow

-------->Code flow
iram_size_t udi_cdc_read_buf(void* buf, iram_size_t size)
{
	return udi_cdc_multi_read_buf(0, buf, size);
}


-------->Code flow
iram_size_t udi_cdc_multi_read_buf(uint8_t port, void* buf, iram_size_t size)
{
    irqflags_t flags;
    uint8_t *ptr_buf = (uint8_t *)buf;
    iram_size_t copy_nb;
    uint16_t pos;
    uint8_t buf_sel;
    bool again;
#if UDI_CDC_PORT_NB == 1 // To optimize code
    port = 0;
#endif

udi_cdc_read_buf_loop_wait:
    // Check available data
    flags = cpu_irq_save();
    pos = udi_cdc_rx_pos[port];
    buf_sel = udi_cdc_rx_buf_sel[port];
    again = pos >= udi_cdc_rx_buf_nb[port][buf_sel];
    cpu_irq_restore(flags);
    while (again) {
        if (!udi_cdc_data_running) {
            return size;
        }
        goto udi_cdc_read_buf_loop_wait;
    }
    // Read data
copy_nb = udi_cdc_rx_buf_nb[port][buf_sel] - pos;
if (copy_nb>size) {
        copy_nb = size;
    }
    memcpy(ptr_buf, &udi_cdc_rx_buf[port][buf_sel][pos], copy_nb); // BUFFER WITH DATA
    udi_cdc_rx_pos[port] += copy_nb;
    ptr_buf += copy_nb;
    size -= copy_nb;
    udi_cdc_rx_start(port);
    if (size) {
        goto udi_cdc_read_buf_loop_wait;
    }
    return 0;
}

Here in the memcpy it is copying the data to the ptr_buf. 

 copy_nb-->Size of buffer to read.  It is set in the udi_cdc_read_buf(buff,size) (Please check the code flow)

IS there any way to copy the entire  data in udi_cdc_rx_buf[port][buf_sel][pos]  to another buffer without losing any data.

 

 

Last Edited: Wed. Jan 23, 2019 - 01:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

Topic locked