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