Skip to content

Instantly share code, notes, and snippets.

@randrews
Last active February 14, 2021 20:30
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 randrews/1e0c03f1883d61069040264f9be931d6 to your computer and use it in GitHub Desktop.
Save randrews/1e0c03f1883d61069040264f9be931d6 to your computer and use it in GitHub Desktop.
#!/home/randrews/.rbenv/shims/ruby
# WARNING! Remove head completely from rectum before operating!
# This program requires ruby, rubygems, and the serialport gem. Ensure
# that the above shebang line is correct, and that the following two
# requires will work:
require 'rubygems'
require 'serialport'
# Shut up serialport's irrelevant warnings in Ruby 2.7:
require 'stringio'
$stderr = StringIO.new
filename = nil
dest = 'ram'
device = '/dev/ttyUSB0'
if ARGV.length == 0 || ARGV.length > 3
puts 'Usage: fsend <filename> [dest = ram / flash] [device = /dev/ttyUSB0]'
exit(0)
end
filename = ARGV[0] if ARGV.length > 0
dest = ARGV[1] if ARGV.length > 1
device = ARGV[2] if ARGV.length > 2
port = nil
begin
port = SerialPort.new(device, 115200)
rescue
puts "Unable to open serial device #{device}"
exit(1)
end
begin
File.open(filename) do |f|
if dest == 'ram'
port.write("compiletoram\n")
elsif dest == 'flash'
port.write("compiletoflash\n")
else
puts "Unrecognized dest '#{dest}'"
exit(1)
end
port.readline
f.each_with_index do |line, idx|
port.write(line)
response = port.readline
unless response =~ /ok\.$/
puts "On line #{idx+1}:"
puts response
exit(2)
end
end
port.write("compiletoram\n")
port.readline
end
rescue Errno::ENOENT
puts "Unable to open #{filename}"
exit(1)
end
$40010800 constant A
$40010C00 constant B
$40011000 constant C
: CRL ( port -- addr ) ;
: CRH ( port -- addr ) $4 + ;
: IDR ( port -- addr ) $8 + ;
: ODR ( port -- addr ) $C + ;
$4 constant IN
$8 constant PULL
$3 constant OUT
: mode? ( port pin -- mode )
dup 8 < if swap CRL
else 8 - swap CRH then @ swap
4 * rshift $f and ;
: mode! ( port pin mode -- )
-rot 2dup mode? ( mode port pin currentMode )
-rot 2swap xor -rot ( mask port pin )
dup 8 < if swap CRL
else 8 - swap CRH then ( mask pin reg )
-rot 4 * lshift swap xor! ;
: write ( port pin value -- )
rot ODR -rot ( reg pin value )
$1 rot lshift swap ( reg mask value )
if swap bis! else swap bic! then ;
: read ( port pin -- value )
swap IDR @ swap rshift $1 and ;
: led-modeset C #13 OUT mode! ;
: led-on C #13 1 write ;
: led-off C #13 0 write ;
\ examples:
\ C 13 OUT mode!
\ A 3 read
\ C 13 1 write
\ C 1 mode?
0 variable vPort
0 variable vMiso
0 variable vMosi
0 variable vClk
0 variable vCs
: clk vPort @ vClk @ 1 write ;
: ~clk vPort @ vClk @ 0 write ;
: cs vPort @ vCs @ 1 write ;
: ~cs vPort @ vCs @ 0 write ;
: mosi ( val -- ) vPort @ vMosi @ rot write ;
: miso ( -- val ) vPort @ vMiso @ read ;
: init-spi ( port miso mosi clk cs -- )
vCs ! vClk ! vMosi ! vMiso ! vPort !
vPort @ vCs @ OUT mode!
vPort @ vClk @ OUT mode!
vPort @ vMosi @ OUT mode!
vPort @ vMiso @ IN mode!
cs ~clk ;
: spi-bit
dup $80 and mosi
#1 lshift
clk
miso if #1 or then
~clk ;
: spi ( sent -- recv )
~clk \ It does weird things with nested loops and running out of ram, so:
spi-bit spi-bit spi-bit spi-bit spi-bit spi-bit spi-bit spi-bit ;
: spi, spi , ;
\ Examples:
\ a11 miso, a12 mosi, a2 clk, a7 cs
\ A #11 #12 #2 #7 init-spi
\ Talking to an SPI SRAM:
: unpack-addr ( 24bitAddr -- hh mm ll )
dup $ff and swap
dup $ff00 and 8 rshift swap
$ff0000 and #16 rshift ;
: ramread ( 24bitaddr -- byte )
~cs
$3 spi, \ read cmd
unpack-addr spi, spi, spi, \ address, high byte first
0 spi \ get the byte
cs ;
: rammr
~cs
$5 spi ,
0 spi cs ;
: ramwrite ( value 24bitaddr -- )
~cs
$2 spi,
unpack-addr spi, spi, spi, \ address, high byte first
spi, \ write the byte
cs ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment