ESP 32 Spi example cant reach 40 Mhz

seppel123
Posts: 10
Joined: Fri Jan 14, 2022 2:08 pm

ESP 32 Spi example cant reach 40 Mhz

Postby seppel123 » Fri Jan 14, 2022 2:48 pm

Hello im trying to test the SPI speed with a loop between the MISO and MOSI pin.
To get a fast entry point i used the \examples\peripherals\spi_slave\sender project.
I changed the pins to the VSPI iomux pins so can still debug.
My Problem is now i can only go up to 26 mhz instead of up to 80 Mhz like the ref man api ref and datasheet r telling is the max Speed for SPI master.
My hardware is the https://docs.espressif.com/projects/esp ... it-v3.html

1. cant setup 80 Mhz for clock speed than the rxbuffer has the wrong data inside
2. i recognized that the dutycycle is not 50/50 its more 30/70 see logicAnalyzer screenshot in the attachments(ok solved this it was bc logic analyzers Sample rate was to low with an oszi it was good)

Would be very nice if someone can have a look and help me.

[Codebox]

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"

#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "lwip/igmp.h"

#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "soc/rtc_periph.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "esp_spi_flash.h"

#include "driver/gpio.h"
#include "esp_intr_alloc.h"


#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
//#define GPIO_HANDSHAKE 2
#define GPIO_MOSI 23
#define GPIO_MISO 19
#define GPIO_SCLK 18
#define GPIO_CS 5

#elif CONFIG_IDF_TARGET_ESP32C3
#define GPIO_HANDSHAKE 3
#define GPIO_MOSI 7
#define GPIO_MISO 2
#define GPIO_SCLK 6
#define GPIO_CS 10

#endif //CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2


#ifdef CONFIG_IDF_TARGET_ESP32
#define SENDER_HOST 2

#elif defined CONFIG_IDF_TARGET_ESP32S2
#define SENDER_HOST SPI2_HOST

#elif defined CONFIG_IDF_TARGET_ESP32C3
#define SENDER_HOST SPI2_HOST

#endif


//The semaphore indicating the slave is ready to receive stuff.
static xQueueHandle rdySem;


//Main application
void app_main(void)
{
esp_err_t ret;
spi_device_handle_t handle;

//Configuration for the SPI bus
spi_bus_config_t buscfg={
.mosi_io_num=GPIO_MOSI,
.miso_io_num=GPIO_MISO,
.sclk_io_num=GPIO_SCLK,
.quadwp_io_num=22,
.quadhd_io_num=21
};

//Configuration for the SPI device on the other side of the bus
spi_device_interface_config_t devcfg={
.command_bits=0,
.address_bits=0,
.dummy_bits=0,
.clock_speed_hz=SPI_MASTER_FREQ_80M,
.duty_cycle_pos=0, //50% duty cycle
.mode=0,
.spics_io_num=5,
.cs_ena_posttrans=0,
.queue_size=3
};



int n=0;
char sendbuf[128] = {0};
char recvbuf[128] = {0};
spi_transaction_t t;
memset(&t, 0, sizeof(t));

//Create the semaphore.
rdySem=xSemaphoreCreateBinary();


//Initialize the SPI bus and add the device we want to send stuff to.
ret=spi_bus_initialize(SENDER_HOST, &buscfg, SPI_DMA_CH_AUTO);
assert(ret==ESP_OK);
ret=spi_bus_add_device(SENDER_HOST, &devcfg, &handle);
assert(ret==ESP_OK);

//Assume the slave is ready for the first transmission: if the slave started up before us, we will not detect
//positive edge on the handshake line.
xSemaphoreGive(rdySem);

while(1) {
int res = snprintf(sendbuf, sizeof(sendbuf),
"Sender, transmission no. %04i. Last time, I received: \"%s\"", n, recvbuf);
if (res >= sizeof(sendbuf)) {
printf("Data truncated\n");
}
// t.length=sizeof(sendbuf)*8;


t.length=8*strlen(sendbuf);
t.tx_buffer=sendbuf;
t.rx_buffer=recvbuf;


//Wait for slave to be ready for next byte before sending
// xSemaphoreTake(rdySem, portMAX_DELAY); //Wait until slave is ready
ret=spi_device_transmit(handle, &t);
printf("Received: %s\n", recvbuf);
n++;
}

//Never reached.
ret=spi_bus_remove_device(handle);
assert(ret==ESP_OK);
}
[/Codebox]
error1.png
error1.png (22.58 KiB) Viewed 1947 times
sender.7z
(11.26 MiB) Downloaded 47 times
Last edited by seppel123 on Mon Jan 17, 2022 2:23 pm, edited 1 time in total.

ESP_Sprite
Posts: 6336
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP 32 Spi example cant reach 40 Mhz

Postby ESP_Sprite » Sat Jan 15, 2022 2:21 am

You probably want to look at the SPI-Master driver docs.

seppel123
Posts: 10
Joined: Fri Jan 14, 2022 2:08 pm

Re: ESP 32 Spi example cant reach 40 Mhz

Postby seppel123 » Mon Jan 17, 2022 2:20 pm

@ESP_Sprite
which part of the timing Considerations u have in mind that i did wrong bc cant find an issue here?

ESP_Sprite
Posts: 6336
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP 32 Spi example cant reach 40 Mhz

Postby ESP_Sprite » Tue Jan 18, 2022 1:36 am

Ah, sorry, I did not spot that you used the dedicated VSPI pins. What corruption do you get when you try the example at 80MHz? Also note that wiring at those speeds becomes important: if you're using a breadboard you want a wire as short as possible between MISO and MOSI, for instance.

chegewara
Posts: 1893
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP 32 Spi example cant reach 40 Mhz

Postby chegewara » Tue Jan 18, 2022 3:40 am

What is the logic analyzer you are using and what sampling freq are you using?

seppel123
Posts: 10
Joined: Fri Jan 14, 2022 2:08 pm

Re: ESP 32 Spi example cant reach 40 Mhz

Postby seppel123 » Tue Jan 18, 2022 4:16 pm

ok thank you guys for helping esp spirit is right i used a to long cabble to loop the signal that made some problems and i setup the spi now with input delay of 50 ns and set the flag SPI_DEVICE_NO_DUMMY with this setup i can run up to 80 Mhz

always never forget rule #1 read from top to bottom
but maybe someone will find it and it helps him xD

@chegewara I m using a salea Logic Pro 8 normaly but when its going real fast i use a Rohde&Schwarz Ozci

Who is online

Users browsing this forum: No registered users and 35 guests