-->
Page 1 of 2

LUA SPI (master) example?

PostPosted: Sat Jan 31, 2015 6:43 pm
by tz1
I have nodemcu updated with the spi functions, but there is not yet any example. A simple, even trivial one that would send and receive some bytes from any peripheral would be welcome (I specifically need master mode).

But I also need wiring and need to know if I have to do something with the GPIO pin settings before I use the SPI pins (and which pins - yes, I've seen the data sheet, but it would help to confirm which is HSPI v.s. SPI, which one will I be talking to with the call).

Also, can I just do spi.recv(id,N) without doing any transmits (right now I just need to receive)? And if I need to transmit and receive at the same time, can it be done and how does it work?

There really isn't any explanation of what the functions actually do in the wiki yet. But one complete working example with the hardware and software would make it easy to understand.

Re: LUA SPI (master) example?

PostPosted: Sun Feb 01, 2015 11:38 am
by elevation
I also have some difficulties to understand how SPI connection should be made so I would appreciate examples and schematics.

//Mattias

Re: LUA SPI (master) example?

PostPosted: Mon Feb 02, 2015 6:09 am
by papagenic
hello,
I would also be interested to see an exemple of SPI usage.

Re: LUA SPI (master) example?

PostPosted: Wed Feb 04, 2015 11:16 am
by elevation
Had some time to play around and seems to be able to talk to a CC1101 now with the HSPI module. I have to control he CS myself, not sure if it should be possible to just do a send and the ESP8266 should have handled it! My connection looks like this:

GPIO12 - MISO
GPIO13 - MOSI
GPIO14 - CLK
GPIO15 - CS (with an 10k pulldown to ground so the ESP8266 will boot up)

And the code I've tested so far is listed below. After I have setup the SPI interface i reconfigure GPIO15 as normal GPIO-pin and control it manually. This code intitalize the CC1101 and then i write 56 to address 0x00, and I can read the same value back!

Code: Select allpin=8
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0);
gpio.mode(pin, gpio.OUTPUT)

gpio.write(pin, gpio.HIGH)
tmr.delay(30)
gpio.write(pin, gpio.LOW)
tmr.delay(30)
gpio.write(pin, gpio.HIGH)
tmr.delay(45)

gpio.write(pin, gpio.LOW)
spi.send(1,0x30)
gpio.write(pin, gpio.HIGH)
tmr.delay(100)

gpio.write(pin, gpio.LOW)
spi.send(1,0x00)
spi.send(1,56)
gpio.write(pin, gpio.HIGH)
tmr.delay(100)

gpio.write(pin, gpio.LOW)
spi.send(1,0x80)
read = spi.recv(1, 1)
print(string.byte(read))
gpio.write(pin, gpio.HIGH)
tmr.delay(100)