Ruby, ioctl and complex structures

I have a piece of hardware that I am trying to control using the built-in SPI driver on my computer. The SPI driver is controlled through ioctl.

I can successfully manage equipment from a small C program; but when I try to duplicate a C program in Ruby, I run into problems.

Using IO # ioctl to set the main registers (with u32 and u8 ints) works fine (I know, because I can also use ioctl to return the values ​​I set); but as soon as I try to establish a complex structure, the program fails with

small.rb:51:in 'ioctl': Connection timed out @ rb_ioctl - /dev/spidev32766.0 (Errno::ETIMEDOUT)

I may have problems because the spi_ioc_transfer struct has two pointers to byte buffers, but pointers are entered as unsigned 64-bit ints even on 32-bit platforms, which requires casting to (unsigned long)C. I am trying to reproduce this in Ruby, but I completely unsure of myself.

Below is a C program that works, and a Ruby port that does not work. Functions are do_latchneeded so that I can see the result in my equipment; but probably not related to this problem.

C (which works):

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>

int do_latch() {
    int fd = open("/sys/class/gpio/gpio1014/value", O_RDWR);
    write(fd, "1", 1);
    write(fd, "0", 1);

    close(fd);
}

int do_transfer(int fd, uint8_t *bytes, size_t len) {
    uint8_t *rx_bytes = malloc(sizeof(uint8_t) * len);

    struct spi_ioc_transfer transfer = {
        .tx_buf = (unsigned long)bytes,
        .rx_buf = (unsigned long)rx_bytes,

        .len = len,
        .speed_hz = 100000,

        .delay_usecs = 0,
        .bits_per_word = 8,
        .cs_change = 0,
        .tx_nbits = 0,
        .rx_nbits = 0,
        .pad = 0
    };

    if(ioctl(fd, SPI_IOC_MESSAGE(1), &transfer) < 1) {
        perror("Could not send SPI message"); 
        exit(1); 
    }

    free(rx_bytes);
}

int main() {
    int fd = open("/dev/spidev32766.0", O_RDWR);

    uint8_t mode = 0;
    ioctl(fd, SPI_IOC_WR_MODE, &mode);

    uint8_t lsb_first = 0;
    ioctl(fd, SPI_IOC_WR_LSB_FIRST, lsb_first);

    uint32_t speed_hz = 100000;
    ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, speed_hz);

    size_t data_len = 36;
    uint8_t *tx_data = malloc(sizeof(uint8_t) * data_len);

    memset(tx_data, 0xFF, data_len);

    do_transfer(fd, tx_data, data_len);
    do_latch();

    sleep(2);

    memset(tx_data, 0x00, data_len);

    do_transfer(fd, tx_data, data_len);
    do_latch();

    free(tx_data);
    close(fd);
    return 0;
}

Ruby (which doesn't work on ioctl's line do_transfer):

SPI_IOC_WR_MODE = 0x40016b01
SPI_IOC_WR_LSB_FIRST = 0x40016b02
SPI_IOC_WR_BITS_PER_WORD = 0x40016b03
SPI_IOC_WR_MAX_SPEED_HZ = 0x40046b04
SPI_IOC_WR_MODE32 = 0x40046b05
SPI_IOC_MESSAGE_1 = 0x40206b00 

def do_latch()
  File.open("/sys/class/gpio/gpio1014/value", File::RDWR) do |file|
    file.write("1")
    file.write("0")
  end
end

def do_transfer(file, bytes)
  ##########################################################################################
  #begin spi_ioc_transfer struct (cat /usr/include/linux/spi/spidev.h)

    #pack bytes into a buffer; create a new buffer (filled with zeroes) for the rx
    tx_buff = bytes.pack("C*")        
    rx_buff = (Array.new(bytes.size) { 0 }).pack("C*")       

    #on 32-bit, the struct uses a zero-extended pointer for the buffers (so it the same
    #byte layout on 64-bit as well) -- so do some trickery to get the buffer addresses 
    #as 64-bit strings even though this is running on a 32-bit computer
    tx_buff_pointer = [tx_buff].pack("P").unpack("L!")[0]   #u64 (zero-extended pointer)
    rx_buff_pointer = [rx_buff].pack("P").unpack("L!")[0]   #u64 (zero-extended pointer)

    buff_len = bytes.size                                   #u32
    speed_hz = 100000                                       #u32

    delay_usecs = 0                                         #u16
    bits_per_word = 8                                       #u8
    cs_change = 0                                           #u8
    tx_nbits = 0                                            #u8
    rx_nbits = 0                                            #u8
    pad = 0                                                 #u16

    struct_array = [tx_buff_pointer, rx_buff_pointer, buff_len, speed_hz, delay_usecs, bits_per_word, cs_change, tx_nbits, rx_nbits, pad]
    struct_packed = struct_array.pack("QQLLSCCCCS")

    #in C, I pass a pointer to the the structure; so mimic that here
    struct_pointer_packed = [struct_packed].pack("P")
  #end spi_ioc_transfer struct
  ##########################################################################################

  file.ioctl(SPI_IOC_MESSAGE_1, struct_pointer_packed)
end

File.open("/dev/spidev32766.0", File::RDWR) do |file|

  file.ioctl(SPI_IOC_WR_MODE, [0].pack("C"));
  file.ioctl(SPI_IOC_WR_LSB_FIRST, [0].pack("C"));
  file.ioctl(SPI_IOC_WR_MAX_SPEED_HZ, [0].pack("L"));

  data_bytes = Array.new(36) { 0x00 }

  do_transfer(file, data_bytes)
  do_latch()

  sleep(2) 

  data_bytes = []
  data_bytes = Array.new(36) { 0xFF }

  do_transfer(file, data_bytes)
  do_latch()

end

- , C ( C). , ; ioctl, (SPI_IOC_MESSAGE_1), , . , , , , C.

C, Ruby, , , - , . , .

, , , 32 , 64 , ( - ARM).

C ():

60200200 00000000 a8200200 00000000 24000000 40420f00 00000800 00000000

Ruby ( ):

a85da27f 00000000 08399b7f 00000000 24000000 40420f00 00000800 00000000

, , Ruby? - , ?

- C FFI Ruby. ; ioctl , - .

Update

,

    struct_array = [tx_buff_pointer, rx_buff_pointer, buff_len, speed_hz, delay_usecs, bits_per_word, cs_change, tx_nbits, rx_nbits, pad]
    struct_packed = struct_array.pack("QQLLSCCCCS")

    #in C, I pass a pointer to the the structure; so mimic that here
    struct_pointer_packed = [struct_packed].pack("P")
    file.ioctl(SPI_IOC_MESSAGE_1, struct_pointer_packed)

C. , !

    struct_array = [tx_buff_pointer, rx_buff_pointer, buff_len, speed_hz, delay_usecs, bits_per_word, cs_change, tx_nbits, rx_nbits, pad]
    struct_packed = struct_array.pack("QQLLSCCCCS")

    file.ioctl(SPI_IOC_MESSAGE_1, struct_packed)

, Ruby , ?

, . , , . .

+4
1

, .

Flush: ios ( , Ruby, ).

rb_io_flush(VALUE io)
{
    return rb_io_flush_raw(io, 1);
}
+1

Source: https://habr.com/ru/post/1667169/


All Articles