main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2021, YICHIP Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-09-09 WSY first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <rtdbg.h>
  13. /* defined the LED pin: PA12 */
  14. #define LED_PIN (51)
  15. #define FS_PARTITION_NAME "filesystem"
  16. #ifdef BSP_USING_INTER_FLASH
  17. #include <dfs_elm.h>
  18. #include <dfs_file.h>
  19. #include <unistd.h>
  20. #include <dfs_fs.h>
  21. #include <fal.h>
  22. static void elmfs_sample(void)
  23. {
  24. fal_init();
  25. struct rt_device *flash_dev = fal_blk_device_create(FS_PARTITION_NAME);
  26. if (flash_dev == NULL)
  27. {
  28. LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
  29. }
  30. else
  31. {
  32. LOG_I("Create a block device on the %s partition of flash successful...", FS_PARTITION_NAME);
  33. }
  34. if (dfs_mkfs("elm", flash_dev->parent.name) == 0)
  35. {
  36. LOG_I("dfs_mkfs ok!\n");
  37. }
  38. else
  39. {
  40. LOG_E("dfs_mkfs err!\n");
  41. }
  42. if (dfs_mount(flash_dev->parent.name, "/", "elm", 0, 0) == 0)
  43. {
  44. LOG_I("Filesystem initialized!");
  45. }
  46. else
  47. {
  48. LOG_E("Failed to initialize filesystem!");
  49. LOG_D("You should create a filesystem on the block device first!");
  50. }
  51. struct statfs elm_stat;
  52. if (statfs("/", &elm_stat) == 0)
  53. {
  54. LOG_I("elmfat filesystem block size:0x%x,total blocks:0x%x,free blocks:0x%x\n", elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);
  55. }
  56. if (mkdir("/user", 0x777) == 0)
  57. {
  58. LOG_I("make a directory: '/user'.\n");
  59. }
  60. LOG_I("open file\n");
  61. int fd = open("/user/test.txt", O_WRONLY | O_CREAT);
  62. LOG_I("open file ok\n");
  63. char str[] = "elmfat mount";
  64. if (fd >= 0)
  65. {
  66. LOG_I("write file\n");
  67. if (write(fd, str, sizeof(str)) == sizeof(str))
  68. LOG_I("write data done.\n");
  69. close(fd);
  70. }
  71. int size;
  72. char buf[20];
  73. fd = open("/user/test.txt", O_RDONLY);
  74. if (fd >= 0)
  75. {
  76. LOG_I("read file\n");
  77. size = read(fd, buf, sizeof(buf));
  78. close(fd);
  79. if (size == sizeof(str))
  80. {
  81. LOG_I("Read data from file test.txt(size:%d):%s\n", size, buf);
  82. }
  83. }
  84. else
  85. {
  86. LOG_E("open err\n");
  87. }
  88. if (statfs("/", &elm_stat) == 0)
  89. {
  90. LOG_I("elmfat filesystem block size:0x%x,total blocks:0x%x,free blocks:0x%x\n", elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);
  91. }
  92. }
  93. #endif
  94. int main(void)
  95. {
  96. #ifdef BSP_USING_INTER_FLASH
  97. elmfs_sample();
  98. #endif
  99. int count = 1;
  100. /* set LED4 pin mode to output */
  101. rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
  102. while (count++)
  103. {
  104. rt_pin_write(LED_PIN, PIN_HIGH);
  105. rt_thread_mdelay(500);
  106. rt_pin_write(LED_PIN, PIN_LOW);
  107. rt_thread_mdelay(500);
  108. }
  109. return RT_EOK;
  110. }