| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Accelerating SPI Interface Writes

Page history last edited by MrTundraMan 10 years, 10 months ago

SPI writes are a great improvement over bit-banging Arduino ports. There are a couple of ways of accelerating SPI writes with the Arduino.

 

  • Most example software uses digitalWrite() to set the chip select low and high.
    • digitalWrite() has a lot of overhead and turns out to be pretty slow
    • Directly accessing the Chip Select bit can speed up writes dramatically.
      • Direct access requires you know the register and bit that the port is connected to.
        • For instance, for an ATMega32U4 based design the SS line is D17 on the Arduino or PB0 on the Atmega32U4.
        • To directly set the PB0 bit:
          • PORTB &= 0xFE;  // Directly set the Chip Select line low
          • PORTB |= 0x01;   // Directly set the Chip Select line high
  • The SPI clock register (SPR1 and SPR0) and the SPI2X bits can be set to faster speeds.

    • There is a warning in the datasheet that the SPI needs to be run at fosc/4 when used in slave mode (N/A in this case).
    • The fastest rate that the ATMega32U4 allows is half the processor clock.
    • For a 16 Mhz processor, that's 8 Mhz which is a 125 nS clock rate.
      • In comparison, an AD9833 DDS chip can run with a 40 nS clock min (25 MHz max or 3X the speed of the Arduino SPI).
    • To set the clock to the higher rate:
      • SPI.setClockDivider(SPI_CLOCK_DIV2); // Set the SPI clock to 8 MHz rate

 

Measurements - 16-bit Transfers

 

Resulting speed for a 16-bit transfer is 3.26 uS from chip select low to chip select high.

 


 

 

 

Top Trace = SCK

 

 

Middle Trace = MOSI (Data)

 

 

 

 

 

Bottom Trace = SS (chip select)

 

 

Cycle Time (chip select low to chip select low) is 3.64 uS.

 


 

 

 

Top Trace = SCK

 

 

Middle Trace = MOSI (Data)

 

 

 

 

 

Bottom Trace = SS (chip select)

 

 


 

The Front edge (chip select falling) timing is:

 

 

 

 

Top Trace = SCK

 

 

Middle Trace = MOSI (Data)

 

 

 

 

 

Bottom Trace = SS (chip select)

 

 


 

The back edge timing is:

 

 

 

 

Top Trace = SCK

 

 

Middle Trace = MOSI (Data)

 

 

 

 

 

Bottom Trace = SS (chip select)

 


 

Links

 

 



Comments (0)

You don't have permission to comment on this page.