mirror of
https://github.com/STMicroelectronics/STM32CubeF7.git
synced 2025-05-05 19:30:14 +08:00
882 lines
27 KiB
C
882 lines
27 KiB
C
/**
|
|
******************************************************************************
|
|
* @file QSPI/QSPI_ReadWrite/Src/main.c
|
|
* @author MCD Application Team
|
|
* @brief This example describes how to configure and use QuadSPI through
|
|
* the STM32F7xx HAL API.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© Copyright (c) 2016 STMicroelectronics.
|
|
* All rights reserved.</center></h2>
|
|
*
|
|
* This software component is licensed by ST under BSD 3-Clause license,
|
|
* the "License"; You may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at:
|
|
* opensource.org/licenses/BSD-3-Clause
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
|
|
/** @addtogroup STM32F7xx_HAL_Examples
|
|
* @{
|
|
*/
|
|
|
|
/** @addtogroup QSPI_ReadWrite
|
|
* @{
|
|
*/
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
#define BUFFER_SIZE ((uint32_t)0x0200)
|
|
#define WRITE_READ_ADDR ((uint32_t)0x0050)
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
QSPI_HandleTypeDef QSPIHandle;
|
|
|
|
uint8_t qspi_aTxBuffer[BUFFER_SIZE];
|
|
uint8_t qspi_aRxBuffer[BUFFER_SIZE];
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
static void SystemClock_Config(void);
|
|
static void CPU_CACHE_Enable(void);
|
|
static void Error_Handler(void);
|
|
static void Fill_Buffer (uint8_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset);
|
|
|
|
static uint8_t QSPI_Init(void);
|
|
static uint8_t QSPI_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size);
|
|
static uint8_t QSPI_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size);
|
|
static uint8_t QSPI_Erase_Block(uint32_t BlockAddress);
|
|
static uint8_t QSPI_WriteEnable(QSPI_HandleTypeDef *hqspi);
|
|
static uint8_t QSPI_AutoPollingMemReady (QSPI_HandleTypeDef *hqspi, uint32_t Timeout);
|
|
static uint8_t QSPI_EnterMemory_QPI(QSPI_HandleTypeDef *hqspi);
|
|
static uint8_t QSPI_EnterFourBytesAddress(QSPI_HandleTypeDef *hqspi);
|
|
static uint8_t QSPI_DummyCyclesCfg(QSPI_HandleTypeDef *hqspi);
|
|
static uint8_t QSPI_OutDrvStrengthCfg(QSPI_HandleTypeDef *hqspi);
|
|
static uint8_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength);
|
|
|
|
/**
|
|
* @brief Main program
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
int main(void)
|
|
{
|
|
/* STM32F7xx 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();
|
|
|
|
/* Enable the CPU Cache */
|
|
CPU_CACHE_Enable();
|
|
|
|
/* Configure the system clock to 216 Mhz */
|
|
SystemClock_Config();
|
|
|
|
/* Configure LED5, LED6 */
|
|
BSP_LED_Init(LED5);
|
|
BSP_LED_Init(LED6);
|
|
|
|
/*##-1- Configure the QSPI device ##########################################*/
|
|
QSPI_Init();
|
|
|
|
/*##-2- Erase QSPI memory ##################################################*/
|
|
QSPI_Erase_Block(WRITE_READ_ADDR);
|
|
|
|
/*##-3- QSPI memory read/write access #####################################*/
|
|
/* Fill the buffer to write */
|
|
Fill_Buffer(qspi_aTxBuffer, BUFFER_SIZE, 0xD20F);
|
|
|
|
/* Write data to the QSPI memory */
|
|
QSPI_Write(qspi_aTxBuffer, WRITE_READ_ADDR, BUFFER_SIZE);
|
|
|
|
/* Read back data from the QSPI memory */
|
|
QSPI_Read(qspi_aRxBuffer, WRITE_READ_ADDR, BUFFER_SIZE);
|
|
|
|
/*##-4- Check read data integrity ##########################################*/
|
|
if(Buffercmp(qspi_aRxBuffer, qspi_aTxBuffer, BUFFER_SIZE) > 0)
|
|
{
|
|
/* Comparison failed */
|
|
Error_Handler();
|
|
}
|
|
else
|
|
{
|
|
/* Comparison successfull */
|
|
BSP_LED_On(LED6);
|
|
}
|
|
|
|
/* Infinte loop */
|
|
while (1)
|
|
{
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief QSPI Init
|
|
* @param None
|
|
* @retval HAL_ERROR or HAL_OK
|
|
*/
|
|
uint8_t QSPI_Init(void)
|
|
{
|
|
QSPIHandle.Instance = QUADSPI;
|
|
|
|
/* Call the DeInit function to reset the driver */
|
|
if (HAL_QSPI_DeInit(&QSPIHandle) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* System level initialization */
|
|
HAL_QSPI_MspInit(&QSPIHandle);
|
|
|
|
/* QSPI initialization */
|
|
/* QSPI freq = SYSCLK /(1 + ClockPrescaler) = 216 MHz/(1+1) = 108 Mhz */
|
|
QSPIHandle.Init.ClockPrescaler = 1; /* QSPI freq = 216 MHz/(1+1) = 108 Mhz */
|
|
QSPIHandle.Init.FifoThreshold = 16;
|
|
QSPIHandle.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
|
|
QSPIHandle.Init.FlashSize = POSITION_VAL(QSPI_FLASH_SIZE) - 1;
|
|
QSPIHandle.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
|
|
QSPIHandle.Init.ClockMode = QSPI_CLOCK_MODE_0;
|
|
QSPIHandle.Init.FlashID = QSPI_FLASH_ID_1;
|
|
QSPIHandle.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
|
|
|
|
if (HAL_QSPI_Init(&QSPIHandle) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Put QSPI memory in QPI mode */
|
|
if( QSPI_EnterMemory_QPI( &QSPIHandle )!=HAL_OK )
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Set the QSPI memory in 4-bytes address mode */
|
|
if (QSPI_EnterFourBytesAddress(&QSPIHandle) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configuration of the dummy cycles on QSPI memory side */
|
|
if (QSPI_DummyCyclesCfg(&QSPIHandle) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configuration of the Output driver strength on memory side */
|
|
if( QSPI_OutDrvStrengthCfg( &QSPIHandle ) != HAL_OK )
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief Erases the specified block of the QSPI memory.
|
|
* @param BlockAddress: Block address to erase
|
|
* @retval QSPI memory status
|
|
*/
|
|
uint8_t QSPI_Erase_Block(uint32_t BlockAddress)
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
|
|
/* Initialize the erase command */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = SUBSECTOR_ERASE_4_BYTE_ADDR_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_4_LINES;
|
|
s_command.AddressSize = QSPI_ADDRESS_32_BITS;
|
|
s_command.Address = BlockAddress;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_NONE;
|
|
s_command.DummyCycles = 0;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Enable write operations */
|
|
if (QSPI_WriteEnable(&QSPIHandle) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Send the command */
|
|
if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configure automatic polling mode to wait for end of erase */
|
|
if (QSPI_AutoPollingMemReady(&QSPIHandle, QSPI_SUBSECTOR_ERASE_MAX_TIME) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief Writes an amount of data to the QSPI memory.
|
|
* @param pData: Pointer to data to be written
|
|
* @param WriteAddr: Write start address
|
|
* @param Size: Size of data to write
|
|
* @retval QSPI memory status
|
|
*/
|
|
uint8_t QSPI_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
uint32_t end_addr, current_size, current_addr;
|
|
|
|
/* Calculation of the size between the write address and the end of the page */
|
|
current_addr = 0;
|
|
|
|
while (current_addr <= WriteAddr)
|
|
{
|
|
current_addr += QSPI_PAGE_SIZE;
|
|
}
|
|
current_size = current_addr - WriteAddr;
|
|
|
|
/* Check if the size of the data is less than the remaining place in the page */
|
|
if (current_size > Size)
|
|
{
|
|
current_size = Size;
|
|
}
|
|
|
|
/* Initialize the address variables */
|
|
current_addr = WriteAddr;
|
|
end_addr = WriteAddr + Size;
|
|
|
|
/* Initialize the program command */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = QPI_PAGE_PROG_4_BYTE_ADDR_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_4_LINES;
|
|
s_command.AddressSize = QSPI_ADDRESS_32_BITS;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Perform the write page by page */
|
|
do
|
|
{
|
|
s_command.Address = current_addr;
|
|
s_command.NbData = current_size;
|
|
|
|
/* Enable write operations */
|
|
if (QSPI_WriteEnable(&QSPIHandle) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configure the command */
|
|
if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Transmission of the data */
|
|
if (HAL_QSPI_Transmit(&QSPIHandle, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configure automatic polling mode to wait for end of program */
|
|
if (QSPI_AutoPollingMemReady(&QSPIHandle, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Update the address and size variables for next page programming */
|
|
current_addr += current_size;
|
|
pData += current_size;
|
|
current_size = ((current_addr + QSPI_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : QSPI_PAGE_SIZE;
|
|
} while (current_addr < end_addr);
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief Reads an amount of data from the QSPI memory.
|
|
* @param pData: Pointer to data to be read
|
|
* @param ReadAddr: Read start address
|
|
* @param Size: Size of data to read
|
|
* @retval QSPI memory status
|
|
*/
|
|
uint8_t QSPI_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
|
|
/* Initialize the read command */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = QPI_READ_4_BYTE_ADDR_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_4_LINES;
|
|
s_command.AddressSize = QSPI_ADDRESS_32_BITS;
|
|
s_command.Address = ReadAddr;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = QSPI_DUMMY_CYCLES_READ_QUAD_IO;
|
|
s_command.NbData = Size;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Configure the command */
|
|
if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Reception of the data */
|
|
if (HAL_QSPI_Receive(&QSPIHandle, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief This function send a Write Enable and wait it is effective.
|
|
* @param hqspi: QSPI handle
|
|
* @retval None
|
|
*/
|
|
static uint8_t QSPI_WriteEnable(QSPI_HandleTypeDef *hqspi)
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
QSPI_AutoPollingTypeDef s_config;
|
|
|
|
/* Enable write operations */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = WRITE_ENABLE_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_NONE;
|
|
s_command.DummyCycles = 0;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configure automatic polling mode to wait for write enabling */
|
|
s_config.Match = QSPI_SR_WREN;
|
|
s_config.Mask = QSPI_SR_WREN;
|
|
s_config.MatchMode = QSPI_MATCH_MODE_AND;
|
|
s_config.StatusBytesSize = 1;
|
|
s_config.Interval = 0x10;
|
|
s_config.AutomaticStop = QSPI_AUTOMATIC_STOP_ENABLE;
|
|
|
|
s_command.Instruction = READ_STATUS_REG_CMD;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
|
|
if (HAL_QSPI_AutoPolling(hqspi, &s_command, &s_config, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief This function read the SR of the memory and wait the EOP.
|
|
* @param hqspi: QSPI handle
|
|
* @param Timeout
|
|
* @retval None
|
|
*/
|
|
static uint8_t QSPI_AutoPollingMemReady(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
QSPI_AutoPollingTypeDef s_config;
|
|
|
|
/* Configure automatic polling mode to wait for memory ready */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = READ_STATUS_REG_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
s_config.Match = 0;
|
|
s_config.Mask = QSPI_SR_WIP;
|
|
s_config.MatchMode = QSPI_MATCH_MODE_AND;
|
|
s_config.StatusBytesSize = 1;
|
|
s_config.Interval = 0x10;
|
|
s_config.AutomaticStop = QSPI_AUTOMATIC_STOP_ENABLE;
|
|
|
|
if (HAL_QSPI_AutoPolling(hqspi, &s_command, &s_config, Timeout) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief This function put QSPI memory in QPI mode (quad I/O).
|
|
* @param hqspi: QSPI handle
|
|
* @retval None
|
|
*/
|
|
static uint8_t QSPI_EnterMemory_QPI( QSPI_HandleTypeDef *hqspi )
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
QSPI_AutoPollingTypeDef s_config;
|
|
|
|
/* Initialize the QPI enable command */
|
|
/* QSPI memory is supported to be in SPI mode, so CMD on 1 LINE */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
|
|
s_command.Instruction = ENTER_QUAD_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_NONE;
|
|
s_command.DummyCycles = 0;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Send the command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configure automatic polling mode to wait the QUADEN bit=1 and WIP bit=0 */
|
|
s_config.Match = QSPI_SR_QUADEN;
|
|
s_config.Mask = QSPI_SR_QUADEN|QSPI_SR_WIP;
|
|
s_config.MatchMode = QSPI_MATCH_MODE_AND;
|
|
s_config.StatusBytesSize = 1;
|
|
s_config.Interval = 0x10;
|
|
s_config.AutomaticStop = QSPI_AUTOMATIC_STOP_ENABLE;
|
|
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = READ_STATUS_REG_CMD;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
|
|
if (HAL_QSPI_AutoPolling(hqspi, &s_command, &s_config, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief This function set the QSPI memory in 4-byte address mode
|
|
* @param hqspi: QSPI handle
|
|
* @retval None
|
|
*/
|
|
static uint8_t QSPI_EnterFourBytesAddress(QSPI_HandleTypeDef *hqspi)
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
|
|
/* Initialize the command */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = ENTER_4_BYTE_ADDR_MODE_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_NONE;
|
|
s_command.DummyCycles = 0;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Enable write operations */
|
|
if (QSPI_WriteEnable(hqspi) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Send the command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Configure automatic polling mode to wait the memory is ready */
|
|
if (QSPI_AutoPollingMemReady(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief This function configure the dummy cycles on memory side.
|
|
* @param hqspi: QSPI handle
|
|
* @retval None
|
|
*/
|
|
static uint8_t QSPI_DummyCyclesCfg(QSPI_HandleTypeDef *hqspi)
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
uint8_t reg[2];
|
|
|
|
/* Initialize the reading of status register */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = READ_STATUS_REG_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.NbData = 1;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Configure the command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Reception of the data */
|
|
if (HAL_QSPI_Receive(hqspi, &(reg[0]), HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Initialize the reading of configuration register */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = READ_CFG_REG_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.NbData = 1;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Configure the command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Reception of the data */
|
|
if (HAL_QSPI_Receive(hqspi, &(reg[1]), HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Enable write operations */
|
|
if (QSPI_WriteEnable(hqspi) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Update the configuration register with new dummy cycles */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = WRITE_STATUS_CFG_REG_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.NbData = 2;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* QSPI_DUMMY_CYCLES_READ_QUAD = 3 for 10 cycles in QPI mode */
|
|
MODIFY_REG( reg[1], QSPI_CR_NB_DUMMY, (QSPI_DUMMY_CYCLES_READ_QUAD << POSITION_VAL(QSPI_CR_NB_DUMMY)));
|
|
|
|
/* Configure the write volatile configuration register command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Transmission of the data */
|
|
if (HAL_QSPI_Transmit(hqspi, &(reg[0]), HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* 40ms Write Status/Configuration Register Cycle Time */
|
|
HAL_Delay( 40 );
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief This function configure the Output driver strength on memory side.
|
|
* @param hqspi: QSPI handle
|
|
* @retval None
|
|
*/
|
|
static uint8_t QSPI_OutDrvStrengthCfg( QSPI_HandleTypeDef *hqspi )
|
|
{
|
|
QSPI_CommandTypeDef s_command;
|
|
uint8_t reg[2];
|
|
|
|
/* Initialize the reading of status register */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = READ_STATUS_REG_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.NbData = 1;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Configure the command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Reception of the data */
|
|
if (HAL_QSPI_Receive(hqspi, &(reg[0]), HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Initialize the reading of configuration register */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = READ_CFG_REG_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.NbData = 1;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Configure the command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Reception of the data */
|
|
if (HAL_QSPI_Receive(hqspi, &(reg[1]), HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Enable write operations */
|
|
if (QSPI_WriteEnable(&QSPIHandle) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Update the configuration register with new output driver strength */
|
|
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
|
|
s_command.Instruction = WRITE_STATUS_CFG_REG_CMD;
|
|
s_command.AddressMode = QSPI_ADDRESS_NONE;
|
|
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
|
|
s_command.DataMode = QSPI_DATA_4_LINES;
|
|
s_command.DummyCycles = 0;
|
|
s_command.NbData = 2;
|
|
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
|
|
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
|
|
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
|
|
|
|
/* Set Output Strength of the QSPI memory 15 ohms */
|
|
MODIFY_REG( reg[1], QSPI_CR_ODS, (QSPI_CR_ODS_15 << POSITION_VAL(QSPI_CR_ODS)));
|
|
|
|
/* Configure the write volatile configuration register command */
|
|
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
/* Transmission of the data */
|
|
if (HAL_QSPI_Transmit(hqspi, &(reg[0]), HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
|
|
{
|
|
return HAL_ERROR;
|
|
}
|
|
|
|
return HAL_OK;
|
|
}
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
BSP_LED_On(LED5);
|
|
while(1)
|
|
{
|
|
/* Insert a delay */
|
|
HAL_Delay(50);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* The system Clock is configured as follow :
|
|
* System Clock source = PLL (HSE)
|
|
* SYSCLK(Hz) = 216000000
|
|
* HCLK(Hz) = 216000000
|
|
* AHB Prescaler = 1
|
|
* APB1 Prescaler = 4
|
|
* APB2 Prescaler = 2
|
|
* HSE Frequency(Hz) = 25000000
|
|
* PLL_M = 25
|
|
* PLL_N = 432
|
|
* PLL_P = 2
|
|
* PLL_Q = 9
|
|
* PLL_R = 7
|
|
* VDD(V) = 3.3
|
|
* Main regulator output voltage = Scale1 mode
|
|
* Flash Latency(WS) = 7
|
|
* @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_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLM = 25;
|
|
RCC_OscInitStruct.PLL.PLLN = 432;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLQ = 9;
|
|
|
|
|
|
ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
|
if(ret != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* Activate the OverDrive to reach the 216 MHz Frequency */
|
|
ret = HAL_PWREx_EnableOverDrive();
|
|
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_DIV4;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
|
|
|
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);
|
|
if(ret != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief CPU L1-Cache enable.
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void CPU_CACHE_Enable(void)
|
|
{
|
|
/* Enable I-Cache */
|
|
SCB_EnableICache();
|
|
|
|
/* Enable D-Cache */
|
|
SCB_EnableDCache();
|
|
}
|
|
|
|
/**
|
|
* @brief Fills buffer with user predefined data.
|
|
* @param pBuffer: pointer on the buffer to fill
|
|
* @param uwBufferLenght: size of the buffer to fill
|
|
* @param uwOffset: first value to fill on the buffer
|
|
* @retval None
|
|
*/
|
|
static void Fill_Buffer(uint8_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset)
|
|
{
|
|
uint32_t tmpIndex = 0;
|
|
|
|
/* Put in global buffer different values */
|
|
for (tmpIndex = 0; tmpIndex < uwBufferLenght; 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(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_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 /* USE_FULL_ASSERT */
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|