summaryrefslogtreecommitdiff
path: root/microwire.lua
diff options
context:
space:
mode:
Diffstat (limited to 'microwire.lua')
-rw-r--r--microwire.lua56
1 files changed, 50 insertions, 6 deletions
diff --git a/microwire.lua b/microwire.lua
index e177731..993a8b5 100644
--- a/microwire.lua
+++ b/microwire.lua
@@ -2,13 +2,57 @@
-- it resembles SPI but need exrta pin which sends Load Enable (LE) pulse
-- otherwise SPI sequence is sent into 24 bit FIFO buffer
-function setup_interface ()
- -- Pins assignment
- local sda = TBA; -- microwire DATA pin
- local scl = TBA; -- microwire Clock (CLK) pin
- local sle = TBA; -- microwire Load Enable pin
+-- Pins assignment
+-- SPI Id selects which pins are used for CLK, MOSI, MISO, CS
+microwire_sle = 4; -- microwire Load Enable pin. 2 --> GPIO2
+-- lmx2487 can hadle up to 20 MHz but optocoupler is good for 240kHz
+-- so let's aim to 100 kHz
+maxMicrowireInterfaceSpeed = 100000; -- in Hz
+spiID = 1; -- HSPI in nodemcu language
+databits=24
+-- default SPI clock is 80 MHz
+fSPI = 80000000
+clock_divider = math.floor(fSPI/maxMicrowireInterfaceSpeed)
+
+function microwire_setup_interface ()
+ -- Load Enable pin settings
+ gpio.mode(microwire_sle, gpio.OUTPUT)
+ gpio.write(microwire_sle, gpio.LOW)
+
+ -- now microwire <--> SPI setup
+ -- microwire is SPI with CPOL=0 and CPHA=0 i.e. SPI mode 0 = 00 = CPOL.CPHA
+ -- see https://en.wikipedia.org/wiki/Serial_Peripheral_Interface#Mode_numbers
+ -- see https://www.totalphase.com/support/articles/200348506-Using-the-Aardvark-adapter-with-a-Microwire-device
+ -- we are setting SPI Id = 1, since Id = 0 used for communication with memory
+ spi.setup(spiID, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, databits, clock_divider)
+ -- SPI Id=1 uses following pins https://nodemcu.readthedocs.io/en/master/modules/spi
+ -- Signal | IO index | ESP8266 pin
+ -- HSPI CLK | 5 | GPIO14
+ -- HSPI /CS | 8 | GPIO15
+ -- HSPI MOSI | 7 | GPIO13
+ -- HSPI MISO | 6 | GPIO12
end
-function microwire_write_data( data )
+function microwire_send_data( data )
+ -- essentially send data via spi interface and raise Load Enable
+ -- first low LE pin
+ gpio.write(microwire_sle, gpio.LOW)
+ -- second send the data
+ local nBytes = spi.send(spiID, data)
+ -- finally raise LE pin for at least 25 ns,
+
+ gpio.write(microwire_sle, gpio.HIGH)
+ -- but since optocoupler is limiting us we will do it for longer
+ local microwireLEonTime = 100; -- in uS
+
+ -- the easiest to use tmr.delay( timeIn_uS ) but this halts everything
+ -- tmr.delay( microwireLEonTime )
+ -- so we use timer but unfortunately the smallest time is 1ms
+ microwireLEonTime = 1; -- now time is in mS
+ microwire_tmr = tmr.create()
+ microwire_tmr:register(microwireLEonTime, tmr.ALARM_SINGLE, function() gpio.write(microwire_sle, gpio.LOW) end)
+ microwire_tmr:start()
end
+microwire_setup_interface()
+