How sharing SPI for ethernet w5500, lora sx1276 and sdcard

mauribr123
Posts: 4
Joined: Mon Mar 22, 2021 7:47 pm

How sharing SPI for ethernet w5500, lora sx1276 and sdcard

Postby mauribr123 » Tue Aug 31, 2021 6:57 pm

Good afternoon,
I have a firmware doubt that totally implies my hardware design. I'm designing a PCB and I wanted to share the same SPI for ethernet w5500, lora sx1276 and sdcard, since HSPI or VSPI make it possible to use 3 CS slaves.

However, looking at all the ESP-NETIF API for w5500 and the mqtt and tcp socket layers that I will use, it is not clear how I can implement a mutex in the call of these functions to protect shared access to this SPI by various tasks, since the SDCARD and LORA will make use of the same SPI, with the lib lora I built, I managed to insert the mutexes properly, however, in the case of the LIB of the sdcard and ethernet ESP-NETIF, I have no idea how to build this from a thread-safe way.

Since in the case of Ethernett mainly, I will receive external interrupts and I need to ensure that the lora and not sdcard will be consuming the bus at this time.

I saw in the documentation that there are functions spi_device_acquire_bus() and spi_device_release_bus(), which are a kind of mutex, but again, how will I ensure that the MQTT callback event loop at the time it happens is pulling data via ESP-NETIF , do not collide with my SPI when I am occupying bus with LORA ? It would be awesome to have this clear of how to properly insert the FreeRTOS mutexes into the ethernet API, so that you can share the SPI with as much confidence as possible.

Att. Maurício Alencar

ESP_Sprite
Posts: 5160
Joined: Thu Nov 26, 2015 4:08 am

Re: How sharing SPI for ethernet w5500, lora sx1276 and sdcard

Postby ESP_Sprite » Wed Sep 01, 2021 3:07 am

As long as you use the SPI API to add devices to the bus, you should not have to worry about multithreading race conditions: the SPI driver is designed to solve that for you. The spi acquire/release calls can be useful if you have a device that needs continuous, uninterrupted access to the SPI bus without transactions to other devices going on: unless you have a SPI device that has specific requirements, you generally don't need it.

mauribr123
Posts: 4
Joined: Mon Mar 22, 2021 7:47 pm

Re: How sharing SPI for ethernet w5500, lora sx1276 and sdcard

Postby mauribr123 » Wed Sep 01, 2021 1:45 pm

Thanks for the reply, ESP Sprite.

Just to finish my doubt, when I call the spi_bus_add_device() function, will I be adding the devices to the VSPI bus correct? as in the code used below!

Code: Select all

ESP_ERROR_CHECK(spi_bus_add_device(VSPI_HOST, &cfg_eth, &spi_handle[i]));

Code: Select all

spi_bus_add_device(VSPI_HOST, &cfg_lora, &__spi);

Code: Select all

spi_bus_add_device(VSPI_HOST, &cfg_sdcard, &sd_handle);

And from the moment I added the devices to the bus, can I call, for example, the lora_write_reg function under any task without worrying about additional mutex because the driver itself will take care of it? even knowing that I can pull the cs pin to LOW and capture the SPI bus on different devices (like a task calling lora_write_reg and another task writing on sdcard or ethernet?). So my last question is how the driver ensures that this CS pin is used 1 at a time, since it is the user's responsibility to set it down before calling spi_device_transmit(), as in the lora_write_reg code below?

Code: Select all

void 
lora_write_reg(int reg, int val)
{
   uint8_t out[2] = { 0x80 | reg, val };
   uint8_t in[2];

   spi_transaction_t t = {
      .flags = 0,
      .length = 8 * sizeof(out),
      .tx_buffer = out,
      .rx_buffer = in  
   };

   gpio_set_level(CONFIG_CS_GPIO, 0);
   spi_device_transmit(__spi, &t);
   gpio_set_level(CONFIG_CS_GPIO, 1);
}

ESP_Sprite
Posts: 5160
Joined: Thu Nov 26, 2015 4:08 am

Re: How sharing SPI for ethernet w5500, lora sx1276 and sdcard

Postby ESP_Sprite » Thu Sep 02, 2021 2:34 am

Given that your program makes use of the SPI driver properly, you can do exactly what you sketched out. (However, do note it cannot protect you from issues if multiple threads try to access *the same* SPI device. If you were to have e.g. two threads writing to the LoRa device, you would have to implement your own muxing there.)

That LoRa driver does not seem like it makes proper use of the SPI driver, however. It should be re-written so the CS line is controlled by the SPI driver, and not tweaked manually, or (in case it needs some weird CS behaviour) it should claim the SPI bus for itself when doing a transaction, releasing it afterwards.

mauribr123
Posts: 4
Joined: Mon Mar 22, 2021 7:47 pm

Re: How sharing SPI for ethernet w5500, lora sx1276 and sdcard

Postby mauribr123 » Wed Sep 15, 2021 1:45 am

Good night ESP_Sprite, I was able to successfully share the SPI for the W5500 and LORA, with the drive initialization at 20MHz and using the spi_bus_add_device() function, but I'm trying to integrate the SDCARD example that I'll leave the link below, and there's no example in it where I can configure which SPI pins will be used, what bus speed will be used and how I add this SDCard to the SPI driver with the function spi_bus_add_device(), could you help me please? before asking this question, i've already gone over the example a lot and haven't found a solution... Here's the link to the SDSPI example that I used as a basis to try to modify the use of the SPI according to the other devices, such as modifying the GPIO of the SPI, change to SPI2_HOST and change to 20MHz so that the bus is compatible between SDCARD, LORA and W5500.

https://github.com/espressif/esp-idf/tr ... card/sdspi

ESP_Sprite
Posts: 5160
Joined: Thu Nov 26, 2015 4:08 am

Re: How sharing SPI for ethernet w5500, lora sx1276 and sdcard

Postby ESP_Sprite » Wed Sep 15, 2021 10:10 am

Did you read the docs page on that? It may clarify everything a bit. Effectively, you initialize the SPI bus using spi_bus_initialize (you already do that for the LoRa/W5500 chips, so no need to add that to your code) and then you add a SPI card to it using esp_vfs_fat_sdspi_mount().

Who is online

Users browsing this forum: No registered users and 30 guests