| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2026 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"
- #include "i2c.h"
- #include "ltdc.h"
- #include "quadspi.h"
- #include "usart.h"
- #include "gpio.h"
- #include "fmc.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "stdio.h"
- #include "string.h"
- #include "Int_sdram.h"
- #include "Int_mpu.h"
- #include "Int_lcd.h"
- #include "Int_flash.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* 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 ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MPU_Config(void);
- /* USER CODE BEGIN PFP */
- void Int_flash_test(void);
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- void Int_flash_test(void)
- {
- uint8_t wbuf[256];
- uint8_t rbuf[256];
- uint16_t id;
- uint32_t i;
- uint8_t pass;
- printf("\r\n======== QSPI FLASH TEST ========\r\n");
- /* 1. Initialize and read chip ID */
- id = Int_flash_read_id();
- printf("Raw ID (0x90): 0x%04X\r\n", id);
- if (Int_flash_init() != 0)
- {
- printf("[FAIL] Int_flash_init\r\n");
- printf("ID = 0x%04X (expected 0x%04X)\r\n",
- (unsigned int)Int_flash_read_id(), (unsigned int)INT_FLASH_ID_GD25Q128);
- return;
- }
- id = Int_flash_read_id();
- printf("Flash ID: 0x%04X\r\n", id);
- if (id != INT_FLASH_ID_GD25Q128)
- {
- printf("[WARN] Expected GD25Q128 (0x%04X)\r\n", INT_FLASH_ID_GD25Q128);
- }
- /* 2. Read the first 16 bytes of flash (diagnostic, no erase) */
- Int_flash_read(rbuf, 0, 16);
- printf("First 16 bytes: ");
- for (i = 0; i < 16; i++)
- {
- printf("%02X ", rbuf[i]);
- }
- printf("\r\n");
- /* 3. Erase sector 0 (4KB) and verify all bytes = 0xFF */
- Int_flash_erase_sector(0);
- Int_flash_read(rbuf, 0, 256);
- pass = 1;
- for (i = 0; i < 256; i++)
- {
- if (rbuf[i] != 0xFF)
- {
- pass = 0;
- break;
- }
- }
- printf("Sector0 erase verify: %s\r\n", pass ? "PASS" : "FAIL");
- /* 4. Write + readback within a single page (no cross-page) */
- for (i = 0; i < 128; i++)
- {
- wbuf[i] = (uint8_t)i;
- }
- Int_flash_write(wbuf, 0, 128);
- Int_flash_read(rbuf, 0, 128);
- pass = 1;
- for (i = 0; i < 128; i++)
- {
- if (rbuf[i] != wbuf[i])
- {
- pass = 0;
- break;
- }
- }
- printf("Write/Read 128B (page 0): %s\r\n", pass ? "PASS" : "FAIL");
- /* 5. Write spanning page boundary (page 0 ends at addr 255) */
- for (i = 0; i < 64; i++)
- {
- wbuf[i] = (uint8_t)(0xA0 + i);
- }
- Int_flash_write(wbuf, 200, 64); /* spans address 200..263 (crosses page 0/1) */
- Int_flash_read(rbuf, 200, 64);
- pass = 1;
- for (i = 0; i < 64; i++)
- {
- if (rbuf[i] != wbuf[i])
- {
- pass = 0;
- break;
- }
- }
- printf("Write/Read 64B (cross-page): %s\r\n", pass ? "PASS" : "FAIL");
- /* 6. Write spanning sector boundary (sector0 lasts 0..4095) */
- for (i = 0; i < 32; i++)
- {
- wbuf[i] = (uint8_t)(0x50 + i);
- }
- Int_flash_write(wbuf, 4080, 32); /* spans address 4080..4111 (crosses sector 0/1) */
- Int_flash_read(rbuf, 4080, 32);
- pass = 1;
- for (i = 0; i < 32; i++)
- {
- if (rbuf[i] != wbuf[i])
- {
- pass = 0;
- break;
- }
- }
- printf("Write/Read 32B (cross-sector): %s\r\n", pass ? "PASS" : "FAIL");
- /* 7. Rewrite same location after erase (address already erased to 0xFF) */
- for (i = 0; i < 32; i++)
- {
- wbuf[i] = (uint8_t)(0xC0 + i);
- }
- Int_flash_write(wbuf, 16, 32);
- Int_flash_read(rbuf, 16, 32);
- pass = 1;
- for (i = 0; i < 32; i++)
- {
- if (rbuf[i] != wbuf[i])
- {
- pass = 0;
- break;
- }
- }
- printf("Write/Read 32B (re-write sector0): %s\r\n", pass ? "PASS" : "FAIL");
- /* 8. Write full 256B page and read back */
- for (i = 0; i < 256; i++)
- {
- wbuf[i] = (uint8_t)(i ^ 0x5A);
- }
- Int_flash_write(wbuf, 4096, 256); /* sector 1 start */
- Int_flash_read(rbuf, 4096, 256);
- pass = 1;
- for (i = 0; i < 256; i++)
- {
- if (rbuf[i] != wbuf[i])
- {
- pass = 0;
- break;
- }
- }
- printf("Write/Read 256B page: %s\r\n", pass ? "PASS" : "FAIL");
- /* 9. Block erase (64KB) test on block 1 */
- Int_flash_erase_block(1);
- Int_flash_read(rbuf, 0x10000, 64);
- pass = 1;
- for (i = 0; i < 64; i++)
- {
- if (rbuf[i] != 0xFF)
- {
- pass = 0;
- break;
- }
- }
- printf("Block1 erase verify: %s\r\n", pass ? "PASS" : "FAIL");
- printf("======== QSPI FLASH TEST DONE ========\r\n\r\n");
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MPU Configuration--------------------------------------------------------*/
- MPU_Config();
- /* Enable the CPU Cache */
- /* Enable I-Cache---------------------------------------------------------*/
- SCB_EnableICache();
- /* Enable D-Cache---------------------------------------------------------*/
- SCB_EnableDCache();
- /* 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();
- MX_QUADSPI_Init();
- MX_USART1_UART_Init();
- MX_I2C2_Init();
- MX_LTDC_Init();
- /* USER CODE BEGIN 2 */
- printf("hello h743\r\n");
- Int_sdram_init();
- Int_sdram_test();
- Int_flash_test();
- Int_lcd_init();
- Int_lcd_clear(LCD_COLOR_RED);
- printf("System Init OK\r\n");
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- Int_lcd_clear(LCD_COLOR_RED);
- HAL_Delay(1000);
- Int_lcd_clear(LCD_COLOR_GREEN);
- HAL_Delay(1000);
- Int_lcd_clear(LCD_COLOR_BLUE);
- HAL_Delay(1000);
- }
- /* USER CODE END 3 */
- }
- /**
- * @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_SCALE0);
- 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 = 60;
- 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_4) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /* MPU Configuration */
- void MPU_Config(void)
- {
- /* Disables the MPU */
- HAL_MPU_Disable();
- /* Enables the MPU */
- HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
- }
- /**
- * @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 */
- __disable_irq();
- while (1)
- {
- }
- /* 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(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* 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) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
|