PSRAM example.
Dependencies: BSP_DISCO_F413ZH mbed
main.cpp@1:61482ef96890, 2017-09-27 (annotated)
- Committer:
- bcostm
- Date:
- Wed Sep 27 07:47:28 2017 +0000
- Revision:
- 1:61482ef96890
- Parent:
- 0:e86d70f1f2ce
Add mbed library v151 + BSP library + improvements
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arostm | 0:e86d70f1f2ce | 1 | #include "mbed.h" |
arostm | 0:e86d70f1f2ce | 2 | #include "stm32f413h_discovery.h" |
arostm | 0:e86d70f1f2ce | 3 | #include "stm32f413h_discovery_lcd.h" |
arostm | 0:e86d70f1f2ce | 4 | #include "stm32f413h_discovery_psram.h" |
arostm | 0:e86d70f1f2ce | 5 | |
arostm | 0:e86d70f1f2ce | 6 | #define BUFFER_SIZE ((uint32_t)0x1000) |
arostm | 0:e86d70f1f2ce | 7 | #define PSRAM_WRITE_READ_ADDR ((uint32_t)0x0800) |
bcostm | 1:61482ef96890 | 8 | |
arostm | 0:e86d70f1f2ce | 9 | uint16_t sram_aTxBuffer[BUFFER_SIZE]; |
arostm | 0:e86d70f1f2ce | 10 | uint16_t sram_aRxBuffer[BUFFER_SIZE]; |
bcostm | 1:61482ef96890 | 11 | |
arostm | 0:e86d70f1f2ce | 12 | uint8_t ubSramWrite = 0, ubSramRead = 0, ubSramInit = 0, ubCompare = 0; |
arostm | 0:e86d70f1f2ce | 13 | __IO uint8_t write_complete = 0; |
bcostm | 1:61482ef96890 | 14 | |
bcostm | 1:61482ef96890 | 15 | static void print_demo_title(void); |
arostm | 0:e86d70f1f2ce | 16 | static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset); |
arostm | 0:e86d70f1f2ce | 17 | static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength); |
arostm | 0:e86d70f1f2ce | 18 | |
arostm | 0:e86d70f1f2ce | 19 | int main() |
arostm | 0:e86d70f1f2ce | 20 | { |
bcostm | 1:61482ef96890 | 21 | print_demo_title(); |
arostm | 0:e86d70f1f2ce | 22 | |
arostm | 0:e86d70f1f2ce | 23 | /*##-1- Configure the PSRAM device ##########################################*/ |
bcostm | 1:61482ef96890 | 24 | if (BSP_PSRAM_Init() != PSRAM_OK) |
arostm | 0:e86d70f1f2ce | 25 | { |
arostm | 0:e86d70f1f2ce | 26 | ubSramInit++; |
arostm | 0:e86d70f1f2ce | 27 | } |
arostm | 0:e86d70f1f2ce | 28 | |
arostm | 0:e86d70f1f2ce | 29 | /*##-2- PSRAM memory read/write access ######################################*/ |
arostm | 0:e86d70f1f2ce | 30 | /* Fill the buffer to write */ |
arostm | 0:e86d70f1f2ce | 31 | Fill_Buffer(sram_aTxBuffer, BUFFER_SIZE, 0xC20F); |
arostm | 0:e86d70f1f2ce | 32 | |
arostm | 0:e86d70f1f2ce | 33 | /* Write data to the PSRAM memory */ |
bcostm | 1:61482ef96890 | 34 | if (BSP_PSRAM_WriteData(PSRAM_DEVICE_ADDR + PSRAM_WRITE_READ_ADDR, sram_aTxBuffer, BUFFER_SIZE) != PSRAM_OK) |
arostm | 0:e86d70f1f2ce | 35 | { |
arostm | 0:e86d70f1f2ce | 36 | ubSramWrite++; |
arostm | 0:e86d70f1f2ce | 37 | } |
arostm | 0:e86d70f1f2ce | 38 | |
arostm | 0:e86d70f1f2ce | 39 | /* Read back data from the PSRAM memory */ |
bcostm | 1:61482ef96890 | 40 | if (BSP_PSRAM_ReadData(PSRAM_DEVICE_ADDR + PSRAM_WRITE_READ_ADDR, sram_aRxBuffer, BUFFER_SIZE) != PSRAM_OK) |
arostm | 0:e86d70f1f2ce | 41 | { |
arostm | 0:e86d70f1f2ce | 42 | ubSramRead++; |
arostm | 0:e86d70f1f2ce | 43 | } |
arostm | 0:e86d70f1f2ce | 44 | |
arostm | 0:e86d70f1f2ce | 45 | /*##-3- Checking data integrity ############################################*/ |
arostm | 0:e86d70f1f2ce | 46 | /* Enable the LCD */ |
arostm | 0:e86d70f1f2ce | 47 | BSP_LCD_DisplayOn(); |
arostm | 0:e86d70f1f2ce | 48 | |
bcostm | 1:61482ef96890 | 49 | if (ubSramInit != 0) |
arostm | 0:e86d70f1f2ce | 50 | { |
bcostm | 1:61482ef96890 | 51 | BSP_LCD_DisplayStringAt(20, 100, (uint8_t *)"PSRAM Initialization FAIL", LEFT_MODE); |
bcostm | 1:61482ef96890 | 52 | BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"PSRAM Test Aborted", LEFT_MODE); |
bcostm | 1:61482ef96890 | 53 | while(1); |
arostm | 0:e86d70f1f2ce | 54 | } |
arostm | 0:e86d70f1f2ce | 55 | else |
arostm | 0:e86d70f1f2ce | 56 | { |
bcostm | 1:61482ef96890 | 57 | BSP_LCD_DisplayStringAt(20, 100, (uint8_t *)"PSRAM Initialization OK", LEFT_MODE); |
arostm | 0:e86d70f1f2ce | 58 | } |
bcostm | 1:61482ef96890 | 59 | |
bcostm | 1:61482ef96890 | 60 | if (ubSramWrite != 0) |
arostm | 0:e86d70f1f2ce | 61 | { |
bcostm | 1:61482ef96890 | 62 | BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"PSRAM WRITE FAIL", LEFT_MODE); |
bcostm | 1:61482ef96890 | 63 | BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"PSRAM Test Aborted", LEFT_MODE); |
bcostm | 1:61482ef96890 | 64 | while(1); |
arostm | 0:e86d70f1f2ce | 65 | } |
arostm | 0:e86d70f1f2ce | 66 | else |
arostm | 0:e86d70f1f2ce | 67 | { |
bcostm | 1:61482ef96890 | 68 | BSP_LCD_DisplayStringAt(20, 115, (uint8_t *)"PSRAM WRITE OK", LEFT_MODE); |
arostm | 0:e86d70f1f2ce | 69 | } |
arostm | 0:e86d70f1f2ce | 70 | |
bcostm | 1:61482ef96890 | 71 | if (ubSramRead != 0) |
arostm | 0:e86d70f1f2ce | 72 | { |
bcostm | 1:61482ef96890 | 73 | BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"PSRAM READ FAIL", LEFT_MODE); |
bcostm | 1:61482ef96890 | 74 | BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"PSRAM Test Aborted", LEFT_MODE); |
bcostm | 1:61482ef96890 | 75 | while(1); |
arostm | 0:e86d70f1f2ce | 76 | } |
arostm | 0:e86d70f1f2ce | 77 | else |
arostm | 0:e86d70f1f2ce | 78 | { |
bcostm | 1:61482ef96890 | 79 | BSP_LCD_DisplayStringAt(20, 130, (uint8_t *)"PSRAM READ OK", LEFT_MODE); |
arostm | 0:e86d70f1f2ce | 80 | } |
bcostm | 1:61482ef96890 | 81 | |
bcostm | 1:61482ef96890 | 82 | if (Buffercmp(sram_aRxBuffer, sram_aTxBuffer, BUFFER_SIZE) > 0) |
arostm | 0:e86d70f1f2ce | 83 | { |
bcostm | 1:61482ef96890 | 84 | BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"PSRAM COMPARE FAIL", LEFT_MODE); |
bcostm | 1:61482ef96890 | 85 | BSP_LCD_DisplayStringAt(20, 160, (uint8_t *)"PSRAM Test Aborted", LEFT_MODE); |
bcostm | 1:61482ef96890 | 86 | while(1); |
arostm | 0:e86d70f1f2ce | 87 | } |
arostm | 0:e86d70f1f2ce | 88 | else |
arostm | 0:e86d70f1f2ce | 89 | { |
bcostm | 1:61482ef96890 | 90 | BSP_LCD_DisplayStringAt(20, 145, (uint8_t *)"PSRAM Test OK", LEFT_MODE); |
arostm | 0:e86d70f1f2ce | 91 | } |
arostm | 0:e86d70f1f2ce | 92 | |
bcostm | 1:61482ef96890 | 93 | BSP_LCD_DisplayStringAt(0, 200, (uint8_t *)"Demo finished OK", CENTER_MODE); |
bcostm | 1:61482ef96890 | 94 | while(1); |
arostm | 0:e86d70f1f2ce | 95 | } |
arostm | 0:e86d70f1f2ce | 96 | |
bcostm | 1:61482ef96890 | 97 | static void print_demo_title(void) |
arostm | 0:e86d70f1f2ce | 98 | { |
bcostm | 1:61482ef96890 | 99 | BSP_LCD_Init(); |
bcostm | 1:61482ef96890 | 100 | |
arostm | 0:e86d70f1f2ce | 101 | /* Clear the LCD */ |
arostm | 0:e86d70f1f2ce | 102 | BSP_LCD_Clear(LCD_COLOR_WHITE); |
arostm | 0:e86d70f1f2ce | 103 | |
arostm | 0:e86d70f1f2ce | 104 | /* Set LCD Demo description */ |
arostm | 0:e86d70f1f2ce | 105 | BSP_LCD_SetTextColor(LCD_COLOR_GREEN); |
arostm | 0:e86d70f1f2ce | 106 | BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80); |
arostm | 0:e86d70f1f2ce | 107 | BSP_LCD_SetTextColor(LCD_COLOR_BLACK); |
arostm | 0:e86d70f1f2ce | 108 | BSP_LCD_SetBackColor(LCD_COLOR_GREEN); |
arostm | 0:e86d70f1f2ce | 109 | BSP_LCD_SetFont(&Font24); |
arostm | 0:e86d70f1f2ce | 110 | BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"PSRAM", CENTER_MODE); |
arostm | 0:e86d70f1f2ce | 111 | BSP_LCD_SetFont(&Font12); |
arostm | 0:e86d70f1f2ce | 112 | BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE); |
arostm | 0:e86d70f1f2ce | 113 | BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the PSRAM", CENTER_MODE); |
arostm | 0:e86d70f1f2ce | 114 | |
arostm | 0:e86d70f1f2ce | 115 | /* Set the LCD Text Color */ |
arostm | 0:e86d70f1f2ce | 116 | BSP_LCD_SetTextColor(LCD_COLOR_GREEN); |
arostm | 0:e86d70f1f2ce | 117 | BSP_LCD_DrawRect(10, 90, BSP_LCD_GetXSize() - 20, BSP_LCD_GetYSize()- 100); |
arostm | 0:e86d70f1f2ce | 118 | BSP_LCD_DrawRect(11, 91, BSP_LCD_GetXSize() - 22, BSP_LCD_GetYSize()- 102); |
arostm | 0:e86d70f1f2ce | 119 | |
arostm | 0:e86d70f1f2ce | 120 | BSP_LCD_SetTextColor(LCD_COLOR_BLACK); |
arostm | 0:e86d70f1f2ce | 121 | BSP_LCD_SetBackColor(LCD_COLOR_WHITE); |
bcostm | 1:61482ef96890 | 122 | |
bcostm | 1:61482ef96890 | 123 | /* Disable the LCD to avoid refresh from the SDRAM */ |
bcostm | 1:61482ef96890 | 124 | BSP_LCD_DisplayOff(); |
arostm | 0:e86d70f1f2ce | 125 | } |
arostm | 0:e86d70f1f2ce | 126 | |
arostm | 0:e86d70f1f2ce | 127 | /** |
arostm | 0:e86d70f1f2ce | 128 | * @brief Fills buffer with user predefined data. |
arostm | 0:e86d70f1f2ce | 129 | * @param pBuffer: pointer on the buffer to fill |
arostm | 0:e86d70f1f2ce | 130 | * @param uwBufferLength: size of the buffer to fill |
arostm | 0:e86d70f1f2ce | 131 | * @param uwOffset: first value to fill on the buffer |
arostm | 0:e86d70f1f2ce | 132 | * @retval None |
arostm | 0:e86d70f1f2ce | 133 | */ |
arostm | 0:e86d70f1f2ce | 134 | static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset) |
arostm | 0:e86d70f1f2ce | 135 | { |
arostm | 0:e86d70f1f2ce | 136 | uint32_t tmpindex = 0; |
arostm | 0:e86d70f1f2ce | 137 | |
arostm | 0:e86d70f1f2ce | 138 | /* Put in global buffer different values */ |
arostm | 0:e86d70f1f2ce | 139 | for (tmpindex = 0; tmpindex < uwBufferLength; tmpindex++ ) |
arostm | 0:e86d70f1f2ce | 140 | { |
arostm | 0:e86d70f1f2ce | 141 | pBuffer[tmpindex] = tmpindex + uwOffset; |
arostm | 0:e86d70f1f2ce | 142 | } |
arostm | 0:e86d70f1f2ce | 143 | } |
arostm | 0:e86d70f1f2ce | 144 | |
arostm | 0:e86d70f1f2ce | 145 | /** |
arostm | 0:e86d70f1f2ce | 146 | * @brief Compares two buffers. |
arostm | 0:e86d70f1f2ce | 147 | * @param pBuffer1, pBuffer2: buffers to be compared. |
arostm | 0:e86d70f1f2ce | 148 | * @param BufferLength: buffer's length |
arostm | 0:e86d70f1f2ce | 149 | * @retval 1: pBuffer identical to pBuffer1 |
arostm | 0:e86d70f1f2ce | 150 | * 0: pBuffer differs from pBuffer1 |
arostm | 0:e86d70f1f2ce | 151 | */ |
arostm | 0:e86d70f1f2ce | 152 | static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength) |
arostm | 0:e86d70f1f2ce | 153 | { |
arostm | 0:e86d70f1f2ce | 154 | while (BufferLength--) |
arostm | 0:e86d70f1f2ce | 155 | { |
arostm | 0:e86d70f1f2ce | 156 | if (*pBuffer1 != *pBuffer2) |
arostm | 0:e86d70f1f2ce | 157 | { |
arostm | 0:e86d70f1f2ce | 158 | return 1; |
arostm | 0:e86d70f1f2ce | 159 | } |
arostm | 0:e86d70f1f2ce | 160 | |
arostm | 0:e86d70f1f2ce | 161 | pBuffer1++; |
arostm | 0:e86d70f1f2ce | 162 | pBuffer2++; |
arostm | 0:e86d70f1f2ce | 163 | } |
arostm | 0:e86d70f1f2ce | 164 | |
arostm | 0:e86d70f1f2ce | 165 | return 0; |
arostm | 0:e86d70f1f2ce | 166 | } |