Port of ChaN's FAT SD/MMC SPI to libopencm3 STM32F103
has provided a package for using the Microsoft FAT filesystem, FatFs, in
embedded microcontroller applications. This is a generic package
intended for use with various storage media including disk drives and
memory cards or USB sticks. The software relies on a lower level driver
library being provided to access the specific storage using the
microcontroller hardware. A number of driver packages have been provided by ChaN and others.
In 2010 Martin Thomas released driver code for the FatFs filesystem, extending it to SD/MMC cards using the SPI interface with hardware drivers for ARM Cortex-M3 STM32F103.
Martin's code to make use of libopencm3
rather than the STM library. This makes FatFs usable in libopencm3
based projects and may ease its adaptation to other ARM Cortex-M
processors. In particular other STM32F series microcontrollers may be
used with only minor configuration changes. In addition to this,
Martin's code has been updated to the
latest FatFs filesystem package release (R0.09b patched). There were
some minor API changes which seemed to be only changes in variable
names. Any future releases of FatFs should hopefully be
simply a drop-in replacement, provided the API doesn't change any more.
The changes are minor,
with a number of redundant files removed and others shifted around. The
main modifications are to replace the disk driver file sd_spi_stm32.c
with sd_spi_loc3_stm32.c. Some changes were also made to rtc.c which access the RTC counter. The
Makefile is intended only as a template and should be replaced by one
suited to the environment in which it will be used. Two other files sd_spi_stm32_freertos.c, sd_spi_stm32_almost_reentrant.c add FreeRTOS calls for timing and attempt (so far unsuccessfully) to make the driver code re-entrant.
The driver code uses a board.h header file to define the hardware ports and peripherals to be used by the drivers. There are some issues to be dealt with when setting up this file. The SPI interface is used for the SPM32F103 to access the storage media. In the header file provided there are several boards defined, some using SPI1 and others SPI2. Note carefully that in the STM32F series the SPI2 peripheral is clocked differently to the SPI1 peripheral and so the RCC setup is different, in particular the base clock divider for SPI2 will be different. This setup may impact on other peripherals as it is a global setting for all peripherals (there is more than one clock distribution network in the devices).
A second point to note is that the driver code supplies a function called disk_timerproc() which is not used by the FatFs code. This is used to update timer variables for providing delays and other timing within the driver code. This must be called in a timer ISR every 10ms. The ISR may be provided by any timer; preferably by systick (which is common to much of the ARM family).
Before writing code, test the hardware with Martin's ff_test_term using the USART1 for communications (the readme.txt packaged with this code includes a short tutorial sequence). The only commands needed to get started are fi to initialize the filesystem, and fs to print out the file system details.