Leonardo Multiple Master/Slave SPI confusion

Hello everyone, I'm new to this forum.

I'm designing a project on a breadboard, using a standalone ATMEGA32u4 loaded with Arduino bootloader.

I both burn the bootloader and upload sketches using SPI (using "Burn bootloader" and "Upload using programmer" from arduino IDE). Everything works fine.

My project use a second MCU (U2) which also speak SPI with the Atmega (U1). At run-time, U1 is the master, and use a dedicated pin to set U2 as slave (CS pin on U2 is set to Low). Here again, everything's fine.

So now the problem,

  • My SPI lines (excepting CS) are shared between the programmer, U1, and U2
  • When I upload my sketch, the Programmer is the SPI master and U1 the slave (have to set U1 CS to gnd, and U2 CS to vcc)
  • At run-time, U1 become the master and U2 is the slave (have to disconnect U2 CS from vcc). The programmer is also disconnected.

This is bit annoying since this would require either a switch or some pad to short on my final PCB, just for programming the atmega. I have very limited space on the PCB, and I'd like to avoid that if possible.

Since this is my first real project, and because I'm still learning electronics and arduino, I wonder if this is the right way to use shared SPI lines when multiple devices must become master at some points (the Programmer, and U1).

Can I use non-default SPI pins (miso,mosi,clk) on the atmega32u4 to create two separated SPI lines (one for programming, one for run-time)?
Any other techniques that I haven't considered ?

Thanks for your light!
Regards

When I upload my sketch, the Programmer is the SPI master and U1 the slave (have to set U1 CS to gnd, and U2 CS to vcc)

No, the programmer uses ICSP to program the chip. Although it uses the SPI pins it doesn't use SPI for that (no CS).

Can I use non-default SPI pins (miso,mosi,clk) on the atmega32u4 to create two separated SPI lines (one for programming, one for run-time)?

No.

Any other techniques that I haven't considered ?

I don't see your problem at the moment. You didn't describe yet what in that setup doesn't work.

pylon:
No, the programmer uses ICSP to program the chip. Although it uses the SPI pins it doesn't use SPI for that (no CS).

Thanks for this clarification. As said, I'm still learning.

My problem is during the sketch upload.

If CS of U2 is not set to Vcc, the upload fails. I made the assumption (maybe wrong) that it was because U2 is connected to the same SPI lines, but not properly 'disabled' during the sketch upload to U1 (CS of U2 is connected to a pin of U1, but has no real state at that moment. right?). So U2 could potentially disturb what I though to be an SPI communication.

So basically, the programmer, U1 and U2 are all connected to MISO,MOSI, and CLK, and I can't upload my sketch to U1 until CS of U2 is set to Vcc.

The error is

avrdude: Expected signature for ATmega32U4 is 1E 95 87
         Double check chip, or use -F to override this check.
Wrong microcontroller found.  Did you select the right board from the Tools > Board menu?

If CS of U2 is set to Vcc, then the upload works fine (but not the run-time of course)
If CS of U2 is set to GND, then the same error occurs.

put a pullup resistor on the CS line of U2 - something like 10k to Vcc. Thus even when the pin on U1 that it's connected to is tristated because it's in programming, CS is held high - but during operation U1 can still assert that pin.

I feel really stupid :o .. It works like a charms. Thank you very much :slight_smile: