simple spi with multiple devices

bmartin0
Posts: 33
Joined: Tue Aug 03, 2021 10:38 pm

simple spi with multiple devices

Postby bmartin0 » Fri Oct 08, 2021 7:22 pm

Hello,
I'm working with the esp idf 4.3 example hd_eeprom. I've got 3 different devices connected to the spi bus with separate chip selects. Is there a simpler example that shows how to init the bus, read and write bytes or sequences of bytes to a particular device, one at a time by only changing the respective chip select for the device being talked to? The hd_eeprom example seems taylored to one device only and is a bit difficult to follow. Any simpler code examples would be appreciated.
Thanks

nopnop2002
Posts: 46
Joined: Thu Oct 03, 2019 10:52 pm

Re: simple spi with multiple devices

Postby nopnop2002 » Tue Oct 12, 2021 4:58 am

What kind of SPI device do you want to run?

dmaxben
Posts: 108
Joined: Thu Nov 16, 2017 6:04 pm

Re: simple spi with multiple devices

Postby dmaxben » Sun Oct 17, 2021 12:06 am

Im curious about this too. Id like to use an SD card and an MCP2515 CAN controller on the same SPI bus...hopefully its possible...?

kgschlosser
Posts: 7
Joined: Mon Oct 11, 2021 12:21 pm

Re: simple spi with multiple devices

Postby kgschlosser » Sun Oct 17, 2021 4:25 am

dmaxben wrote:
Sun Oct 17, 2021 12:06 am
Im curious about this too. Id like to use an SD card and an MCP2515 CAN controller on the same SPI bus...hopefully its possible...?
why are you using an SPI can interface when there is a can interface built into the ESP32?? Look up TWAI in the esp-idf documentation.
You need a transceiver like the MCP2562. This gives you all of the same features as the MCP2515 but without having to muck about with the registers to get and set he data. It can also be set up so when there is no CAN traffic you can put the ESP32 into a deep sleep and also put the transceiver into a standby state. When the transceiver sees any activity on the bus it will wake the ESP32. The transceiver accepts 3.3v and 5.0v and if you are using a 5.0v supply there is a ref pin you can attach 3.3v to so you won't need a logic level shifter. The cost is something like 1.00USD and it comes in a through hole variant so testing can be done using a bread board.

dmaxben
Posts: 108
Joined: Thu Nov 16, 2017 6:04 pm

Re: simple spi with multiple devices

Postby dmaxben » Mon Oct 18, 2021 11:16 pm

kgschlosser wrote:
Sun Oct 17, 2021 4:25 am
dmaxben wrote:
Sun Oct 17, 2021 12:06 am
Im curious about this too. Id like to use an SD card and an MCP2515 CAN controller on the same SPI bus...hopefully its possible...?
why are you using an SPI can interface when there is a can interface built into the ESP32?? Look up TWAI in the esp-idf documentation.
You need a transceiver like the MCP2562. This gives you all of the same features as the MCP2515 but without having to muck about with the registers to get and set he data. It can also be set up so when there is no CAN traffic you can put the ESP32 into a deep sleep and also put the transceiver into a standby state. When the transceiver sees any activity on the bus it will wake the ESP32. The transceiver accepts 3.3v and 5.0v and if you are using a 5.0v supply there is a ref pin you can attach 3.3v to so you won't need a logic level shifter. The cost is something like 1.00USD and it comes in a through hole variant so testing can be done using a bread board.
Im well aware of how the ESP32's internal CAN controller works. Ive been using it for years........ :roll:

My application requires two separate CAN busses. The ESP32 only has one CAN controller built in, thats why my design also has an MCP2515.

kgschlosser
Posts: 7
Joined: Mon Oct 11, 2021 12:21 pm

Re: simple spi with multiple devices

Postby kgschlosser » Wed Oct 20, 2021 2:04 am

dmaxben wrote:
Mon Oct 18, 2021 11:16 pm
My application requires two separate CAN busses. The ESP32 only has one CAN controller built in, thats why my design also has an MCP2515.
well that would make sense.

kgschlosser
Posts: 7
Joined: Mon Oct 11, 2021 12:21 pm

Re: simple spi with multiple devices

Postby kgschlosser » Wed Oct 20, 2021 2:07 am

and yes you can use an SD card reader and also the CAN interface on the same bus. You are going to have to have a cable select pin set up for each device and when you want that device to listen you turn off the select pin for that device. When finished turn it back on.

errr I think it's off is selected for use and on is idle.You would have to double check that, been a while since I have used SPI.

Victoria Nope
Posts: 75
Joined: Fri Dec 04, 2020 9:56 pm

Re: simple spi with multiple devices

Postby Victoria Nope » Wed Oct 20, 2021 10:30 am

Generally speaking (have not checked IDF implementation), there can be issues with card initialization (at its insertion, before the card enters SPI communication mode).
Last edited by Victoria Nope on Thu Oct 21, 2021 8:44 am, edited 1 time in total.

dmaxben
Posts: 108
Joined: Thu Nov 16, 2017 6:04 pm

Re: simple spi with multiple devices

Postby dmaxben » Wed Oct 20, 2021 12:46 pm

kgschlosser wrote:
Wed Oct 20, 2021 2:07 am
and yes you can use an SD card reader and also the CAN interface on the same bus. You are going to have to have a cable select pin set up for each device and when you want that device to listen you turn off the select pin for that device. When finished turn it back on.

errr I think it's off is selected for use and on is idle.You would have to double check that, been a while since I have used SPI.
yes...we all know how SPI works. Obviously you need chip-select lines for each slave................

Im guessing you havent actually tried using an SD card in 1-bit SPI mode and an MCP2515 together on the same bus in real life since you're just giving elementary level answers that are better suited for an Arduino website.

Sometimes real life applications dont work they way they're supposed to, sometimes certain SPI slaves dont exactly follow protocol. Sometimes slaves dont tri-state their MISO outputs, sometimes they output garbage on power up, etc.

Im saying this because prototyping on the bench, im having some issues getting them to both work together. The SD card seems to initialize correctly, but when working side-by-side with the MCP2515, I have to read the SD card twice after suspending MCP2515 communications. The first read returns a 0x107 or 0x108 error.

So obviously theres something in software or hardware that is preventing them from coexisting seamlessly.

kgschlosser
Posts: 7
Joined: Mon Oct 11, 2021 12:21 pm

Re: simple spi with multiple devices

Postby kgschlosser » Tue Jan 30, 2024 1:33 am

As the thread title states... "simple spi with multiple devices." The key words in that being SIMPLE So perhaps you need to go back to school and learn how to read instead of incorrectly criticizing my answer

and as far as your bench test is concerned. maybe using different hardware instead of something that is flawed will give you a correct outcome. maybe that is too much of an entry level solution for ya.

Who is online

Users browsing this forum: Baidu [Spider] and 130 guests