summaryrefslogtreecommitdiff
path: root/microwire.lua
diff options
context:
space:
mode:
Diffstat (limited to 'microwire.lua')
-rw-r--r--microwire.lua19
1 files changed, 9 insertions, 10 deletions
diff --git a/microwire.lua b/microwire.lua
index cc98a68..d6c1ff4 100644
--- a/microwire.lua
+++ b/microwire.lua
@@ -1,28 +1,27 @@
-- functions to write registers over microwire interface
--- it resembles SPI but need exrta pin which sends Load Enable (LE) pulse
--- otherwise SPI sequence is sent into 24 bit FIFO buffer
+-- it resembles SPI but need extra pin which sends Load Enable (LE) pulse
+-- otherwise SPI sequence is sent into 24 bit FIFO latch register on chip
-- Pins assignment
-- SPI Id selects which pins are used for CLK, MOSI, MISO, CS
-spiID = 1; -- ID=1 --> HSPI in nodemcu language
-databits=24; -- LMX2487 loads 24 bits sequence per write into a register
--- default SPI clock is 80 MHz
-fSPI = 80000000
+microwire_spiID = 1; -- ID=1 --> HSPI in nodemcu language
+microwire_databits=24; -- LMX2487 loads 24 bits sequence per write into a register
-- lmx2487 can handle up to 20 MHz speed (50 nS per clock) but optocoupler is good for 240kHz
-- so let's aim for 100 kHz
-maxMicrowireInterfaceSpeed = 100000; -- in Hz
-clock_divider = math.floor(fSPI/maxMicrowireInterfaceSpeed)
+microwire_interface_speed = 100000; -- in Hz
-- we will use SPI ChipSelect pin for LoadEnable pin
microwire_LE_pin = 8; -- microwire Load Enable pin. 8 --> GPIO15
function microwire_setup_interface ()
+ local fSPI = 80000000; -- default SPI clock is 80 MHz
+ local clock_divider = math.floor(fSPI/microwire_interface_speed)
-- 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.setup(microwire_spiID, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, microwire_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
@@ -45,7 +44,7 @@ function microwire_send_data( data )
gpio.write(microwire_LE_pin, gpio.LOW)
-- second send the data
- local nBytes = spi.send(spiID, data)
+ local nBytes = spi.send(microwire_spiID, data)
-- finally raise LE pin for at least 25 ns,