diff --git a/Examples/testLED.jl b/Examples/testLED.jl index a1ca59942854ca6c4542387967468c72f4478ef0..66d634dfc9b8e6ca32f4ca63fa1810e71054baff 100644 --- a/Examples/testLED.jl +++ b/Examples/testLED.jl @@ -15,30 +15,40 @@ led4 = SysLED(4) # and adds a ref in motor and led to stream init_devices!(stream, led1, led2, led3, led4) ledon = true -for i = 1:50 - set!(led1, ledon) - set!(led2, !ledon) - set!(led3, ledon) - set!(led4, !ledon) +for i = 1:100 + put!(led1, ledon) + put!(led2, !ledon) + put!(led3, ledon) + put!(led4, !ledon) send(stream) #Sends all the outputs to the stream in one atomic call - #read(stream) #Sends request to read, reads all inputs - sleep(0.1) + #read(stream) + get(led1) + get(led2) + get(led3) + #sleep(0.1) + v1,v2,v3 = read(stream) #Sends request to read, reads all inputs for which get! was called + v1 == !v2 == v3 == ledon ? nothing : println("ledon is $ledon, but read v1, v2, v3 = $v1, $v2, $v3") ledon = !ledon end for i = 1:40 send(led1, ledon) - sleep(0.03) + #sleep(0.03) + v1 = read(led1) send(led2, ledon) - sleep(0.03) + #sleep(0.03) + v2 = read(led2) send(led3, ledon) - sleep(0.03) + #sleep(0.03) + v3 = read(led3) send(led4, ledon) - sleep(0.03) + #sleep(0.03) + v4 = read(led4) + v1 == v2 == v3 == v4 == ledon ? nothing : println("ledon is $ledon, but read v1, v2, v3, v4 = $v1, $v2, $v3, $v4") ledon = !ledon end -set!(led1, false) -set!(led2, false) -set!(led3, false) -set!(led4, false) +put!(led1, false) +put!(led2, false) +put!(led3, false) +put!(led4, false) send(stream) close(stream) #Tells BeagleBone to stop listening and close outputs diff --git a/src/BeagleBone/BeagleBone.jl b/src/BeagleBone/BeagleBone.jl index adb09efd7effe8f88f7506b93f5a13afbcbf6ce0..3b419a52643e76327b400b246b1ac825abf059f3 100644 --- a/src/BeagleBone/BeagleBone.jl +++ b/src/BeagleBone/BeagleBone.jl @@ -29,27 +29,43 @@ Parse and execute the command `cmd` """ bbparse(any) = error("Unexpected input: $any") + +function bbsend(sock, vals)#, timestamps) + serialize(sock, vals)#, (timestamps...))) +end + """ - bbparse(l::Tuple) -Parse input on the form `l=(iswrite, ndev, cmd1, cmd2, ..., cmdn)`` + bbparse(l::Tuple, sock) +Parse input on the form `l=(iswrite, ndev, cmd1, cmd2, ..., cmdn)` where if `iswrite` `cmdi = (devname, id, val)` and if not `iswrite` `cmdi = (devname, id)` + +and send back on socket (vals, timestamps) """ -function bbparse(l::Tuple) +function bbparse(l::Tuple, sock) iswrite = l[1]::Bool #True if write command, false if read ndev = l[2]::Int32 #Number of devices/commands - for i = 1:ndev - command = l[2+i]::Tuple - dev = getdev(command[1]) - if iswrite + if iswrite + for i = 1:ndev + command = l[2+i]::Tuple + dev = getdev(command[1]) write!(dev, command[2], command[3]) - else - val = read(dev, command[2]) - println("$val") - #TODO return somewhere end + return + else + #TODO fix to have at least partial type stability + vals = Array{Any,1}(ndev) + timestamps = Array{UInt64,1}(ndev) + for i = 1:ndev + command = l[2+i]::Tuple + dev = getdev(command[1]) + vals[i] = read(dev, command[2]) + timestamps[i] = UInt64(0)#time_ns() + end + bbsend(sock, (vals, timestamps)) + return end end @@ -81,8 +97,8 @@ function run_server(port=2001; debug=false) @async while isopen(sock) try l = deserialize(sock); - println("deserialize: $l") - bbparse(l) + #println("deserialize: $l") + bbparse(l, sock) catch err if !isopen(sock) && isa(err, Base.EOFError) println("Connection to server closed") diff --git a/src/BeagleBone/SysLED.jl b/src/BeagleBone/SysLED.jl index 472fd5df5bc76b67827d9cb7c256b60f194376ba..d17c360cf6e281134e253ad57d19f4fe9df62b1a 100644 --- a/src/BeagleBone/SysLED.jl +++ b/src/BeagleBone/SysLED.jl @@ -18,8 +18,8 @@ function read(::SysLED, ind::Int32, debug::Bool=false) ind ∉ [1,2,3,4] && error("Invalid SysLEND ind: $ind") filename = "/sys/class/leds/beaglebone:green:usr$(ind-1)/brightness" file = open(filename, "r") - l = readline(file) - (l != "1" && l != "0") && error("Invalid value \"$l\" read from SysLed") + l = read(file,Char) + (l != '1' && l != '0') && error("Invalid value \"$l\" read from SysLed") close(file) - return l == "1" + return l == '1' end diff --git a/src/BeagleBone/config/pins.jl b/src/BeagleBone/config/pins.jl deleted file mode 100644 index 73635976eb1ece8c5ecdb0d139e4deb605d127f4..0000000000000000000000000000000000000000 --- a/src/BeagleBone/config/pins.jl +++ /dev/null @@ -1,131 +0,0 @@ -const GPIO_AVAILABLE = [ - ("P8.11","GPIO_30"), - ("P8.12","GPIO_60"), - ("P8.13","GPIO_31"), - ("P8.14","GPIO_40"), - ("P8.15","GPIO_48"), - ("P8.16","GPIO_51"), - ("P8.17","GPIO_4"), - ("P8.18","GPIO_5"), - ("P8.19","GPIO_13"), - ("P8.20","GPIO_12"), - ("P8.21","GPIO_3"), - ("P8.22","GPIO_2"), - ("P8.23","GPIO_49"), - ("P8.24","GPIO_15"), - ("P8.25","GPIO_117"), - ("P8.26","GPIO_14"), - ("P8.27","GPIO_125"), - ("P8.28","GPIO_123"), - ("P8.29","GPIO_111"), - ("P8.30","GPIO_112"), - ("P8.31","GPIO_110"), - ("P8.41","GPIO_20"), - ("P8.42","GPIO_7"), - ("P9.3", "GPIO_38"), - ("P9.4", "GPIO_39"), - ("P9.5", "GPIO_34"), - ("P9.6", "GPIO_35"), - ("P9.7", "GPIO_66"), - ("P9.8", "GPIO_67"), - ("P9.9", "GPIO_69"), - ("P9.10","GPIO_68"), - ("P9.11","GPIO_45"), - ("P9.12","GPIO_44"), - ("P9.13","GPIO_23"), - ("P9.14","GPIO_26"), - ("P9.15","GPIO_47"), - ("P9.16","GPIO_46"), - ("P9.17","GPIO_27"), - ("P9.18","GPIO_65"), - ("P9.19","GPIO_22"), - ("P9.20","GPIO_63"), - ("P9.21","GPIO_62"), - ("P9.22","GPIO_37"), - ("P9.23","GPIO_36"), - ("P9.24","GPIO_33"), - ("P9.25","GPIO_32"), - ("P9.26","GPIO_61"), - ("P9.27","GPIO_86"), - ("P9.28","GPIO_88"), - ("P9.29","GPIO_87"), - ("P9.30","GPIO_89"), - ("P9.31","GPIO_10"), - ("P9.32","GPIO_11"), - ("P9.33","GPIO_9"), - ("P9.34","GPIO_81"), - ("P9.35","GPIO_8"), - ("P9.36","GPIO_80"), - ("P9.37","GPIO_78"), - ("P9.38","GPIO_79"), - ("P9.39","GPIO_76"), - ("P9.40","GPIO_77"), - ("P9.41","GPIO_74"), - ("P9.42","GPIO_75"), - ("P9.43","GPIO_72"), - ("P9.44","GPIO_73"), - ("P9.45","GPIO_70"), - ("P9.46","GPIO_71")] - -const PWM_AVAILABLE = [ - ("P8.14","PWM1A"), - ("P8.16","PWM1B"), - ("P8.21","PWM0B"), - ("P8.22","PWM0A"), - ("P8.28","ECAPPWM2"), - ("P8.29","PWM0B"), - ("P8.31","PWM0A"), - ("P8.42","ECAPPWM0"), - ("P9.13","PWM2B"), - ("P9.19","PWM2A"), - ("P9.34","PWM1B"), - ("P9.36","PWM1A"), - ("P9.45","PWM2A"), - ("P9.46","PWM2B")] - -const SPI_AVAILABLE = [ - ("P8.17","SPI0_CS0"), - ("P8.18","SPI0_D1"), - ("P8.21","SPI0_D0"), - ("P8.22","SPI0_SCLK"), - ("P8.28","SPI1_CS0"), - ("P8.29","SPI1_D0"), - ("P8.30","SPI1_D1"), - ("P8.31","SPI1_SCLK")] - -const UART_AVAILABLE = [ - ("P8.11","UART4_RX"), - ("P8.13","UART4_TX"), - ("P8.21","UART2_TX"), - ("P8.22","UART2_RX"), - ("P8.24","UART1_TX"), - ("P8.26","UART1_RX"), - ("P9.37","UART5_TX"), - ("P9.38","UART5_RX")] - -const I2C_AVAILABLE = [ - ("P8.17", "I2C1_SCL"), - ("P8.18", "I2C1_SDA"), - ("P8.19", "I2C2_SCL"), - ("P8.20", "I2C2_SDA")] - -const EQEP_AVAILABLE = [ - ("P8.25", "eQEP0_strobe"), - ("P8.27", "eQEP0B_in"), - ("P8.41", "eQEP0_index"), - ("P8.42", "eQEP0A_in"), - ("P9.11", "eQEP2B_in"), - ("P9.12", "eQEP2A_in"), - ("P9.15", "eQEP2_strobe"), - ("P9.16", "eQEP2_index"), - ("P9.31", "eQEP1_index"), - ("P9.32", "eQEP1_strobe"), - ("P9.33", "eQEP1B_in"), - ("P9.34", "eQEP1A_in"), -] - -const TIMER_AVAILABLE = [ - ("P9.7", "TIMER4"), - ("P9.8", "TIMER7"), - ("P9.9", "TIMER2"), - ("P9.10", "TIMER1")] diff --git a/src/BeagleBone/config/pins.yml b/src/BeagleBone/config/pins.yml new file mode 100644 index 0000000000000000000000000000000000000000..5b693c68de31a9092f8fd527b9ed63d5c85309d3 --- /dev/null +++ b/src/BeagleBone/config/pins.yml @@ -0,0 +1,127 @@ +--- +available_pins: + gpio: + P8.11: GPIO_30 + P8.12: GPIO_60 + P8.13: GPIO_31 + P8.14: GPIO_40 + P8.15: GPIO_48 + P8.16: GPIO_51 + P8.17: GPIO_4 + P8.18: GPIO_5 + P8.19: GPIO_13 + P8.20: GPIO_12 + P8.21: GPIO_3 + P8.22: GPIO_2 + P8.23: GPIO_49 + P8.24: GPIO_15 + P8.25: GPIO_117 + P8.26: GPIO_14 + P8.27: GPIO_125 + P8.28: GPIO_123 + P8.29: GPIO_111 + P8.30: GPIO_112 + P8.31: GPIO_110 + P8.41: GPIO_20 + P8.42: GPIO_7 + P9.3: GPIO_38 + P9.4: GPIO_39 + P9.5: GPIO_34 + P9.6: GPIO_35 + P9.7: GPIO_66 + P9.8: GPIO_67 + P9.9: GPIO_69 + P9.10: GPIO_68 + P9.11: GPIO_45 + P9.12: GPIO_44 + P9.13: GPIO_23 + P9.14: GPIO_26 + P9.15: GPIO_47 + P9.16: GPIO_46 + P9.17: GPIO_27 + P9.18: GPIO_65 + P9.19: GPIO_22 + P9.20: GPIO_63 + P9.21: GPIO_62 + P9.22: GPIO_37 + P9.23: GPIO_36 + P9.24: GPIO_33 + P9.25: GPIO_32 + P9.26: GPIO_61 + P9.27: GPIO_86 + P9.28: GPIO_88 + P9.29: GPIO_87 + P9.30: GPIO_89 + P9.31: GPIO_10 + P9.32: GPIO_11 + P9.33: GPIO_9 + P9.34: GPIO_81 + P9.35: GPIO_8 + P9.36: GPIO_80 + P9.37: GPIO_78 + P9.38: GPIO_79 + P9.39: GPIO_76 + P9.40: GPIO_77 + P9.41: GPIO_74 + P9.42: GPIO_75 + P9.43: GPIO_72 + P9.44: GPIO_73 + P9.45: GPIO_70 + P9.46: GPIO_71 + pwm: + P8.14: PWM1A + P8.16: PWM1B + P8.21: PWM0B + P8.22: PWM0A + P8.28: ECAPPWM2 + P8.29: PWM0B + P8.31: PWM0A + P8.42: ECAPPWM0 + P9.13: PWM2B + P9.19: PWM2A + P9.34: PWM1B + P9.36: PWM1A + P9.45: PWM2A + P9.46: PWM2B + spi: + P8.17: SPI0_CS0 + P8.18: SPI0_D1 + P8.21: SPI0_D0 + P8.22: SPI0_SCLK + P8.28: SPI1_CS0 + P8.29: SPI1_D0 + P8.30: SPI1_D1 + P8.31: SPI1_SCLK + uart: + P8.11: UART4_RX + P8.13: UART4_TX + P8.21: UART2_TX + P8.22: UART2_RX + P8.24: UART1_TX + P8.26: UART1_RX + P9.37: UART5_TX + P9.38: UART5_RX + i2c: + P8.17: I2C1_SCL + P8.18: I2C1_SDA + P8.19: I2C2_SCL + P8.20: I2C2_SDA + eqep: + P8.25: eQEP0_strobe + P8.27: eQEP0B_in + P8.41: eQEP0_index + P8.42: eQEP0A_in + P9.11: eQEP2B_in + P9.12: eQEP2A_in + P9.15: eQEP2_strobe + P9.16: eQEP2_index + P9.31: eQEP1_index + P9.32: eQEP1_strobe + P9.33: eQEP1B_in + P9.34: eQEP1A_in + timer: + P9.7: TIMER4 + P9.8: TIMER7 + P9.9: TIMER2 + P9.10: TIMER1 +... diff --git a/src/Computer/AbstractDevice.jl b/src/Computer/AbstractDevice.jl index f4d65c3907517a26fd2ebd5387d0203a67193bf0..f5c5552622e44f7ac8ad8edcceab48f1168eb57f 100644 --- a/src/Computer/AbstractDevice.jl +++ b/src/Computer/AbstractDevice.jl @@ -1,4 +1,4 @@ -export initialize, getstream, setstream!, send, set! +export initialize, getstream, setstream!, send ###### Defaults for AbstractDevice #What to do when connecting @@ -37,17 +37,18 @@ function send(dev::AbstractDevice, val) end function read(dev::AbstractDevice) cmd, stream = safe_getreadcommand(dev) - return read(stream, cmd) + vals, ts = read(stream, cmd) + return vals end -function set!(dev::AbstractDevice, val) +function put!(dev::AbstractDevice, val) cmd, stream = safe_getwritecommand(dev, val) push!(stream.sendbuffer,cmd) return end -function get(dev::AbstractDevice, val) - cmd, stream = safe_getreadcommand(dev, val) +function get(dev::AbstractDevice) + cmd, stream = safe_getreadcommand(dev) push!(stream.readbuffer,cmd) return end diff --git a/src/Computer/BeagleBoneStream.jl b/src/Computer/BeagleBoneStream.jl index ee9253d020075872e47ff79dc10d190209b6f571..d04600067554843ab993c20adaaee7a0157cdd61 100644 --- a/src/Computer/BeagleBoneStream.jl +++ b/src/Computer/BeagleBoneStream.jl @@ -34,51 +34,18 @@ function send(bbstream::BeagleBoneStream) empty!(bbstream.sendbuffer) return end -function read(comedistream::BeagleBoneStream) +#TODO know the types of outputs some way +function read(bbstream::BeagleBoneStream) ncmds = length(bbstream.readbuffer) serialize(bbstream.stream, (false, Int32(ncmds), bbstream.readbuffer...)) - #TODO wait for answer - vals = nothing empty!(bbstream.readbuffer) - return vals + vals, timestamps = deserialize(bbstream.stream) + length(vals) == ncmds || error("Wrong number of return values in $vals on request $(bbstream.readbuffer)") + #TODO Do something with timestamps + return (vals...,) #Converting from array to tuple end -# -# function send(stream::BeagleBoneStream) -# cmds = Tuple[] -# for dev in stream.devices -# val = getsetvalue(dev) -# cmd, devstream = safe_getwritecommand(dev, val) -# devstream == stream || error("Device $dev is connected to other stream $devstream") -# push!(cmds, cmd) -# end -# ncmds = Int32(length(cmds)) -# if ncmds > 0 -# allcmds = (true, ncmds, cmds...) -# println("Sending command: $allcmds") -# serialize(stream, allcmds) -# end -# return -# end -# function read(stream::BeagleBoneStream) -# cmds = Tuple[] -# for dev in stream.devices -# cmd, devstream = safe_getreadcommand(dev) -# devstream == stream || error("Device $dev is connected to other stream $devstream") -# push!(cmds, cmd) -# end -# ncmds = Int32(length(cmds)) -# if ncmds > 0 -# allcmds = (false, ncmds, cmds...) -# println("Sending command: $allcmds") -# serialize(stream, allcmds) -# #TODO save values in dev -# # update_read_vale!(dev, val) -# end -# return -# end - -#Maybe rethink a bit to support IObox +#The following are for interal use only function send(bbstream::BeagleBoneStream, cmd) allcmds = (true, Int32(1), cmd) println("Sending single command: $allcmds") @@ -89,8 +56,10 @@ function read(bbstream::BeagleBoneStream, cmd) allcmds = (false, Int32(1), cmd) println("Sending single command: $allcmds") serialize(bbstream.stream, allcmds) - #TODO get, wait for and return response - return + vals, timestamps = deserialize(bbstream.stream) + length(vals) == 1 || error("Wrong number of return values in $vals on request $cmd") + #TODO Do something with timestamps + return vals[1], timestamps[1] end function close(bbstream::BeagleBoneStream) diff --git a/src/LabConnections.jl b/src/LabConnections.jl index c564687919f676a8a6a64950235bb674f3973680..0b631489882871da15f88a7a471089ed480f6271 100644 --- a/src/LabConnections.jl +++ b/src/LabConnections.jl @@ -8,12 +8,12 @@ module LabConnections include(joinpath("BeagleBone","BeagleBone.jl")) include(joinpath("BeagleBone","precompile.jl")) println("Precompiling BB") - precompile_bb() + #precompile_bb() return end module Computer - import Base: read, send, close, get, serialize + import Base: read, send, close, get, put!, serialize println("Initializing Computer") include(joinpath("Computer","Computer.jl")) end diff --git a/test/BeagleBone/GPIO_test.jl b/test/BeagleBone/GPIO_test.jl index 90afe80d2711c605e05ca8db430eb0ea5ed6a44e..b2a2731c467c5e7b7ae1c60cdda9e88d514360b2 100644 --- a/test/BeagleBone/GPIO_test.jl +++ b/test/BeagleBone/GPIO_test.jl @@ -1,4 +1,3 @@ -include("../../src/LabConnections.jl") using LabConnections.BeagleBone import LabConnections.BeagleBone: getdev, write!, channels diff --git a/test/BeagleBone/PWM_test.jl b/test/BeagleBone/PWM_test.jl index 41a630a72eb0acd20ffe8ae0e4d1d175eb09e010..f5f35503823152e2b34bac4260d4251569ce4fc3 100644 --- a/test/BeagleBone/PWM_test.jl +++ b/test/BeagleBone/PWM_test.jl @@ -1,4 +1,3 @@ -include("../../src/LabConnections.jl") using LabConnections.BeagleBone import LabConnections.BeagleBone: getdev, write!, setup, teardown @@ -25,9 +24,8 @@ for pin in keys(pins) teardown(dev, pin) end - -println("Running second experiment on pin $(pin)...") pin = "P9.22" +println("Running second experiment on pin $(pin)...") setup(dev, pin) write!(dev, pin, 2, "1000000000") write!(dev, pin, 3, "250000000") diff --git a/test/BeagleBone/Sys_LED_test.jl b/test/BeagleBone/Sys_LED_test.jl index b52ef567d07db2899f438893656f21638d6d95b5..3a48f065a2f00ad8493501b347fb13dab5b33f07 100644 --- a/test/BeagleBone/Sys_LED_test.jl +++ b/test/BeagleBone/Sys_LED_test.jl @@ -1,4 +1,3 @@ -include("../../src/LabConnections.jl") using LabConnections.BeagleBone import LabConnections.BeagleBone: getdev, write! diff --git a/test/runtests.jl b/test/runtests.jl index 833ef7555fb22ab1285e70cce11631aafdb37510..e380b9a522b09a1a22481bbdca3da16c38c1a25f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using LabConnections using Base.Test -# write your own tests here -@test 1 == 2 +include("BeagleBone/PWM_test.jl") +include("BeagleBone/Sys_LED_test.jl") +include("BeagleBone/GPIO_test.jl")