2017-03-31 03:05:25 -04:00
|
|
|
/* SPI Slave example, sender (uses SPI master driver)
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#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"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/rtc_periph.h"
|
2017-03-31 03:05:25 -04:00
|
|
|
#include "driver/spi_master.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_spi_flash.h"
|
|
|
|
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "esp_intr_alloc.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
SPI sender (master) example.
|
|
|
|
|
2019-06-13 02:21:35 -04:00
|
|
|
This example is supposed to work together with the SPI receiver. It uses the standard SPI pins (MISO, MOSI, SCLK, CS) to
|
2017-03-31 03:05:25 -04:00
|
|
|
transmit data over in a full-duplex fashion, that is, while the master puts data on the MOSI pin, the slave puts its own
|
|
|
|
data on the MISO pin.
|
|
|
|
|
|
|
|
This example uses one extra pin: GPIO_HANDSHAKE is used as a handshake pin. The slave makes this pin high as soon as it is
|
2019-06-13 02:21:35 -04:00
|
|
|
ready to receive/send data. This code connects this line to a GPIO interrupt which gives the rdySem semaphore. The main
|
2017-03-31 03:05:25 -04:00
|
|
|
task waits for this semaphore to be given before queueing a transmission.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pins in use. The SPI Master can use the GPIO mux, so feel free to change these if needed.
|
|
|
|
*/
|
2020-12-14 22:00:02 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
|
2017-03-31 03:05:25 -04:00
|
|
|
#define GPIO_HANDSHAKE 2
|
|
|
|
#define GPIO_MOSI 12
|
|
|
|
#define GPIO_MISO 13
|
|
|
|
#define GPIO_SCLK 15
|
|
|
|
#define GPIO_CS 14
|
|
|
|
|
2020-12-14 22:00:02 -05:00
|
|
|
#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
|
|
|
|
|
|
|
|
|
2019-06-13 02:21:35 -04:00
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
|
|
|
#define SENDER_HOST HSPI_HOST
|
|
|
|
#define DMA_CHAN 2
|
|
|
|
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif defined CONFIG_IDF_TARGET_ESP32S2
|
2019-06-13 02:21:35 -04:00
|
|
|
#define SENDER_HOST SPI2_HOST
|
|
|
|
#define DMA_CHAN SENDER_HOST
|
|
|
|
|
2020-12-14 22:00:02 -05:00
|
|
|
#elif defined CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#define SENDER_HOST SPI2_HOST
|
|
|
|
#define DMA_CHAN SENDER_HOST
|
|
|
|
|
2019-06-13 02:21:35 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-03-31 03:05:25 -04:00
|
|
|
//The semaphore indicating the slave is ready to receive stuff.
|
|
|
|
static xQueueHandle rdySem;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This ISR is called when the handshake line goes high.
|
|
|
|
*/
|
|
|
|
static void IRAM_ATTR gpio_handshake_isr_handler(void* arg)
|
|
|
|
{
|
|
|
|
//Sometimes due to interference or ringing or something, we get two irqs after eachother. This is solved by
|
|
|
|
//looking at the time between interrupts and refusing any interrupt too close to another one.
|
|
|
|
static uint32_t lasthandshaketime;
|
2020-12-14 22:00:02 -05:00
|
|
|
uint32_t currtime=esp_cpu_get_ccount();
|
2017-03-31 03:05:25 -04:00
|
|
|
uint32_t diff=currtime-lasthandshaketime;
|
|
|
|
if (diff<240000) return; //ignore everything <1ms after an earlier irq
|
|
|
|
lasthandshaketime=currtime;
|
|
|
|
|
|
|
|
//Give the semaphore.
|
|
|
|
BaseType_t mustYield=false;
|
|
|
|
xSemaphoreGiveFromISR(rdySem, &mustYield);
|
|
|
|
if (mustYield) portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Main application
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-03-31 03:05:25 -04:00
|
|
|
{
|
|
|
|
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=-1,
|
|
|
|
.quadhd_io_num=-1
|
|
|
|
};
|
|
|
|
|
|
|
|
//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=5000000,
|
|
|
|
.duty_cycle_pos=128, //50% duty cycle
|
|
|
|
.mode=0,
|
|
|
|
.spics_io_num=GPIO_CS,
|
|
|
|
.cs_ena_posttrans=3, //Keep the CS low 3 cycles after transaction, to stop slave from missing the last bit when CS has less propagation delay than CLK
|
|
|
|
.queue_size=3
|
|
|
|
};
|
|
|
|
|
|
|
|
//GPIO config for the handshake line.
|
|
|
|
gpio_config_t io_conf={
|
2020-06-19 00:00:58 -04:00
|
|
|
.intr_type=GPIO_INTR_POSEDGE,
|
2017-03-31 03:05:25 -04:00
|
|
|
.mode=GPIO_MODE_INPUT,
|
|
|
|
.pull_up_en=1,
|
|
|
|
.pin_bit_mask=(1<<GPIO_HANDSHAKE)
|
|
|
|
};
|
|
|
|
|
|
|
|
int n=0;
|
2018-08-28 09:34:44 -04:00
|
|
|
char sendbuf[128] = {0};
|
|
|
|
char recvbuf[128] = {0};
|
2017-03-31 03:05:25 -04:00
|
|
|
spi_transaction_t t;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
|
|
|
|
|
|
//Create the semaphore.
|
|
|
|
rdySem=xSemaphoreCreateBinary();
|
|
|
|
|
|
|
|
//Set up handshake line interrupt.
|
|
|
|
gpio_config(&io_conf);
|
|
|
|
gpio_install_isr_service(0);
|
2020-06-19 00:00:58 -04:00
|
|
|
gpio_set_intr_type(GPIO_HANDSHAKE, GPIO_INTR_POSEDGE);
|
2017-03-31 03:05:25 -04:00
|
|
|
gpio_isr_handler_add(GPIO_HANDSHAKE, gpio_handshake_isr_handler, NULL);
|
|
|
|
|
|
|
|
//Initialize the SPI bus and add the device we want to send stuff to.
|
2019-06-13 02:21:35 -04:00
|
|
|
ret=spi_bus_initialize(SENDER_HOST, &buscfg, DMA_CHAN);
|
2017-03-31 03:05:25 -04:00
|
|
|
assert(ret==ESP_OK);
|
2019-06-13 02:21:35 -04:00
|
|
|
ret=spi_bus_add_device(SENDER_HOST, &devcfg, &handle);
|
2017-03-31 03:05:25 -04:00
|
|
|
assert(ret==ESP_OK);
|
|
|
|
|
2019-06-13 02:21:35 -04:00
|
|
|
//Assume the slave is ready for the first transmission: if the slave started up before us, we will not detect
|
2017-03-31 03:05:25 -04:00
|
|
|
//positive edge on the handshake line.
|
|
|
|
xSemaphoreGive(rdySem);
|
|
|
|
|
|
|
|
while(1) {
|
2018-08-28 09:34:44 -04:00
|
|
|
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;
|
2017-03-31 03:05:25 -04:00
|
|
|
t.tx_buffer=sendbuf;
|
|
|
|
t.rx_buffer=recvbuf;
|
|
|
|
//Wait for slave to be ready for next byte before sending
|
2018-08-29 06:40:21 -04:00
|
|
|
xSemaphoreTake(rdySem, portMAX_DELAY); //Wait until slave is ready
|
2017-03-31 03:05:25 -04:00
|
|
|
ret=spi_device_transmit(handle, &t);
|
|
|
|
printf("Received: %s\n", recvbuf);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Never reached.
|
|
|
|
ret=spi_bus_remove_device(handle);
|
|
|
|
assert(ret==ESP_OK);
|
|
|
|
}
|