Page 1 of 2
simple spi with multiple devices
Posted: Fri Oct 08, 2021 7:22 pm
by bmartin0
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
Re: simple spi with multiple devices
Posted: Tue Oct 12, 2021 4:58 am
by nopnop2002
What kind of SPI device do you want to run?
Re: simple spi with multiple devices
Posted: Sun Oct 17, 2021 12:06 am
by dmaxben
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...?
Re: simple spi with multiple devices
Posted: Sun Oct 17, 2021 4:25 am
by kgschlosser
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.
Re: simple spi with multiple devices
Posted: Mon Oct 18, 2021 11:16 pm
by dmaxben
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........
My application requires two separate CAN busses. The ESP32 only has one CAN controller built in, thats why my design also has an MCP2515.
Re: simple spi with multiple devices
Posted: Wed Oct 20, 2021 2:04 am
by kgschlosser
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.
Re: simple spi with multiple devices
Posted: Wed Oct 20, 2021 2:07 am
by kgschlosser
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.
Re: simple spi with multiple devices
Posted: Wed Oct 20, 2021 10:30 am
by Victoria Nope
Generally speaking (have not checked IDF implementation), there can be issues with card initialization (at its insertion, before the card enters SPI communication mode).
Re: simple spi with multiple devices
Posted: Wed Oct 20, 2021 12:46 pm
by dmaxben
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.
Re: simple spi with multiple devices
Posted: Tue Jan 30, 2024 1:33 am
by kgschlosser
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.