FatFs library extended for SDRAM
As mentioned at the beginning of post about FATFS with SDCARD, I’ve updated library to extend support for SDRAM on STM32F429-Discovery or STM324x9-EVAL board.
Low level functions have been added in library for write/read data ti SDRAM.
SDRAM set up
To use SDRAM with my FATFS library, you will need to include my SDRAM library, which implements low level functions for read/write in SDRAM and initialization process.
Next thing, you need to do, is to open defines.h configuration file and add define:
1 2 3 4 5 6 7 8 9 |
/* Enable SDRAM support in FATFS */ #define FATFS_USE_SDRAM 1 /* After define above, SDCARD is disabled */ /* If you need SDCARD and SDRAM at the same time as 2 drivers, use define */ /* Use FATFS with SDIO */ #define FATFS_USE_SDIO 1 /* Use FATFS with SPI */ #define FATFS_USE_SDIO 0 |
Functions for interface between SDRAM and FATFS are stored in fatfs/drivers/fatfs_sdram.c .h. Make sure you have included them in your project!
Low level functions for FATFS and SDRAM are now set. You can start working with mounting, opening files, everything you need.
The only problem is, that SDRAM is non-volatile memory which will not store data if there is no power. That’s the reason, why you will always have to make fat file system “on the fly” using “f_mkfs” function from FATFS. Look for example below:
defines.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/** * Defines for your entire project at one place * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @version v1.0 * @ide Keil uVision 5 * @license GNU GPL v3 * * |---------------------------------------------------------------------- * | Copyright (C) Tilen Majerle, 2014 * | * | This program is free software: you can redistribute it and/or modify * | it under the terms of the GNU General Public License as published by * | the Free Software Foundation, either version 3 of the License, or * | any later version. * | * | This program is distributed in the hope that it will be useful, * | but WITHOUT ANY WARRANTY; without even the implied warranty of * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | GNU General Public License for more details. * | * | You should have received a copy of the GNU General Public License * | along with this program. If not, see <http://www.gnu.org/licenses/>. * |---------------------------------------------------------------------- */ #ifndef TM_DEFINES_H #define TM_DEFINES_H /* Put your global defines for all libraries here used in your project */ /* Use SDRAM with FATFS */ #define FATFS_USE_SDRAM 1 #endif |
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
/** * Keil project template for using FATFS with SDRAM * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx Keil packs version 2.4.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.5.0 or greater required * * Check defines.h file for configuration settings about SDRAM and FATFS */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_fatfs.h" #include "stdio.h" #include "stdlib.h" #include "string.h" /* FATFS related */ FATFS fs; FIL fil; FRESULT fres; uint8_t SD_Buffer[15]; int main(void) { /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Mount FATFS on SDRAM */ fres = f_mount(&fs, "SDRAM:", 0); /* Create FAT volume with default cluster size, IMPORTANT for SDRAM because content is not stored forever! */ /* This has to be done only once, when you first time init your SDRAM */ /* It is initialized first time using f_mount() function call above */ fres = f_mkfs("SDRAM:", 0, 0); /* Open file */ fres = f_open(&fil, "SDRAM:test.txt", FA_CREATE_ALWAYS | FA_READ | FA_WRITE); /* Write data */ fres = f_write(&fil, "Hello world", 11, NULL); /* Close file */ fres = f_close(&fil); /* Unmount */ f_mount(NULL, "SDRAM:", 1); /* If everything succedded */ if (fres == FR_OK) { /* Mount FATFS on SDRAM */ fres = f_mount(&fs, "SDRAM:", 0); /* No need to call MKFS here again because fat system is already on SDRAM */ /* Open file */ fres = f_open(&fil, "SDRAM:test.txt", FA_READ | FA_WRITE); /* Read data */ fres = f_read(&fil, SD_Buffer, 11, NULL); /* Close file */ fres = f_close(&fil); /* Unmount SDRAM */ f_mount(NULL, "SDRAM:", 1); /* Turn on LED GREEN */ if (strncmp((char *)SD_Buffer, "Hello world", 11) == 0) { /* Turn on GREEN led */ TM_DISCO_LedOn(LED_GREEN); } else { /* Red led */ TM_DISCO_LedOn(LED_RED); } } else { /* Red led */ TM_DISCO_LedOn(LED_RED); } /* Main loop */ while (1) { } } |
Download library update below.
Read SD card with SDIO or SPI on STM32F4xx using FatFS by Chan
Recent comments