0

Hello I am devolping a Qt app for a imx6 freescale microprocessor. I am using qtcreator 4.2.1. I need to use the SPI module of imx6. At firs I build a 3.14 kernel version and I made a program bassed of spidev_test example. My program is:

First SPI configuration:

int ret = 0;
device = "/dev/spidev0.0";
bits = 8;
speed = 4500000;
mode = 0;
delay = 0;

fd = open(device, O_RDWR);
if (fd < 0){
    pabort("can't open device");
}
else{
    qDebug("Open device");
}

/*
 * spi mode
 */
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1){
    pabort("can't set spi mode");
}

ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1){
    pabort("can't get spi mode");
}
/*
 * bits per word
 */
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1){
    pabort("can't set bits per word");
}

ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1){
    pabort("can't get bits per word");
}
/*
 * max speed hz
 */
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1){
    pabort("can't set max speed hz");
}
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1){
    pabort("can't get max speed hz");
}
qDebug("spi mode: %d\n", mode);
qDebug("bits per word: %d\n", bits);
qDebug("max speed: %d Hz (%d KHz)\n", speed, speed/1000);

Then the transfer function:

void SpiCom::transfer(int fd, unsigned char word){

int ret;
uint8_t rx[] = {0, };

spi_ioc_transfer tr;
unsigned char data[] = {word};
tr.tx_buf = (unsigned long)data;
tr.rx_buf = (unsigned long)rx;
tr.len = 1;
tr.delay_usecs = delay;
tr.speed_hz = speed;
tr.bits_per_word = bits;

ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
    pabort("can't send spi message");

dataReaded = (unsigned char)rx[0];

}

With 3.14 version kernel my program works well but I had to update to a 4.9 kernel version and then my program doesn't work. When I want to transfer data always show "can't send spi message" message error. If I compile this part of program with gcc compiler works but when I use qt compiler and execute the app It shows message error. I am searching for everywhere but I couldn't find nothing. If someone can help me.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Emmanuel
  • 11
  • 2
  • 1
    "It shows message error." What error, exactly? – Lundin Aug 15 '18 at 15:05
  • Possibly the evil casts `(unsigned long)data` could be the culprit. It is non-portable code. `__u64` is expected and a pointer is not necessarily representable as `unsigned long`. Use `__u64` or `uint64_t` instead. In addition, I suppose the compiler may not realize that the contents of those buffers have been updated unless you voltatile-qualify them. – Lundin Aug 15 '18 at 15:11
  • Thank you for your help. The error message is: "can't send SPI message: Invalid argument". I've changed (unsigned long) by __u64 as you said, but I've got the same error. When youy said that I need to volatile-qualify, what do you mean exactly? I understand that I need to put (volatile __u64) Is that correct? Thank you very much in advance! – Emmanuel Aug 15 '18 at 15:34
  • That's sounds like an application output. Simply place a breakpoint at the line and examine all variables involved? – Lundin Aug 16 '18 at 06:24
  • I could resolve It adding memset(&tr, 0, sizeof(tr)); after spi_ioc_transfer tr; – Emmanuel Sep 10 '18 at 14:52

0 Answers0