Pin Muxing VSPI or HSPI

Discode
Posts: 2
Joined: Wed Apr 14, 2021 12:10 pm

Pin Muxing VSPI or HSPI

Postby Discode » Wed Apr 14, 2021 12:31 pm

Hello,

I was able to use the SPI Slave API using the original pins and communicate with the master. I am now trying to switch the pins to the other side of the ESP32, however I'm not sure how to do so. If anyone could point me in the right direction or any links to an example of IO Muxing the ESP32 that would be great. I followed the documentation but I am not having any luck.

Code: Select all

	Original 	To Use	
MISO		19	32
MOSI		23	33
SCLK		18	25
SS		05	26
  1.  
  2. // SPI
  3. #include "driver/spi_slave.h"
  4. #define GPIO_CS   26 // I
  5. #define GPIO_SCLK 25 // I
  6. #define GPIO_MOSI 33 // I
  7. #define GPIO_MISO 32 // O
  8. #define RCV_HOST    HSPI_HOST
  9. #define DMA_CHAN    2
  10.  
  11. // SPI
  12. uint32_t sendbuf[1];
  13. uint32_t recvbuf[1];
  14. spi_slave_transaction_t t;
  15. esp_err_t ret;
  16. int n=0;
  17.  
  18. void SetupSPI()
  19. {
  20.   // Enable Pin Muxing to map the peripheral to their non-regular pins
  21.   //
  22.   gpio_iomux_in(GPIO_MOSI, VSPID_IN_IDX);
  23.   gpio_iomux_out(GPIO_MISO, FUNC_GPIO19_VSPIQ, false);
  24.   gpio_iomux_in(GPIO_SCLK, VSPICLK_IN_IDX);
  25.   gpio_iomux_in(GPIO_CS, VSPICS0_IN_IDX);
  26.  
  27.   spi_bus_config_t buscfg={
  28.       .mosi_io_num=GPIO_MOSI,
  29.       .miso_io_num=GPIO_MISO,
  30.       .sclk_io_num=GPIO_SCLK,
  31.       .quadwp_io_num = -1,
  32.       .quadhd_io_num = -1,
  33.       .max_transfer_sz = 32,
  34.   };
  35.  
  36.   spi_slave_interface_config_t slvcfg={
  37.     .spics_io_num=GPIO_CS,
  38.     .flags=0,
  39.     .queue_size=3,
  40.     .mode=1
  41.   };
  42.  
  43.   //Initialize SPI slave interface
  44.   ret=spi_slave_initialize(RCV_HOST, &buscfg, &slvcfg, DMA_CHAN);
  45.   printf("ret: %s\n", esp_err_to_name(ret));
  46.   assert(ret==ESP_OK);
  47.  
  48.   //Set up a transaction of 128 bytes to send/receive
  49.   t.length=32;
  50.   t.trans_len=32;
  51.   t.tx_buffer=sendbuf;
  52.   t.rx_buffer=recvbuf;
  53. }
Edit After some testing, I was able to switch the CS pin while keeping everything else in the original pinout.

Code: Select all

	
MISO		19
MOSI		23	
SCLK		18	
SS		26	
H098cb5d5159841b398fcfd4ebf705725W.png
H098cb5d5159841b398fcfd4ebf705725W.png (589.32 KiB) Viewed 1518 times

Discode
Posts: 2
Joined: Wed Apr 14, 2021 12:10 pm

Re: Pin Muxing VSPI or HSPI

Postby Discode » Sat Apr 17, 2021 9:47 am

Just an update, I was able to re-map the MISO pin from GPIO19 to GPIO12. This was done by simply passing the new pin into the slave driver function. I'm guessing the reason the other pins didnt work is due to the "disadvantage: talked about in this thread. So using the technical reference manual 4.10 IO_MUX Pad List - Table 4-3, I saw that GPIO12 can be used as a MISO aka Q pin and verified it was working by initiating a transaction. I'm sure this can be done to other SPI pins as well.
  1. // SPI
  2. #include "driver/spi_slave.h"
  3. #define GPIO_CS   26 // I
  4. #define GPIO_SCLK 18 // I
  5. #define GPIO_MOSI NULL // Not used
  6. #define GPIO_MISO 12 // Re-mapped from GPIO19, see  4.10 IO_MUX Pad List - Table 4-3
  7. #define RCV_HOST    HSPI_HOST
  8. #define DMA_CHAN    2
  9.  
  10. void SetupSPI()
  11. {
  12.  
  13.   spi_bus_config_t buscfg={
  14.       .mosi_io_num=GPIO_MOSI,
  15.       .miso_io_num=GPIO_MISO,
  16.       .sclk_io_num=GPIO_SCLK,
  17.       .quadwp_io_num = -1,
  18.       .quadhd_io_num = -1,
  19.       .max_transfer_sz = 16,
  20.   };
  21.  
  22.   spi_slave_interface_config_t slvcfg={
  23.     .spics_io_num=GPIO_CS,
  24.     .flags=0,
  25.     .queue_size=3,
  26.     .mode=1
  27.   };
  28.  
  29.   //Initialize SPI slave interface
  30.   ret=spi_slave_initialize(RCV_HOST, &buscfg, &slvcfg, DMA_CHAN);
  31.   printf("ret: %s\n", esp_err_to_name(ret));
  32.   assert(ret==ESP_OK);
  33.  
  34.   //Set up a transaction of 128 bytes to send/receive
  35.   t.length=32;
  36.   t.trans_len=32;
  37.   t.tx_buffer=sendbuf;
  38.   t.rx_buffer=recvbuf;
  39. }

Who is online

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