SPI1 on atmega328PB

Hello.
In a custom PCB with atmega328PB, i want to use SPI1 (minicore library) to communicate and save data on an SD card.
I tried to use SD library with SPI1.h but it doesn't work

#include <SPI1.h>
#include <SD.h>

#define carteSD PIN_PE2 // pin SS1

Sd2Card card;
SdVolume volume;
SdFile root;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }

  Serial.print("\nInitializing SD card...");

  if (!card.init(SPI_HALF_SPEED, carteSD)) { 
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

I'm not a good programmer, so I don't know what to do to make it works.

Does anyone knows this problem and found a solution for that ?
It is possible to only rewrite the SD library for communicating with SPI1 port instead of SPI0 ?

Thanks for your help.

What is the reason for not using the standard SPI port?

Please post a wiring diagram, showing how you wired the SD module.

Is there an I/O voltage mismatch between the processor and the SD module?

Finally, define "doesn't work".

1 Like

Because, after uploading the bootloader, the SPI is not accessible anymore on my PCB, the pin concerned are used as I/O only (component soldered on pins), so I can only use SPI1.

I use the DFROBOT DFR0071 directly connected to the 328pb adequates pins
image
image

the wiring is correct for me, so the problem comes from my program

I get the error `"initialization failed"

That doesn't make sense.

In any case, the SD library assumes the standard SPI port. There is nothing in your code that tells it otherwise, and glancing through the library code, it does not appear that such an option is built in to the SD library.

You can edit the library to change register names to those of the SPI1 peripheral, and address the appropriate ports & pins.

can you please tell me how to do that ? I'm beginner in programming, so its too difficult for me

Why not instead fix whatever the problem is with the normal SPI port?

So far what you have said makes no sense. The bootloader has nothing to do with SPI.

What you are trying to do is NOT a beginner problem, and you are in way over your head.

But for the record, wherever the SD/SDFat library refers to a port register like SPCR, substitute the SPI1 alternative, SPCR1. You will need to study the data sheets for both the ATmega328P and the ATmega328PB to make sure of the differences.

1 Like

I took a closer look at the SD/SDFat library, and see that it can be configured either to use the SPI module registers directly, or call the methods in the standard SPI library, <SPI.h>.

I did not look closely enough to see what the default configuration is, as installed in the Arduino IDE, but if configured to use the standard SPI library, then there is no need to change the register names to refer the SPI1 module, and that would not work anyway, since they are not used.

Instead, change only the calls to the SPI library methods to reflect their counterparts in SPI1.h, e.g. SPI.begin() becomes SPI1.begin(), etc.

Because my pcb is already done and some SPI0 pin are used to make PWM for my project.
But if there is no solution, I'll have to make another kind of pcb

Thank you for that.

Okay, I'll try it

So I'm trying things with SD library, less files, so the easiest for me.

I found on the "src" folder, "Sd2Card.h" and .cpp where "SPI_MISO_PIN" and others appears, but I don't find "atmega328" word on any of this file.

So I don't know which SPI pin to modify for my test. Can you help me with this please ?

It would be easier to fix or replace the PCB, which is the real problem.

My question, the subject of this topic and my goal are clear, I don't understand why you insist on replacing the PCB....

It's NOT the choice I want at this moment, it doesn't fit my needs because I explain it twice...

At this moment, I need to know if there is a simple way to use SPI1 with "SD" or "SDfat" modified library. So if someone or you can help me in this way, I'll appreciate.

I'll already try to replace all words with "MISO, SS, MOSI, SCK" in all files founded in SD or SDfat library, and also on SPI arduino library, but nothing out to SPI1

So I hope someone will help me in that way

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.