Manually controlling CS pins
When adding a SPI device, set its .spics_io_num to -1
//----- SETUP IO PINS -----
//Set CS high
gpio_set_level(EXAMPLE_SPI_PIN_CS, 1);
gpio_set_direction(EXAMPLE_SPI_PIN_CS, GPIO_MODE_OUTPUT);
//----- ADD A SPI DEVICE -----
spi_device_interface_config_t SpiDeviceInterfaceConfig1 = {
.clock_speed_hz = 10*1000*1000, //Clock out at 10 MHz
.mode = 0, //<<< SPI mode 0
.spics_io_num = -1, //<<< CS pin number (-1 = usunsed, you will control it yourself using GPIO)
.queue_size = 1, //<<< Number of transactions we want to be able to queue at a time using spi_device_queue_trans()
};
spi_bus_add_device(EXAMPLE_SPI_PORT_NAME, &SpiDeviceInterfaceConfig1, &OurSpiDeviceHandle1); //<<<You can use ESP_ERROR_CHECK() on this call if you are having issues
Now when you access other device, ensure you set CS low before you use it and high again afterwards
//CS Low
gpio_set_level(EXAMPLE_SPI_PIN_CS, 0);
//your SPI access code here...
//CS high
gpio_set_level(EXAMPLE_SPI_PIN_CS, 1);
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.