2 -- LuaTools需要PROJECT和VERSION这两个信息
3 PROJECT
= "w25q_spi_demo"
12 local cspin
= gpio
.setup(cs
, 1)
15 local function sendRecv(data
,len
)
18 if data
then spi
.send(spiId
,data
) end
19 if len
then r
= spi
.recv(spiId
,len
) end
25 sys
.taskInit(function()
27 local result
= spi
.setup(
34 -- spi.MSB,--高低位顺序 可选,默认高位在前
35 -- spi.master,--主模式 可选,默认主
36 -- spi.full--全双工 可选,默认全双工
39 if result
~= 0 then--返回值为0,表示打开成功
40 print("spi open error",result
)
45 local chip
= sendRecv(string.char(0x9f),3)
46 if chip
== string.char(0xef,0x40,0x16) then
47 log.info("spi", "chip id read ok 0xef,0x40,0x16")
49 log.info("spi", "chip id read error")
56 local data
= "test data 123456"
59 sendRecv(string.char(0x06))
62 sendRecv(string.char(0x02,0x00,0x00,0x01)..data
)
63 log.info("spi","write",data
)
65 sys
.wait(500)--等写入操作完成
68 local r
= sendRecv(string.char(0x03,0x00,0x00,0x01),data
:len())
69 log.info("spi","read",r
)
72 sendRecv(string.char(0x04))