/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stm32h7xx_hal.h" //#include "stm32h743i_eval.h" #include "stdio.h" typedef enum { FAILED = 0, PASSED = !FAILED } TestStatus; #define BUFFER_SIZE ((uint32_t)0x1000) #define NOR_BANK_ADDR ((uint32_t)0x60000000) #define ERASE_TIMEOUT 0x00A00000U // Example value #define PROGRAM_TIMEOUT 0x00004400U // Example value #define CHIPERASE_TIMEOUT ((uint32_t)0x30000000) #define WRITE_READ_ADDR ((uint32_t)0x0000) void Error_Handler(void); static void CPU_CACHE_Enable(void); static void MPU_Config(void); static void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLenght, uint16_t uwOffset); static TestStatus Buffercmp(uint16_t *pBuffer1, uint16_t *pBuffer2, uint16_t BufferLength); /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ void Fill_Buffer(uint16_t *pBuffer, uint32_t uwBufferLenght, uint16_t uwOffset) { uint16_t tmpIndex = 0; /* Put in global buffer different values */ for (tmpIndex = 0; tmpIndex < uwBufferLenght; tmpIndex++ ) { pBuffer[tmpIndex] = tmpIndex + uwOffset; } } /** * @brief Compares two buffers. * pBuffer1, pBuffer2: buffers to be compared. * BufferLength: buffer's length * @retval PASSED: pBuffer identical to pBuffer1 * FAILED: pBuffer differs from pBuffer1 */ static TestStatus Buffercmp(uint16_t* pBuffer1, uint16_t* pBuffer2, uint16_t BufferLength) { while (BufferLength--) { if (*pBuffer1 != *pBuffer2) { return FAILED; } pBuffer1++; pBuffer2++; } return PASSED; } static void CPU_CACHE_Enable(void) { SCB_EnableICache(); SCB_EnableDCache(); } NOR_HandleTypeDef hnor1; NOR_IDTypeDef NOR_Id; uint16_t aTxBuffer[BUFFER_SIZE]; uint16_t aRxBuffer[BUFFER_SIZE]; __IO uint32_t uwWriteReadStatus = 0; uint32_t uwIndex = 0; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ NOR_HandleTypeDef hnor1; /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_FMC_Init(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 */ int main(void) { /* USER CODE BEGIN 1 */ CPU_CACHE_Enable(); MPU_Config(); /* 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(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_FMC_Init(); /* USER CODE BEGIN 2 */ HAL_NOR_Read_ID(&hnor1, &NOR_Id); // Step 1: Erase the NOR flash Fill_Buffer(aTxBuffer, BUFFER_SIZE, 0xD20F); /* Erase block */ HAL_NOR_Erase_Block(&hnor1, WRITE_READ_ADDR, NOR_BANK_ADDR); /* Wait until NOR is ready */ if(HAL_NOR_GetStatus(&hnor1, NOR_BANK_ADDR, ERASE_TIMEOUT) != HAL_NOR_STATUS_SUCCESS) { return HAL_NOR_STATUS_ERROR; } /* Write data to the NOR memory */ for (uwIndex = 0; uwIndex < BUFFER_SIZE; uwIndex++) { /* Write data to NOR */ HAL_NOR_Program(&hnor1, (uint32_t *)(NOR_BANK_ADDR + WRITE_READ_ADDR + 2*uwIndex), &aTxBuffer[uwIndex]); /* Read NOR device status */ if(HAL_NOR_GetStatus(&hnor1, NOR_BANK_ADDR, PROGRAM_TIMEOUT) != HAL_NOR_STATUS_SUCCESS) { return HAL_NOR_STATUS_ERROR; } } /* Read back data from the NOR memory */ if(HAL_NOR_ReadBuffer(&hnor1, NOR_BANK_ADDR + WRITE_READ_ADDR, &aRxBuffer[0], BUFFER_SIZE) != HAL_OK) { return HAL_ERROR; } /*##-3- Checking data integrity ############################################*/ uwWriteReadStatus = Buffercmp(aTxBuffer, aRxBuffer, BUFFER_SIZE); if (uwWriteReadStatus != PASSED) { /* Set LED */ while(1) { HAL_GPIO_WritePin(GPIOI, GPIO_PIN_8, GPIO_PIN_SET); } } else { /* OK */ /* Toggle LED */ while(1) { HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_8); HAL_Delay(500); } } /* USER CODE END 2 */ /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ // If we reach here, all operations are successful while (1) { } /* USER CODE END 2 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Supply configuration update enable */ HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY); /** Configure the main internal regulator output voltage */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {} /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_DIV1; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = 4; RCC_OscInitStruct.PLL.PLLN = 50; RCC_OscInitStruct.PLL.PLLP = 2; RCC_OscInitStruct.PLL.PLLQ = 2; RCC_OscInitStruct.PLL.PLLR = 2; RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3; RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE; RCC_OscInitStruct.PLL.PLLFRACN = 0; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2; RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* FMC initialization function */ static void MX_FMC_Init(void) { FMC_NORSRAM_TimingTypeDef Timing = {0}; /** Perform the NOR1 memory initialization sequence */ hnor1.Instance = FMC_NORSRAM_DEVICE; hnor1.Extended = FMC_NORSRAM_EXTENDED_DEVICE; /* hnor1.Init */ hnor1.Init.NSBank = FMC_NORSRAM_BANK1; hnor1.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE; hnor1.Init.MemoryType = FMC_MEMORY_TYPE_NOR; hnor1.Init.MemoryDataWidth = FMC_NORSRAM_MEM_BUS_WIDTH_16; hnor1.Init.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE; hnor1.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW; hnor1.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS; hnor1.Init.WriteOperation = FMC_WRITE_OPERATION_ENABLE; hnor1.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE; hnor1.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE; hnor1.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_ENABLE; hnor1.Init.WriteBurst = FMC_WRITE_BURST_DISABLE; hnor1.Init.ContinuousClock = FMC_CONTINUOUS_CLOCK_SYNC_ONLY; hnor1.Init.WriteFifo = FMC_WRITE_FIFO_DISABLE; hnor1.Init.PageSize = FMC_PAGE_SIZE_NONE; /* Timing */ Timing.AddressSetupTime = 15; Timing.AddressHoldTime = 15; Timing.DataSetupTime = 255; Timing.BusTurnAroundDuration = 15; Timing.CLKDivision = 16; Timing.DataLatency = 17; Timing.AccessMode = FMC_ACCESS_MODE_A; if (HAL_NOR_Init(&hnor1, &Timing, NULL) != HAL_OK) { Error_Handler( ); } } /** * @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_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOI_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(led_test_GPIO_Port, led_test_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : led_test_Pin */ GPIO_InitStruct.Pin = led_test_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(led_test_GPIO_Port, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ /* MPU Configuration */ static void MPU_Config(void) { MPU_Region_InitTypeDef MPU_InitStruct; HAL_MPU_Disable(); MPU_InitStruct.Enable = MPU_REGION_ENABLE; MPU_InitStruct.BaseAddress = NOR_BANK_ADDR; MPU_InitStruct.Size = MPU_REGION_SIZE_16MB; MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE; MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE; MPU_InitStruct.Number = MPU_REGION_NUMBER0; MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0; MPU_InitStruct.SubRegionDisable = 0x00; MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; HAL_MPU_ConfigRegion(&MPU_InitStruct); HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT); } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { __disable_irq(); while (1) { } } #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) */ } #endif /* USE_FULL_ASSERT */