SDRAM for DISCO_F746NG basic example

Dependencies:   BSP_DISCO_F746NG

Committer:
adustm
Date:
Mon Mar 21 12:16:41 2016 +0000
Revision:
0:e674bc8aa29d
Child:
3:2bd1b98de6ec
SDRAM class for DISCO_F746NG basic demo

Who changed what in which revision?

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