For this tutorial I'm using a very simple hardware setup, to make it clear how things work. I'll hopefully revisit this at a later date and show some better ways to connect it, which use up fewer pins and allow faster send/receive code. The simple setup is fine though to begin with.
Your SD module should have pins labeled CS, SCK, MOSI, and MISO, as well as power pins. If so then great, it's the same as mine!
But if your module has differently-labeled pins, such as DI, DO, CLK, etc, then please refer to Wikipedia's page on the SPI protocol to read how they correspond to the names I'm using here.
You might also have some additional pins, such as a signal to tell you whether a card is inserted. Some modules have it and others don't. It's not important for now.
The function of the main pins is as follows:
- CS - chip select - usually high, pull it low before sending a command
- SCK - serial clock - transition this from low to high to send and receive a bit
- MOSI - master out, slave in - used to send data to the SD card
- MISO - master in, slave out - used to receive data from the SD card
Make sure your SD adapter module does level-shifting before connecting it up! If it doesn't, then this could damage your SD card.
- Connect CS to PA4
- Connect SCK to PA3
- Connect MOSI to PA2
- Connect MISO to PA1
You also need to connect any power pins. If your module doesn't have its own regulator, you may need to give it an additional 3.3V supply.
Unfortunately there's no easy way to check that this is working - you need to write proper SPI send/receive functions first, which are covered in the next chapter.