SD library modification for using SPI1 minicore

You see in the official library code,

It already has provisions for accepting an externally set SDCARD_SPI macro value. In the not-defined case (#ifndef) it defines that macro to its fallback value of SPI. And since nothing else in this library defines SDCARD_SPI, this is the default case when no additional options are set. Meaning that if build_flags = -D SDCARD_SPI=SPI1 is set in the platformio.ini, that macro will be defined, to SPI1, and the correct SPI object will be used in every code that references SDCARD_SPI, i.e., the code below.

^-- Writing to this register of the SPI1 peripheral will trigger action on the microcontroller’s hardware SPI1 pins, as listed in the datasheet.

The SD card library has no idea what the SPI pins are in the current configuration and it does need to. It delegates the work to the SPI(1) library and just calls SPI1.transfer(). The SPI(1) library code is then responsible for righting to the right hardware peripheral registers which will trigger actions on the hardcoded / hardware SPI pins.

The only pin the SD library explicitly knows about is the chip select pin you give it. It saves that in the SD.init() call in its chipSelectPin_ member variable and later on uses regular digitalWrite() on it.

grafik

OKay. Thank you very for taking the time to answer to all this beginner question.
Its time for me to read learn more about C++ to better understand future problem.

Thanks again !

Last thing I would recommend that instead instead of refering to the “live link” of the library via lib_dep in the platformio.ini, you just copy the library in the lib/ folder of the project and remove the lib_deps lin then. Then it’ll be saved locally in the project and no need to go to the internet to get the lib.

Okay, thank you for the advice.
If I understand, the advantage of using libs_deps is to not save an online library on a computer. But the disadvantage is to not be able to compile if we don’t have access to network.

I just realise that I haven’t understand what you said here. what’s .pio/libdeps/` ? How do you see which sections are activated or not ?

Finally, I understand that the (AVR_ATmega328PB) is used in arduino.h but, I can’t find where this words are defined before being read by arduino.h.
Still trying to understand how the program knows that it works with this exact version of AVR Uc ^^

__AVR_ATmega328PB__ is not defined by any header file in the Arduino core. As indicated by the __ at the start and front, which is programmer speak for “INTERNAL STUFF, DO NOT TOUCH”, it’s a macro automatically defined by the compiler. The compiler defines a whole bunch of macros automatically when it’s invoked, so that the C files can react to the architecture and MCU the compiler is compiling for. You can see all those when you e.g. ask the compiler to output the pre-processed file given an empty input file like this. E.g., when I do that on Windows as

C:\Users\Max\.platformio\packages\toolchain-atmelavr\bin>avr-gcc -mmcu=atmega328pb -dM -E - < NUL
#define __DBL_MIN_EXP__ (-125)
#define __HQ_FBIT__ 15
#define __UINT_LEAST16_MAX__ 0xffffU
#define __ATOMIC_ACQUIRE 2
#define __SFRACT_IBIT__ 0
#define __FLT_MIN__ 1.17549435e-38F
#define __GCC_IEC_559_COMPLEX 0
#define __BUILTIN_AVR_SLEEP 1
#define __BUILTIN_AVR_COUNTLSULLK 1
#define __BUILTIN_AVR_COUNTLSULLR 1
[...]
#define __SIZEOF_PTRDIFF_T__ 2
#define __AVR 1
#define __BUILTIN_AVR_ABSLK 1
#define __BUILTIN_AVR_ABSLR 1
#define __AVR_ATmega328PB__ 1
#define __LACCUM_EPSILON__ 0x1P-31LK
[....]
#define AVR 1
#define __BUILTIN_AVR_FMULS 1
#define __INTPTR_TYPE__ int
#define __UINT16_TYPE__ unsigned int
#define __WCHAR_TYPE__ int
#define __SIZEOF_FLOAT__ 4
#define __AVR__ 1
#define __AVR_HAVE_JMP_CALL__ 1
[...]
#define __BUILTIN_AVR_FMUL 1

(Output heavily shortened.)

You can see that when the AVR compiler is set to compile for ATMgea328PB (as set with -mmcu=atmega328pb), the automatically set defines include #define __AVR_ATmega328PB__ 1 as a global macro. As such, C code can react to the user-set compiler settings.

Ho wow, thank you for all these explanation,

is the key :slight_smile: thanks again !