main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2026 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "i2c.h"
  22. #include "ltdc.h"
  23. #include "quadspi.h"
  24. #include "usart.h"
  25. #include "gpio.h"
  26. #include "fmc.h"
  27. /* Private includes ----------------------------------------------------------*/
  28. /* USER CODE BEGIN Includes */
  29. #include "stdio.h"
  30. #include "string.h"
  31. #include "Int_sdram.h"
  32. #include "Int_mpu.h"
  33. #include "Int_lcd.h"
  34. #include "Int_flash.h"
  35. /* USER CODE END Includes */
  36. /* Private typedef -----------------------------------------------------------*/
  37. /* USER CODE BEGIN PTD */
  38. /* USER CODE END PTD */
  39. /* Private define ------------------------------------------------------------*/
  40. /* USER CODE BEGIN PD */
  41. /* USER CODE END PD */
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44. /* USER CODE END PM */
  45. /* Private variables ---------------------------------------------------------*/
  46. /* USER CODE BEGIN PV */
  47. /* USER CODE END PV */
  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. static void MPU_Config(void);
  51. /* USER CODE BEGIN PFP */
  52. void Int_flash_test(void);
  53. /* USER CODE END PFP */
  54. /* Private user code ---------------------------------------------------------*/
  55. /* USER CODE BEGIN 0 */
  56. void Int_flash_test(void)
  57. {
  58. uint8_t wbuf[256];
  59. uint8_t rbuf[256];
  60. uint16_t id;
  61. uint32_t i;
  62. uint8_t pass;
  63. printf("\r\n======== QSPI FLASH TEST ========\r\n");
  64. /* 1. Initialize and read chip ID */
  65. id = Int_flash_read_id();
  66. printf("Raw ID (0x90): 0x%04X\r\n", id);
  67. if (Int_flash_init() != 0)
  68. {
  69. printf("[FAIL] Int_flash_init\r\n");
  70. printf("ID = 0x%04X (expected 0x%04X)\r\n",
  71. (unsigned int)Int_flash_read_id(), (unsigned int)INT_FLASH_ID_GD25Q128);
  72. return;
  73. }
  74. id = Int_flash_read_id();
  75. printf("Flash ID: 0x%04X\r\n", id);
  76. if (id != INT_FLASH_ID_GD25Q128)
  77. {
  78. printf("[WARN] Expected GD25Q128 (0x%04X)\r\n", INT_FLASH_ID_GD25Q128);
  79. }
  80. /* 2. Read the first 16 bytes of flash (diagnostic, no erase) */
  81. Int_flash_read(rbuf, 0, 16);
  82. printf("First 16 bytes: ");
  83. for (i = 0; i < 16; i++)
  84. {
  85. printf("%02X ", rbuf[i]);
  86. }
  87. printf("\r\n");
  88. /* 3. Erase sector 0 (4KB) and verify all bytes = 0xFF */
  89. Int_flash_erase_sector(0);
  90. Int_flash_read(rbuf, 0, 256);
  91. pass = 1;
  92. for (i = 0; i < 256; i++)
  93. {
  94. if (rbuf[i] != 0xFF)
  95. {
  96. pass = 0;
  97. break;
  98. }
  99. }
  100. printf("Sector0 erase verify: %s\r\n", pass ? "PASS" : "FAIL");
  101. /* 4. Write + readback within a single page (no cross-page) */
  102. for (i = 0; i < 128; i++)
  103. {
  104. wbuf[i] = (uint8_t)i;
  105. }
  106. Int_flash_write(wbuf, 0, 128);
  107. Int_flash_read(rbuf, 0, 128);
  108. pass = 1;
  109. for (i = 0; i < 128; i++)
  110. {
  111. if (rbuf[i] != wbuf[i])
  112. {
  113. pass = 0;
  114. break;
  115. }
  116. }
  117. printf("Write/Read 128B (page 0): %s\r\n", pass ? "PASS" : "FAIL");
  118. /* 5. Write spanning page boundary (page 0 ends at addr 255) */
  119. for (i = 0; i < 64; i++)
  120. {
  121. wbuf[i] = (uint8_t)(0xA0 + i);
  122. }
  123. Int_flash_write(wbuf, 200, 64); /* spans address 200..263 (crosses page 0/1) */
  124. Int_flash_read(rbuf, 200, 64);
  125. pass = 1;
  126. for (i = 0; i < 64; i++)
  127. {
  128. if (rbuf[i] != wbuf[i])
  129. {
  130. pass = 0;
  131. break;
  132. }
  133. }
  134. printf("Write/Read 64B (cross-page): %s\r\n", pass ? "PASS" : "FAIL");
  135. /* 6. Write spanning sector boundary (sector0 lasts 0..4095) */
  136. for (i = 0; i < 32; i++)
  137. {
  138. wbuf[i] = (uint8_t)(0x50 + i);
  139. }
  140. Int_flash_write(wbuf, 4080, 32); /* spans address 4080..4111 (crosses sector 0/1) */
  141. Int_flash_read(rbuf, 4080, 32);
  142. pass = 1;
  143. for (i = 0; i < 32; i++)
  144. {
  145. if (rbuf[i] != wbuf[i])
  146. {
  147. pass = 0;
  148. break;
  149. }
  150. }
  151. printf("Write/Read 32B (cross-sector): %s\r\n", pass ? "PASS" : "FAIL");
  152. /* 7. Rewrite same location after erase (address already erased to 0xFF) */
  153. for (i = 0; i < 32; i++)
  154. {
  155. wbuf[i] = (uint8_t)(0xC0 + i);
  156. }
  157. Int_flash_write(wbuf, 16, 32);
  158. Int_flash_read(rbuf, 16, 32);
  159. pass = 1;
  160. for (i = 0; i < 32; i++)
  161. {
  162. if (rbuf[i] != wbuf[i])
  163. {
  164. pass = 0;
  165. break;
  166. }
  167. }
  168. printf("Write/Read 32B (re-write sector0): %s\r\n", pass ? "PASS" : "FAIL");
  169. /* 8. Write full 256B page and read back */
  170. for (i = 0; i < 256; i++)
  171. {
  172. wbuf[i] = (uint8_t)(i ^ 0x5A);
  173. }
  174. Int_flash_write(wbuf, 4096, 256); /* sector 1 start */
  175. Int_flash_read(rbuf, 4096, 256);
  176. pass = 1;
  177. for (i = 0; i < 256; i++)
  178. {
  179. if (rbuf[i] != wbuf[i])
  180. {
  181. pass = 0;
  182. break;
  183. }
  184. }
  185. printf("Write/Read 256B page: %s\r\n", pass ? "PASS" : "FAIL");
  186. /* 9. Block erase (64KB) test on block 1 */
  187. Int_flash_erase_block(1);
  188. Int_flash_read(rbuf, 0x10000, 64);
  189. pass = 1;
  190. for (i = 0; i < 64; i++)
  191. {
  192. if (rbuf[i] != 0xFF)
  193. {
  194. pass = 0;
  195. break;
  196. }
  197. }
  198. printf("Block1 erase verify: %s\r\n", pass ? "PASS" : "FAIL");
  199. printf("======== QSPI FLASH TEST DONE ========\r\n\r\n");
  200. }
  201. /* USER CODE END 0 */
  202. /**
  203. * @brief The application entry point.
  204. * @retval int
  205. */
  206. int main(void)
  207. {
  208. /* USER CODE BEGIN 1 */
  209. /* USER CODE END 1 */
  210. /* MPU Configuration--------------------------------------------------------*/
  211. MPU_Config();
  212. /* Enable the CPU Cache */
  213. /* Enable I-Cache---------------------------------------------------------*/
  214. SCB_EnableICache();
  215. /* Enable D-Cache---------------------------------------------------------*/
  216. SCB_EnableDCache();
  217. /* MCU Configuration--------------------------------------------------------*/
  218. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  219. HAL_Init();
  220. /* USER CODE BEGIN Init */
  221. /* USER CODE END Init */
  222. /* Configure the system clock */
  223. SystemClock_Config();
  224. /* USER CODE BEGIN SysInit */
  225. /* USER CODE END SysInit */
  226. /* Initialize all configured peripherals */
  227. MX_GPIO_Init();
  228. MX_FMC_Init();
  229. MX_QUADSPI_Init();
  230. MX_USART1_UART_Init();
  231. MX_I2C2_Init();
  232. MX_LTDC_Init();
  233. /* USER CODE BEGIN 2 */
  234. printf("hello h743\r\n");
  235. Int_sdram_init();
  236. Int_sdram_test();
  237. Int_flash_test();
  238. Int_lcd_init();
  239. Int_lcd_clear(LCD_COLOR_RED);
  240. printf("System Init OK\r\n");
  241. /* USER CODE END 2 */
  242. /* Infinite loop */
  243. /* USER CODE BEGIN WHILE */
  244. while (1)
  245. {
  246. /* USER CODE END WHILE */
  247. /* USER CODE BEGIN 3 */
  248. Int_lcd_clear(LCD_COLOR_RED);
  249. HAL_Delay(1000);
  250. Int_lcd_clear(LCD_COLOR_GREEN);
  251. HAL_Delay(1000);
  252. Int_lcd_clear(LCD_COLOR_BLUE);
  253. HAL_Delay(1000);
  254. }
  255. /* USER CODE END 3 */
  256. }
  257. /**
  258. * @brief System Clock Configuration
  259. * @retval None
  260. */
  261. void SystemClock_Config(void)
  262. {
  263. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  264. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  265. /** Supply configuration update enable
  266. */
  267. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  268. /** Configure the main internal regulator output voltage
  269. */
  270. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  271. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  272. /** Initializes the RCC Oscillators according to the specified parameters
  273. * in the RCC_OscInitTypeDef structure.
  274. */
  275. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  276. RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  277. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  278. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  279. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  280. RCC_OscInitStruct.PLL.PLLM = 4;
  281. RCC_OscInitStruct.PLL.PLLN = 60;
  282. RCC_OscInitStruct.PLL.PLLP = 2;
  283. RCC_OscInitStruct.PLL.PLLQ = 2;
  284. RCC_OscInitStruct.PLL.PLLR = 2;
  285. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  286. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  287. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  288. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  289. {
  290. Error_Handler();
  291. }
  292. /** Initializes the CPU, AHB and APB buses clocks
  293. */
  294. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  295. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  296. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  297. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  298. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  299. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  300. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  301. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  302. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  303. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  304. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  305. {
  306. Error_Handler();
  307. }
  308. }
  309. /* USER CODE BEGIN 4 */
  310. /* USER CODE END 4 */
  311. /* MPU Configuration */
  312. void MPU_Config(void)
  313. {
  314. /* Disables the MPU */
  315. HAL_MPU_Disable();
  316. /* Enables the MPU */
  317. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  318. }
  319. /**
  320. * @brief This function is executed in case of error occurrence.
  321. * @retval None
  322. */
  323. void Error_Handler(void)
  324. {
  325. /* USER CODE BEGIN Error_Handler_Debug */
  326. /* User can add his own implementation to report the HAL error return state */
  327. __disable_irq();
  328. while (1)
  329. {
  330. }
  331. /* USER CODE END Error_Handler_Debug */
  332. }
  333. #ifdef USE_FULL_ASSERT
  334. /**
  335. * @brief Reports the name of the source file and the source line number
  336. * where the assert_param error has occurred.
  337. * @param file: pointer to the source file name
  338. * @param line: assert_param error line source number
  339. * @retval None
  340. */
  341. void assert_failed(uint8_t *file, uint32_t line)
  342. {
  343. /* USER CODE BEGIN 6 */
  344. /* User can add his own implementation to report the file name and line number,
  345. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  346. /* USER CODE END 6 */
  347. }
  348. #endif /* USE_FULL_ASSERT */