OK. I actually have a device attached to my Raspberry Pi where I can start testing the SPI routines. It’s an MCP3008 chip and I’ve tested it with some C++ code and it works great. Now, comes the Julia issues.
The C++ code (the values are from his class):
enum SPIMODE{
MODE0 = 0, //!< Low at idle, capture on rising clock edge
MODE1 = 1, //!< Low at idle, capture on falling clock edge
MODE2 = 2, //!< High at idle, capture on falling clock edge
MODE3 = 3 //!< High at idle, capture on rising clock edge
};
SPIMODE mode;
uint8_t bits;
uint32_t speed;
uint16_t delay;
int SPIDevice::setMode(SPIDevice::SPIMODE mode){
this->mode = mode;
if (ioctl(this->file, SPI_IOC_WR_MODE, &this->mode)==-1){
perror("SPI: Can't set SPI mode.");
return -1;
}
if (ioctl(this->file, SPI_IOC_RD_MODE, &this->mode)==-1){
perror("SPI: Can't get SPI mode.");
return -1;
}
return 0;
}
The Julia code:
type SPIDevice
bus::UInt8
device::UInt8
file::Int
filename::String
mode::UInt8
bits::UInt8
speed::UInt32
delay::UInt16
SPIDevice(bus, device) = new(
bus,
device,
0,
"/dev/spidev" * string(bus) * "." * string(device),
3,
8,
488000,
0)
end
function spi_setMode(spi::SPIDevice, mode::UInt8)
spi.mode = mode
print("1) spi.mode: ")
println(spi.mode)
# Set SPI_POL and SPI_PHA
ret = ccall((:ioctl, "libc"), Int, (Cint, Cuint, Ref{Cuchar}...), spi.file, SPI_IOC_WR_MODE, spi.mode)
if (ret < 0)
error("SPI (SPI_IOC_WR_MODE): Can't set SPI mode.")
return -1
end
print("2) spi.mode: ")
println(spi.mode)
ret = ccall((:ioctl, "libc"), Int, (Cint, Cuint, Ref{Cuchar}...), spi.file, SPI_IOC_RD_MODE, spi.mode)
if (ret < 0)
error("SPI (SPI_IOC_RD_MODE): Can't get SPI mode.")
return -1
end
print("3) spi.mode: ")
println(spi.mode)
return 0
end
I’m calling like:
include("spidev_h.jl")
include("spidev.jl")
import spidev
using spidev
function combineValues(upper::UInt8, lower::UInt8)
return (Int16(upper) << 8) | Int16(lower)
end
println("Starting RPi SPI ADC Example")
spi = spidev.SPIDevice(0,0)
ret = spidev.spi_open(spi)
ret = spidev.spi_setMode(spi, UInt8(SPI_MODE_0))
and here’s the results:
julia> import spidev
julia> using spidev
julia> function combineValues(upper::UInt8, lower::UInt8)
return (Int16(upper) << 8) | Int16(lower)
end
combineValues (generic function with 1 method)
julia> println("Starting RPi SPI ADC Example")
Starting RPi SPI ADC Example
julia> spi = spidev.SPIDevice(0,0)
spidev.SPIDevice(0x00, 0x00, 0, "/dev/spidev0.0", 0x03, 0x08, 0x00077240, 0x0000)
julia> ret = spidev.spi_open(spi)
0
julia> ret = spidev.spi_setMode(spi, UInt8(SPI_MODE_0))
1) spi.mode: 0
2) spi.mode: 0
ERROR: SPI (SPI_IOC_RD_MODE): Can't get SPI mode.
Stacktrace:
[1] spi_setMode(::spidev.SPIDevice, ::UInt8) at /home/julia-user/julia-0.6.0/bin/spidev.jl:91
So, this is telling me it got by the write call OK (SPI_IOC_WR_MODE):
spi.mode = mode
print("1) spi.mode: ")
println(spi.mode)
# Set SPI_POL and SPI_PHA
ret = ccall((:ioctl, "libc"), Int, (Cint, Cuint, Ref{Cuchar}...), spi.file, SPI_IOC_WR_MODE, spi.mode)
if (ret < 0)
error("SPI (SPI_IOC_WR_MODE): Can't set SPI mode.")
return -1
end
print("2) spi.mode: ")
println(spi.mode)
and, get’s a -1 back on the read call (SPI_IOC_RD_MODE):
ret = ccall((:ioctl, "libc"), Int, (Cint, Cuint, Ref{Cuchar}...), spi.file, SPI_IOC_RD_MODE, spi.mode)
Do you have any ideas?