External PSRAM using STM32 HAL libraries

Thread Starter

zazas321

Joined Nov 29, 2015
432
Hello. I need to interface with an external RAM chip through FSMC interface. I have 22 address lines and 16 data lines. The ram chip that I am using is :
http://www.issi.com/WW/pdf/66-67WVE4M16EALL-BLL-CLL.pdf

and the microcontroller is STM32F412 144pin BGA.

I am using STM32cubeMX generated libraries for the FSMC interface, however, I cannot quite understand what is READ_WRITE_ADDRESS of the RAM chip as every example program uses (most commonly it is 0x0800 for whatever reason). Also, example programs are always using address offset that I am also unsure why and how to determine.

The example code that I am reffering to:

C:
/**
  ******************************************************************************
  * @file    FMC/FMC_PSRAM/Src/main.c
  * @author  MCD Application Team
  * @brief   This sample code shows how to use STM32F4xx FMC HAL API to access
  *          by read and write operation the SRAM external memory device.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/** @addtogroup STM32F4xx_HAL_Examples
  * @{
  */

/** @addtogroup FMC_PSRAM
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BUFFER_SIZE         ((uint32_t)0x1000)
#define WRITE_READ_ADDR     ((uint32_t)0x0800)
#define WRITING_OFFSET      ((uint32_t)0xC20F)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
SRAM_HandleTypeDef psramHandle;
FMC_NORSRAM_TimingTypeDef Timing;

/* Read/Write Buffers */
uint16_t aTxBuffer[BUFFER_SIZE];
uint16_t aRxBuffer[BUFFER_SIZE];

/* Status variables */
__IO uint32_t uwWriteReadStatus = 0;

/* Counter index */
uint32_t uwIndex = 0;

/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
static void PSRAM_Init(void);
static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset);
static uint8_t Buffercmp(uint16_t *pBuffer1, uint16_t *pBuffer2, uint16_t BufferLength);
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure the system clock to 100 MHz */
  SystemClock_Config();

  /* Configure LED4, LED3 */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);

  /*##-1- Configure the SRAM device ##########################################*/
  PSRAM_Init();

  /*##-2- SRAM memory read/write access ######################################*/
  /* Fill the buffer to write */
  Fill_Buffer(aTxBuffer, BUFFER_SIZE, WRITING_OFFSET);

  /* Write data to the SRAM memory */
  HAL_SRAM_Write_16b(&psramHandle, (uint32_t *)(PSRAM_BANK_ADDR + WRITE_READ_ADDR), aTxBuffer, BUFFER_SIZE);

  /* Read back data from the SRAM memory */
  HAL_SRAM_Read_16b(&psramHandle, (uint32_t *)(PSRAM_BANK_ADDR + WRITE_READ_ADDR), aRxBuffer, BUFFER_SIZE);

  /*##-3- Checking data integrity ############################################*/
  uwWriteReadStatus = Buffercmp(aTxBuffer, aRxBuffer, BUFFER_SIZE);

  if(uwWriteReadStatus) /* KO */
  {
    /* Turn on LED4 */
    BSP_LED_On(LED4);
  }
  else /* OK */
  {
    /* Turn on LED3 */
    BSP_LED_On(LED3);
  }

  /* Infinite loop */
  while (1)
  {
  }
}

/**
  * @brief  Initializes the PSRAM device.
  * @retval PSRAM status
  */
static void PSRAM_Init(void)
{
  /* PSRAM device configuration */
  psramHandle.Instance = FSMC_NORSRAM_DEVICE;
  psramHandle.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
  /* PSRAM device configuration */
  /* Timing configuration derived from system clock (up to 100Mhz)*/
  Timing.AddressSetupTime      = 3;
  Timing.AddressHoldTime       = 1;
  Timing.DataSetupTime         = 4;
  Timing.BusTurnAroundDuration = 1;
  Timing.CLKDivision           = 2;
  Timing.DataLatency           = 2;
  Timing.AccessMode            = FSMC_ACCESS_MODE_A;
  psramHandle.Init.NSBank             = FSMC_NORSRAM_BANK1;
  psramHandle.Init.DataAddressMux     = FSMC_DATA_ADDRESS_MUX_DISABLE;
  psramHandle.Init.MemoryType         = FSMC_MEMORY_TYPE_SRAM;
  psramHandle.Init.MemoryDataWidth    = PSRAM_MEMORY_WIDTH;
  psramHandle.Init.BurstAccessMode    = FSMC_BURST_ACCESS_MODE_DISABLE;
  psramHandle.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
  psramHandle.Init.WaitSignalActive   = FSMC_WAIT_TIMING_BEFORE_WS;
  psramHandle.Init.WriteOperation     = FSMC_WRITE_OPERATION_ENABLE;
  psramHandle.Init.WaitSignal         = FSMC_WAIT_SIGNAL_DISABLE;
  psramHandle.Init.ExtendedMode       = FSMC_EXTENDED_MODE_DISABLE;
  psramHandle.Init.AsynchronousWait   = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
  psramHandle.Init.WriteBurst         = FSMC_WRITE_BURST_DISABLE;
  psramHandle.Init.ContinuousClock    = FSMC_CONTINUOUS_CLOCK_SYNC_ONLY;

  /* Initialize the SRAM controller */
  if(HAL_SRAM_Init(&psramHandle, &Timing, &Timing) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
}

/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow :
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 100000000
  *            HCLK(Hz)                       = 100000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 2
  *            APB2 Prescaler                 = 1
  *            HSE Frequency(Hz)              = 8000000
  *            PLL_M                          = 8
  *            PLL_N                          = 200
  *            PLL_P                          = 2
  *            PLL_Q                          = 7
  *            PLL_R                          = 2
  *            VDD(V)                         = 3.3
  *            Main regulator output voltage  = Scale1 mode
  *            Flash Latency(WS)              = 3
  * @param  None
  * @retval None
  */
static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
  HAL_StatusTypeDef ret = HAL_OK;

  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();

  /* The voltage scaling allows optimizing the power consumption when the device is
     clocked below the maximum system frequency, to update the voltage scaling value
     regarding system frequency refer to product datasheet.  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 200;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  RCC_OscInitStruct.PLL.PLLR = 2;
  ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  if(ret != HAL_OK)
  {
    Error_Handler();
  }

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
  if(ret != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
static void Error_Handler(void)
{
  /* Turn LED4 on */
  BSP_LED_On(LED4);
  while (1)
  {
  }
}

/**
  * @brief  Fills buffer with user predefined data.
  * @param  pBuffer: pointer on the buffer to fill
  * @param  uwBufferLength: size of the buffer to fill
  * @param  uwOffset: first value to fill on the buffer
  * @retval None
  */
static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
{
  uint32_t tmpindex = 0;

  /* Put in global buffer different values */
  for (tmpindex = 0; tmpindex < uwBufferLength; tmpindex++ )
  {
    pBuffer[tmpindex] = tmpindex + uwOffset;
  }
}

/**
  * @brief  Compares two buffers.
  * @param  pBuffer1, pBuffer2: buffers to be compared.
  * @param  BufferLength: buffer's length
  * @retval 1: pBuffer identical to pBuffer1
  *         0: pBuffer differs from pBuffer1
  */
static uint8_t Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength)
{
  while (BufferLength--)
  {
    if (*pBuffer1 != *pBuffer2)
    {
      return 1;
    }

    pBuffer1++;
    pBuffer2++;
  }

  return 0;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


and my code:
C:
//#include "..\EWARM\PSRAM\PSRAM.h"
#include "main.h"
#include "PSRAM.h"


#define PSRAM_BUFFER_SIZE         0x0003

#define WRITE_READ_ADDR           0x0800

#define PSRAM_BANK_ADDR                 ((uint32_t)0x60000000)


extern SRAM_HandleTypeDef hsram1;

/* Read/Write Buffers */
uint16_t PSRAM_TxBuffer[PSRAM_BUFFER_SIZE];
uint16_t PSRAM_RxBuffer[PSRAM_BUFFER_SIZE];

/* Status variables */
__IO uint32_t uwWriteReadStatus = 0;

/* Counter index */
uint32_t uwIndex = 0;


void PSRAM_READ_WRITE(){



  debug_printf("TXbuffer before filling=%i \n",PSRAM_TxBuffer[0]);
  debug_printf("TXbuffer before filling=%i \n",PSRAM_TxBuffer[1]);
  debug_printf("TXbuffer before filling=%i \n",PSRAM_TxBuffer[2]);
  debug_printf("TXbuffer before filling=%i \n",PSRAM_TxBuffer[3]);
  Fill_Buffer(PSRAM_TxBuffer, PSRAM_BUFFER_SIZE,0xDFC0);


  debug_printf("\nTXbuffer after filling=%i \n",PSRAM_TxBuffer[0]);
  debug_printf("TXbuffer after filling=%i \n",PSRAM_TxBuffer[1]);
  debug_printf("TXbuffer after filling=%i \n",PSRAM_TxBuffer[2]);
  debug_printf("TXbuffer after filling=%i \n",PSRAM_TxBuffer[3]);

  debug_printf("RXbuffer after filling=%i \n",PSRAM_RxBuffer[0]);
  debug_printf("RXbuffer after filling=%i \n",PSRAM_RxBuffer[1]);
  debug_printf("RXbuffer after filling=%i \n",PSRAM_RxBuffer[2]);
  debug_printf("RXbuffer after filling=%i \n",PSRAM_RxBuffer[3]);

  HAL_SRAM_Write_16b(&hsram1, (uint32_t *)(PSRAM_BANK_ADDR + WRITE_READ_ADDR), PSRAM_TxBuffer, PSRAM_BUFFER_SIZE);
  debug_printf("\nRXbuffer after writing=%i \n",PSRAM_RxBuffer[0]);
  debug_printf("RXbuffer after writing=%i \n",PSRAM_RxBuffer[1]);
  debug_printf("RXbuffer after writing=%i \n",PSRAM_RxBuffer[2]);
  debug_printf("RXbuffer after writing=%i \n",PSRAM_RxBuffer[3]);

  HAL_SRAM_Read_16b(&hsram1, (uint32_t *)(PSRAM_BANK_ADDR + WRITE_READ_ADDR), PSRAM_RxBuffer, PSRAM_BUFFER_SIZE);

  debug_printf("\nRXbuffer after reading=%i \n",PSRAM_RxBuffer[0]);
  debug_printf("RXbuffer after reading=%i \n",PSRAM_RxBuffer[1]);
  debug_printf("RXbuffer after reading=%i \n",PSRAM_RxBuffer[2]);
  debug_printf("RXbuffer after reading=%i \n",PSRAM_RxBuffer[3]);

}







void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLength, uint16_t uwOffset)
{
  //uint32_t tmpIndex = 0;

   //for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ )
  //{
   // pBuffer[tmpIndex] = (tmpIndex*2)+uwOffset;
// }

PSRAM_TxBuffer[0]=0xffff;
PSRAM_TxBuffer[1]=0x0001;
PSRAM_TxBuffer[2]=0xAAAA;
PSRAM_TxBuffer[3]=0x1A1A;
}
And the debug monitor:


https://i.stack.imgur.com/4YyRh.png


As can be seen from the debug monitor, receive buffer seems to be filling with some values but they are not the right values. I feel like I havent properly set the write_read address and the offset as i cant find any relevant information in the SRAM datasheet.[/code]
 
Top