ATXMega16E5 USART and SPI problem

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

Hi,

 

I am using SPI to read data from ADC with clock frequency 4MHz. Similarly I am using UART with baudrate setting of 4Mbps.

When I am using UART only, the total time it takes for 2 bytes to be transmitted is 5us ( 20bits / 4Mbps = 5us) which is okay.

When I am using SPI only, the total time it takes for 3 bytes to be read is about 7.6us (total cycle time) which is also okay.

But when I am using both SPI and UART, the total time it takes is around 8.5us which is quite strange. In my opinion the total time should be the sum 5+7.6 = 12.6us.

In my program, first I read the ADC via SPI and then transmit data via UART and it is sequential.

Any ideas?

 

Regards

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Any ideas?

 

Yes.  POST YOUR CODE!

 

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is my code:

 

while(1)
{
PORTC.OUTCLR = PIN4_bm;	// *SS = low

SPIC.DATA = 0x00;  // Send 1. byte
while(!(SPIC.STATUS & 0x80)); // wait for transmit complete

SPIC.DATA = 0x00; // Send 2. byte
rx_byte1 = SPIC_DATA; // Grab first byte from ADC
while(!(SPIC.STATUS & 0x80));  // Wait for transmit complete

SPIC.DATA = 0x00;  // Send 3. byte
rx_byte2 = SPIC_DATA;  // Grab saecond byte from ADC
while(!(SPIC.STATUS & 0x80));  // Wait for transmit complete

rx_byte3 = SPIC_DATA; // grab third byte from ADC			

PORTC.OUTSET = PIN4_bm; // *SS = high

// Wait for empty transmit buffer
while ( !( USARTC0.STATUS & USART_DREIF_bm) );
USARTC0.DATA = rx_byte1; // Put data into buffer and send
// Wait for empty transmit buffer
while ( !( USARTC0.STATUS & USART_DREIF_bm) );
USARTC0.DATA = rx_byte2; // Put data into buffer and send
}

 

 

 

Thanks

Last Edited: Tue. Aug 15, 2017 - 03:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AXM wrote:
the total time it takes...

 

How are you measuring this time?

 

Remember that when you load .DATA that the byte hasn't been transmitted yet.  And I'd have to poke at that particular family's manual and datasheet, but I'd guess at least double-buffered.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With the SPI, your code waits for Tx/Rx to complete.  With the USART, your code waits for .DATA to become empty.  The code doesn't wait for the USART to finish.  Use USART_TXCIF_bm instead of USART_DREIF_bm.

 

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want maximum performance I'd suggest trying to use DMA. Combined with the event system you might be able to offload this entirely from the CPU.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your replies.

 

Yes I am using DMA now. After reading data from the SPI, I trigger DMA manually and then USART data is transmitted. The trigger source is EDMA_CH_TRIGSRC_USARTC0_DRE_gc. So now it is working.

 

Regards