Thinking about the SPI library ...?

It's perfectly clear: there are two - one on each processor - and they are totally independent.

So have you written some special code for the 16U2 ?

Post your code, and some good, clear photos of your setup.

No. it is incorrect.
MEGA pins 50,51,52 are not connected to ATMEGA 16U2

1 Like

:point_up_2: :point_up_2: This! :point_up_2: :point_up_2:

(my emphasis)

1 Like

A: Arduino UNO:
1. ICSP (In Circuit Serial Programming Interface) Connector
This 6-pin ICSP connector is located near the ATmega328P MCU. It bears (connected with) the MOSI, MISO, and SCK signals of the SPI Port of the ATmega328P MCU.

Purpose of ICSP Connectror:
The flash/EEPROM memory of another ATmega328P MCU (target MCU) can be programmed using a Commercial Parallel ROM Programmer (expensive item). Alternatively, the target MCU has a built-in "In system/Circuit Programming Interface" which can be eanbled and driven by the SPI Port of a host MCU (here the MCU of the UNO Board) to program the target MCU by passing the data in serial fashion. The SPI lines of the target MCU is connected with the SPI signals of ICSP Connector.

2. ICSP1 (In Circuit Serial Programming Interface) Connector 1.
This 6-pin ICSP1 connector is located near the USB/TTL converter chip (ATmega8U2). It bears (connected with) the MOSI, MISO, and SCK signals of the SPI Port of ATmega8U2 MCU.

Purpose ICSP1 Connector: Similar to the description made for ICSP. Here, the firmware of the USB/TTL converter is changed/updated by a host MCU which could be the MCU of the UNO Board. In this case, the ICSP and ICSP1 are wired as appropriate/needed.

B: Arduino MEGA:
Same as Arduino UNO.

C: The SPI Ports of ATmega328P and ATmegaU8 are independent. The SPI port of ATmega328P of the UNO baord can acquire data from a SPI-based sensor; but, the ATmegaU8 cannot do that as the PC always uploads the application sketch into the ATmega328P MCU.

Well - not unless you write special code for the ATmegaU8, which would have to be loaded via the ICSP1 connector...

1 Like

Of course! A Software Driver is needed in addition to the Hardware Driver (the SPI Port and the ISP Port),

There is a program named ArduinoISP in the Examples of the IDE to program a target MCU using SPI-ISP Interface of ATmega328P of UNO. (I never tried!)

OK I stand corrected. And my apologies for this misleading statement about my MEGA design, it's basically rubbish:

"On a MEGA I can run a SPI LCD off pins D10-D13 and an SD card off SPI / ICSP1 pins 50,51,52 (+CS) . The first SPI port is connected to the main chip however the latter SPI is connected to the 16U2."

I was thinking of an old design I did in 2016, but when I looked back the LCD isn't a SPI device at all. doh!
I do run an SD card off SPI / ICSP (not ICSP1 !!) pins 50,51,52 + CS.

So in summary there's one usable SPI bus on UNO (D11-D13)+CS and one on MEGA (50,51,52+CS). These are both wired to the ICSP connector. And the other ICSP1 connector from the ATMega 16U2 does not support SPI on either board.

many thanks all round.

1 Like

There's one usable hardware SPI bus.

A number of software SPI bus(es) can be added.

The USART(s) can be pressed into SPI Master service if further hardware SPI bus(es) are really required...

1 Like

It is not strictly correct.
ICSP1 connector supports SPI, but it is connected to the different controller, ATMega 16U2.
So this SPI interface is not available for programs, running in the main chip.

2 Likes

A number of software SPI bus(es) can be added.

That sounds interesting ... how does this work?

@ninja2

Why do you looking for multiple SPI buses? Do you know that you can connect a more than one SPI device to the single bus?

1 Like

The software "manually" sets output pins high/low, and reads input pins - often known as "bit-banging" or "bit-bashing"

eg, the Arduino software serial library:

because it doesn't rely on specific hardware, it can use any pins.

The downsides are that it consumes CPU resources, may interfere with timing for other parts of the code, and usually can't match the performance of dedicated hardware.

As @b707 said, there really shouldn't be any need to have multiple SPI buses as the whole point of a bus is that multiple things can all be on it together - as shown in Post #4.

Yes, I completely understand that. I posted this topic mainly out of curiousity, and now I understand the sparate SPI ports much better. So thanks :grinning:

Thanks for your detailed response. Still curious: Have you seen anyone actually write code for the ATMEGA16U2 SPI port on this forum?

No - but then I've never looked!

I'd imagine it's a very rare occurrence?

... :grinning:

What for?

For anything - any purpose at all.

After all, it's just another microcontroller - it could be programmed to do anything one wishes ...

1 Like

I don’t see any problems with this, the ICSP header is available.
You are perfectly correct, saying this:

Exactly: it's entirely possible, but the question was, "has anyone ever actually done it in practice?"