This project is designed as an example of a STM32CubeIDE-generated system with FatFs middleware controlling an SPI-connected MMC/SD memory card.
The project was initially created in CubeIDE, and then code written by ChaN was ported to fit the CubeIDE generator.
This is a remake of the original project provided here for CubeIDE instead of CubeMX.
I also wrote a blog post which accompanies this repository and explains how to use it.
FATFS/Target/user_diskio_spi.c
and FATFS/Target/user_diskio_spi.h
into your CubeIDE project which has been configured to use FatFS.FATFS/Target/user_diskio.c
add #include "user_diskio_spi.h"
FATFS/Target/user_diskio.c
call USER_SPI_initialize(...)
in USER_initialize(...)
, USER_SPI_status(...)
in USER_status(...)
, USER_SPI_read(...)
in USER_read(...)
, USER_SPI_write(...)
in USER_write(...)
, and USER_SPI_ioctl(...)
in USER_ioctl(...)
.
USER
comment blocks.main.h
ensure that you have #define
s for SD_SPI_HANDLE
(e.g. hspi2), SD_CS_GPIO_Port
, and SD_CS_Pin
.user_diskio_spi.c:
//(Note that the _256 is used as a mask to clear the prescalar bits as it provides binary 111 in the correct position)
You can now call the FatFS functions from your `main()`. For more detailed explanation of this, with examples, check out [the blog post](https://01001000.xyz/2020-08-09-Tutorial-STM32CubeIDE-SD-card/).
## Additional note
Also note that if you're using this process with your own custom circuit, you may need pull-up resistors on the SCK, MISO, and MOSI lines. The SD card module I used in this post includes them internally - if you're wiring your own design, you might find you need to add them. You can also consider enabling the internal pull up resistors. More details are included [here](https://github.com/kiwih/cubeide-sd-card/issues/2).