Atmel ASF CDC transmission speed not stable

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

 

Hi everyone,

 

Nice to meet you guys. I am a newcomer here:) 

Recently I encountered a strange problem while developing a piece of software using SAMD21G18A + Atmel Studio 7 + ASF3, for my company. I was trying to send data to PC (let's say inside the infinite while loop we send 64 bytes each time then delay 20 ms then repeat), and on the PC side I have a host software that parses the received data. When I tried to measure the sending time I found that it is not stable. In the below figure I create a simple project just focusing on this problem, hope that makes sense:

#include <asf.h>
unsigned long Timer1 = 0;
unsigned long elapsedTimer1 = 0;
uint8_t DataBuffer[64] = {0xEE};
uint8_t crc8Maxim(const uint8_t data[], uint8_t dataLen)
{
	uint8_t len = dataLen;
	uint8_t crc = 0x00;
	while (len--)
	{
		uint8_t extract = *data++;
		for (uint8_t tempI = 8; tempI; tempI--)
		{
			uint8_t sum = (crc ^ extract) & 0x01;
			crc >>= 1;
			if (sum)
			{
				crc ^= 0x8C;
			}
			extract >>= 1;
		}
	}
	return crc;
}

int main (void)
{
	//irq_initialize_vectors();
	//cpu_irq_enable();
	system_init();
	udc_start();
	delay_init();
	/* Insert application code here, after the board has been initialized. */
	while(1)
	{
		DataBuffer[1] = (elapsedTimer1 >> 16) & 0xFF;
		DataBuffer[2] = (elapsedTimer1 >> 8) & 0xFF;
		DataBuffer[3] = elapsedTimer1 & 0xFF;
		DataBuffer[63] = crc8Maxim(DataBuffer, 63);
		Timer1 = SysTick->VAL;
		udi_cdc_write_buf(DataBuffer, 64);
		elapsedTimer1 = Timer1 - SysTick->VAL;
		delay_ms(20);
	}
}

Here is the measuring result, so normally the time period used for sending a package of 64 bytes is around 24 microseconds, but we can see that sometimes it takes almost double time to transfer the same data.

However, when I tried to do the same thing with Arduino framework (so I measured the time using micros()), I got a relatively stable result with measured time always between 22-25 microseconds.

 

The ASF version that I am using is 3.49.1, for delay routines I utilize systick configuration. 

 

 

Anyone can help me with this problem?

 

Sorry for any confusion, my English is not so good.

Kind regards,

 

Xin

 

Last Edited: Mon. Dec 21, 2020 - 10:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please see Tip #1 in my signature, below, for how to post source code - not as images:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry! The original post is modified. Thanks for the information.