Skip to content

Instantly share code, notes, and snippets.

@yamori813
Last active Dec 11, 2016
Embed
What would you like to do?
# 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