Hi dear all,
just got stuck on the small problem:
I need to send few bytes to particular register in I2C device. It could be done easily with C-code wrapped to Nim, but why not to do it with pure Nim directly?
Here is my test proc:
proc i2c_write(busNo, devAddress, regAddress: int; data: seq[int]) : int =
let i2cPortFile = open("/dev/i2c-" & $busNo, fmWrite)
let dataToSend = map(@[devAddress, regAddress] & data, proc(x:int):uint8 = x.uint8)
result = i2cPortFile.writeBytes(dataToSend, 0, len(dataToSend))
i2cPortFile.close()
it compiles fine but does not work. Any ideas?
Is the int actually int32 or int64? Depend on your arch, you need to cast to seq[uint8] instead of converting to uint8.
Maybe it should be like this (untested):
import sequtils
proc i2c_write(busNo, devAddress, regAddress: int64; data: seq[int64]) : int =
let i2cPortFile = open("/dev/i2c-" & $busNo, fmWrite)
let dataToSend = foldr(map(@[devAddress, regAddress] & data,
proc(x:int64):seq[uint8] = @cast[array[8, uint8]](x)),
concat(a, b))
result = i2cPortFile.writeBytes(dataToSend, 0, len(dataToSend))
i2cPortFile.close()
dxb, totally agreed, that is how it works in c-lib I found
well, now the question is how to call IOCTL from Nim directly...
This is how I implement it.
https://github.com/centurysys/msp430_writer/blob/master/src/lib/i2c.nim
let res = posix.ioctl(self.fd.getFileHandle, I2C_RDWR, addr packets)
Yes, but in order to put MSP430 into BSL mode, we need to control the RESET/TEST pins, so it is specific to a particular board.