Reading Multiple Bytes of data using USB CDC on SAM D21 Xplained Pro.

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm working USB CDC on SAM D21 Xplained Pro.

 

Application:

-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.

-Documentation and framework

 

Issue:

-From the host it is sending data as a string if host is sending data "ATMEL" 10 times device is receiving the data 10 time but only able to read "A" the first character for 10 time.

-The remaining 4 characters are missing.

 

Requirement:

-Need to receive the entire string and how can i read the data without missing?

 

Here i'm adding the code

static void usart_tx_callback(struct usart_module *const module)
{
	tx_callback_flag = 1;
	/* Data ready to be sent */
	if (udi_cdc_is_rx_ready()) {
		/* Transmit next data */
		ui_com_rx_start();
		tx_data = udi_cdc_getc();
		usart_write_buffer_job(&usart_module_edbg, &tx_data, 5);
	} else {
		
		usart_disable_callback(&usart_module_edbg, USART_CALLBACK_BUFFER_TRANSMITTED);
		ui_com_rx_stop();
	}
	tx_callback_flag = 0;
}



static void usart_rx_callback(struct usart_module *const module)
{
	/* Data received */
	ui_com_tx_start();

	/* Transfer UART RX fifo to CDC TX */
	if (!udi_cdc_is_tx_ready()) {
		/* Fifo full */
		udi_cdc_signal_overrun();
		ui_com_overflow();
	} else {
		udi_cdc_putc(rx_data);
	}

	ui_com_tx_stop();

	usart_read_buffer_job(&usart_module_edbg, &rx_data, 5);

	return;
}




void uart_rx_notify(uint8_t port)
{
	UNUSED(port);
	if (!tx_callback_flag) {
		/* Transmit first data */
		ui_com_rx_start();
		usart_enable_callback(&usart_module_edbg, USART_CALLBACK_BUFFER_TRANSMITTED);
		tx_data = udi_cdc_getc();   
		rx_data=tx_data; 
		udi_cdc_putc(rx_data);                                                  //Waits and gets a value on CDC line. 
		usart_write_buffer_job(&usart_module_edbg, &tx_data, 5); //Asynchronous buffer write.Sets up the driver to write a given buffer over the USART.      If                                                                        if registered and enabled, a callback function will be called.
                                         
	}
}

Changes Made:

-The changes i made in this code is changed the Data buffer length from 1 to 5.

-Added a udi_cdc_putc(rx_data);  in uart_rx_notify(uint8_t port) function to send back the data.

-udi_cdc_getc() & udi_cdc_putc() functions will only read and write one character.

 

Requirement:

-Instead of this get() & put() functions which functions can i use?

-What changes i need to make to the code to read multiple bytes?

 

Any help will be appreciated.

 

 

Last Edited: Thu. Dec 6, 2018 - 01:13 PM