HAL library 11- SDRAM for STM32Fxxx
SDRAM library was designed to be used on STM32F429-Discovery, STM32F439-EVAL and STM32F7-Discovery boards. With single define, library will know which board is used and which settings should use to get proper working for RAM.
FMC peripheral is used for driving SDRAM.
Library
Features
- Use SDRAM on STM32F429-Discovery board
- Use SDRAM on STM32F439-EVAL board
- Use SDRAM on STM32F7-Discovery board
- Supports custom pins initialization in case user has own board with RAM
- Supports commands for read and write different types of numbers
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h
- attributes.h
- TM GPIO
Board pinouts
SDRAM pins are different for different board. Using proper defines (explained below) you can select which board is used.
For full pinout list, download and open SDRAM library and check header file with description.
Select board
The most important thing is to tell library, which board is used in target system. There are 2 possible ways to do that. One (and I prefer it) is to check my DISCO library and follow instructions which define you have to set for proper board, and another, explained below.
If you choose option one, then you will have no worries in my future libraries where library needs to know exactly which board is used!
The second option (I don’t prefer it) is, to set define only for this library. Copy define corresponding to your board into defines.h file or make a global defines in compiler’s settings. I made global defines in Keil uVision under “Options for Target” -> “C/C++” tab.
1 2 3 4 5 6 7 8 |
//Use SDRAM on STM32F439-EVAL board #define SDRAM_USE_STM32F439_EVAL //Use SDRAM on STM32F7-Discovery board #define SDRAM_USE_STM32F7_DISCOVERY //Use SDRAM on STM32F429-Discovery board #define SDRAM_USE_STM32F429_DISCOVERY |
Functions and enumerations
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 101 102 103 104 105 |
/** * @defgroup TM_SDRAM_Functions * @brief Library Functions * @{ */ /** * @brief Initializes SDRAM * @param None * @retval SDRAM status * - 0: SDRAM is not configured OK * - > 0: SDRAM is configured OK and prepared to work */ uint8_t TM_SDRAM_Init(void); /** * @brief Gets SDRAM memory size * @param None * @retval SDRAM memory size in bytes */ #define TM_SDRAM_GetSize() (SDRAM_MEMORY_SIZE) /** * @brief Writes 8-bit value to SDRAM * @param address: Offset address from starting address of SDRAM * @param value: 8-bit value to be saved in SDRAM * @retval None * @note Defined as macro for faster execution */ #define TM_SDRAM_Write8(address, value) (*(__IO uint8_t *) (SDRAM_START_ADR + (address)) = (value)) /** * @brief Reads 8-bit value from SDRAM * @param address: offset address from starting address of SDRAM * @retval 8-bit value stored at desired location * @note Defined as macro for faster execution */ #define TM_SDRAM_Read8(address) (*(__IO uint8_t *) (SDRAM_START_ADR + (address))) /** * @brief Writes 16-bit value to SDRAM * @param address: Offset address from starting address of SDRAM * @param value: 16-bit value to be saved in SDRAM * @retval None * @note Defined as macro for faster execution */ #define TM_SDRAM_Write16(address, value) (*(__IO uint16_t *) (SDRAM_START_ADR + (address)) = (value)) /** * @brief Reads 16-bit value from SDRAM * @param address: offset address from starting address of SDRAM * @retval 16-bit value stored at desired location * @note Defined as macro for faster execution */ #define TM_SDRAM_Read16(address) (*(__IO uint16_t *) (SDRAM_START_ADR + (address))) /** * @brief Writes 32-bit value to SDRAM * @param address: Offset address from starting address of SDRAM * @param value: 32-bit value to be saved in SDRAM * @retval None * @note Defined as macro for faster execution */ #define TM_SDRAM_Write32(address, value) (*(__IO uint32_t *) (SDRAM_START_ADR + (address)) = (value)) /** * @brief Reads 32-bit value from SDRAM * @param address: offset address from starting address of SDRAM * @retval 32-bit value stored at desired location * @note Defined as macro for faster execution */ #define TM_SDRAM_Read32(address) (*(__IO uint32_t *) (SDRAM_START_ADR + (address))) /** * @brief Writes float value to SDRAM * @param address: Offset address from starting address of SDRAM * @param value: float value to be saved in SDRAM * @retval None * @note Defined as macro for faster execution */ #define TM_SDRAM_WriteFloat(address, value) (*(__IO float *) (SDRAM_START_ADR + (address)) = (value)) /** * @brief Reads float value from SDRAM * @param address: offset address from starting address of SDRAM * @retval float value stored at desired location * @note Defined as macro for faster execution */ #define TM_SDRAM_ReadFloat(address) (*(__IO float *) (SDRAM_START_ADR + (address))) /** * @brief Initialize custom pins callback. * It can be used by user to implement custom pins for application if needed * @note This function is called every time @ref TM_SDRAM_Init() function is called * @param AlternateFunction: Alternate function to be used when initializing GPIO pins * @retval Initialization status: * - 0: User did not initialize custom pins, default will be used, depending on already supported boards * - > 0: User has initialized custom pins, default pins will be ignored * @note With __weak parameter to prevent link errors if not defined by user */ uint8_t TM_SDRAM_InitCustomPinsCallback(uint16_t AlternateFunction); /** * @} */ |
Example
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 |
/** * Keil project example for 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/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_sdram.h" #include "string.h" /* Data to write */ uint32_t Write[32]; uint32_t Read[32]; int main(void) { uint8_t i = 0; /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init SDRAM */ TM_SDRAM_Init(); /* Fill write data */ for (i = 0; i < 32; i++) { Write[i] = 1 << i; /* Write to SDRAM */ TM_SDRAM_Write32(4 * i, Write[i]); } /* Read data from SDRAM */ for (i = 0; i < 32; i++) { Read[i] = TM_SDRAM_Read32(4 * i); } /* Memory compare */ if (memcmp((uint8_t *)Read, (uint8_t *)Write, sizeof(Read)) == 0) { /* SDRAM is OK */ TM_DISCO_LedOn(LED_GREEN); } else { /* SDRAM failed */ TM_DISCO_LedOn(LED_RED); } while (1) { } } |
Project is available in Github, download all libraries below.
Recent comments