Re-map SPI pins

I am trying to get an SD card reader to work with a mega2560. I want to use pins 10-13 for CS DI D0 and SCK respectively. On the mega these would normally be mapped to 53 51 50 and 52

Using the example code Cardinfo, I can set CS to be 10.

const int chipSelect = 10;

This makes sense on the surface how ever digging through the .H files for SD, Sd2, SPI as well as the pins_arduino file for the mega I can not find any reference to "chipSelect". Plenty of SS MOSI MISO SCLK or variations of that with _PIN

in the pins_arduino file for the mega I could just:

#define PIN_SPI_SS    (10) // rather than 53
#define PIN_SPI_MOSI  (11) // 51
#define PIN_SPI_MISO  (12) // 50
#define PIN_SPI_SCK   (13) // 52

static const uint8_t SS   = PIN_SPI_SS;
static const uint8_t MOSI = PIN_SPI_MOSI;
static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK  = PIN_SPI_SCK;

I would how ever like to do it at the same time as redefining chipSelect, but would need to know what const ints to change, any ideas on where chipSelect would have been defined in either the SD libraries or the SPI libraries.

My main question is where would I find chipSelect being defined in the libraries.

The SPI interface is always mapped to those pins on a Mega.

There is a softSPI library, but it can be a bit slow.

Yes by the pins_arduino file if I understand it correctly. How ever can they be remapped without using another library and just sticking with SD.h and SPI.h or this a hardware issue. It must be possible to get it to work this way as other libraries can make it work, but I am just trying to understand the functionality from the ground up as far as possible without short cutting through libraries that just "work"

OK, looking at the chip it self these are pins PCINT0~3 and I want to shift it to PCINT4~7.

You cannot re-map the hardware SPI pins on a Mega.

1 Like

Gotcha. I have changed pins to use the relevant hardware pins. With SD/SPI libraries, is there still a limitation on SD card size? I have format as FAT32 currently on a 16gig

The SPI library has no impact on the SD card size. It is the SD library that controls that.

Hence "SD/SPI" nothing is explcitly stated besides SD card format being Fat16 or 32. Seeing as I changed my pinout to the coreect hardware pins it really leaves very few issues. Either the shield is dead or the SD card. Or should I post a new topic. Most of the questions I found are from a few years back and apparently it is no longer an issue but nothing seems to explicitly state as such.

But did you? You can't change the mapping of the pins that attach to the hardware SPI pins.

Yes. The appropriate pins 50 to 53. Without any changes to libraries. Tried setting CS(SS) pin as both 53 and 10. 10 being the the pin on the sheild as somewhere I came across "ChipSelect" meaning the pin on the shield. (2.8 tft shield)

Its still bothers me that I cant find were the "ChipSelect" value is actually used. It is defined but not used as far as I can see. Going to go through example code again.

OK have it your own way. Best of luck with your project. I will not reply again to this thread.

What?? How is using the correct pins doing it my own way? On the mega2560 board the 53 pin is SS. The MOSI pin is 51. The MISO pin is 50. SCK pin is 52 and I have it wired accordingly.

The example sketch asks for a ChipSelect pin to be defined. Its either 53 as SlaveSelect is now ChipSelect. Or its 10 as that is the pin on the shield. With neither option is the example sketch which is meant to identify an SD card able to work.

How this equates to me trying to do it my own way I do not understand.

EDIT: ChipSelect does refer to the module pin rather than the SS pin of the of the Arduino so

const int chipSelect = 10;

Should be correct in the context of the Cardinfo example sketch

Turned out the shield has no level shifter between the sd card slot and arduino. I do have another question on pin mapping, can additional external interrupt pins be defined in the pin map file for the mega? The AVR data sheet does indicate that PB06 is a PCINT capable pin.

This is because the chip select is determined by the hardware to hand and has nothing to do with the SPI bus.

If your shield says pin 10 is SD's CS, then pin 10 it is, so stop arguing. On Mega, I believe it's still the case that you set pin 53 as output, irrespective of whether it is being used or not.

The Select pin on the controller may be many, one per device that doesn't have its own select wired open like a daisy-chained device, shift register (aka pin multiplier) for example.

The 2560 has 4 UARTs that can all act as master-mode SPI ports. Nick Gammon includes that in his SPI tutorial. Those pins can be used.

To use pin 10 as /SS, the hardware pin assigned to /SS must be declared OUTPUT, even if it is not used. If you don't, the SPI hardware will be in slave mode and will not work as expected.

Level shifters are required for all logic lines connected to the SD card module.

Thanks for all the responses. Yea, the reason nothing was working before when it came to chip select and writing to the SD card was as result of the SD card signals not being level shifted. The shield I am using has level shifters on it but only for the TFT display and nothing for the SD card.

74HC4050 level shifts 6 channels as a 16 pin chip, very clean, small, cheap.
SD needs 3 pins leveled. MISO at 3V works on 5V Arduino digital pins.

You may have killed the SD card with 5V.

That's going to be tricky since the Mega board does not bring any of the USART XCK lines to the headers.

I see so I refreshed my read of Nick Gammon covers SPI including UART-SPI.

What I see him do with an Uno is connect TX and RX to MOSI and MISO then use 2 other pins to bit-bang as clock and select, problem hacked!