Fast SPI on stm32f103cbt6

Working libraries, libraries being ported and related hardware
stm32_discoverer
Posts: 17
Joined: Sun Jan 01, 2017 10:28 am

Fast SPI on stm32f103cbt6

Postby stm32_discoverer » Sun Jan 01, 2017 10:59 am

Hi guys, I have read that spi stm32f104cbt6 can operate at a frequency of 36 MHz, but the standard initialization of spi mode in the Arduino IDE provides only 18 MHz speed. This CPU has 2 SPI hardware interface, but as I understand only one of them works at the speed of 36 MHz.Here is standard init code from ARDUINO IDE - STM32F1 LIBRARY.Are there any settings SPI to work at the speed of 36 MHz ?I'm using Arduino IDE 1.6.5.

Code: Select all

#include <SPI.h>

#define SPI1_NSS_PIN PA4    //SPI_1 Chip Select pin is PA4. You can change it to the STM32 pin you want.
#define SPI2_NSS_PIN PB12   //SPI_2 Chip Select pin is PB12. You can change it to the STM32 pin you want.

SPIClass SPI_2(2); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port
byte data;

void setup() {

  // Setup SPI 1
  SPI.begin(); //Initialize the SPI_1 port.
  SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
  SPI.setDataMode(SPI_MODE0); //Set the  SPI_2 data mode 0
  SPI.setClockDivider(SPI_CLOCK_DIV2);      // Slow speed (72 / 16 = 4.5 MHz SPI_1 speed)
  pinMode(SPI1_NSS_PIN, OUTPUT);

  // Setup SPI 2
  SPI_2.begin(); //Initialize the SPI_2 port.
  SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order
  SPI_2.setDataMode(SPI_MODE0); //Set the  SPI_2 data mode 0
  SPI_2.setClockDivider(SPI_CLOCK_DIV2);  // Use a different speed to SPI 1
  pinMode(SPI2_NSS_PIN, OUTPUT);


}

User avatar
Pito
Posts: 792
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Fast SPI on stm32f103cbt6

Postby Pito » Sun Jan 01, 2017 11:56 am

viewtopic.php?t=1495#p19393

With SPI1

Code: Select all

SPI.setClockDivider(SPI_CLOCK_DIV2); // (72 MHz / 2 =  36MHz)

stm32_discoverer
Posts: 17
Joined: Sun Jan 01, 2017 10:28 am

Re: Fast SPI on stm32f103cbt6

Postby stm32_discoverer » Sun Jan 01, 2017 12:22 pm

Ok, i'm trying to do some test about it, and what have we got?

Code: Select all

void loop() {
  duration=micros();
  for (int i=0;i<1000;i++){
    data = SPI.transfer(0xFF);
  }
  duration=micros()-duration;
  Serial.println(duration);
}

SPI1 transmit 1000 bytes in 1312 microseconds.
___________________________________________

Code: Select all

void loop() {
  duration=micros();
  for (int i=0;i<1000;i++){
    data = SPI_2.transfer(0xFF);
  }
  duration=micros()-duration;
  Serial.println(duration);
}

But if we try to transmit 1000 bytes to SPI2, the transmission time is 1785 microseconds.
But the idea is the difference between SPI1 and SPI2 in the transmission time must be twice.what's the catch?

User avatar
Pito
Posts: 792
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Fast SPI on stm32f103cbt6

Postby Pito » Sun Jan 01, 2017 12:25 pm

You have to subtract the for_loop timing.. Also the call to the transfer function takes some time.
You will not get exact 2x speedup, of course.
Best is to look at SPI clock with an LA or an O'scope.
Btw, to transfer 1000bytes @ 36MHz shall take about 250usecs (overhead exclusive)..

stm32_discoverer
Posts: 17
Joined: Sun Jan 01, 2017 10:28 am

Re: Fast SPI on stm32f103cbt6

Postby stm32_discoverer » Sun Jan 01, 2017 12:35 pm

what about SPI with DMA.Сould this increase the transfer speed?

stm32_discoverer
Posts: 17
Joined: Sun Jan 01, 2017 10:28 am

Re: Fast SPI on stm32f103cbt6

Postby stm32_discoverer » Sun Jan 01, 2017 12:43 pm

in fact, we have to transfer 1000 bytes 1312 microseconds, I think that is too much

User avatar
Pito
Posts: 792
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Fast SPI on stm32f103cbt6

Postby Pito » Sun Jan 01, 2017 1:05 pm

FYI - with your code above

SPI12 TRX vs Clk.JPG
SPI12 TRX vs Clk.JPG (69.82 KiB) Viewed 325 times

From these data you have to be able to determine what is the SPI TRX overhead contribution to the duration measured :)
Last edited by Pito on Sun Jan 01, 2017 2:20 pm, edited 6 times in total.

stm32_discoverer
Posts: 17
Joined: Sun Jan 01, 2017 10:28 am

Re: Fast SPI on stm32f103cbt6

Postby stm32_discoverer » Sun Jan 01, 2017 1:23 pm

thank's for the visual representation, but how to speed up the transfer of SPI1, at least up to 2x

User avatar
Pito
Posts: 792
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Fast SPI on stm32f103cbt6

Postby Pito » Sun Jan 01, 2017 1:38 pm

:idea:
1. Use DMA
2. Optimize the SPI.transfer() [ie. unroll the loop, use SPI Fifo if available, etc.]
3. Use F407+

danieleff
Posts: 129
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: Fast SPI on stm32f103cbt6

Postby danieleff » Sun Jan 01, 2017 1:48 pm

stm32_discoverer wrote:thank's for the visual representation, but how to speed up the transfer of SPI1, at least up to 2x

Try SPI.write(buffer, 1000) to transfer all the data in one function call. (I do not know why is it not called SPI.transfer(buffer, size) like in the arduino reference).


Return to “Libraries & Hardware”

Who is online

Users browsing this forum: No registered users and 1 guest