SRAM demo using ST BSP driver.
Dependencies: BSP_DISCO_L496AG
main.cpp@3:727e0290bde8, 2019-11-22 (annotated)
- Committer:
- Jerome Coutant
- Date:
- Fri Nov 22 16:06:21 2019 +0100
- Revision:
- 3:727e0290bde8
- Parent:
- 2:3fbe5f24c51d
Update with CubeL4 V1.14.0
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 Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset); |
bcostm | 1:292265849500 | 16 | static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength); |
bcostm | 2:3fbe5f24c51d | 17 | |
bcostm | 1:292265849500 | 18 | int main() |
bcostm | 1:292265849500 | 19 | { |
bcostm | 2:3fbe5f24c51d | 20 | print_demo_title(); |
bcostm | 2:3fbe5f24c51d | 21 | |
Jerome Coutant
3:727e0290bde8
|
22
|
ThisThread::sleep_for(500);
|
|
bcostm | 1:292265849500 | 23 | |
bcostm | 2:3fbe5f24c51d | 24 | /*##-1- Configure the SRAM device ##########################################*/ |
bcostm | 2:3fbe5f24c51d | 25 | if (BSP_SRAM_Init() == SRAM_OK) { |
bcostm | 2:3fbe5f24c51d | 26 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 27 | } else { |
bcostm | 2:3fbe5f24c51d | 28 | BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 29 | } |
bcostm | 2:3fbe5f24c51d | 30 | |
Jerome Coutant
3:727e0290bde8
|
31
|
ThisThread::sleep_for(500);
|
|
bcostm | 2:3fbe5f24c51d | 32 | |
bcostm | 2:3fbe5f24c51d | 33 | /*##-2- SRAM memory read/write access ######################################*/ |
bcostm | 2:3fbe5f24c51d | 34 | |
bcostm | 2:3fbe5f24c51d | 35 | /* Fill the buffer to write */ |
bcostm | 2:3fbe5f24c51d | 36 | Fill_Buffer(sram_aTxBuffer, BUFFER_SIZE, 0xC000); |
bcostm | 2:3fbe5f24c51d | 37 | /* Empty the buffer to read */ |
bcostm | 2:3fbe5f24c51d | 38 | Fill_Buffer(sram_aRxBuffer, BUFFER_SIZE, 0); |
bcostm | 1:292265849500 | 39 | |
bcostm | 2:3fbe5f24c51d | 40 | /* Write data to the SRAM memory */ |
bcostm | 2:3fbe5f24c51d | 41 | if (BSP_SRAM_WriteData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aTxBuffer, BUFFER_SIZE) == SRAM_OK) { |
bcostm | 2:3fbe5f24c51d | 42 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 43 | } else { |
bcostm | 2:3fbe5f24c51d | 44 | BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 45 | } |
bcostm | 2:3fbe5f24c51d | 46 | |
Jerome Coutant
3:727e0290bde8
|
47
|
ThisThread::sleep_for(500);
|
|
bcostm | 2:3fbe5f24c51d | 48 | |
bcostm | 2:3fbe5f24c51d | 49 | /* Read back data from the SRAM memory */ |
bcostm | 2:3fbe5f24c51d | 50 | if (BSP_SRAM_ReadData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aRxBuffer, BUFFER_SIZE) == SRAM_OK) { |
bcostm | 2:3fbe5f24c51d | 51 | BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 52 | } else { |
bcostm | 2:3fbe5f24c51d | 53 | BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 54 | } |
bcostm | 2:3fbe5f24c51d | 55 | |
Jerome Coutant
3:727e0290bde8
|
56
|
ThisThread::sleep_for(1000);
|
|
bcostm | 2:3fbe5f24c51d | 57 | |
bcostm | 2:3fbe5f24c51d | 58 | /* Check read data */ |
bcostm | 2:3fbe5f24c51d | 59 | if (Buffercmp(sram_aRxBuffer, sram_aTxBuffer, BUFFER_SIZE) == 0) { |
bcostm | 2:3fbe5f24c51d | 60 | BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE OK", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 61 | } else { |
bcostm | 2:3fbe5f24c51d | 62 | BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE FAIL", LEFT_MODE); |
bcostm | 2:3fbe5f24c51d | 63 | } |
bcostm | 1:292265849500 | 64 | } |
bcostm | 2:3fbe5f24c51d | 65 | |
bcostm | 1:292265849500 | 66 | static void print_demo_title(void) |
bcostm | 1:292265849500 | 67 | { |
bcostm | 2:3fbe5f24c51d | 68 | BSP_LCD_Init(); |
bcostm | 2:3fbe5f24c51d | 69 | BSP_LCD_Clear(LCD_COLOR_WHITE); |
bcostm | 2:3fbe5f24c51d | 70 | |
bcostm | 2:3fbe5f24c51d | 71 | BSP_LCD_SetTextColor(LCD_COLOR_BLUE); |
bcostm | 2:3fbe5f24c51d | 72 | BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80); |
bcostm | 2:3fbe5f24c51d | 73 | |
bcostm | 2:3fbe5f24c51d | 74 | BSP_LCD_SetTextColor(LCD_COLOR_WHITE); |
bcostm | 2:3fbe5f24c51d | 75 | BSP_LCD_SetBackColor(LCD_COLOR_BLUE); |
bcostm | 2:3fbe5f24c51d | 76 | BSP_LCD_SetFont(&Font24); |
bcostm | 2:3fbe5f24c51d | 77 | BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SRAM", CENTER_MODE); |
bcostm | 2:3fbe5f24c51d | 78 | BSP_LCD_SetFont(&Font12); |
bcostm | 2:3fbe5f24c51d | 79 | BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE); |
bcostm | 2:3fbe5f24c51d | 80 | BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the SRAM", CENTER_MODE); |
bcostm | 2:3fbe5f24c51d | 81 | BSP_LCD_SetFont(&Font20); |
bcostm | 1:292265849500 | 82 | } |
bcostm | 1:292265849500 | 83 | |
bcostm | 2:3fbe5f24c51d | 84 | |
bcostm | 1:292265849500 | 85 | /** |
bcostm | 1:292265849500 | 86 | * @brief Fills buffer with user predefined data. |
bcostm | 1:292265849500 | 87 | * @param pBuffer: pointer on the buffer to fill |
bcostm | 1:292265849500 | 88 | * @param uwBufferLength: size of the buffer to fill |
bcostm | 1:292265849500 | 89 | * @param uwOffset: first value to fill on the buffer |
bcostm | 1:292265849500 | 90 | * @retval None |
bcostm | 1:292265849500 | 91 | */ |
bcostm | 1:292265849500 | 92 | static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset) |
bcostm | 1:292265849500 | 93 | { |
bcostm | 2:3fbe5f24c51d | 94 | uint32_t tmpindex = 0; |
bcostm | 2:3fbe5f24c51d | 95 | |
bcostm | 2:3fbe5f24c51d | 96 | /* Put in global buffer different values */ |
bcostm | 2:3fbe5f24c51d | 97 | for (tmpindex = 0; tmpindex < uwBufferLength; tmpindex++ ) { |
bcostm | 2:3fbe5f24c51d | 98 | pBuffer[tmpindex] = tmpindex + uwOffset; |
bcostm | 2:3fbe5f24c51d | 99 | } |
bcostm | 1:292265849500 | 100 | } |
bcostm | 2:3fbe5f24c51d | 101 | |
bcostm | 1:292265849500 | 102 | /** |
bcostm | 1:292265849500 | 103 | * @brief Compares two buffers. |
bcostm | 1:292265849500 | 104 | * @param pBuffer1, pBuffer2: buffers to be compared. |
bcostm | 1:292265849500 | 105 | * @param BufferLength: buffer's length |
bcostm | 1:292265849500 | 106 | * @retval 0: pBuffer identical to pBuffer1 |
bcostm | 1:292265849500 | 107 | * 1: pBuffer differs from pBuffer1 |
bcostm | 1:292265849500 | 108 | */ |
bcostm | 1:292265849500 | 109 | static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength) |
bcostm | 1:292265849500 | 110 | { |
bcostm | 2:3fbe5f24c51d | 111 | while (BufferLength--) { |
bcostm | 2:3fbe5f24c51d | 112 | if (*pBuffer1 != *pBuffer2) { |
bcostm | 2:3fbe5f24c51d | 113 | return 1; |
bcostm | 2:3fbe5f24c51d | 114 | } |
bcostm | 2:3fbe5f24c51d | 115 | |
bcostm | 2:3fbe5f24c51d | 116 | pBuffer1++; |
bcostm | 2:3fbe5f24c51d | 117 | pBuffer2++; |
bcostm | 1:292265849500 | 118 | } |
bcostm | 2:3fbe5f24c51d | 119 | |
bcostm | 2:3fbe5f24c51d | 120 | return 0; |
bcostm | 1:292265849500 | 121 | } |