main.c 11 KB

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