Example showing how to drive the IS42S16400J SDRAM memory mounted on STM32F429I-Discovery board.

Dependencies:   BSP_DISCO_F429ZI SDRAM_DISCO_F429ZI mbed

Committer:
jeromecoutant
Date:
Tue Jul 04 15:57:54 2017 +0000
Revision:
3:5702fea0c868
Parent:
0:3a9aa7743ad9
Update with MBED rev145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:3a9aa7743ad9 1 #include "mbed.h"
bcostm 0:3a9aa7743ad9 2 #include "SDRAM_DISCO_F429ZI.h"
bcostm 0:3a9aa7743ad9 3
bcostm 0:3a9aa7743ad9 4 SDRAM_DISCO_F429ZI sdram;
bcostm 0:3a9aa7743ad9 5
bcostm 0:3a9aa7743ad9 6 DigitalOut led_green(LED1);
bcostm 0:3a9aa7743ad9 7 DigitalOut led_red(LED2);
bcostm 0:3a9aa7743ad9 8
bcostm 0:3a9aa7743ad9 9 Serial pc(USBTX, USBRX);
bcostm 0:3a9aa7743ad9 10
bcostm 0:3a9aa7743ad9 11 #define BUFFER_SIZE ((uint32_t)0x0100)
bcostm 0:3a9aa7743ad9 12 #define WRITE_READ_ADDR ((uint32_t)0x0800)
bcostm 0:3a9aa7743ad9 13
bcostm 0:3a9aa7743ad9 14 void FillBuffer(uint32_t *pBuffer, uint32_t BufferLength, uint32_t Offset);
bcostm 0:3a9aa7743ad9 15 uint8_t CompareBuffer(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength);
bcostm 0:3a9aa7743ad9 16
bcostm 0:3a9aa7743ad9 17 int main()
bcostm 0:3a9aa7743ad9 18 {
bcostm 0:3a9aa7743ad9 19 uint32_t WriteBuffer[BUFFER_SIZE];
bcostm 0:3a9aa7743ad9 20 uint32_t ReadBuffer[BUFFER_SIZE];
bcostm 0:3a9aa7743ad9 21 FMC_SDRAM_CommandTypeDef SDRAMCommandStructure;
bcostm 0:3a9aa7743ad9 22
bcostm 0:3a9aa7743ad9 23 pc.printf("\n\nSDRAM demo started\n");
bcostm 0:3a9aa7743ad9 24 led_red = 0;
bcostm 0:3a9aa7743ad9 25
bcostm 0:3a9aa7743ad9 26 // Fill the write buffer
bcostm 0:3a9aa7743ad9 27 FillBuffer(WriteBuffer, BUFFER_SIZE, 0xA244250F);
bcostm 0:3a9aa7743ad9 28
bcostm 0:3a9aa7743ad9 29 // Write buffer
bcostm 0:3a9aa7743ad9 30 sdram.WriteData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR, WriteBuffer, BUFFER_SIZE);
bcostm 0:3a9aa7743ad9 31 pc.printf("Write data DONE\n");
bcostm 0:3a9aa7743ad9 32
bcostm 0:3a9aa7743ad9 33 // Issue self-refresh command to SDRAM device
bcostm 0:3a9aa7743ad9 34 SDRAMCommandStructure.CommandMode = FMC_SDRAM_CMD_SELFREFRESH_MODE;
bcostm 0:3a9aa7743ad9 35 SDRAMCommandStructure.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK2;
bcostm 0:3a9aa7743ad9 36 SDRAMCommandStructure.AutoRefreshNumber = 1;
bcostm 0:3a9aa7743ad9 37 SDRAMCommandStructure.ModeRegisterDefinition = 0;
bcostm 0:3a9aa7743ad9 38 if (sdram.Sendcmd(&SDRAMCommandStructure) != HAL_OK)
bcostm 0:3a9aa7743ad9 39 {
bcostm 0:3a9aa7743ad9 40 led_red = 1;
bcostm 0:3a9aa7743ad9 41 error("BSP_SDRAM_Sendcmd FAILED\n");
bcostm 0:3a9aa7743ad9 42 }
bcostm 0:3a9aa7743ad9 43
bcostm 0:3a9aa7743ad9 44 // SDRAM memory read back access
bcostm 0:3a9aa7743ad9 45 SDRAMCommandStructure.CommandMode = FMC_SDRAM_CMD_NORMAL_MODE;
bcostm 0:3a9aa7743ad9 46 if (sdram.Sendcmd(&SDRAMCommandStructure) != HAL_OK)
bcostm 0:3a9aa7743ad9 47 {
bcostm 0:3a9aa7743ad9 48 led_red = 1;
bcostm 0:3a9aa7743ad9 49 error("BSP_SDRAM_Sendcmd FAILED\n");
bcostm 0:3a9aa7743ad9 50 }
bcostm 0:3a9aa7743ad9 51
bcostm 0:3a9aa7743ad9 52 while(1) {
bcostm 0:3a9aa7743ad9 53
bcostm 0:3a9aa7743ad9 54 // Read back data from the SDRAM memory
bcostm 0:3a9aa7743ad9 55 sdram.ReadData(SDRAM_DEVICE_ADDR + WRITE_READ_ADDR, ReadBuffer, BUFFER_SIZE);
bcostm 0:3a9aa7743ad9 56 pc.printf("\nRead data DONE\n");
bcostm 0:3a9aa7743ad9 57
bcostm 0:3a9aa7743ad9 58 // Checking data integrity
bcostm 0:3a9aa7743ad9 59 if (CompareBuffer(WriteBuffer, ReadBuffer, BUFFER_SIZE) != 0)
bcostm 0:3a9aa7743ad9 60 {
bcostm 0:3a9aa7743ad9 61 led_red = !led_red;
bcostm 0:3a9aa7743ad9 62 pc.printf("Write/Read buffers are different\n");
bcostm 0:3a9aa7743ad9 63 }
bcostm 0:3a9aa7743ad9 64 else
bcostm 0:3a9aa7743ad9 65 {
bcostm 0:3a9aa7743ad9 66 led_green = !led_green;
bcostm 0:3a9aa7743ad9 67 pc.printf("Write/Read buffers are identical\n");
bcostm 0:3a9aa7743ad9 68 }
bcostm 0:3a9aa7743ad9 69
bcostm 0:3a9aa7743ad9 70 wait(1);
bcostm 0:3a9aa7743ad9 71 }
bcostm 0:3a9aa7743ad9 72 }
bcostm 0:3a9aa7743ad9 73
bcostm 0:3a9aa7743ad9 74 /**
bcostm 0:3a9aa7743ad9 75 * @brief Fills buffer with user predefined data.
bcostm 0:3a9aa7743ad9 76 * @param pBuffer: pointer on the buffer to fill
bcostm 0:3a9aa7743ad9 77 * @param BufferLength: size of the buffer to fill
bcostm 0:3a9aa7743ad9 78 * @param Value: first value to fill on the buffer
bcostm 0:3a9aa7743ad9 79 * @retval None
bcostm 0:3a9aa7743ad9 80 */
bcostm 0:3a9aa7743ad9 81 void FillBuffer(uint32_t *pBuffer, uint32_t BufferLength, uint32_t Value)
bcostm 0:3a9aa7743ad9 82 {
bcostm 0:3a9aa7743ad9 83 uint32_t tmpIndex = 0;
bcostm 0:3a9aa7743ad9 84
bcostm 0:3a9aa7743ad9 85 /* Put in global buffer different values */
bcostm 0:3a9aa7743ad9 86 for (tmpIndex = 0; tmpIndex < BufferLength; tmpIndex++ )
bcostm 0:3a9aa7743ad9 87 {
bcostm 0:3a9aa7743ad9 88 pBuffer[tmpIndex] = tmpIndex + Value;
bcostm 0:3a9aa7743ad9 89 }
bcostm 0:3a9aa7743ad9 90 }
bcostm 0:3a9aa7743ad9 91
bcostm 0:3a9aa7743ad9 92 /**
bcostm 0:3a9aa7743ad9 93 * @brief Compares two buffers.
bcostm 0:3a9aa7743ad9 94 * @param pBuffer1, pBuffer2: buffers to be compared.
bcostm 0:3a9aa7743ad9 95 * @param BufferLength: buffer's length
bcostm 0:3a9aa7743ad9 96 * @retval 0: pBuffer2 identical to pBuffer1
bcostm 0:3a9aa7743ad9 97 * 1: pBuffer2 differs from pBuffer1
bcostm 0:3a9aa7743ad9 98 */
bcostm 0:3a9aa7743ad9 99 uint8_t CompareBuffer(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength)
bcostm 0:3a9aa7743ad9 100 {
bcostm 0:3a9aa7743ad9 101 while (BufferLength--)
bcostm 0:3a9aa7743ad9 102 {
bcostm 0:3a9aa7743ad9 103 if (*pBuffer1 != *pBuffer2)
bcostm 0:3a9aa7743ad9 104 {
bcostm 0:3a9aa7743ad9 105 return 1;
bcostm 0:3a9aa7743ad9 106 }
bcostm 0:3a9aa7743ad9 107
bcostm 0:3a9aa7743ad9 108 pBuffer1++;
bcostm 0:3a9aa7743ad9 109 pBuffer2++;
bcostm 0:3a9aa7743ad9 110 }
bcostm 0:3a9aa7743ad9 111
bcostm 0:3a9aa7743ad9 112 return 0;
bcostm 0:3a9aa7743ad9 113 }