main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #if 1
  8. #include <stdio.h>
  9. #include "board.h"
  10. #include "test.h"
  11. #ifdef TEST_EN
  12. #include "bsp_V8M_YY_led.h"
  13. #include "bsp_V8M_YY_pwm.h"
  14. #include "bsp_V8M_YY_adc.h"
  15. #include "hard_system.h"
  16. #include "hard_system_time.h"
  17. #include "hard_system_delay.h"
  18. #include "hard_system_timer.h"
  19. #include "hard_imu_uart3.h"
  20. #include "hard_rc_sbus.h"
  21. #include "hard_can.h"
  22. #include "hard_sbus_out.h"
  23. #include "hard_flash_gd25q16.h"
  24. #include "drv_usart.h"
  25. #include "soft_sdcard.h"
  26. #include "main.h"
  27. #include "hpm_math.h"
  28. #endif
  29. /*
  30. 1 手册:中断源优先级,有效值为 0 到 7。
  31. 2 注意:内存缓存问题 catch
  32. 如果DMA要访问​ → 必须用非缓存宏
  33. 如果多核要共享​ → 必须用非缓存宏
  34. 如果频繁被中断更新​ → 建议用非缓存宏
  35. 其他情况​ → 不用修饰,让编译器优化
  36. 3 注意配置顺序 IO-时钟-外设
  37. 4 XDMA,作为主设备连接到 AXI 系统总线 HDMA,作为主设备连接到 AHB 外设总线
  38. 当 XDMA 的 destination 为 DRAM 时,如果 burst 大于等于 16,那 destsize 必须为 64bit。
  39. DMAMUX 的输出 0∼7 连接到外设总线 DMA 控制器 HDMA,DMAMUX 的输出 8∼15 连接到系统总线 DMA 控制器 XDMA
  40. 它们都连接在统一的 DMAMUX(DMA 多路复用器)
  41. DMAMUX将所有外设的 DMA 请求(Request)统一管理,然后根据你的配置分配给 HDMA 或 XDMA 的任意空闲通道
  42. 5 注意cfg文件和一些前置的工程配置文件,可能导致编译出错 运行出错 仿真不了 重点:链接文件、yaml、板级cfg文件
  43. */
  44. uint64_t delta_time;
  45. #define PI (3.1415926f)
  46. void start_time(void)
  47. {
  48. delta_time = hpm_csr_get_core_mcycle();
  49. }
  50. uint32_t get_end_time(void)
  51. {
  52. delta_time = hpm_csr_get_core_mcycle() - delta_time;
  53. return delta_time;
  54. }
  55. float theta ;
  56. float sin_theta;
  57. static void test_hard(void)
  58. {
  59. // v8m_yy_led_test();
  60. // v8m_yy_motor_pwm_test();
  61. // v8m_yy_adc_test();
  62. // timer0_test();
  63. // cpu_delay_test();
  64. // timer1_test();
  65. // can2_test();
  66. // imu_uart3_test();
  67. // uart2_sbus_test();
  68. // system_test();
  69. // sbus_uart2_out_test();
  70. // test_gd25q16_quick();
  71. main_test_demo();
  72. while(1)
  73. {
  74. theta += PI*0.1;
  75. sin_theta = hpm_dsp_sin_f32(theta);
  76. //uart_test_main(0);
  77. board_delay_ms(200);
  78. //printf("sin theta is %f\r\n", sin_theta);
  79. }
  80. }
  81. int main(void)
  82. {
  83. board_init();
  84. float i = 9.8f;
  85. sin_theta = hpm_dsp_sin_f32(theta);
  86. //printf("hello world %f\n", i);
  87. //printf("sin theta is %f\r\n", sin_theta);
  88. test_hard();
  89. return 0;
  90. }
  91. // DMA 最大4k
  92. #endif
  93. #if 0
  94. /*
  95. * Copyright (c) 2021 HPMicro
  96. *
  97. * SPDX-License-Identifier: BSD-3-Clause
  98. *
  99. */
  100. #include "board.h"
  101. #include "hpm_sdmmc_sd.h"
  102. #include "ff.h"
  103. #include "diskio.h"
  104. FATFS s_sd_disk;
  105. FIL s_file;
  106. DIR s_dir;
  107. FRESULT fatfs_result;
  108. BYTE work[FF_MAX_SS];
  109. const TCHAR driver_num_buf[3] = { DEV_SD + '0', ':', '/' };
  110. #define TEST_DIR_NAME "hpmicro_sd_test_dir0"
  111. void show_menu(void);
  112. const char *show_error_string(FRESULT fresult);
  113. static FRESULT sd_mount_fs(void);
  114. static FRESULT sd_mkfs(void);
  115. static FRESULT sd_write_file(void);
  116. static FRESULT sd_read_file(void);
  117. static FRESULT sd_dir_test(void);
  118. static FRESULT sd_big_file_test(void);
  119. int main(void)
  120. {
  121. bool need_init_filesystem = true;
  122. board_init();
  123. show_menu();
  124. while (1) {
  125. char option = getchar();
  126. /* Before doing FATFS operation, ensure the SD card is present */
  127. DSTATUS dstatus = disk_status(DEV_SD);
  128. printf("sd state is %d\r\n", dstatus); // 找到SD卡
  129. if (dstatus == STA_NODISK) {
  130. printf("No disk in the SD slot, please insert an SD card...\n");
  131. do {
  132. dstatus = disk_status(DEV_SD);
  133. } while (dstatus == STA_NODISK);
  134. board_delay_ms(100);
  135. printf("Detected SD card, re-initialize the filesystem...\n");
  136. need_init_filesystem = true;
  137. }
  138. dstatus = disk_initialize(DEV_SD);
  139. if (dstatus != RES_OK) {
  140. printf("Failed to initialize SD disk\n");
  141. }
  142. if (need_init_filesystem) {
  143. fatfs_result = sd_mount_fs();
  144. if (fatfs_result == FR_NO_FILESYSTEM) {
  145. printf("There is no File system available, making file system...\n");
  146. fatfs_result = sd_mkfs();
  147. if (fatfs_result != FR_OK) {
  148. printf("Failed to make filesystem, cause:%s\n", show_error_string(fatfs_result));
  149. } else {
  150. need_init_filesystem = false;
  151. }
  152. }
  153. }
  154. switch (option) {
  155. case '1':
  156. fatfs_result = sd_mkfs();
  157. break;
  158. case '2':
  159. fatfs_result = sd_write_file();
  160. break;
  161. case '3':
  162. fatfs_result = sd_read_file();
  163. break;
  164. case '4':
  165. fatfs_result = sd_dir_test();
  166. break;
  167. case 's':
  168. fatfs_result = sd_big_file_test();
  169. break;
  170. default:
  171. show_menu();
  172. break;
  173. }
  174. }
  175. }
  176. void show_menu(void)
  177. {
  178. const char menu_str[] = "SD FATFS demo\n-----------------------------------\n"
  179. "1 - Format the SD card with FATFS\n"
  180. "2 - Create hello.txt\n"
  181. "3 - Read 1st line from hello.txt\n"
  182. "4 - Directory related test\n"
  183. "s - Large file write test\n"
  184. "Others - Show menu\n";
  185. printf(menu_str);
  186. }
  187. static FRESULT sd_mount_fs(void)
  188. {
  189. FRESULT fresult = f_mount(&s_sd_disk, driver_num_buf, 1);
  190. if (fresult == FR_OK) {
  191. printf("SD card has been mounted successfully\n");
  192. } else {
  193. printf("Failed to mount SD card, cause: %s\n", show_error_string(fresult));
  194. }
  195. fresult = f_chdrive(driver_num_buf);
  196. return fresult;
  197. }
  198. static FRESULT sd_mkfs(void)
  199. {
  200. printf("Formatting the SD card, depending on the SD card capacity, the formatting process may take a long time\n");
  201. FRESULT fresult = f_mkfs(driver_num_buf, NULL, work, sizeof(work));
  202. if (fresult != FR_OK) {
  203. printf("Making File system failed, cause: %s\n", show_error_string(fresult));
  204. } else {
  205. printf("Making file system is successful\n");
  206. }
  207. return fresult;
  208. }
  209. static FRESULT sd_write_file(void)
  210. {
  211. FRESULT fresult = f_open(&s_file, "readme.txt", FA_WRITE | FA_CREATE_ALWAYS);
  212. if (fresult != FR_OK) {
  213. printf("Create new file failed, cause: %d\n", show_error_string(fresult));
  214. } else {
  215. printf("Create new file successfully, status=%d\n", fresult);
  216. }
  217. char hello_str[] = "Hello, this is SD card FATFS demo\n";
  218. UINT byte_written;
  219. fresult = f_write(&s_file, hello_str, sizeof(hello_str), &byte_written);
  220. if (fresult != FR_OK) {
  221. printf("Write file failed, cause: %s\n", show_error_string(fresult));
  222. } else {
  223. printf("Write file operation is successfully\n");
  224. }
  225. f_close(&s_file);
  226. return fresult;
  227. }
  228. static FRESULT sd_read_file(void)
  229. {
  230. FRESULT fresult = f_open(&s_file, "readme.txt", FA_READ);
  231. if (fresult != FR_OK) {
  232. printf("Open file failed, cause: %s\n", show_error_string(fresult));
  233. } else {
  234. printf("Open file successfully\n");
  235. }
  236. if (fresult != FR_OK) {
  237. return fresult;
  238. }
  239. TCHAR str[100];
  240. f_gets(str, sizeof(str), &s_file);
  241. printf("%s\n", str);
  242. f_close(&s_file);
  243. return fresult;
  244. }
  245. static FRESULT sd_big_file_test(void)
  246. {
  247. FRESULT fresult = f_open(&s_file, "big_file.bin", FA_WRITE | FA_CREATE_ALWAYS);
  248. if (fresult != FR_OK) {
  249. printf("Create new file failed, cause: %s\n", show_error_string(fresult));
  250. } else {
  251. printf("Create new file successfully\n");
  252. }
  253. uint32_t write_size = 1024UL * 1024UL * 100UL;
  254. static uint8_t buf[32768];
  255. for (uint32_t i = 0; i < sizeof(buf); i++) {
  256. buf[i] = i & 0xFF;
  257. }
  258. while (write_size > 0) {
  259. UINT byte_written;
  260. fresult = f_write(&s_file, buf, sizeof(buf), &byte_written);
  261. if (fresult != FR_OK) {
  262. printf("Write file failed, cause: %s\n", show_error_string(fresult));
  263. return fresult;
  264. }
  265. write_size -= byte_written;
  266. }
  267. printf("Write file operation is successful\n");
  268. f_close(&s_file);
  269. return fresult;
  270. }
  271. static FRESULT sd_dir_test(void)
  272. {
  273. FRESULT fresult = f_mkdir(TEST_DIR_NAME);
  274. if (fresult != FR_OK) {
  275. printf("Creating new directory failed, cause: %s\n", show_error_string(fresult));
  276. } else {
  277. printf("Creating new directory succeeded\n");
  278. }
  279. fresult = f_rmdir(TEST_DIR_NAME);
  280. if (fresult != FR_OK) {
  281. printf("Removing new directory failed, cause: %s\n", show_error_string(fresult));
  282. } else {
  283. printf("Removing new directory succeeded\n");
  284. }
  285. return fresult;
  286. }
  287. const char *show_error_string(FRESULT fresult)
  288. {
  289. const char *result_str;
  290. switch (fresult) {
  291. case FR_OK:
  292. result_str = "succeeded";
  293. break;
  294. case FR_DISK_ERR:
  295. result_str = "A hard error occurred in the low level disk I/O level";
  296. break;
  297. case FR_INT_ERR:
  298. result_str = "Assertion failed";
  299. break;
  300. case FR_NOT_READY:
  301. result_str = "The physical drive cannot work";
  302. break;
  303. case FR_NO_FILE:
  304. result_str = "Could not find the file";
  305. break;
  306. case FR_NO_PATH:
  307. result_str = "Could not find the path";
  308. break;
  309. case FR_INVALID_NAME:
  310. result_str = "Tha path name format is invalid";
  311. break;
  312. case FR_DENIED:
  313. result_str = "Access denied due to prohibited access or directory full";
  314. break;
  315. case FR_EXIST:
  316. result_str = "Access denied due to prohibited access";
  317. break;
  318. case FR_INVALID_OBJECT:
  319. result_str = "The file/directory object is invalid";
  320. break;
  321. case FR_WRITE_PROTECTED:
  322. result_str = "The physical drive is write protected";
  323. break;
  324. case FR_INVALID_DRIVE:
  325. result_str = "The logical driver number is invalid";
  326. break;
  327. case FR_NOT_ENABLED:
  328. result_str = "The volume has no work area";
  329. break;
  330. case FR_NO_FILESYSTEM:
  331. result_str = "There is no valid FAT volume";
  332. break;
  333. case FR_MKFS_ABORTED:
  334. result_str = "THe f_mkfs() aborted due to any problem";
  335. break;
  336. case FR_TIMEOUT:
  337. result_str = "Could not get a grant to access the volume within defined period";
  338. break;
  339. case FR_LOCKED:
  340. result_str = "The operation is rejected according to the file sharing policy";
  341. break;
  342. case FR_NOT_ENOUGH_CORE:
  343. result_str = "LFN working buffer could not be allocated";
  344. break;
  345. case FR_TOO_MANY_OPEN_FILES:
  346. result_str = "Number of open files > FF_FS_LOCK";
  347. break;
  348. case FR_INVALID_PARAMETER:
  349. result_str = "Given parameter is invalid";
  350. break;
  351. default:
  352. result_str = "Unknown error";
  353. break;
  354. }
  355. return result_str;
  356. }
  357. #endif