Library 14- Working with SDRAM on STM32F429 Discovery

STM32F429 Discovery board has external 64Mbits or 8MBytes SDRAM chip ISSI IS42S16400.

STM32F429 has a FMC (Flexible Memory Control) peripheral to driving external SDRAM with hardware. FMC hardware is able to store up to 32bits variables at same time.

External SDRAM capatibilities
Variable size Max value Max address Max variables stored
8bit 0xFF 0x7FFFFF 8388608
16bit 0xFFFF 0x7FFFFE 4194304
32bit 0xFFFFFFFF 0x7FFFFC 2097152

Note: This library does not work on STM32F4 Discovery, because this MCU does not have FMC peripheral.

8MByte is a lot for that microcontroller, but can be very useful if you are working with LCD (storing images, …) which can increase LCD’s refresh rate.

In Standard Peripheral libraries pack is also included FMC example for SDRAM. I modified settings for our board and make a simple library.

SDRAM library

Features

  • Operates with external 64Mb SDRAM
  • Functions to read/write 8, 16 or 32bit variable at a time
  • Version 1.1 – November 23, 2014
    • Functions for read/write are now defined as macros. This allows you to increase speed performance because you don’t need to call functions and put data to the stack
  • Supports STM32F429-Discovery or STM324x9-EVAL boards

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx FMC
  • TM
    • TM GPIO
    • defines.h
    • attributes.h

STM324x9-EVAL support

By default, library is designed for low cost STM32F429-Discovery board. If you want to use this library on STM324x9-EVAL board, open defines.h file and add define:

Initialize

As always, here is the same. First we have to initialize SDRAM library. This is done with

Function returns 1 if sdram was properly initialized, to check this, function just simple write data to SDRAM’s location and read it back. If they are the same, everything is OK.

Write operation

For writing data to SDRAM I made 3 functions.

Read operation

Example

Project available on Github, download library below.

Icon
TM STM32F4 SDRAM Library

External SDRAM on STM32F429 Discovery board

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Roman Deneš

    Hi,
    can I ask you how to define matrix (let’s say 200×300) in sdram? I tried it in RAM (using uint8_t matrix[200][300];) but the matrix is probably too large. Thank you.

    • Hi,

      200*300 is quite huge array.

      If you want to use something like this in external ram, you can easily make “emulator” for that using 2 defines.

      As you maybe know, matrix[0][0] is at address = 0;

      then matrix[0][1] is at address 1 and so on.

      You can make a define which will do:
      //define X and Y sizes
      #define X_SIZE 200
      #define Y_SIZE 300

      //write matrix[x][y] = val;
      #define MATRIX_SET_VALUE(x, y, val) TM_SDRAM_Write8(x * Y_SIZE + y, val)

      //read: val = matrix[x][y];
      #define MATRIX_GET_VALUE(x, y) TM_SDRAM_Read8(x * Y_SIZE + y)

      If you have any problems, contact me via email.

      • Roman Deneš

        Thank you, it works perfectly 🙂

  • Petr Machala

    Hi,
    may I ask if there is a way to use this SDRAM library to display images on LCD without the LTDC driver? Something like a FSMC on previous discovery boards.

    I have used your LTDC code and it is brilliant but only as a standalone app. I need to display an image from DCMI bus to 32F429IDISCOVERY LCD. Here a DCMI overlaps with LTDC (i.e. PA4-DCMI_HSYNC/LCD_VSYNC) and makes only noise. Multiplexing is quite unreliable.

    So I ended up with DCMI->RAM (over DMA)->SPI(your code)->LCD and this is very, very slow. I need something quicker like LTDC. The best would be a direct connecion like DCMI->LCD(over DMA). Is it possible? Thank you very much.

    • Hi,

      You can use external ram to store data, but for transfer you can use ltdc or spi od f429 discovery board.
      Try to set spi5 preacaler to 2,you will get about 45MHz clock.

      • Petr Machala

        So there are only these two options. LTDC or SPI right?

        • Yep.

          • Petr Machala

            That sucks 🙂 … maybe I’ll migrate to older discovery board so that I can use FSMC, but for now I need to at least boost the SPI to the max… But thanks for your help…It’s superb!

          • You need bigger stm version, maybe 176pins will be good.
            No problem 😉

          • Petr Machala

            Do you have some experince with those? Would you recomend me something?

          • The same device, just more pins. Maybe with more dcmi pins possible.

          • Petr Machala

            I think there isnt a DISCO board with that amount of IO pins…only individual MCUs…

          • Correct. That was my point.

          • Petr Machala

            Again … that sucks 🙂 … I am trying to have simple app and now I have to make a whole PCB 🙂 … anyway again thanks very much for your help …

  • Christian Julius

    Hello,

    when compiling with EmBlocks I always get

    can’t find FSMC_Bank1E etc 30 times or more. Dont know what the reason is. I checked the files and thats
    true, this expression is never defined in the StdPeriph Libs.

    Regards,
    Christian

    • remove …_fsmc.c file from project.
      Add …_fmc.c to project if it is not already!

      • Christian Julius

        Hi,

        yeah, thats it! ST never fixed the bug since 2012. (Their is also a bug in the STM32F4xx.h with the BSRR Register at 16 bit.) One last question Majerle:
        Does it make sense to define the sdram in the linker as a section, e.g. sdram
        to make it avaliable with var myvar __attribute(.sdram)). ? Is this possible, because the sdram is only avalable after the Init Process. Any attempt to acess it before crashes the CPU.process flow. GCC like any other compiler inits static variables from ROM table to RAM in the startup process.

        I dont like using function liekm write and read, I prefer defining a Variable with their location. If you mean, yes it makes sense, what is the right definition in the cryptic linker file?

        sdram
        {
        ????
        } > SDRAM

        Regards from germany,
        Christian

        • Ok, please first make sure that you know what are you talking!
          This is not a bug, because F407 has FSMC peripheral, F429 has FMC.
          It’s not bug at all, it’s user problem because he don’t know everything about STM32F4xx 😉

          Hum, well, I didn’t try that.
          If you need a biig array of data, you can declare heap memory on SDRAM and use malloc to allocate it.

          • Christian Julius

            Hi,

            ok. I will try it. I like simulations like “Game of Life” and they need huge amounts of RAM and fast algorithms to calculate new Generations. The Board is a playground for me, have no real application for it in hobby. Most physical sensor things are done with Arduino or my UDOO board much faster.

            Thank you and beste regards,
            Christian

  • abdurrahman korkmaz

    hi,
    after write sdram and then power off, will data stay there or not?

  • mukhe

    Hey Majerle,
    are your previous libraries based on the Std peripheral lib or did you port them all to the new HAL?

    • Previous? Which one are previous?
      Have you seen any library ported for HAL? Nope, you didn’t 😉

      • mukhe

        hmm..I thought you mentioned about porting your previous libraries (for example this one on sdram) here: http://stm32f4-discovery.com/2015/03/library-53-gpio-for-stm32f4/

        so was just curious. anyways clear now

        • This library was made to be independent from user selected drivers. So any driver (or noone) can be used and GPIO library will still work.

          But yeah, if I will start with F7 libraries, then I will have to start with HAL becaust STD drivers are not available. In this case, all libraries will also be able to be used with other series.
          The idea is that with single define, you select which series (F0/F4/F7 for now) will be used in project.

          Only different low level hal drivers from ST will need to be provided.

          But yeah, this is now just an idea, waiting for some responses from community. Discovery for F7 is at my place, so I can technically start but will see how will go this.

          Have a nice day.

          • mukhe

            Aight thanks for clearing that out

  • rboldu

    Hi Majerle,

    The library looks great, It’s super easy to use! Thanks! I would like to ask you about the number of instructions need it for an internal RAM vs an external SDRAM. Is it much slower the external one?

    Thanks!
    Roger

    • Clock for external ram is SystemClock/2 and data bus is 16bit wide.
      Internal has sysclock speed and 32bit bus. You can calculate difference 😀

      I dont know what you mean number of instruction.

  • Jeremy Vance

    Would this library be a good starting point for interfacing to NAND flash? I’m using the F407 Discovery board and would like to add a 1GB NAND flash chip to my project instead of an SD Card or USB Drive.

  • Pat Rogers

    Can the LCD on the F429 Disco be driven without using SDRAM too? I just need to output text and the SDRAM is using a lot of GPIO pins I need for other purposes. Thanks!

    • Theoretically, it can, but only one layer.
      You need lcdW * lcdH * 2 of interbal RAM reserved for LCD to be able to use it.

      • Pat Rogers

        Thank you! Do you know of any existing drivers available?

        • You can use my lib as starting point. Set lcd layer 1 address and remove initialization part for layer 2.
          For ram address, create a variable with previously told site and put location of this variable to location of lcd frame buffer.

          • Pat Rogers

            OK, and thanks again. (You have a very impressive body of work, by the way!)

          • Pat Rogers

            Just to be sure, rather than make assumptions, you mean “tm_stm32f4_ili9341.h”, right?

          • Right! For lcd wo ext ram, then use my spi based ili driver, library 8. Filename is as you said

          • Pat Rogers

            OK, thanks!

  • Sakul Relda

    Hello Majerle Tilen,
    i have a Question about your Project. Is it possible to use this Project with Keil – uVision and the HAL-Support?
    I would like to get a simple Application which could read and write from the external SDRAM on the STM32F429 Discovery Board.
    I hope you can help me.
    Best regards

  • Majid Barz

    Hi Tilen. First of all thanks for your libraries and hard work
    I have a little trouble using your SDRAM library. my problem is:
    when i initialize SDRAM (by TM_SDRAM_Init() ) and I store some data in SDRAM, then I initialie PG8 as an output (which I need in my project), after that it looks like SDRAM is cleared and all those data stored is now gone. I think it is because PG8 is the pin used for SDCLK in SDRAM. I can work without using PG8 and leave it for SDRAM to work I only need it to be LOW always but when I don’t initialize it as an OUTPUT and leave it for SDRAM to work, it’s always HIGH which is not good for me. what should I do?
    Thanks

    • I don’t understand why is PG8 need for your use. There is around 100 other pins.
      CLK is needed for SDRAM to work, obviously as this is not static ram and it refreshes continuously.

      • Majid Barz

        Thanks for reply..
        I designed my board and I didn’t know PG8 was necessary for SDRAM so I used PG8. but I must probably fix it physically.
        one more question please, is there any other Pin that SDRAM must take control of? like PG8?

        • None of pins designed for SDRAM cannot be used for other purpose if SDRAM is there.

          • Majid Barz

            Thank you.

      • Majid Barz

        Hi again Tilen.
        I checked and I saw there is so many pins that SDRAM uses(around 40 pins) and I definitely need some of those pins. at the same time I need SDRAM simultaneously. (for example I need PE12, PE13 and PE14 for SPI but SDRAM uses them too)
        Can I do anything to use SDRAM while using those pins?

        • Im sire you need those pins for spi. And im also sure you have other pins for that spi. Check schematics before you use it.