main.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "fatfs"
  3. VERSION = "1.0.0"
  4. -- sys库是标配
  5. _G.sys = require("sys")
  6. --[[
  7. 接线要求:
  8. SPI 使用常规4线解法
  9. 开发板(Air105) TF模块
  10. PB3 CS
  11. PB2(SPI2_CLK) CLK
  12. PB4(SPI2_MISO) MOSI
  13. PB5(SPI2_MISO) MISO
  14. 3.3V VCC
  15. GND GND
  16. 核心要义: 找对应SPI端口的3个脚, CLK时钟, MISO和MOSI, CS脚可以选硬件默认的,也可以自选一个普通GPIO
  17. SD/TF模块请选不带电平转换的版本!!
  18. https://detail.tmall.com/item.htm?abbucket=10&id=634710962749&ns=1&spm=a21n57.1.0.0.696f523csnpBFA&skuId=4710673879054
  19. 如果是带电平转换的, SPI波特率要限制在10M或以下
  20. ]]
  21. -- 特别提醒, 由于FAT32是DOS时代的产物, 文件名超过8个字节是需要额外支持的(需要更大的ROM)
  22. -- 例如 /sd/boottime 是合法文件名, 而/sd/boot_time就不是合法文件名, 需要启用长文件名支持.
  23. local rtos_bsp = rtos.bsp()
  24. -- spi_id,pin_cs
  25. local function fatfs_spi_pin()
  26. if rtos_bsp == "AIR101" then
  27. return 0, pin.PB04
  28. elseif rtos_bsp == "AIR103" then
  29. return 0, pin.PB04
  30. elseif rtos_bsp == "AIR105" then
  31. return 2, pin.PB03
  32. elseif rtos_bsp == "ESP32C3" then
  33. return 2, 7
  34. elseif rtos_bsp == "ESP32S3" then
  35. return 2, 14
  36. elseif rtos_bsp == "EC618" then
  37. return 0, 8
  38. elseif string.find(rtos_bsp,"EC718") then
  39. return 0, 8
  40. elseif string.find(rtos_bsp,"Air810") then
  41. return 0, 3, fatfs.SDIO
  42. else
  43. log.info("main", "bsp not support")
  44. return
  45. end
  46. end
  47. sys.taskInit(function()
  48. sys.wait(1000)
  49. -- fatfs.debug(1) -- 若挂载失败,可以尝试打开调试信息,查找原因
  50. -- 此为spi方式
  51. local spi_id, pin_cs,tp = fatfs_spi_pin()
  52. if tp and tp == fatfs.SPI then
  53. -- 仅SPI方式需要自行初始化spi, sdio不需要
  54. spi.setup(spi_id, nil, 0, 0, 8, 400 * 1000)
  55. gpio.setup(pin_cs, 1)
  56. end
  57. fatfs.mount(tp or fatfs.SPI, "/sd", spi_id, pin_cs, 24 * 1000 * 1000)
  58. local data, err = fatfs.getfree("/sd")
  59. if data then
  60. log.info("fatfs", "getfree", json.encode(data))
  61. else
  62. log.info("fatfs", "err", err)
  63. end
  64. -- #################################################
  65. -- 文件操作测试
  66. -- #################################################
  67. local f = io.open("/sd/boottime", "rb")
  68. local c = 0
  69. if f then
  70. local data = f:read("*a")
  71. log.info("fs", "data", data, data:toHex())
  72. c = tonumber(data)
  73. f:close()
  74. end
  75. log.info("fs", "boot count", c)
  76. if c == nil then
  77. c = 0
  78. end
  79. c = c + 1
  80. f = io.open("/sd/boottime", "wb")
  81. if f ~= nil then
  82. log.info("fs", "write c to file", c, tostring(c))
  83. f:write(tostring(c))
  84. f:close()
  85. else
  86. log.warn("sdio", "mount not good?!")
  87. end
  88. if fs then
  89. log.info("fsstat", fs.fsstat("/"))
  90. log.info("fsstat", fs.fsstat("/sd"))
  91. end
  92. -- 测试一下追加, fix in 2021.12.21
  93. os.remove("/sd/test_a")
  94. sys.wait(50)
  95. f = io.open("/sd/test_a", "w")
  96. if f then
  97. f:write("ABC")
  98. f:close()
  99. end
  100. f = io.open("/sd/test_a", "a+")
  101. if f then
  102. f:write("def")
  103. f:close()
  104. end
  105. f = io.open("/sd/test_a", "r")
  106. if f then
  107. local data = f:read("*a")
  108. log.info("data", data, data == "ABCdef")
  109. f:close()
  110. end
  111. -- 测试一下按行读取, fix in 2022-01-16
  112. f = io.open("/sd/testline", "w")
  113. if f then
  114. f:write("abc\n")
  115. f:write("123\n")
  116. f:write("wendal\n")
  117. f:close()
  118. end
  119. sys.wait(100)
  120. f = io.open("/sd/testline", "r")
  121. if f then
  122. log.info("sdio", "line1", f:read("*l"))
  123. log.info("sdio", "line2", f:read("*l"))
  124. log.info("sdio", "line3", f:read("*l"))
  125. f:close()
  126. end
  127. -- #################################################
  128. end)
  129. -- 用户代码已结束---------------------------------------------
  130. -- 结尾总是这一句
  131. sys.run()
  132. -- sys.run()之后后面不要加任何语句!!!!!