Keep SPI CS active

floitsch_toit
Posts: 18
Joined: Wed Jun 29, 2022 4:25 pm

Keep SPI CS active

Postby floitsch_toit » Wed Jun 29, 2022 5:03 pm

Hi,

Is there an easy way to keep the CS active after having done an SPI operation?

There is the SPI_PIN_REG register and the corresponding SPI_CS_KEEP_ACTIVE bit, but I would prefer not to write directly into the registers to keep my code portable.

More context:
I'm working with the Si4x55 sensor (https://www.silabs.com/documents/public ... 4x55-C.pdf).
On page 23, it is explained that operations consist of pulling the nSEL low, and then polling the CTS Value until the sensor indicates that it is ready.

I would like to implement this, but don't see any high-level functions that allow me to keep the CS active for multiple operations.

My current approach would now be to add a boolean parameter to my transfer function that en/disables the bit.

Code: Select all

  REG_SET_FIELD(SPI_PIN_REG(host_device + 1),
                SPI_CS_KEEP_ACTIVE,
                keep_cs_active ? 1 : 0);
and then to have a 'deactivate' function (just in case I want to turn it off without doing more SPI operations).

Code: Select all

  REG_CLR_BIT(SPI_PIN_REG(host_device + 1), SPI_CS_KEEP_ACTIVE);

  // Deactivate any CS that is currently high.
  REG_SET_BIT(SPI_PIN_REG(host_device + 1), SPI_CS2_DIS);
  REG_SET_BIT(SPI_PIN_REG(host_device + 1), SPI_CS1_DIS);
  REG_SET_BIT(SPI_PIN_REG(host_device + 1), SPI_CS0_DIS);
I think this approach would work, but it's really low level. If there was a higher-level approach I would prefer that.

Also, is there a good way to map from spi_host_device_t to the ID that needs to be passed to SPI_PIN_REG? Currently I'm just adding 1, but that feels extremely brittle.

Finally, do I need to call spi_device_acquire_bus before I start using the SPI_CS_KEEP_ACTIVE bit?

Thanks

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

Re: Keep SPI CS active

Postby ESP_Sprite » Thu Jun 30, 2022 3:10 am

Not sure when we introduced it, but at least the latest ESP_IDF already implements this: it has a SPI_TRANS_CS_KEEP_ACTIVE flag you can set in the spi_transaction_t->flags variable.

floitsch_toit
Posts: 18
Joined: Wed Jun 29, 2022 4:25 pm

Re: Keep SPI CS active

Postby floitsch_toit » Thu Jun 30, 2022 9:54 am

Thanks.

We were still using 4.3, which doesn't have it yet. Seems like a good moment to upgrade...

The new documentation also explains that one has to call spi_device_acquire_bus before using the flag.

In case the user doesn't finish with a transfer that has the flag disabled, will spi_device_release_bus disable the CS?
If not, is there another way to force the deactivation of the CS for the device?

Who is online

Users browsing this forum: ESP_rrtandler, Google [Bot] and 112 guests