USB CDC payload overflow error after 70 packets

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

Hi Guys,

 

I have set up a basic USB application on an SAMD11 following the application note. I have just created a simple application that accepts a full payload through the rx callback, checks for the termination character and sends this payload back. I started cutting down my application after seeing some inconsistencies with what I was sending and what I was getting back.

 

I am running a simple c# console app and using the driver for my device that comes with AS7.

 

The console app sends the following line:

{ 2, 1, 20, 3, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 181, 2, 3 };

 

to my device, my device sends this packet back and i check if there are any differences. The interesting thing is that I have a wireshark capture running to watching the packets to see what is happening and I can see that after roughly 70 packets, all of a sudden the application send back 160 byte packets 40 times. Once I read these packets on my c# app, it sends several hundred packets of 160 bytes.

 

I can see that the data in this packet is made up from my previous transmissions, but somehow it has all appended into a single large payload :(

 

Looking at my code I honestly can't see what I have done wrong.

 


#define UART_BUF_LEN 600

typedef struct
{
	struct
	{
		uint8_t Buffer[UART_BUF_LEN];
		volatile uint32_t Head;
		volatile uint32_t Tail;
	} Rx;
	
	uint32_t ToProcess_Index;
	uint8_t ToProcess_Buffer[UART_BUF_LEN];
	uint16_t TxIndex;

} _UART;
extern _UART UART;

int main (void)
{
	irq_initialize_vectors();
	cpu_irq_enable();

	system_init();

	// Start USB stack to authorize VBus monitoring
	udc_start();
	uint8_t magicNum = 0;

	while (1)
	{
		while (UART.Rx.Head != UART.Rx.Tail)
		{
			uint8_t byte = UART.Rx.Buffer[UART.Rx.Tail++];
			
			if (UART.Rx.Tail == UART_BUF_LEN)
			{
				UART.Rx.Tail = 0;
			}

			UART.ToProcess_Buffer[UART.ToProcess_Index++] = byte;
			if (magicNum)
			{
				magicNum = 0;
				
				if (byte == 0x03)
				{
					//end send data
					while (!udi_cdc_is_tx_ready());

					udi_cdc_write_buf(UART.ToProcess_Buffer, UART.ToProcess_Index);
					
					UART.ToProcess_Index = 0;
				}
			}
			else
			{
				if (byte == 0x02)
				{
					magicNum = 1;
				}
			}
		}
	}
}

void my_callback_rx_notify(uint8_t port)
{
	iram_size_t Number_of_Rcvd_Bytes = udi_cdc_get_nb_received_data();
	while (Number_of_Rcvd_Bytes--)
	{
		if (UART.Rx.Head == UART_BUF_LEN)
		{
			UART.Rx.Head = 0;
		}
		
		uint8_t ch = udi_cdc_getc();
		
		UART.Rx.Buffer[UART.Rx.Head++] = ch;
	}
}

 

Can anyone see anything wrong with the above code?

 

Also not sure if this means anything but I am running my DFLL clock in recovery mode. I couldn't get closed loop working...to be honest, I still dont fully understand the difference between closed, open and recovery angel

 

Any assistance would be awesome!

 

Ryan.