summaryrefslogtreecommitdiff
path: root/microwire.lua
diff options
context:
space:
mode:
Diffstat (limited to 'microwire.lua')
-rw-r--r--microwire.lua20
1 files changed, 12 insertions, 8 deletions
diff --git a/microwire.lua b/microwire.lua
index e2b5654..1fc87b4 100644
--- a/microwire.lua
+++ b/microwire.lua
@@ -4,7 +4,6 @@
-- 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
@@ -14,11 +13,10 @@ databits=24
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)
+-- we will use SPI ChipSelect pin for LoadEnable pin
+microwire_LE_pin = 8; -- microwire Load Enable pin. 8 --> GPIO15
+function microwire_setup_interface ()
-- 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
@@ -31,6 +29,12 @@ function microwire_setup_interface ()
-- HSPI /CS | 8 | GPIO15
-- HSPI MOSI | 7 | GPIO13
-- HSPI MISO | 6 | GPIO12
+
+ -- Load Enable pin settings
+ -- note that it overwrite unused ChipSelect if pin=8, i.e. GPIO15
+ gpio.mode(microwire_LE_pin, gpio.OUTPUT)
+ gpio.write(microwire_LE_pin, gpio.LOW)
+
end
function microwire_send_data( data )
@@ -38,13 +42,13 @@ function microwire_send_data( data )
-- and raise Load Enable pin for a bit (at least for25 nS) at the end of transmission
-- first low LE pin
- gpio.write(microwire_sle, gpio.LOW)
+ gpio.write(microwire_LE_pin, 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)
+ gpio.write(microwire_LE_pin, gpio.HIGH)
-- but since optocoupler is limiting us we will do it for longer
local microwireLEonTime = 100; -- time is in uS
@@ -53,7 +57,7 @@ function microwire_send_data( data )
-- so we use timer but unfortunately the smallest time is 1ms
microwireLEonTime = 1; -- time is in mS for timers
microwire_tmr = tmr.create()
- microwire_tmr:register(microwireLEonTime, tmr.ALARM_SINGLE, function() gpio.write(microwire_sle, gpio.LOW) end)
+ microwire_tmr:register(microwireLEonTime, tmr.ALARM_SINGLE, function() gpio.write(microwire_LE_pin, gpio.LOW) end)
microwire_tmr:start()
end