main.c 11 KB

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