SRAM demo using ST BSP driver.

Dependencies:   BSP_DISCO_L496AG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "stm32l496g_discovery.h"
00003 #include "stm32l496g_discovery_lcd.h"
00004 #include "stm32l496g_discovery_sram.h"
00005 
00006 #define BUFFER_SIZE              ((uint32_t)0x1000)
00007 #define SRAM_WRITE_READ_ADDR     ((uint32_t)0x0800)
00008 
00009 uint16_t sram_aTxBuffer[BUFFER_SIZE];
00010 uint16_t sram_aRxBuffer[BUFFER_SIZE];
00011 
00012 __IO uint8_t  write_complete = 0;
00013 
00014 static void print_demo_title(void);
00015 static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset);
00016 static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength);
00017 
00018 int main()
00019 {
00020     print_demo_title();
00021 
00022     ThisThread::sleep_for(500);
00023 
00024     /*##-1- Configure the SRAM device ##########################################*/
00025     if (BSP_SRAM_Init() == SRAM_OK) {
00026         BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT OK", LEFT_MODE);
00027     } else {
00028         BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"SRAM INIT FAIL", LEFT_MODE);
00029     }
00030 
00031     ThisThread::sleep_for(500);
00032 
00033     /*##-2- SRAM memory read/write access ######################################*/
00034 
00035     /* Fill the buffer to write */
00036     Fill_Buffer(sram_aTxBuffer, BUFFER_SIZE, 0xC000);
00037     /* Empty the buffer to read */
00038     Fill_Buffer(sram_aRxBuffer, BUFFER_SIZE, 0);
00039 
00040     /* Write data to the SRAM memory */
00041     if (BSP_SRAM_WriteData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aTxBuffer, BUFFER_SIZE) == SRAM_OK) {
00042         BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE OK", LEFT_MODE);
00043     } else {
00044         BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"SRAM WRITE FAIL", LEFT_MODE);
00045     }
00046 
00047     ThisThread::sleep_for(500);
00048 
00049     /* Read back data from the SRAM memory */
00050     if (BSP_SRAM_ReadData(SRAM_DEVICE_ADDR + SRAM_WRITE_READ_ADDR, sram_aRxBuffer, BUFFER_SIZE) == SRAM_OK) {
00051         BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ OK", LEFT_MODE);
00052     } else {
00053         BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)"SRAM READ FAIL", LEFT_MODE);
00054     }
00055 
00056     ThisThread::sleep_for(1000);
00057 
00058     /* Check read data */
00059     if (Buffercmp(sram_aRxBuffer, sram_aTxBuffer, BUFFER_SIZE) == 0) {
00060         BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE OK", LEFT_MODE);
00061     } else {
00062         BSP_LCD_DisplayStringAt(0, LINE(8), (uint8_t *)"SRAM COMPARE FAIL", LEFT_MODE);
00063     }
00064 }
00065 
00066 static void print_demo_title(void)
00067 {
00068     BSP_LCD_Init();
00069     BSP_LCD_Clear(LCD_COLOR_WHITE);
00070 
00071     BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
00072     BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 80);
00073 
00074     BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
00075     BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
00076     BSP_LCD_SetFont(&Font24);
00077     BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"SRAM", CENTER_MODE);
00078     BSP_LCD_SetFont(&Font12);
00079     BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"This example shows how to write", CENTER_MODE);
00080     BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"and read data on the SRAM", CENTER_MODE);
00081     BSP_LCD_SetFont(&Font20);
00082 }
00083 
00084 
00085 /**
00086   * @brief  Fills buffer with user predefined data.
00087   * @param  pBuffer: pointer on the buffer to fill
00088   * @param  uwBufferLength: size of the buffer to fill
00089   * @param  uwOffset: first value to fill on the buffer
00090   * @retval None
00091   */
00092 static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
00093 {
00094     uint32_t tmpindex = 0;
00095 
00096     /* Put in global buffer different values */
00097     for (tmpindex = 0; tmpindex < uwBufferLength; tmpindex++ ) {
00098         pBuffer[tmpindex] = tmpindex + uwOffset;
00099     }
00100 }
00101 
00102 /**
00103   * @brief  Compares two buffers.
00104   * @param  pBuffer1, pBuffer2: buffers to be compared.
00105   * @param  BufferLength: buffer's length
00106   * @retval 0: pBuffer identical to pBuffer1
00107   *         1: pBuffer differs from pBuffer1
00108   */
00109 static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength)
00110 {
00111     while (BufferLength--) {
00112         if (*pBuffer1 != *pBuffer2) {
00113             return 1;
00114         }
00115 
00116         pBuffer1++;
00117         pBuffer2++;
00118     }
00119 
00120     return 0;
00121 }