main.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "fatfs"
  3. VERSION = "1.0.0"
  4. -- sys库是标配
  5. _G.sys = require("sys")
  6. local rtos_bsp = rtos.bsp()
  7. local USE_CH390 = true -- 使用ch390时,设置为true,否则为false
  8. -- spi_id,pin_cs
  9. local function fatfs_spi_pin()
  10. return 1, 20 -- Air8000整机开发板上的pin_cs为gpio20
  11. end
  12. local function main_task()
  13. if USE_CH390 then -- Air8000 的ch390 和 tf卡共用SPI ,ch390 如果不打开的话会让SPI 无法正常
  14. gpio.setup(140, 1, gpio.PULLUP) -- 打开ch390供电
  15. end
  16. sys.wait(1000)
  17. -- fatfs.debug(1) -- 若挂载失败,可以尝试打开调试信息,查找原因
  18. -- 此为spi方式
  19. local spi_id, pin_cs,tp = fatfs_spi_pin()
  20. -- 仅SPI方式需要自行初始化spi, sdio不需要
  21. spi.setup(spi_id, nil, 0, 0, pin_cs, 400 * 1000)
  22. gpio.setup(pin_cs, 1)
  23. fatfs.mount(fatfs.SPI, "/sd", spi_id, pin_cs, 24 * 1000 * 1000)
  24. local data, err = fatfs.getfree("/sd")
  25. if data then
  26. log.info("fatfs", "getfree", json.encode(data))
  27. else
  28. log.info("fatfs", "err", err)
  29. end
  30. -- #################################################
  31. -- 文件操作测试
  32. -- #################################################
  33. local f = io.open("/sd/boottime", "rb")
  34. local c = 0
  35. if f then
  36. local data = f:read("*a")
  37. log.info("fs", "data", data, data:toHex())
  38. c = tonumber(data)
  39. f:close()
  40. end
  41. log.info("fs", "boot count", c)
  42. if c == nil then
  43. c = 0
  44. end
  45. c = c + 1
  46. f = io.open("/sd/boottime", "wb")
  47. if f ~= nil then
  48. log.info("fs", "write c to file", c, tostring(c))
  49. f:write(tostring(c))
  50. f:close()
  51. else
  52. log.warn("sdio", "mount not good?!")
  53. end
  54. if fs then
  55. log.info("fsstat", fs.fsstat("/"))
  56. log.info("fsstat", fs.fsstat("/sd"))
  57. end
  58. -- 测试一下追加, fix in 2021.12.21
  59. os.remove("/sd/test_a")
  60. sys.wait(50)
  61. f = io.open("/sd/test_a", "w")
  62. if f then
  63. f:write("ABC")
  64. f:close()
  65. end
  66. f = io.open("/sd/test_a", "a+")
  67. if f then
  68. f:write("def")
  69. f:close()
  70. end
  71. f = io.open("/sd/test_a", "r")
  72. if f then
  73. local data = f:read("*a")
  74. log.info("data", data, data == "ABCdef")
  75. f:close()
  76. end
  77. -- 测试一下按行读取, fix in 2022-01-16
  78. f = io.open("/sd/testline", "w")
  79. if f then
  80. f:write("abc\n")
  81. f:write("123\n")
  82. f:write("wendal\n")
  83. f:close()
  84. end
  85. sys.wait(100)
  86. f = io.open("/sd/testline", "r")
  87. if f then
  88. log.info("sdio", "line1", f:read("*l"))
  89. log.info("sdio", "line2", f:read("*l"))
  90. log.info("sdio", "line3", f:read("*l"))
  91. f:close()
  92. end
  93. end
  94. sys.taskInit(main_task)
  95. -- 用户代码已结束---------------------------------------------
  96. -- 结尾总是这一句
  97. sys.run()
  98. -- sys.run()之后后面不要加任何语句!!!!!