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