/* USER CODE BEGIN Header */ /* USER CODE END Header */ /*--------------- Function description: PSRAM using APMemory QSPI SDR , first enter Quad mode, and then read and write 16Byte in a cycle. If there is an error the LED of GPIO_PIN_14 lights up. -------------- */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE END Includes */ #define BUFFER_LENGTH 16 uint8_t aTxBuffer[BUFFER_LENGTH]; uint8_t aRX_Buffer[BUFFER_LENGTH]={0}; OSPI_HandleTypeDef hospi1; /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_OCTOSPI1_Init(void); void SettingPSRAM(void); void EnterQuadMode(void); void Error_Handler(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ __IO uint8_t *mem_addr; uint32_t address = 0; uint16_t index1; /*index1 counter of bytes used when reading/writing 256 bytes buffer */ uint16_t index2; /*index2 counter of 256 bytes buffer used when reading/writing the 1Mbytes extended buffer */ uint8_t WD,RD; int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_OCTOSPI1_Init(); HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14|GPIO_PIN_13, GPIO_PIN_SET); /*reset the external leds (reset when GPIO is high)*/ /*initialize refresh feature for the OCTOSPI*/ hospi1.Init.Refresh = 500; if (HAL_OSPI_Init(&hospi1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN 2 */ EnterQuadMode(); SettingPSRAM(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { for(uint8_t i=0;i toggle led here*/ HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_14); } mem_addr++; } /*can toggle led here*/ HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_13); /* USER CODE END WHILE */ } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Configure the main internal regulator output voltage */ if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = 0; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; RCC_OscInitStruct.PLL.PLLM = 1; RCC_OscInitStruct.PLL.PLLN = 60; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_OSPI; PeriphClkInit.OspiClockSelection = RCC_OSPICLKSOURCE_SYSCLK; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /** * @brief OCTOSPI1 Initialization Function * @param None * @retval None */ static void MX_OCTOSPI1_Init(void) { /* USER CODE BEGIN OCTOSPI1_Init 0 */ /* USER CODE END OCTOSPI1_Init 0 */ OSPIM_CfgTypeDef OSPIM_Cfg_Struct = {0}; /* USER CODE BEGIN OCTOSPI1_Init 1 */ /* USER CODE END OCTOSPI1_Init 1 */ /* OCTOSPI1 parameter configuration*/ hospi1.Instance = OCTOSPI1; hospi1.Init.FifoThreshold = 1; hospi1.Init.DualQuad = HAL_OSPI_DUALQUAD_DISABLE; hospi1.Init.MemoryType = HAL_OSPI_MEMTYPE_APMEMORY; hospi1.Init.DeviceSize = 23; hospi1.Init.ChipSelectHighTime = 2; hospi1.Init.FreeRunningClock = HAL_OSPI_FREERUNCLK_DISABLE; hospi1.Init.ClockMode = HAL_OSPI_CLOCK_MODE_0; hospi1.Init.ClockPrescaler = 3; hospi1.Init.SampleShifting = HAL_OSPI_SAMPLE_SHIFTING_NONE; hospi1.Init.DelayHoldQuarterCycle = HAL_OSPI_DHQC_DISABLE; hospi1.Init.ChipSelectBoundary = 9; if (HAL_OSPI_Init(&hospi1) != HAL_OK) { Error_Handler(); } OSPIM_Cfg_Struct.ClkPort = 1; OSPIM_Cfg_Struct.NCSPort = 1; OSPIM_Cfg_Struct.IOLowPort = HAL_OSPIM_IOPORT_1_LOW; if (HAL_OSPIM_Config(&hospi1, &OSPIM_Cfg_Struct, HAL_OSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN OCTOSPI1_Init 2 */ /* USER CODE END OCTOSPI1_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE(); HAL_PWREx_EnableVddIO2(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14|GPIO_PIN_13, GPIO_PIN_RESET); /*Configure GPIO pins : PG14 PG13 */ GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ void SettingPSRAM(void) { OSPI_RegularCmdTypeDef sCommand; OSPI_MemoryMappedTypeDef sMemMappedCfg; sCommand.FlashId = HAL_OSPI_FLASH_ID_1; sCommand.InstructionMode = HAL_OSPI_INSTRUCTION_4_LINES; sCommand.InstructionSize = HAL_OSPI_INSTRUCTION_8_BITS; sCommand.InstructionDtrMode = HAL_OSPI_INSTRUCTION_DTR_DISABLE; sCommand.AddressMode = HAL_OSPI_ADDRESS_4_LINES; sCommand.AddressSize = HAL_OSPI_ADDRESS_24_BITS; sCommand.AddressDtrMode = HAL_OSPI_ADDRESS_DTR_DISABLE; sCommand.AlternateBytesMode = HAL_OSPI_ALTERNATE_BYTES_NONE; sCommand.DataMode = HAL_OSPI_DATA_4_LINES; sCommand.DataDtrMode = HAL_OSPI_DATA_DTR_DISABLE; sCommand.SIOOMode = HAL_OSPI_SIOO_INST_EVERY_CMD; sCommand.Address = 0; sCommand.NbData = 1; /* Memory configuration for Linear burst read operations */ sCommand.OperationType = HAL_OSPI_OPTYPE_READ_CFG; sCommand.Instruction = FAST_READ_QUAD; sCommand.DummyCycles = 6; if (HAL_OSPI_Command(&hospi1, &sCommand, HAL_OSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) { Error_Handler(); } /* Memory configuration for Linear burst write operations */ sCommand.OperationType = HAL_OSPI_OPTYPE_WRITE_CFG; sCommand.Instruction = QUAD_WRITE; sCommand.DummyCycles = 0; sCommand.DQSMode = HAL_OSPI_DQS_ENABLE; if (HAL_OSPI_Command(&hospi1, &sCommand, HAL_OSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) { Error_Handler(); } /*Disable timeout counter for memory mapped mode*/ sMemMappedCfg.TimeOutActivation = HAL_OSPI_TIMEOUT_COUNTER_DISABLE; /*Enable memory mode*/ if (HAL_OSPI_MemoryMapped(&hospi1, &sMemMappedCfg) != HAL_OK) { Error_Handler(); } } void EnterQuadMode(void) { OSPI_RegularCmdTypeDef sCommand; sCommand.OperationType = HAL_OSPI_OPTYPE_COMMON_CFG; sCommand.FlashId = HAL_OSPI_FLASH_ID_1; sCommand.Instruction = ENTER_QUAD_MODE; sCommand.InstructionMode = HAL_OSPI_INSTRUCTION_1_LINE; sCommand.InstructionSize = HAL_OSPI_INSTRUCTION_8_BITS; sCommand.InstructionDtrMode = HAL_OSPI_INSTRUCTION_DTR_DISABLE; sCommand.AddressMode = HAL_OSPI_ADDRESS_NONE; sCommand.AlternateBytesMode = HAL_OSPI_ALTERNATE_BYTES_NONE; sCommand.DataMode = HAL_OSPI_DATA_NONE; sCommand.DummyCycles = 0; sCommand.DQSMode = HAL_OSPI_DQS_DISABLE; sCommand.SIOOMode = HAL_OSPI_SIOO_INST_EVERY_CMD; /*Enter QUAD mode*/ if (HAL_OSPI_Command(&hospi1, &sCommand, HAL_OSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) { Error_Handler(); } } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ /* USER CODE END Error_Handler_Debug */ } #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(char *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/