main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. #include "Int_eeprom.h"
  36. /* USER CODE END Includes */
  37. /* Private typedef -----------------------------------------------------------*/
  38. /* USER CODE BEGIN PTD */
  39. /* USER CODE END PTD */
  40. /* Private define ------------------------------------------------------------*/
  41. /* USER CODE BEGIN PD */
  42. /* USER CODE END PD */
  43. /* Private macro -------------------------------------------------------------*/
  44. /* USER CODE BEGIN PM */
  45. /* USER CODE END PM */
  46. /* Private variables ---------------------------------------------------------*/
  47. /* USER CODE BEGIN PV */
  48. /* USER CODE END PV */
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51. static void MPU_Config(void);
  52. /* USER CODE BEGIN PFP */
  53. void Int_flash_test(void);
  54. void Int_eeprom_test(void);
  55. /* USER CODE END PFP */
  56. /* Private user code ---------------------------------------------------------*/
  57. /* USER CODE BEGIN 0 */
  58. void Int_flash_test(void)
  59. {
  60. uint8_t wbuf[256];
  61. uint8_t rbuf[256];
  62. uint16_t id;
  63. uint32_t i;
  64. uint8_t pass;
  65. printf("\r\n======== QSPI FLASH TEST ========\r\n");
  66. /* 1. Initialize and read chip ID */
  67. id = Int_flash_read_id();
  68. printf("Raw ID (0x90): 0x%04X\r\n", id);
  69. if (Int_flash_init() != 0)
  70. {
  71. printf("[FAIL] Int_flash_init\r\n");
  72. printf("ID = 0x%04X (expected 0x%04X)\r\n",
  73. (unsigned int)Int_flash_read_id(), (unsigned int)INT_FLASH_ID_GD25Q128);
  74. return;
  75. }
  76. id = Int_flash_read_id();
  77. printf("Flash ID: 0x%04X\r\n", id);
  78. if (id != INT_FLASH_ID_GD25Q128)
  79. {
  80. printf("[WARN] Expected GD25Q128 (0x%04X)\r\n", INT_FLASH_ID_GD25Q128);
  81. }
  82. /* 2. Read the first 16 bytes of flash (diagnostic, no erase) */
  83. Int_flash_read(rbuf, 0, 16);
  84. printf("First 16 bytes: ");
  85. for (i = 0; i < 16; i++)
  86. {
  87. printf("%02X ", rbuf[i]);
  88. }
  89. printf("\r\n");
  90. /* 3. Erase sector 0 (4KB) and verify all bytes = 0xFF */
  91. Int_flash_erase_sector(0);
  92. Int_flash_read(rbuf, 0, 256);
  93. pass = 1;
  94. for (i = 0; i < 256; i++)
  95. {
  96. if (rbuf[i] != 0xFF)
  97. {
  98. pass = 0;
  99. break;
  100. }
  101. }
  102. printf("Sector0 erase verify: %s\r\n", pass ? "PASS" : "FAIL");
  103. /* 4. Write + readback within a single page (no cross-page) */
  104. for (i = 0; i < 128; i++)
  105. {
  106. wbuf[i] = (uint8_t)i;
  107. }
  108. Int_flash_write(wbuf, 0, 128);
  109. Int_flash_read(rbuf, 0, 128);
  110. pass = 1;
  111. for (i = 0; i < 128; i++)
  112. {
  113. if (rbuf[i] != wbuf[i])
  114. {
  115. pass = 0;
  116. break;
  117. }
  118. }
  119. printf("Write/Read 128B (page 0): %s\r\n", pass ? "PASS" : "FAIL");
  120. /* 5. Write spanning page boundary (page 0 ends at addr 255) */
  121. for (i = 0; i < 64; i++)
  122. {
  123. wbuf[i] = (uint8_t)(0xA0 + i);
  124. }
  125. Int_flash_write(wbuf, 200, 64); /* spans address 200..263 (crosses page 0/1) */
  126. Int_flash_read(rbuf, 200, 64);
  127. pass = 1;
  128. for (i = 0; i < 64; i++)
  129. {
  130. if (rbuf[i] != wbuf[i])
  131. {
  132. pass = 0;
  133. break;
  134. }
  135. }
  136. printf("Write/Read 64B (cross-page): %s\r\n", pass ? "PASS" : "FAIL");
  137. /* 6. Write spanning sector boundary (sector0 lasts 0..4095) */
  138. for (i = 0; i < 32; i++)
  139. {
  140. wbuf[i] = (uint8_t)(0x50 + i);
  141. }
  142. Int_flash_write(wbuf, 4080, 32); /* spans address 4080..4111 (crosses sector 0/1) */
  143. Int_flash_read(rbuf, 4080, 32);
  144. pass = 1;
  145. for (i = 0; i < 32; i++)
  146. {
  147. if (rbuf[i] != wbuf[i])
  148. {
  149. pass = 0;
  150. break;
  151. }
  152. }
  153. printf("Write/Read 32B (cross-sector): %s\r\n", pass ? "PASS" : "FAIL");
  154. /* 7. Rewrite same location after erase (address already erased to 0xFF) */
  155. for (i = 0; i < 32; i++)
  156. {
  157. wbuf[i] = (uint8_t)(0xC0 + i);
  158. }
  159. Int_flash_write(wbuf, 16, 32);
  160. Int_flash_read(rbuf, 16, 32);
  161. pass = 1;
  162. for (i = 0; i < 32; i++)
  163. {
  164. if (rbuf[i] != wbuf[i])
  165. {
  166. pass = 0;
  167. break;
  168. }
  169. }
  170. printf("Write/Read 32B (re-write sector0): %s\r\n", pass ? "PASS" : "FAIL");
  171. /* 8. Write full 256B page and read back */
  172. for (i = 0; i < 256; i++)
  173. {
  174. wbuf[i] = (uint8_t)(i ^ 0x5A);
  175. }
  176. Int_flash_write(wbuf, 4096, 256); /* sector 1 start */
  177. Int_flash_read(rbuf, 4096, 256);
  178. pass = 1;
  179. for (i = 0; i < 256; i++)
  180. {
  181. if (rbuf[i] != wbuf[i])
  182. {
  183. pass = 0;
  184. break;
  185. }
  186. }
  187. printf("Write/Read 256B page: %s\r\n", pass ? "PASS" : "FAIL");
  188. /* 9. Block erase (64KB) test on block 1 */
  189. Int_flash_erase_block(1);
  190. Int_flash_read(rbuf, 0x10000, 64);
  191. pass = 1;
  192. for (i = 0; i < 64; i++)
  193. {
  194. if (rbuf[i] != 0xFF)
  195. {
  196. pass = 0;
  197. break;
  198. }
  199. }
  200. printf("Block1 erase verify: %s\r\n", pass ? "PASS" : "FAIL");
  201. printf("======== QSPI FLASH TEST DONE ========\r\n\r\n");
  202. }
  203. void Int_eeprom_test(void)
  204. {
  205. uint8_t wbuf[64];
  206. uint8_t rbuf[64];
  207. uint16_t i;
  208. uint8_t pass;
  209. printf("\r\n======== EEPROM TEST (BL24C16F) ========\r\n");
  210. /* 1. 初始化并检测芯片应答 */
  211. if (Int_eeprom_init() != 0)
  212. {
  213. printf("[FAIL] Int_eeprom_init\r\n");
  214. return;
  215. }
  216. printf("EEPROM present (I2C2 @ 0x%02X)\r\n", INT_EEPROM_ADDR);
  217. /* 2. 单字节写入+回读:地址 0x000 */
  218. wbuf[0] = 0xAA;
  219. Int_eeprom_write(wbuf, 0x000, 1);
  220. Int_eeprom_read(rbuf, 0x000, 1);
  221. printf("Byte write/read (addr 0x000): %s\r\n",
  222. (rbuf[0] == 0xAA) ? "PASS" : "FAIL");
  223. /* 3. 页写入+回读:地址 0x010(页对齐,16 字节) */
  224. for (i = 0; i < 16; i++)
  225. {
  226. wbuf[i] = (uint8_t)i;
  227. }
  228. Int_eeprom_write(wbuf, 0x010, 16);
  229. Int_eeprom_read(rbuf, 0x010, 16);
  230. pass = 1;
  231. for (i = 0; i < 16; i++)
  232. {
  233. if (rbuf[i] != wbuf[i])
  234. {
  235. pass = 0;
  236. break;
  237. }
  238. }
  239. printf("Page write/read (page1): %s\r\n", pass ? "PASS" : "FAIL");
  240. /* 4. 跨页写入+回读:地址 0x00C 写 32 字节(跨页 0/1/2) */
  241. for (i = 0; i < 32; i++)
  242. {
  243. wbuf[i] = (uint8_t)(0x50 + i);
  244. }
  245. Int_eeprom_write(wbuf, 0x00C, 32);
  246. Int_eeprom_read(rbuf, 0x00C, 32);
  247. pass = 1;
  248. for (i = 0; i < 32; i++)
  249. {
  250. if (rbuf[i] != wbuf[i])
  251. {
  252. pass = 0;
  253. break;
  254. }
  255. }
  256. printf("Cross-page write/read: %s\r\n", pass ? "PASS" : "FAIL");
  257. /* 5. 全片擦除后回读验证全 0xFF */
  258. Int_eeprom_erase_chip();
  259. Int_eeprom_read(rbuf, 0x000, 16);
  260. pass = 1;
  261. for (i = 0; i < 16; i++)
  262. {
  263. if (rbuf[i] != 0xFF)
  264. {
  265. pass = 0;
  266. break;
  267. }
  268. }
  269. printf("Chip erase verify: %s\r\n", pass ? "PASS" : "FAIL");
  270. /* 6. 最后一页边界:地址 0x7F0 写 16 字节 */
  271. for (i = 0; i < 16; i++)
  272. {
  273. wbuf[i] = (uint8_t)(0xC0 + i);
  274. }
  275. Int_eeprom_write(wbuf, 0x7F0, 16);
  276. Int_eeprom_read(rbuf, 0x7F0, 16);
  277. pass = 1;
  278. for (i = 0; i < 16; i++)
  279. {
  280. if (rbuf[i] != wbuf[i])
  281. {
  282. pass = 0;
  283. break;
  284. }
  285. }
  286. printf("Last page write/read (addr 0x7F0): %s\r\n", pass ? "PASS" : "FAIL");
  287. printf("======== EEPROM TEST DONE ========\r\n\r\n");
  288. }
  289. /* USER CODE END 0 */
  290. /**
  291. * @brief The application entry point.
  292. * @retval int
  293. */
  294. int main(void)
  295. {
  296. /* USER CODE BEGIN 1 */
  297. /* USER CODE END 1 */
  298. /* MPU Configuration--------------------------------------------------------*/
  299. MPU_Config();
  300. /* Enable the CPU Cache */
  301. /* Enable I-Cache---------------------------------------------------------*/
  302. SCB_EnableICache();
  303. /* Enable D-Cache---------------------------------------------------------*/
  304. SCB_EnableDCache();
  305. /* MCU Configuration--------------------------------------------------------*/
  306. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  307. HAL_Init();
  308. /* USER CODE BEGIN Init */
  309. /* USER CODE END Init */
  310. /* Configure the system clock */
  311. SystemClock_Config();
  312. /* USER CODE BEGIN SysInit */
  313. /* USER CODE END SysInit */
  314. /* Initialize all configured peripherals */
  315. MX_GPIO_Init();
  316. MX_FMC_Init();
  317. MX_QUADSPI_Init();
  318. MX_USART1_UART_Init();
  319. MX_I2C2_Init();
  320. MX_LTDC_Init();
  321. /* USER CODE BEGIN 2 */
  322. printf("hello h743\r\n");
  323. Int_sdram_init();
  324. Int_sdram_test();
  325. // Int_flash_test();
  326. Int_eeprom_test();
  327. Int_lcd_init();
  328. Int_lcd_clear(LCD_COLOR_WHITE);
  329. printf("System Init OK\r\n");
  330. /* USER CODE END 2 */
  331. /* Infinite loop */
  332. /* USER CODE BEGIN WHILE */
  333. while (1)
  334. {
  335. /* USER CODE END WHILE */
  336. /* USER CODE BEGIN 3 */
  337. Int_lcd_clear(LCD_COLOR_WHITE);
  338. HAL_Delay(1000);
  339. }
  340. /* USER CODE END 3 */
  341. }
  342. /**
  343. * @brief System Clock Configuration
  344. * @retval None
  345. */
  346. void SystemClock_Config(void)
  347. {
  348. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  349. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  350. /** Supply configuration update enable
  351. */
  352. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  353. /** Configure the main internal regulator output voltage
  354. */
  355. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  356. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  357. /** Initializes the RCC Oscillators according to the specified parameters
  358. * in the RCC_OscInitTypeDef structure.
  359. */
  360. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  361. RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  362. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  363. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  364. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  365. RCC_OscInitStruct.PLL.PLLM = 4;
  366. RCC_OscInitStruct.PLL.PLLN = 60;
  367. RCC_OscInitStruct.PLL.PLLP = 2;
  368. RCC_OscInitStruct.PLL.PLLQ = 2;
  369. RCC_OscInitStruct.PLL.PLLR = 2;
  370. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  371. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  372. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  373. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  374. {
  375. Error_Handler();
  376. }
  377. /** Initializes the CPU, AHB and APB buses clocks
  378. */
  379. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  380. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  381. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  382. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  383. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  384. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  385. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  386. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  387. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  388. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  389. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  390. {
  391. Error_Handler();
  392. }
  393. }
  394. /* USER CODE BEGIN 4 */
  395. /* USER CODE END 4 */
  396. /* MPU Configuration */
  397. void MPU_Config(void)
  398. {
  399. /* Disables the MPU */
  400. HAL_MPU_Disable();
  401. /* Enables the MPU */
  402. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  403. }
  404. /**
  405. * @brief This function is executed in case of error occurrence.
  406. * @retval None
  407. */
  408. void Error_Handler(void)
  409. {
  410. /* USER CODE BEGIN Error_Handler_Debug */
  411. /* User can add his own implementation to report the HAL error return state */
  412. __disable_irq();
  413. while (1)
  414. {
  415. }
  416. /* USER CODE END Error_Handler_Debug */
  417. }
  418. #ifdef USE_FULL_ASSERT
  419. /**
  420. * @brief Reports the name of the source file and the source line number
  421. * where the assert_param error has occurred.
  422. * @param file: pointer to the source file name
  423. * @param line: assert_param error line source number
  424. * @retval None
  425. */
  426. void assert_failed(uint8_t *file, uint32_t line)
  427. {
  428. /* USER CODE BEGIN 6 */
  429. /* User can add his own implementation to report the file name and line number,
  430. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  431. /* USER CODE END 6 */
  432. }
  433. #endif /* USE_FULL_ASSERT */