SRAM demo using ST BSP driver.

Dependencies:   BSP_DISCO_L496AG

Committer:
bcostm
Date:
Wed Apr 18 10:47:19 2018 +0200
Revision:
1:292265849500
Child:
2:3fbe5f24c51d
Add main file

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 1:292265849500 1 #include "mbed.h"
bcostm 1:292265849500 2 #include "stm32l496g_discovery.h"
bcostm 1:292265849500 3 #include "stm32l496g_discovery_lcd.h"
bcostm 1:292265849500 4 #include "stm32l496g_discovery_sram.h"
bcostm 1:292265849500 5
bcostm 1:292265849500 6 #define BUFFER_SIZE ((uint32_t)0x1000)
bcostm 1:292265849500 7 #define SRAM_WRITE_READ_ADDR ((uint32_t)0x0800)
bcostm 1:292265849500 8
bcostm 1:292265849500 9 uint16_t sram_aTxBuffer[BUFFER_SIZE];
bcostm 1:292265849500 10 uint16_t sram_aRxBuffer[BUFFER_SIZE];
bcostm 1:292265849500 11
bcostm 1:292265849500 12 __IO uint8_t write_complete = 0;
bcostm 1:292265849500 13
bcostm 1:292265849500 14 static void print_demo_title(void);
bcostm 1:292265849500 15 static void print_PASS(void);
bcostm 1:292265849500 16 static void print_FAIL(void);
bcostm 1:292265849500 17 static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset);
bcostm 1:292265849500 18 static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength);
bcostm 1:292265849500 19
bcostm 1:292265849500 20 int main()
bcostm 1:292265849500 21 {
bcostm 1:292265849500 22 print_demo_title();
bcostm 1:292265849500 23
bcostm 1:292265849500 24 wait(0.2);
bcostm 1:292265849500 25
bcostm 1:292265849500 26 /*##-1- Configure the SRAM device ##########################################*/
bcostm 1:292265849500 27 if (BSP_SRAM_Init() == SRAM_OK) {
bcostm 1:292265849500 28 BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT OK", LEFT_MODE);
bcostm 1:292265849500 29 }
bcostm 1:292265849500 30 else {
bcostm 1:292265849500 31 BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT FAIL", LEFT_MODE);
bcostm 1:292265849500 32 print_FAIL();
bcostm 1:292265849500 33 }
bcostm 1:292265849500 34
bcostm 1:292265849500 35 wait(0.2);
bcostm 1:292265849500 36
bcostm 1:292265849500 37 /*##-2- SRAM memory read/write access ######################################*/
bcostm 1:292265849500 38
bcostm 1:292265849500 39 /* Fill the buffer to write */
bcostm 1:292265849500 40 Fill_Buffer(sram_aTxBuffer, BUFFER_SIZE, 0xC000);
bcostm 1:292265849500 41 /* Empty the buffer to read */
bcostm 1:292265849500 42 Fill_Buffer(sram_aRxBuffer, BUFFER_SIZE, 0);
bcostm 1:292265849500 43
bcostm 1:292265849500 44 /* Write data to the SRAM memory */
bcostm 1:292265849500 45 if (BSP_SRAM_WriteData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aTxBuffer, BUFFER_SIZE) == SRAM_OK) {
bcostm 1:292265849500 46 BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE OK", LEFT_MODE);
bcostm 1:292265849500 47 }
bcostm 1:292265849500 48 else {
bcostm 1:292265849500 49 BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE FAIL", LEFT_MODE);
bcostm 1:292265849500 50 print_FAIL();
bcostm 1:292265849500 51 }
bcostm 1:292265849500 52
bcostm 1:292265849500 53 wait(0.2);
bcostm 1:292265849500 54
bcostm 1:292265849500 55 /* Read back data from the SRAM memory */
bcostm 1:292265849500 56 if (BSP_SRAM_ReadData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aRxBuffer, BUFFER_SIZE) == SRAM_OK) {
bcostm 1:292265849500 57 BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ OK", LEFT_MODE);
bcostm 1:292265849500 58 }
bcostm 1:292265849500 59 else {
bcostm 1:292265849500 60 BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ FAIL", LEFT_MODE);
bcostm 1:292265849500 61 print_FAIL();
bcostm 1:292265849500 62 }
bcostm 1:292265849500 63
bcostm 1:292265849500 64 wait(0.2);
bcostm 1:292265849500 65
bcostm 1:292265849500 66 /* Check read data */
bcostm 1:292265849500 67 if (Buffercmp(sram_aRxBuffer, sram_aTxBuffer, BUFFER_SIZE) == 0) {
bcostm 1:292265849500 68 BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE OK", LEFT_MODE);
bcostm 1:292265849500 69 }
bcostm 1:292265849500 70 else {
bcostm 1:292265849500 71 BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE FAIL", LEFT_MODE);
bcostm 1:292265849500 72 print_FAIL();
bcostm 1:292265849500 73 }
bcostm 1:292265849500 74
bcostm 1:292265849500 75 wait(0.2);
bcostm 1:292265849500 76 print_PASS();
bcostm 1:292265849500 77 }
bcostm 1:292265849500 78
bcostm 1:292265849500 79 static void print_demo_title(void)
bcostm 1:292265849500 80 {
bcostm 1:292265849500 81 BSP_LCD_Init();
bcostm 1:292265849500 82 BSP_LCD_Clear(LCD_COLOR_WHITE);
bcostm 1:292265849500 83
bcostm 1:292265849500 84 BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
bcostm 1:292265849500 85 BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80);
bcostm 1:292265849500 86
bcostm 1:292265849500 87 BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
bcostm 1:292265849500 88 BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
bcostm 1:292265849500 89 BSP_LCD_SetFont(&Font24);
bcostm 1:292265849500 90 BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SRAM", CENTER_MODE);
bcostm 1:292265849500 91 BSP_LCD_SetFont(&Font12);
bcostm 1:292265849500 92 BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE);
bcostm 1:292265849500 93 BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the SRAM", CENTER_MODE);
bcostm 1:292265849500 94 BSP_LCD_SetFont(&Font20);
bcostm 1:292265849500 95 }
bcostm 1:292265849500 96
bcostm 1:292265849500 97 static void print_PASS(void)
bcostm 1:292265849500 98 {
bcostm 1:292265849500 99 BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
bcostm 1:292265849500 100 BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
bcostm 1:292265849500 101 BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)"Demo OK", CENTER_MODE);
bcostm 1:292265849500 102 while(1);
bcostm 1:292265849500 103 }
bcostm 1:292265849500 104
bcostm 1:292265849500 105 static void print_FAIL(void)
bcostm 1:292265849500 106 {
bcostm 1:292265849500 107 BSP_LCD_SetBackColor(LCD_COLOR_RED);
bcostm 1:292265849500 108 BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)"Demo FAILED", CENTER_MODE);
bcostm 1:292265849500 109 while(1);
bcostm 1:292265849500 110 }
bcostm 1:292265849500 111
bcostm 1:292265849500 112 /**
bcostm 1:292265849500 113 * @brief Fills buffer with user predefined data.
bcostm 1:292265849500 114 * @param pBuffer: pointer on the buffer to fill
bcostm 1:292265849500 115 * @param uwBufferLength: size of the buffer to fill
bcostm 1:292265849500 116 * @param uwOffset: first value to fill on the buffer
bcostm 1:292265849500 117 * @retval None
bcostm 1:292265849500 118 */
bcostm 1:292265849500 119 static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
bcostm 1:292265849500 120 {
bcostm 1:292265849500 121 uint32_t tmpindex = 0;
bcostm 1:292265849500 122
bcostm 1:292265849500 123 /* Put in global buffer different values */
bcostm 1:292265849500 124 for (tmpindex = 0; tmpindex < uwBufferLength; tmpindex++ )
bcostm 1:292265849500 125 {
bcostm 1:292265849500 126 pBuffer[tmpindex] = tmpindex + uwOffset;
bcostm 1:292265849500 127 }
bcostm 1:292265849500 128 }
bcostm 1:292265849500 129
bcostm 1:292265849500 130 /**
bcostm 1:292265849500 131 * @brief Compares two buffers.
bcostm 1:292265849500 132 * @param pBuffer1, pBuffer2: buffers to be compared.
bcostm 1:292265849500 133 * @param BufferLength: buffer's length
bcostm 1:292265849500 134 * @retval 0: pBuffer identical to pBuffer1
bcostm 1:292265849500 135 * 1: pBuffer differs from pBuffer1
bcostm 1:292265849500 136 */
bcostm 1:292265849500 137 static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength)
bcostm 1:292265849500 138 {
bcostm 1:292265849500 139 while (BufferLength--)
bcostm 1:292265849500 140 {
bcostm 1:292265849500 141 if (*pBuffer1 != *pBuffer2)
bcostm 1:292265849500 142 {
bcostm 1:292265849500 143 return 1;
bcostm 1:292265849500 144 }
bcostm 1:292265849500 145
bcostm 1:292265849500 146 pBuffer1++;
bcostm 1:292265849500 147 pBuffer2++;
bcostm 1:292265849500 148 }
bcostm 1:292265849500 149
bcostm 1:292265849500 150 return 0;
bcostm 1:292265849500 151 }
bcostm 1:292265849500 152
bcostm 1:292265849500 153