">
当前位置:   article > 正文

2020-10-24 文件系统

2020-10-24 文件系统

测试代码

  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <dfs_posix.h>
  4. #ifdef RT_USING_DFS
  5. #include <dfs_fs.h>
  6. #endif
  7. #define DBG_SECTION_NAME "MAIN"
  8. #define DBG_COLOR
  9. #define DBG_LEVEL DBG_INFO
  10. #include <rtdbg.h>
  11. int main(int argc, char *argv[])
  12. {
  13. return 0;
  14. }
  15. /* Example: nand flash mount fatfs */
  16. #ifdef RT_USING_DFS
  17. #define NAND_FLASH_NAME "nand0"
  18. #define TEST_DATA_LEN (512*1024)
  19. void thread1_entry(void *pra)
  20. {
  21. int fd, size;
  22. rt_uint8_t *data_ptr;
  23. rt_uint8_t *recv_ptr;
  24. rt_uint32_t index = 0;
  25. rt_uint32_t j = 0;
  26. rt_uint32_t file_size = 0;
  27. rt_uint32_t datasize = 0;
  28. char *file_name;
  29. int i = 0;
  30. file_name = rt_malloc(220);
  31. for(i=0;i<2;i++)
  32. {
  33. sprintf(file_name,"/file%03d.txt",i);
  34. data_ptr = rt_malloc(TEST_DATA_LEN);
  35. if (data_ptr == RT_NULL)
  36. {
  37. rt_kprintf("data ptr out of memory!\n");
  38. return;
  39. }
  40. /* prepare write data */
  41. for (index = 0; index < TEST_DATA_LEN; index ++)
  42. {
  43. data_ptr[index] = '0' + j;
  44. j++;
  45. if (j > 9)
  46. {
  47. j = 0;
  48. }
  49. }
  50. /* open "/test.txt" file with (O_WRONLY | O_CREAT | O_APPEND) mode */
  51. fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND);
  52. if (fd >= 0)
  53. {
  54. datasize = write(fd, data_ptr, TEST_DATA_LEN);
  55. if(datasize != TEST_DATA_LEN)
  56. {
  57. rt_kprintf("Write done %s.\n\n",file_name);
  58. rt_kprintf("Write over.\n");
  59. return;
  60. }
  61. datasize = write(fd, data_ptr, TEST_DATA_LEN);
  62. if(datasize != TEST_DATA_LEN)
  63. {
  64. rt_kprintf("Write done %s.\n\n",file_name);
  65. rt_kprintf("Write over.\n");
  66. return;
  67. }
  68. file_size = lseek(fd, 0, SEEK_END);
  69. rt_kprintf("**************************file size is %d.\n", file_size);
  70. close(fd);
  71. rt_kprintf("Write done %s.\n\n",file_name);
  72. }
  73. rt_free(data_ptr);
  74. }
  75. }
  76. int mnt_init(void)
  77. {
  78. rt_thread_t tid1 = RT_NULL;
  79. /* mount fatfs */
  80. if (dfs_mount(NAND_FLASH_NAME, "/", "elm", 0, 0) == 0)
  81. {
  82. rt_kprintf("Mount FatFs file system to root, Done!\n");
  83. }
  84. else
  85. {
  86. rt_kprintf("Mount FatFs file system failed.\n");
  87. return 0;
  88. }
  89. /* Test: read and write file in thread1 */
  90. tid1 = rt_thread_create("test_thread1", thread1_entry, 0, 4096, 25, 20);
  91. if (tid1 != RT_NULL)
  92. {
  93. rt_thread_startup(tid1);
  94. }
  95. return 0;
  96. }
  97. INIT_APP_EXPORT(mnt_init);
  98. #endif

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/779875
推荐阅读
相关标签
  

闽ICP备14008679号