2017-03-31 03:05:25 -04:00
|
|
|
/* SPI Slave example, receiver (uses SPI Slave driver to communicate with sender)
|
|
|
|
|
|
|
|
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 "esp_event_loop.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "soc/rtc_cntl_reg.h"
|
2019-03-14 05:29:32 -04:00
|
|
|
#include "esp32/rom/cache.h"
|
2017-03-31 03:05:25 -04:00
|
|
|
#include "driver/spi_slave.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_spi_flash.h"
|
global: move the soc component out of the common list
This MR removes the common dependency from every IDF components to the SOC component.
Currently, in the ``idf_functions.cmake`` script, we include the header path of SOC component by default for all components.
But for better code organization (or maybe also benifits to the compiling speed), we may remove the dependency to SOC components for most components except the driver and kernel related components.
In CMAKE, we have two kinds of header visibilities (set by include path visibility):
(Assume component A --(depends on)--> B, B is the current component)
1. public (``COMPONENT_ADD_INCLUDEDIRS``): means this path is visible to other depending components (A) (visible to A and B)
2. private (``COMPONENT_PRIV_INCLUDEDIRS``): means this path is only visible to source files inside the component (visible to B only)
and we have two kinds of depending ways:
(Assume component A --(depends on)--> B --(depends on)--> C, B is the current component)
1. public (```COMPONENT_REQUIRES```): means B can access to public include path of C. All other components rely on you (A) will also be available for the public headers. (visible to A, B)
2. private (``COMPONENT_PRIV_REQUIRES``): means B can access to public include path of C, but don't propagate this relation to other components (A). (visible to B)
1. remove the common requirement in ``idf_functions.cmake``, this makes the SOC components invisible to all other components by default.
2. if a component (for example, DRIVER) really needs the dependency to SOC, add a private dependency to SOC for it.
3. some other components that don't really depends on the SOC may still meet some errors saying "can't find header soc/...", this is because it's depended component (DRIVER) incorrectly include the header of SOC in its public headers. Moving all this kind of #include into source files, or private headers
4. Fix the include requirements for some file which miss sufficient #include directives. (Previously they include some headers by the long long long header include link)
This is a breaking change. Previous code may depends on the long include chain.
You may need to include the following headers for some files after this commit:
- soc/soc.h
- soc/soc_memory_layout.h
- driver/gpio.h
- esp_sleep.h
The major broken include chain includes:
1. esp_system.h no longer includes esp_sleep.h. The latter includes driver/gpio.h and driver/touch_pad.h.
2. ets_sys.h no longer includes soc/soc.h
3. freertos/portmacro.h no longer includes soc/soc_memory_layout.h
some peripheral headers no longer includes their hw related headers, e.g. rom/gpio.h no longer includes soc/gpio_pins.h and soc/gpio_reg.h
BREAKING CHANGE
2019-04-03 01:17:38 -04:00
|
|
|
#include "driver/gpio.h"
|
2017-03-31 03:05:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
SPI receiver (slave) example.
|
|
|
|
|
global: move the soc component out of the common list
This MR removes the common dependency from every IDF components to the SOC component.
Currently, in the ``idf_functions.cmake`` script, we include the header path of SOC component by default for all components.
But for better code organization (or maybe also benifits to the compiling speed), we may remove the dependency to SOC components for most components except the driver and kernel related components.
In CMAKE, we have two kinds of header visibilities (set by include path visibility):
(Assume component A --(depends on)--> B, B is the current component)
1. public (``COMPONENT_ADD_INCLUDEDIRS``): means this path is visible to other depending components (A) (visible to A and B)
2. private (``COMPONENT_PRIV_INCLUDEDIRS``): means this path is only visible to source files inside the component (visible to B only)
and we have two kinds of depending ways:
(Assume component A --(depends on)--> B --(depends on)--> C, B is the current component)
1. public (```COMPONENT_REQUIRES```): means B can access to public include path of C. All other components rely on you (A) will also be available for the public headers. (visible to A, B)
2. private (``COMPONENT_PRIV_REQUIRES``): means B can access to public include path of C, but don't propagate this relation to other components (A). (visible to B)
1. remove the common requirement in ``idf_functions.cmake``, this makes the SOC components invisible to all other components by default.
2. if a component (for example, DRIVER) really needs the dependency to SOC, add a private dependency to SOC for it.
3. some other components that don't really depends on the SOC may still meet some errors saying "can't find header soc/...", this is because it's depended component (DRIVER) incorrectly include the header of SOC in its public headers. Moving all this kind of #include into source files, or private headers
4. Fix the include requirements for some file which miss sufficient #include directives. (Previously they include some headers by the long long long header include link)
This is a breaking change. Previous code may depends on the long include chain.
You may need to include the following headers for some files after this commit:
- soc/soc.h
- soc/soc_memory_layout.h
- driver/gpio.h
- esp_sleep.h
The major broken include chain includes:
1. esp_system.h no longer includes esp_sleep.h. The latter includes driver/gpio.h and driver/touch_pad.h.
2. ets_sys.h no longer includes soc/soc.h
3. freertos/portmacro.h no longer includes soc/soc_memory_layout.h
some peripheral headers no longer includes their hw related headers, e.g. rom/gpio.h no longer includes soc/gpio_pins.h and soc/gpio_reg.h
BREAKING CHANGE
2019-04-03 01:17:38 -04:00
|
|
|
This example is supposed to work together with the SPI sender. 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. After a transmission has been set up and we're
|
|
|
|
ready to send/receive data, this code uses a callback to set the handshake pin high. The sender will detect this and start
|
|
|
|
sending a transaction. As soon as the transaction is done, the line gets set low again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pins in use. The SPI Master can use the GPIO mux, so feel free to change these if needed.
|
|
|
|
*/
|
|
|
|
#define GPIO_HANDSHAKE 2
|
|
|
|
#define GPIO_MOSI 12
|
|
|
|
#define GPIO_MISO 13
|
|
|
|
#define GPIO_SCLK 15
|
|
|
|
#define GPIO_CS 14
|
|
|
|
|
|
|
|
|
|
|
|
//Called after a transaction is queued and ready for pickup by master. We use this to set the handshake line high.
|
|
|
|
void my_post_setup_cb(spi_slave_transaction_t *trans) {
|
|
|
|
WRITE_PERI_REG(GPIO_OUT_W1TS_REG, (1<<GPIO_HANDSHAKE));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Called after transaction is sent/received. We use this to set the handshake line low.
|
|
|
|
void my_post_trans_cb(spi_slave_transaction_t *trans) {
|
|
|
|
WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1<<GPIO_HANDSHAKE));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Main application
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
int n=0;
|
|
|
|
esp_err_t ret;
|
|
|
|
|
|
|
|
//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,
|
|
|
|
.post_setup_cb=my_post_setup_cb,
|
|
|
|
.post_trans_cb=my_post_trans_cb
|
|
|
|
};
|
|
|
|
|
|
|
|
//Configuration for the handshake line
|
|
|
|
gpio_config_t io_conf={
|
|
|
|
.intr_type=GPIO_INTR_DISABLE,
|
|
|
|
.mode=GPIO_MODE_OUTPUT,
|
|
|
|
.pin_bit_mask=(1<<GPIO_HANDSHAKE)
|
|
|
|
};
|
|
|
|
|
|
|
|
//Configure handshake line as output
|
|
|
|
gpio_config(&io_conf);
|
|
|
|
//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
|
|
|
|
ret=spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg, 1);
|
|
|
|
assert(ret==ESP_OK);
|
|
|
|
|
|
|
|
char sendbuf[129]="";
|
|
|
|
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, 0xA5, 129);
|
|
|
|
sprintf(sendbuf, "This is the receiver, sending data for transmission number %04d.", n);
|
|
|
|
|
|
|
|
//Set up a transaction of 128 bytes to send/receive
|
|
|
|
t.length=128*8;
|
|
|
|
t.tx_buffer=sendbuf;
|
|
|
|
t.rx_buffer=recvbuf;
|
|
|
|
/* This call enables the SPI slave interface to send/receive to the sendbuf and recvbuf. The transaction is
|
|
|
|
initialized by the SPI master, however, so it will not actually happen until the master starts a hardware transaction
|
|
|
|
by pulling CS low and pulsing the clock etc. In this specific example, we use the handshake line, pulled up by the
|
|
|
|
.post_setup_cb callback that is called as soon as a transaction is ready, to let the master know it is free to transfer
|
|
|
|
data.
|
|
|
|
*/
|
|
|
|
ret=spi_slave_transmit(HSPI_HOST, &t, portMAX_DELAY);
|
|
|
|
|
|
|
|
//spi_slave_transmit does not return until the master has done a transmission, so by here we have sent our data and
|
|
|
|
//received data from the master. Print it.
|
|
|
|
printf("Received: %s\n", recvbuf);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|