common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. #include "common.h"
  2. #include "flash.h"
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. #include "ff.h"
  6. #include "integer.h"
  7. #include "diskio.h"
  8. //#include "stm32_eval.h"
  9. #include "string.h"
  10. void edit_file(void)
  11. {
  12. FATFS fs;
  13. FIL file;
  14. FRESULT res;
  15. DIR dirs;
  16. FILINFO finfo;
  17. char key = 0;
  18. char path[20];
  19. uint32_t index = 0x00;
  20. uint32_t reindex = 0x00;
  21. uint8_t file_buff[512] = {0};
  22. uint32_t files_num = 0;
  23. uint8_t length = 0;
  24. res = f_mount(0,&fs);
  25. if (res != FR_OK)
  26. {
  27. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  28. return;
  29. }
  30. res = f_opendir(&dirs,"/");
  31. printf("\r\n------------文件列表------------");
  32. if (res == FR_OK)
  33. {
  34. while ((f_readdir(&dirs, &finfo) == FR_OK) && finfo.fname[0])
  35. {
  36. if (finfo.fattrib & AM_DIR)//如果是文件夹
  37. {
  38. continue;
  39. }
  40. else
  41. {
  42. files_num++;
  43. //显示文件名,显示文件实际大小 ,文件实际大小采用四舍五入法
  44. printf("\r\n/%12s%7ld KB ", &finfo.fname[0],(finfo.fsize+512)/1024);
  45. }
  46. }
  47. if( files_num == 0 )//无文件
  48. {
  49. printf("\r\n无文件!");
  50. }
  51. }
  52. else
  53. {
  54. printf("\r\n打开根目录失败!");
  55. printf("\r\n错误代码: %u",res);
  56. }
  57. printf("\r\n输入要编辑文件全名,以回车结束...");
  58. get_file_name((uint8_t *)path,length,FI);
  59. res = f_open(&file,path,FA_READ | FA_WRITE);
  60. if (res == FR_OK)
  61. {
  62. printf("\r\n打开文件 %s 成功",path);
  63. printf("\r\n现在是文件编辑状态,请输入要写入的数据!");
  64. printf("\r\n按ESC或者Ctrl+C结束编辑!\r\n");
  65. while(1)
  66. {
  67. key = GetKey();
  68. if ((key == 0x1B) && (index == 0x00))//key ESC
  69. {
  70. printf("\r\n数据还没有输入,现在处于编辑模式...");
  71. continue;
  72. }
  73. else if ((key == 0x1B)) //key ESC
  74. {
  75. printf("\r\n保存数据...");
  76. res = f_write(&file,file_buff,index,&reindex);
  77. if ((res == FR_OK) && (reindex == index))
  78. {
  79. printf("\r\n保存数据成功!");
  80. f_close(&file);
  81. index = 0x00;
  82. reindex = 0x00;
  83. }
  84. else
  85. {
  86. printf("\r\n保存数据失败!");
  87. printf("\r\n错误代码: %u",res);
  88. }
  89. break;
  90. }
  91. else if (key == 0x03) //key Ctrl+C
  92. {
  93. printf("\r\n结束文件编辑!");
  94. printf("\r\n保存数据...");
  95. res = f_write(&file,file_buff,index,&reindex);
  96. if ((res == FR_OK) && (reindex == index))
  97. {
  98. printf("\r\n保存数据成功!");
  99. f_close(&file);
  100. index = 0x00;
  101. reindex = 0x00;
  102. }
  103. else
  104. {
  105. printf("\r\n保存数据失败!");
  106. printf("\r\n错误代码: %u",res);
  107. }
  108. break;
  109. }
  110. else if ((key < 0x21) || (key > 0x80))
  111. {
  112. continue;
  113. }
  114. else
  115. {
  116. file_buff[index++] = key;
  117. printf("%c",key);
  118. if (index > 512)
  119. {
  120. index = 0x00;
  121. }
  122. }
  123. }
  124. }
  125. else
  126. {
  127. printf("\r\n打开文件失败,错误代码: %u",res);
  128. }
  129. }
  130. void read_file(void)
  131. {
  132. FATFS fs;
  133. FIL file;
  134. FRESULT res;
  135. DIR dirs;
  136. FILINFO finfo;
  137. char path[20];
  138. char buffer[512] = {0};
  139. uint32_t i;
  140. uint8_t length=0;
  141. uint32_t re,files_num = 0;
  142. res = f_mount(0,&fs);
  143. if (res != FR_OK)
  144. {
  145. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  146. return;
  147. }
  148. res = f_opendir(&dirs,"/");
  149. if (res == FR_OK)
  150. {
  151. //i = strlen(path);
  152. printf("\r\n-----------文件列表-------");
  153. while ((f_readdir(&dirs, &finfo) == FR_OK) && finfo.fname[0])
  154. {
  155. if (finfo.fattrib & AM_DIR)//如果是文件夹
  156. {
  157. continue;
  158. }
  159. else
  160. {
  161. files_num++;
  162. //显示文件名,显示文件实际大小 ,文件实际大小采用四舍五入法
  163. printf("\r\n/%12s%7ld KB ", &finfo.fname[0],(finfo.fsize+512)/1024);
  164. }
  165. }
  166. if( files_num == 0 )//无文件
  167. {
  168. printf("\r\n无文件,请返回先创建文件!");
  169. return;
  170. }
  171. }
  172. else
  173. {
  174. printf("\r\n打开根目录失败!");
  175. printf("\r\n错误代码: %u",res);
  176. }
  177. printf("\r\n输入文件全名,以回车结束...");
  178. get_file_name((uint8_t *)path,length,FI);
  179. res = f_open(&file,path,FA_READ);
  180. printf("\r\n正在打开文件,以下是文件数据:\r\n");
  181. if (res == FR_OK)
  182. {
  183. while (1)
  184. {
  185. for(i = 0;i < 512;i++)
  186. {
  187. buffer[i] = 0x00;
  188. }
  189. res = f_read(&file,buffer,512,&re);
  190. printf("%s",buffer);
  191. if (res || re == 0)
  192. {
  193. printf("\r\n文件读取结束,关闭文件!");
  194. f_close(&file);
  195. break;
  196. }
  197. }
  198. }
  199. f_mount(0,NULL);
  200. }
  201. void creat_dir(void)
  202. {
  203. FATFS fs;
  204. FRESULT res;
  205. char path[20];
  206. uint8_t length=0;
  207. res = f_mount(0,&fs);
  208. if (res != FR_OK)
  209. {
  210. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  211. return;
  212. }
  213. printf("\r\n请输入文件夹名,回车确认...格式 8 + 3...");
  214. get_file_name((uint8_t *)path,length,DI);
  215. res = f_mkdir(path);
  216. if (res == FR_OK)
  217. {
  218. printf("\r\n创建文件目录成功!");
  219. }
  220. else
  221. {
  222. printf("\r\n创建目录失败...");
  223. printf("\r\n错误代码: %u",res);
  224. }
  225. f_mount(0,NULL);
  226. }
  227. void get_file_name(uint8_t *file_name,uint8_t length,uint8_t type)
  228. {
  229. uint8_t key;
  230. uint8_t name_leng = 0;
  231. printf("\r\n");
  232. while (1)
  233. {
  234. key = GetKey();
  235. if ((key == 13) && (name_leng == 0))
  236. {
  237. printf("\r\n");
  238. continue;
  239. }
  240. else if ((key == 0x2F) || (key == 0x5C))
  241. {
  242. printf("%c",key);
  243. continue;
  244. }
  245. else if ((key == 13) && (name_leng > 0))
  246. {
  247. printf("\r\n");
  248. if (type == FI)
  249. {
  250. if (check_file_name(file_name,name_leng) == 0)
  251. {
  252. break;
  253. }
  254. }
  255. else
  256. {
  257. break;
  258. }
  259. }
  260. else
  261. {
  262. printf("%c",key);
  263. file_name[name_leng] = key;
  264. name_leng++;
  265. if (name_leng > 12)
  266. {
  267. printf("\r\n文件名格式: 8 + 3,只支持8个字符,3个扩展名!");
  268. printf("\r\n请重新输入...");
  269. name_leng = 0;
  270. continue;
  271. }
  272. }
  273. }
  274. }
  275. void format_disk(void)
  276. {
  277. FATFS fs;
  278. uint8_t res;
  279. res = f_mount(0,&fs);
  280. if (res != FR_OK)
  281. {
  282. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  283. return;
  284. }
  285. printf("\r\n正在格式化磁盘,请稍候...");
  286. res = f_mkfs(0,1,4096);
  287. if (res == FR_OK)
  288. {
  289. printf("\r\n格式化成功...");
  290. }
  291. else
  292. {
  293. printf("\r\n格式化失败...");
  294. printf("\r\n错误代码: %u\r\n",res);
  295. }
  296. f_mount(0,NULL);
  297. }
  298. void creat_file(void)
  299. {
  300. FIL file;
  301. FIL *pf = &file;
  302. FATFS fs;
  303. uint8_t res;
  304. uint8_t name[16] = {0};
  305. uint8_t length = 0;
  306. printf("\r\n请输入文件名,回车确认...格式 8 + 3...");
  307. printf("\r\n例:123.dat\r\n");
  308. get_file_name(name,length,FI);
  309. res = f_mount(0,&fs); /* Mount a Logical Drive 0 */
  310. if (res != FR_OK)
  311. {
  312. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  313. return;
  314. }
  315. res = f_open(pf,(TCHAR *)name,FA_READ | FA_WRITE | FA_CREATE_NEW);
  316. if (res == FR_OK)
  317. {
  318. printf("\r\n创建文件成功!");
  319. res = f_close(pf);
  320. if (res != FR_OK)
  321. {
  322. printf("\r\n创建文件成功,但关闭文件时,失败!");
  323. printf("\r\n错误代码: %u",res);
  324. }
  325. }
  326. else
  327. {
  328. printf("\r\n创建文件失败!");
  329. printf("\r\n错误代码: %u",res);
  330. }
  331. f_mount(0,NULL);
  332. }
  333. void delete_file(void)
  334. {
  335. FATFS fs;
  336. FRESULT res;
  337. DIR dirs;
  338. FILINFO finfo;
  339. uint8_t name[16] = {0};
  340. uint8_t length = 0;
  341. uint32_t files_num = 0;
  342. res = f_mount(0,&fs); /* Mount a Logical Drive 0*/
  343. if (res != FR_OK)
  344. {
  345. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  346. return;
  347. }
  348. res = f_opendir(&dirs,"/"); /* Create a Directroy Object */
  349. if (res == FR_OK)
  350. {
  351. printf("\r\n-----------文件列表-------");
  352. while ((f_readdir(&dirs, &finfo) == FR_OK) && finfo.fname[0])
  353. {
  354. if (finfo.fattrib & AM_DIR)//如果是文件夹
  355. {
  356. continue;
  357. }
  358. else
  359. {
  360. files_num++;
  361. //显示文件名,显示文件实际大小 ,文件实际大小采用四舍五入法
  362. printf("\r\n/%12s%7ld KB ", &finfo.fname[0],(finfo.fsize+512)/1024);
  363. }
  364. }
  365. if( files_num == 0 )//无文件
  366. {
  367. printf("\r\n无文件,请返回先创建文件!");
  368. return;
  369. }
  370. }
  371. get_file_name(name,length,FI); /* Get file name */
  372. res = f_unlink((TCHAR *)name); /* Delete a File or Directory */
  373. if (res == FR_OK)
  374. {
  375. printf("\r\n删除文件成功!");
  376. }
  377. else if (res == FR_NO_FILE)
  378. {
  379. printf("\r\n找不到文件或目录!");
  380. }
  381. else if (res == FR_NO_PATH)
  382. {
  383. printf("\r\n找不到路径!");
  384. }
  385. else
  386. {
  387. printf("\r\n错误代码: %u",res);
  388. }
  389. f_mount(0,NULL);
  390. }
  391. void list_file(void)
  392. {
  393. FATFS fs;
  394. FILINFO finfo;
  395. FRESULT res;
  396. DIR dirs;
  397. int i;
  398. int files_num=0;
  399. res = f_mount(0,&fs);
  400. if (res != FR_OK)
  401. {
  402. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  403. return;
  404. }
  405. res = f_opendir(&dirs,"/"); /* Create a Directroy Object */
  406. printf("\r\n------------文件列表------------");
  407. if (res == FR_OK)
  408. {
  409. while ((f_readdir(&dirs, &finfo) == FR_OK) && finfo.fname[0]) /* Read Directory Entry in Sequense*/
  410. {
  411. i = strlen(finfo.fname);
  412. if (finfo.fattrib & AM_DIR)//如果是文件夹
  413. {
  414. files_num++;
  415. printf("\r\n/%s", &finfo.fname[0]);
  416. switch(i)//作用:输出文件名左对齐
  417. {
  418. case 1:printf(" ");
  419. case 2:printf(" ");
  420. case 3:printf(" ");
  421. case 4:printf(" ");
  422. case 5:printf(" ");
  423. case 6:printf(" ");
  424. case 7:printf(" ");
  425. case 8:printf("%15s"," ");
  426. }
  427. }
  428. else
  429. {
  430. continue;
  431. }
  432. }
  433. }
  434. else
  435. {
  436. printf("\r\n打开根目录失败!");
  437. printf("\r\n错误代码: %u",res);
  438. }
  439. res = f_opendir(&dirs, "/");
  440. if (res == FR_OK)
  441. {
  442. while ((f_readdir(&dirs, &finfo) == FR_OK) && finfo.fname[0])
  443. {
  444. if (finfo.fattrib & AM_DIR)
  445. {
  446. continue;
  447. }
  448. else
  449. {
  450. files_num++;
  451. printf("\r\n/.%12s%7ld KB ", &finfo.fname[0],(finfo.fsize+512)/1024);
  452. }
  453. }
  454. if( files_num==0 )//无文件
  455. {
  456. printf("\r\n无文件!");
  457. }
  458. }
  459. else
  460. {
  461. printf("\r\n打开根目录失败!");
  462. printf("\r\n错误代码: %u",res);
  463. }
  464. f_mount(0,NULL);
  465. }
  466. void get_disk_info(void)
  467. {
  468. FATFS fs;
  469. FATFS *fls = &fs;
  470. FRESULT res;
  471. DWORD fre_clust;
  472. res = f_mount(0,&fs); /* Mount a Logical Drive 0 */
  473. if (res != FR_OK)
  474. {
  475. printf("\r\n挂载文件系统失败,错误代码: %u",res);
  476. return;
  477. }
  478. res = f_getfree("/",&fre_clust,&fls); /* Get Number of Free Clusters */
  479. if (res == FR_OK)
  480. {
  481. /* Print free space in unit of MB (assuming 4096 bytes/sector) */
  482. printf("\r\n%d KB Total Drive Space.\r\n"
  483. "%d KB Available Space.\r\n",
  484. ((fls->n_fatent-2)*fls->csize)*4,(fre_clust*fls->csize)*4);
  485. }
  486. else
  487. {
  488. printf("\r\n获得磁盘信息失败!");
  489. printf("\r\n错误代码: %u",res);
  490. }
  491. f_mount(0,NULL); /*Unmount a Logical Drive 0 */
  492. }
  493. uint8_t check_file_name(uint8_t *file_name,uint8_t length)
  494. {
  495. uint8_t res;
  496. if (length > 13)
  497. {
  498. res = 1;
  499. }
  500. else
  501. {
  502. if (file_name[length - 4] == '.')
  503. {
  504. res = 0;
  505. }
  506. else
  507. {
  508. res = 2;
  509. }
  510. }
  511. return res;
  512. }
  513. void SerialPutChar(uint8_t c)
  514. {
  515. USART_SendData(USART1, c);
  516. while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  517. }
  518. void Serial_PutString(uint8_t *s)
  519. {
  520. while (*s != '\0')
  521. {
  522. SerialPutChar(*s);
  523. s ++;
  524. }
  525. }
  526. /**
  527. * @brief Test to see if a key has been pressed on the HyperTerminal
  528. * @param key: The key pressed
  529. * @retval 1: Correct
  530. * 0: Error
  531. */
  532. uint32_t SerialKeyPressed(uint8_t *key)
  533. {
  534. if ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
  535. {
  536. *key = (uint8_t)USART1->DR;
  537. return 1;
  538. }
  539. else
  540. {
  541. return 0;
  542. }
  543. }
  544. /**
  545. * @brief Get a key from the HyperTerminal
  546. * @param None
  547. * @retval The Key Pressed
  548. */
  549. uint8_t GetKey(void)
  550. {
  551. uint8_t key = 0;
  552. /* Waiting for user input */
  553. while (1)
  554. {
  555. if (SerialKeyPressed((uint8_t*)&key)) break;
  556. }
  557. return key;
  558. }
  559. void Sys_Soft_Reset(void)
  560. {
  561. SCB->AIRCR =0X05FA0000|(u32)0x04;
  562. }
  563. #if 0
  564. void GPIO_KEY_Config(void)
  565. {
  566. GPIO_InitTypeDef GPIO_InitStructure;
  567. /* Configure KEY1 Button */
  568. RCC_APB2PeriphClockCmd(RCC_KEY1, ENABLE);
  569. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  570. GPIO_InitStructure.GPIO_Pin = GPIO_KEY1;
  571. GPIO_Init(GPIO_KEY1_PORT, &GPIO_InitStructure);
  572. /* Configure KEY2 Button */
  573. RCC_APB2PeriphClockCmd(RCC_KEY2, ENABLE);
  574. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  575. GPIO_InitStructure.GPIO_Pin = GPIO_KEY2;
  576. GPIO_Init(GPIO_KEY2_PORT, &GPIO_InitStructure);
  577. /* Configure KEY3 Button */
  578. RCC_APB2PeriphClockCmd(RCC_KEY3, ENABLE);
  579. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  580. GPIO_InitStructure.GPIO_Pin = GPIO_KEY3;
  581. GPIO_Init(GPIO_KEY3_PORT, &GPIO_InitStructure);
  582. /* Configure KEY4 Button */
  583. RCC_APB2PeriphClockCmd(RCC_KEY4, ENABLE);
  584. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  585. GPIO_InitStructure.GPIO_Pin = GPIO_KEY4;
  586. GPIO_Init(GPIO_KEY4_PORT, &GPIO_InitStructure);
  587. }
  588. void USART1_Init(void)
  589. {
  590. USART_InitTypeDef USART_InitStructure;
  591. /* USARTx configured as follow:
  592. - BaudRate = 115200 baud
  593. - Word Length = 8 Bits
  594. - One Stop Bit
  595. - No parity
  596. - Hardware flow control disabled (RTS and CTS signals)
  597. - Receive and transmit enabled
  598. */
  599. USART_InitStructure.USART_BaudRate = 115200;
  600. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  601. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  602. USART_InitStructure.USART_Parity = USART_Parity_No;
  603. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  604. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  605. STM_EVAL_COMInit(COM1, &USART_InitStructure);
  606. }
  607. #endif
  608. int fputc(int ch, FILE *f)
  609. {
  610. USART_SendData(USART1, (u8) ch);
  611. while(!(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == SET))
  612. {
  613. }
  614. return ch;
  615. }
  616. int fgetc(FILE *f)
  617. {
  618. while(!(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET))
  619. {
  620. }
  621. return (USART_ReceiveData(USART1));
  622. }