SRAM demo using ST BSP driver.
Dependencies: BSP_DISCO_L496AG
main.cpp@2:3fbe5f24c51d, 2018-04-18 (annotated)
- Committer:
- bcostm
- Date:
- Wed Apr 18 08:50:07 2018 +0000
- Revision:
- 2:3fbe5f24c51d
- Parent:
- 1:292265849500
- Child:
- 3:727e0290bde8
astyle
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:3fbe5f24c51d | 5 | |
bcostm | 1:292265849500 | 6 | #define BUFFER_SIZE ((uint32_t)0x1000) |
bcostm | 1:292265849500 | 7 | #define SRAM_WRITE_READ_ADDR ((uint32_t)0x0800) |
bcostm | 2:3fbe5f24c51d | 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 | 2:3fbe5f24c51d | 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 | 2:3fbe5f24c51d | 19 | |
bcostm | 1:292265849500 | 20 | int main() |
bcostm | 1:292265849500 | 21 | { |
bcostm | 2:3fbe5f24c51d | 22 | print_demo_title(); |
bcostm | 2:3fbe5f24c51d | 23 | |
bcostm | 2:3fbe5f24c51d | 24 | wait(0.2); |
bcostm | 1:292265849500 | 25 | |
bcostm | 2:3fbe5f24c51d | 26 | /*##-1- Configure the SRAM device ##########################################*/ |
bcostm | 2:3fbe5f24c51d | 27 | if (BSP_SRAM_Init() == SRAM_OK) { |
bcostm | 2:3fbe5f24c51d | 28 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 29 | } else { |
bcostm | 2:3fbe5f24c51d | 30 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 31 | print_FAIL(); |
bcostm | 2:3fbe5f24c51d | 32 | } |
bcostm | 2:3fbe5f24c51d | 33 | |
bcostm | 2:3fbe5f24c51d | 34 | wait(0.2); |
bcostm | 2:3fbe5f24c51d | 35 | |
bcostm | 2:3fbe5f24c51d | 36 | /*##-2- SRAM memory read/write access ######################################*/ |
bcostm | 2:3fbe5f24c51d | 37 | |
bcostm | 2:3fbe5f24c51d | 38 | /* Fill the buffer to write */ |
bcostm | 2:3fbe5f24c51d | 39 | Fill_Buffer(sram_aTxBuffer, BUFFER_SIZE, 0xC000); |
bcostm | 2:3fbe5f24c51d | 40 | /* Empty the buffer to read */ |
bcostm | 2:3fbe5f24c51d | 41 | Fill_Buffer(sram_aRxBuffer, BUFFER_SIZE, 0); |
bcostm | 1:292265849500 | 42 | |
bcostm | 2:3fbe5f24c51d | 43 | /* Write data to the SRAM memory */ |
bcostm | 2:3fbe5f24c51d | 44 | if (BSP_SRAM_WriteData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aTxBuffer, BUFFER_SIZE) == SRAM_OK) { |
bcostm | 2:3fbe5f24c51d | 45 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 46 | } else { |
bcostm | 2:3fbe5f24c51d | 47 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 48 | print_FAIL(); |
bcostm | 2:3fbe5f24c51d | 49 | } |
bcostm | 2:3fbe5f24c51d | 50 | |
bcostm | 2:3fbe5f24c51d | 51 | wait(0.2); |
bcostm | 2:3fbe5f24c51d | 52 | |
bcostm | 2:3fbe5f24c51d | 53 | /* Read back data from the SRAM memory */ |
bcostm | 2:3fbe5f24c51d | 54 | if (BSP_SRAM_ReadData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aRxBuffer, BUFFER_SIZE) == SRAM_OK) { |
bcostm | 2:3fbe5f24c51d | 55 | BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 56 | } else { |
bcostm | 2:3fbe5f24c51d | 57 | BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 58 | print_FAIL(); |
bcostm | 2:3fbe5f24c51d | 59 | } |
bcostm | 2:3fbe5f24c51d | 60 | |
bcostm | 2:3fbe5f24c51d | 61 | wait(0.2); |
bcostm | 2:3fbe5f24c51d | 62 | |
bcostm | 2:3fbe5f24c51d | 63 | /* Check read data */ |
bcostm | 2:3fbe5f24c51d | 64 | if (Buffercmp(sram_aRxBuffer, sram_aTxBuffer, BUFFER_SIZE) == 0) { |
bcostm | 2:3fbe5f24c51d | 65 | BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 66 | } else { |
bcostm | 2:3fbe5f24c51d | 67 | BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 68 | print_FAIL(); |
bcostm | 2:3fbe5f24c51d | 69 | } |
bcostm | 2:3fbe5f24c51d | 70 | |
bcostm | 2:3fbe5f24c51d | 71 | wait(0.2); |
bcostm | 2:3fbe5f24c51d | 72 | print_PASS(); |
bcostm | 1:292265849500 | 73 | } |
bcostm | 2:3fbe5f24c51d | 74 | |
bcostm | 1:292265849500 | 75 | static void print_demo_title(void) |
bcostm | 1:292265849500 | 76 | { |
bcostm | 2:3fbe5f24c51d | 77 | BSP_LCD_Init(); |
bcostm | 2:3fbe5f24c51d | 78 | BSP_LCD_Clear(LCD_COLOR_WHITE); |
bcostm | 2:3fbe5f24c51d | 79 | |
bcostm | 2:3fbe5f24c51d | 80 | BSP_LCD_SetTextColor(LCD_COLOR_BLUE); |
bcostm | 2:3fbe5f24c51d | 81 | BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80); |
bcostm | 2:3fbe5f24c51d | 82 | |
bcostm | 2:3fbe5f24c51d | 83 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
bcostm | 2:3fbe5f24c51d | 84 | BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
bcostm | 2:3fbe5f24c51d | 85 | BSP_LCD_SetFont(&Font24); |
bcostm | 2:3fbe5f24c51d | 86 | BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SRAM", CENTER_MODE); |
bcostm | 2:3fbe5f24c51d | 87 | BSP_LCD_SetFont(&Font12); |
bcostm | 2:3fbe5f24c51d | 88 | BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE); |
bcostm | 2:3fbe5f24c51d | 89 | BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the SRAM", CENTER_MODE); |
bcostm | 2:3fbe5f24c51d | 90 | BSP_LCD_SetFont(&Font20); |
bcostm | 1:292265849500 | 91 | } |
bcostm | 1:292265849500 | 92 | |
bcostm | 1:292265849500 | 93 | static void print_PASS(void) |
bcostm | 1:292265849500 | 94 | { |
bcostm | 1:292265849500 | 95 | BSP_LCD_SetTextColor(LCD_COLOR_BLACK); |
bcostm | 1:292265849500 | 96 | BSP_LCD_SetBackColor(LCD_COLOR_GREEN); |
bcostm | 1:292265849500 | 97 | BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)"Demo OK", CENTER_MODE); |
bcostm | 1:292265849500 | 98 | while(1); |
bcostm | 1:292265849500 | 99 | } |
bcostm | 1:292265849500 | 100 | |
bcostm | 1:292265849500 | 101 | static void print_FAIL(void) |
bcostm | 1:292265849500 | 102 | { |
bcostm | 1:292265849500 | 103 | BSP_LCD_SetBackColor(LCD_COLOR_RED); |
bcostm | 1:292265849500 | 104 | BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)"Demo FAILED", CENTER_MODE); |
bcostm | 1:292265849500 | 105 | while(1); |
bcostm | 1:292265849500 | 106 | } |
bcostm | 2:3fbe5f24c51d | 107 | |
bcostm | 1:292265849500 | 108 | /** |
bcostm | 1:292265849500 | 109 | * @brief Fills buffer with user predefined data. |
bcostm | 1:292265849500 | 110 | * @param pBuffer: pointer on the buffer to fill |
bcostm | 1:292265849500 | 111 | * @param uwBufferLength: size of the buffer to fill |
bcostm | 1:292265849500 | 112 | * @param uwOffset: first value to fill on the buffer |
bcostm | 1:292265849500 | 113 | * @retval None |
bcostm | 1:292265849500 | 114 | */ |
bcostm | 1:292265849500 | 115 | static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset) |
bcostm | 1:292265849500 | 116 | { |
bcostm | 2:3fbe5f24c51d | 117 | uint32_t tmpindex = 0; |
bcostm | 2:3fbe5f24c51d | 118 | |
bcostm | 2:3fbe5f24c51d | 119 | /* Put in global buffer different values */ |
bcostm | 2:3fbe5f24c51d | 120 | for (tmpindex = 0; tmpindex < uwBufferLength; tmpindex++ ) { |
bcostm | 2:3fbe5f24c51d | 121 | pBuffer[tmpindex] = tmpindex + uwOffset; |
bcostm | 2:3fbe5f24c51d | 122 | } |
bcostm | 1:292265849500 | 123 | } |
bcostm | 2:3fbe5f24c51d | 124 | |
bcostm | 1:292265849500 | 125 | /** |
bcostm | 1:292265849500 | 126 | * @brief Compares two buffers. |
bcostm | 1:292265849500 | 127 | * @param pBuffer1, pBuffer2: buffers to be compared. |
bcostm | 1:292265849500 | 128 | * @param BufferLength: buffer's length |
bcostm | 1:292265849500 | 129 | * @retval 0: pBuffer identical to pBuffer1 |
bcostm | 1:292265849500 | 130 | * 1: pBuffer differs from pBuffer1 |
bcostm | 1:292265849500 | 131 | */ |
bcostm | 1:292265849500 | 132 | static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength) |
bcostm | 1:292265849500 | 133 | { |
bcostm | 2:3fbe5f24c51d | 134 | while (BufferLength--) { |
bcostm | 2:3fbe5f24c51d | 135 | if (*pBuffer1 != *pBuffer2) { |
bcostm | 2:3fbe5f24c51d | 136 | return 1; |
bcostm | 2:3fbe5f24c51d | 137 | } |
bcostm | 2:3fbe5f24c51d | 138 | |
bcostm | 2:3fbe5f24c51d | 139 | pBuffer1++; |
bcostm | 2:3fbe5f24c51d | 140 | pBuffer2++; |
bcostm | 1:292265849500 | 141 | } |
bcostm | 2:3fbe5f24c51d | 142 | |
bcostm | 2:3fbe5f24c51d | 143 | return 0; |
bcostm | 1:292265849500 | 144 | } |