Skip to content

Instantly share code, notes, and snippets.

@yamori813
Last active December 11, 2016 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamori813/40efce7ddd4d0b0bb9da557a0367a8a1 to your computer and use it in GitHub Desktop.
Save yamori813/40efce7ddd4d0b0bb9da557a0367a8a1 to your computer and use it in GitHub Desktop.
# http://dlnware.com/theory/SPI-Transfer-Modes
$sck = 7
$cs = 1
$mosi = 3
$miso = 4
def txbyte(t, ch)
for num in 0..7 do
if (ch & (0x80 >> num)) == 0 then
t.set($mosi,0)
p 0
else
t.set($mosi,1)
p 1
end
t.set($sck,1)
usleep(10)
t.set($sck,0)
usleep(10)
end
end
def rxbyte(t)
for num in 0..7 do
t.set($sck,1)
p t.get($miso)
usleep(10)
t.set($sck,0)
usleep(10)
end
end
t = BsdGpio.new(0)
t.setflags($cs, BsdGpio::OUTPUT)
t.setflags($mosi, BsdGpio::OUTPUT)
t.setflags($miso, BsdGpio::INPUT)
t.setflags($sck, BsdGpio::OUTPUT)
t.set($sck,0)
usleep(10)
t.set($sck,1)
usleep(10)
t.set($cs,0)
usleep(10)
t.set($sck,0)
usleep(10)
txbyte(t, 3)
txbyte(t, 0)
rxbyte(t)
t.set($cs,1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment