Created
January 17, 2020 08:47
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "esp_system.h" | |
#include "esp_event.h" | |
#include "driver/spi_master.h" | |
#define TEST_MISO_PIN 19 | |
#define TEST_MOSI_PIN 18 | |
#define TEST_SCLK_PIN 5 | |
#define TEST_CS1_PIN 27 | |
#define TEST_CS2_PIN 32 | |
void app_main(void) | |
{ | |
static spi_device_handle_t spi1; | |
static spi_device_handle_t spi2; | |
// Initialize SPI bus | |
{ | |
spi_bus_config_t buscfg = { | |
.miso_io_num = TEST_MISO_PIN, | |
.mosi_io_num = TEST_MOSI_PIN, | |
.sclk_io_num = TEST_SCLK_PIN, | |
.quadwp_io_num = -1, | |
.quadhd_io_num = -1 | |
}; | |
ESP_ERROR_CHECK(spi_bus_initialize(VSPI_HOST, &buscfg, 0)); | |
} | |
// Setup SPI device 1 (half-duplex) | |
{ | |
spi_device_interface_config_t devcfg = { | |
.clock_speed_hz = 1*100*1000, | |
.mode = 0, | |
.queue_size = 1, | |
.flags = SPI_DEVICE_HALFDUPLEX, | |
.spics_io_num = TEST_CS1_PIN | |
}; | |
ESP_ERROR_CHECK(spi_bus_add_device(VSPI_HOST, &devcfg, &spi1)); | |
} | |
// Setup SPI device 2 (full-duplex, 8 addr. bits) | |
{ | |
spi_device_interface_config_t devcfg = { | |
.clock_speed_hz = 1*100*1000, | |
.mode = 0, | |
.queue_size = 1, | |
.flags = 0, | |
.command_bits = 0, | |
.address_bits = 8, | |
.spics_io_num = TEST_CS2_PIN | |
}; | |
ESP_ERROR_CHECK(spi_bus_add_device(VSPI_HOST, &devcfg, &spi2)); | |
} | |
printf("Starting in 2s...\n"); | |
sleep(2); | |
// Do a dummy transfer on device 1 | |
{ | |
uint8_t cmd = 0x3A; | |
spi_transaction_t t = { | |
.length = 8, | |
.tx_buffer = &cmd | |
}; | |
spi_device_transmit(spi1, &t); | |
} | |
// Do a 16-bit full-duplex transfer (+8 addr. bits) on device 2 | |
{ | |
spi_transaction_t t = { | |
.length = 16, | |
.rxlength = 16, | |
.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA | |
}; | |
uint8_t addr = 0x00; | |
t.addr = addr | 0x80; | |
t.tx_data[0] = (addr+1) | 0x80; | |
t.tx_data[1] = 0x00; | |
t.tx_data[2] = 0x13; | |
t.tx_data[3] = 0x37; | |
ESP_ERROR_CHECK(spi_device_transmit(spi2, &t)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment