RobTillaart / MCP23S17

Arduino library for SPI based MCP23S17 16 channel port expander
MIT License
27 stars 11 forks source link

ESP32 Support for VSPI and HSPI #22

Closed alx-uta closed 1 year ago

alx-uta commented 1 year ago

I started using this library in a personal project and I realised that the ESP32 SPI part is missing.

After checking the other library (MCP_ADC) I was able to use the same code style and format.

RobTillaart commented 1 year ago

Thanks for the PR, I will look into the details asap. Might take a few days.

RobTillaart commented 1 year ago

@alx-uta Looks good, I assume you have tested both HSPI and VSPI with ESP32?

Do you have time to run the performance example and add some results to a (new) performance_0.2.4.md ? you can use the performance_0.2.2.md file as a template. Would be appreciated!

I am working on another PR and as this one is affecting less files I will merge this one first.

alx-uta commented 1 year ago

@RobTillaart that's perfect thanks for checking it.

Yes, I tested using MCP.selectVSPI(); and MCP.selectHSPI();.

I'll have a look at the performance file and I'll run the test.

alx-uta commented 1 year ago

@RobTillaart I used the test file. What I'll need to run for SW SPI?

VAL1: 0 VAL8: 67 VAL16: 17219

Action SW SPI HW 10 MHz notes
TEST digitalWrite(0, value) ------ 27.56
TEST digitalWrite(pin, value) ------ 24.56
TEST digitalRead(pin) ------ 18.06
TEST write8(port, mask) ------ 19.50
TEST read8(port) ------ 21.00
TEST write16(mask) ------ 18.00
TEST read16() ------ 18.50
RobTillaart commented 1 year ago

To use SW SPI you must change the parameters of the constructor call.in the performance sketch.

// MCP23S17 MCP(10, 12, 11, 13);

You can (re)use the HSPI or VSPI pins.

Expectations are a performance quite close to the HW SPI on an ESP32..

RobTillaart commented 1 year ago

Thanks for taking time to test!

alx-uta commented 1 year ago

@RobTillaart I just added the performance results. Sorry, I didn't realise yesterday that the SW was the Software SPI.

RobTillaart commented 1 year ago

Thanks for the update, appreciated! Merged into master branch, Will merge into the other PR asap.

alx-uta commented 1 year ago

Np. Thanks for this great library :)

RobTillaart commented 1 year ago

Did you notice that on ESP32 the software SPI is not far behind the hardware SPI?

alx-uta commented 1 year ago

@RobTillaart yes, the data was quite nice. Is there a specific part of the code that helped with that? Or, that's because of the esp32?

RobTillaart commented 1 year ago

It's how the ESP32 implemented their digitalWrite() et al. On AVR (UNO) you need to find the right register and then the right bit. The ESP32, it looks like directly mapped upon the IO pins, and of course the higher clock speed compensates a lot.

When looking on a scope you can see that software SPI is slower and has a nicer (more square) signal, IN the hardware SPI you see smaller pulses and thus more the RC effect (curve of the rising edge)

alx-uta commented 1 year ago

Ah, I see. That's quite nice then.

I need to get a scope if I want to continue playing with this.