SPI slave mode (example)

rahmaevao
Posts: 11
Joined: Fri Oct 06, 2017 8:59 am

SPI slave mode (example)

Postby rahmaevao » Fri Oct 06, 2017 9:58 am

Friends, I'm trying to start slaving the SIP mode.
the master gives a good premise (see fig.)
Image
I'll use the code from the example:

Code: Select all

#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 "esp_event_loop.h"
#include "nvs_flash.h"
#include "soc/rtc_cntl_reg.h"
#include "rom/cache.h"
#include "driver/spi_slave.h"
#include "esp_log.h"
#include "esp_spi_flash.h"

#define GPIO_MOSI 12
#define GPIO_MISO 13
#define GPIO_SCLK 15
#define GPIO_CS 14



//Main application
void app_main()
{


    //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
    };

    //Configuration for the SPI slave interface
    spi_slave_interface_config_t slvcfg={
        .mode=0,
        .spics_io_num=GPIO_CS,
        .queue_size=3,
        .flags=0,
    };


    //Enable pull-ups on SPI lines so we don't detect rogue pulses when no master is connected.
    gpio_set_pull_mode(GPIO_MOSI, GPIO_PULLUP_ONLY);
    gpio_set_pull_mode(GPIO_SCLK, GPIO_PULLUP_ONLY);
    gpio_set_pull_mode(GPIO_CS, GPIO_PULLUP_ONLY);

    //Initialize SPI slave interface
	spi_slave_initialize(VSPI_HOST, &buscfg, &slvcfg, 1);
	

    char recvbuf[129]="";

    memset(recvbuf, 0, 33);
    spi_slave_transaction_t t;
    memset(&t, 0, sizeof(t));

    while(1) {
        //Clear receive buffer, set send buffer to something sane
		memset(recvbuf, 0x34, 129);
		
        //Set up a transaction of 128 bytes to send/receive
        t.length=128*8;
        t.rx_buffer=recvbuf;

		spi_slave_transmit(VSPI_HOST, &t, portMAX_DELAY);
		
		printf("Received: %s\n", recvbuf);

    }

}
But in the terminal I see only the meaning of the recvbuf (see fig)
Image
D is defined beforehand as 0x34 (in ASCII it is a "4") the help of a cmd memset
How to read the accepted parcel?
I very sad.
Help me :(

rahmaevao
Posts: 11
Joined: Fri Oct 06, 2017 8:59 am

Re: SPI slave mode (example)

Postby rahmaevao » Mon Oct 09, 2017 6:04 am

I read to "ESP32 Technical Reference Manual"what resiving data saved id SPI_W0_REG ~ SPI_W15_REG.
Can I read registers from esp-idf? :roll:
How i can it do?

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

Re: SPI slave mode (example)

Postby ESP_Sprite » Mon Oct 09, 2017 7:07 am

SPI slave at the moment uses DMA if I recall correctly, so no need to read the DMA stuff. We do have the restriction that the length of the transaction (t.length) should be exactly the amount of bits transferred; if not, the DMA engine can get confused and less than the expected amount of bits can be written. We're working on lifting this restriction in a newer version of the slave driver.

rahmaevao
Posts: 11
Joined: Fri Oct 06, 2017 8:59 am

Re: SPI slave mode (example)

Postby rahmaevao » Mon Oct 09, 2017 8:04 am

Thanks for the answer, but I did not understand how to use the SLAVE MODE.
The master mode is very good.
The buffer length is the same for the master and for the slave.
Also, I can not get by sending data from the slave to the master through the MISO line. (I check with an oscilloscope)
I fill in the

Code: Select all

    char sendbuf[3]="";
    char recvbuf[3]="";
    
    sendbuf[0]=0x31;
    sendbuf[1]=0x32;
    sendbuf[2]=0x33;
    
    recvbuf[0]=0;
    recvbuf[1]=0;
    recvbuf[2]=0;
    
    spi_slave_transaction_t t;
    
    t.length=3*8;
    t.tx_buffer=sendbuf;
    t.rx_buffer=recvbuf;
    
.tx_bufer.
But there is no dispatch.
there is neither otvpravki nor reception. What should I do?
Thanks for the answer)

rahmaevao
Posts: 11
Joined: Fri Oct 06, 2017 8:59 am

Re: SPI slave mode (example)

Postby rahmaevao » Mon Oct 09, 2017 8:12 am

How use DMA?
Simple past

Code: Select all

heap_caps_malloc(3, MALLOC_CAP_DMA);
in my code?

AAAwen
Posts: 15
Joined: Fri Sep 08, 2017 6:00 am

Re: SPI slave mode (example)

Postby AAAwen » Mon Oct 09, 2017 8:19 am

Dear Friends,
I used the same sample of spi-slave. I make the M3 module(as master) send 128 bytes to esp32(as slave). But, it doesn't work. Can you help me? Thanks!

我用git上的sample程序调试spi slave功能,但是接收数据存在问题。我用M3做主端发送128字节的数据到esp32,和楼主一样打印出来的接收内容是默认值。请问如何解决这个问题?谢谢!

rahmaevao
Posts: 11
Joined: Fri Oct 06, 2017 8:59 am

Re: SPI slave mode (example)

Postby rahmaevao » Mon Oct 09, 2017 9:42 am

You can provide a working example?

rahmaevao
Posts: 11
Joined: Fri Oct 06, 2017 8:59 am

Re: SPI slave mode (example)

Postby rahmaevao » Mon Oct 09, 2017 11:58 am

Y
ESP_Sprite wrote:SPI slave at the moment uses DMA if I recall correctly, so no need to read the DMA stuff. We do have the restriction that the length of the transaction (t.length) should be exactly the amount of bits transferred; if not, the DMA engine can get confused and less than the expected amount of bits can be written. We're working on lifting this restriction in a newer version of the slave driver.
I restructure my code.

Code: Select all

char *POINTER_T = heap_caps_malloc(3, MALLOC_CAP_DMA);
    
    char *POINTER_R = heap_caps_malloc(3, MALLOC_CAP_DMA);
    
    
    
    while(1) {
		spi_slave_transaction_t t;
		t.length=3*8;
		t.tx_buffer=POINTER_T;
		t.rx_buffer=POINTER_R;
        
		spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);
		
		printf("%d",*POINTER_R);
		printf("\n");
    }
But nothing happens. It is accepted what is "248".
Help me please.

LakshmiDhanaraj
Posts: 2
Joined: Tue Oct 24, 2017 10:11 am

Re: SPI slave mode (example)

Postby LakshmiDhanaraj » Tue Oct 24, 2017 10:36 am

Hi,

I am using the SPI slaver driver example,sender as master and receiver as slave.I can able to transmit/receive data without dma.But it restricts the number of bytes to 32.For transmitting more than 32bytes,I am enabling the dma to 1,won't receive/transmits the data.Please help me out to solve this issue.What I have to change in the code for transmitting more than 32 bytes?

AAAwen
Posts: 15
Joined: Fri Sep 08, 2017 6:00 am

Re: SPI slave mode (example)

Postby AAAwen » Thu Oct 26, 2017 1:16 am

Dear All,
Is Anybody solve this issue? Who can help me? :cry:
Thx.

关于SPI从端的示例遇到的问题是否有大神已经解决了?谢谢。

Who is online

Users browsing this forum: Bing [Bot] and 335 guests