Community
cancel
Showing results for 
Search instead for 
Did you mean: 
bchav
Novice
2,153 Views

Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?

Hi,

From past 2 weeks myself try to run my developed standard Linux application for SPI slave sensor using Galileo Gen 2 SPI interface.

I Already perform PinMuxing for the SPI interface as well as verify the signals on the oscilloscope as well.

I was trying to read the manufacturing ID of slave sensor using following standard logic.

So for that I have write 0x03 on SPI bus for write register and I expect the 0x54 as read Data but I got 0x00 or 0xFF.

Please Help me out here...

struct spi_ioc_transfer xfer[2];

unsigned char buf[32];

buf[0] = 0x03;

xfer[0].tx_buf = (unsigned long)buf;

xfer[0].len = 1;

xfer[1].rx_buf = (unsigned long) buf;

xfer[1].len = 6;

status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);

if (status < 0)

{

perror("SPI_IOC_MESSAGE");

exit(EXIT_FAILURE);

}

20 Replies
idata
Community Manager
371 Views

Hello BhoomsC,

 

 

If you are having issues with SPI using spidev, I'd suggest you to try with mraa. I've been able to try some simple examples of SPI successfully by using the mraa library. Have you tried to use mraa in your project? In case you haven't, you can find examples of how to use it in:

 

 

https://github.com/intel-iot-devkit/mraa/blob/master/examples/spi_max7219.c https://github.com/intel-iot-devkit/mraa/blob/master/examples/spi_max7219.c

 

https://github.com/intel-iot-devkit/mraa/blob/master/examples/spi_mcp4261.c https://github.com/intel-iot-devkit/mraa/blob/master/examples/spi_mcp4261.c

 

 

Peter.
bchav
Novice
371 Views

intel_corp

There are a some requirement at my end to use standard spidev APIs of Linux.

Is there any other solution instead mraa lib?

asss
Valued Contributor II
371 Views

Hi,

1. may you provide a full code (how you initialize SPI dev)?

2. what sensor do you use?

3. are you sure that need to do 2 transactions like shown in your code?

BR,

xbolshe

bchav
Novice
371 Views

Hi xbolshe,

1) here is the test app which I have run on Galileo Gen 2 board with WindRiver IDP 2.0 Linux.

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# define SPEED 4000000

int main(int argc, char **argv)

{

char *name;

int fd;

struct spi_ioc_transfer xfer[2];

unsigned char buf[32], *bp;

int len, status;

name = argv[1];

fd = open(name, O_RDWR);

if (fd < 0) {

perror("open");

exit(EXIT_FAILURE);

}

memset(xfer, 0, sizeof xfer);

memset(buf, 0, sizeof buf);

len = sizeof buf;

__u8 mode, lsb, bits;

__u32 speed_hz = SPEED;

if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0)

{

perror("SPI rd_mode");

exit(EXIT_FAILURE);

}

if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0)

{

perror("SPI rd_lsb_fist");

exit(EXIT_FAILURE);

}

if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)

{

perror("SPI bits_per_word");

exit(EXIT_FAILURE);

}

if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz)<0)

{

perror("can't set max speed hz");

exit(EXIT_FAILURE);

}

if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed_hz) < 0)

{

perror("SPI max_speed_hz");

exit(EXIT_FAILURE);

}

printf("spi mode:%d, %d bits %sper word, %d Hz max\n",mode, bits, lsb ? "(lsb first) " : "", speed_hz);

/*

* Send a GetID command

*/

buf[0] = 0x03;

len = 6;

xfer[0].tx_buf = (unsigned long)buf;

xfer[0].len = 1;

xfer[1].rx_buf = (unsigned long) buf;

xfer[1].len = 6;

status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);

if (status < 0) {

perror("SPI_IOC_MESSAGE");

exit(EXIT_FAILURE);

}

printf("response(%d): ", status);

for (bp = buf; len; len--)

printf("%02x ", *bp++);

printf("\n");

return 0;

}

2) I'm using Microchip TC72 Digital Temperature Sensor with SPI™ Interface

The following snap will provide detail information, what I actually doing is just read manufacring ID.

3) Two transaction required because I need to perform write followed by read mechanism,

For reading value of register I need to write that register address over SPI bus, So It will get back with register value in the second transaction.

Thanks

asss
Valued Contributor II
371 Views

Hi,

how did you connect TC72 to Galileo from a hardware point of view?

May you provide a scheme?

BR,

xbolshe

bchav
Novice
371 Views

So, for SS pin I have changed boot args at grub.conf file.

Append following line,

intel_qrk_plat_galileo_gen2.gpio_cs=1

For MOSI pin I have doing GPIO44 and GPIO72 as HIGH and LOW respectively.

For SCK pin I have doing GPIO46 as HIGH.

For MISO we don't have PinMux-1 and PinMux-2 so I dont perform any activity for MISO pin.

Actually osciiloscope detect the 4MHz SCK and SCK singals at the time of application execution.

Thanks

asss
Valued Contributor II
371 Views

A SPI bus usually (like in Gen2 case) operates with an inverted SS pin.

But a TC72 sensor requires to use a direct Chip Enable pin.

I may expect that TC72 does not recieve a command in your case because of a wrong signal on CE pin of TC72.

May you check it?

BR,

xbolshe

bchav
Novice
371 Views

Ohh,

You mean, In my case the CS should be HIGH???

I just refer the timing diagram in the data sheet, the CS goes from LOW to HIGH duing data transaction.

In most of the case during transaction CS goes from HIGH to LOW.

Let me see...

Thank you so much xbolshe

asss
Valued Contributor II
371 Views

Yes, I do.

BR,

xbolshe

bchav
Novice
371 Views

Actually Now I'm stuck whether I put some logic invert IC

or

change the driver code to make the CE high from Galileo Gen 2 board and Minnowboard Turbot.

asss
Valued Contributor II
371 Views

I guess 3rd method is better:

you may set SPI mode ORed with SPI_CS_HIGH.

It is supported by Gen2. And it may fix the problem.

BR,

xbolshe

bchav
Novice
371 Views

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# include

# define SPEED 4000000

int spifd;

int spi_write(uint8_t data)

{

struct spi_ioc_transfer msg;

memset(&msg, 0, sizeof(msg));

__u32 length = 1;

unsigned long recv = 0;

msg.tx_buf = (unsigned long) &data;

msg.rx_buf = (unsigned long) &recv;

msg.speed_hz = SPEED;

msg.bits_per_word = 8;

msg.delay_usecs = 0;

msg.len = length;

msg.cs_change = 1;

if (ioctl(spifd, SPI_IOC_MESSAGE(1), &msg) < 0) {

perror("spi: Failed to perform dev transfer");

return -1;

}

return (int) recv;

}

int main(int argc, char *argv[])

{

__u8 mode, lsb, bits;

__u32 speed=SPEED;

char *name;

name = argv[1];

spifd = open(name, O_RDWR);

if (spifd < 0) {

perror("open");

exit(EXIT_FAILURE);

}

///////////////

// Verifications

///////////////

//possible modes: mode |= SPI_LOOP; mode |= SPI_CPHA; mode |= SPI_CPOL; mode |= SPI_LSB_FIRST; mode |= SPI_CS_HIGH; mode |= SPI_3WIRE; mode |= SPI_NO_CS; mode |= SPI_READY;

//multiple possibilities using |

mode |= SPI_CS_HIGH;

mode |= SPI_MODE_0;

if (ioctl(spifd, SPI_IOC_WR_MODE, &mode) < 0)

{

perror("can't set spi mode");

exit(EXIT_FAILURE);

}

if (ioctl(spifd, SPI_IOC_RD_MODE, &mode) < 0)

{

perror("SPI rd_mode");

exit(EXIT_FAILURE);

}

if (ioctl(spifd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0)

{

perror("SPI rd_lsb_fist");

exit(EXIT_FAILURE);

}

/*

if (ioctl(spifd, SPI_IOC_WR_BITS_PER_WORD, 8)<0)

{

perror("can't set bits per word");

exit(EXIT_FAILURE);

}

*/

if (ioctl(spifd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)

{

perror("SPI bits_per_word");

exit(EXIT_FAILURE);

}

if (ioctl(spifd, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0)

{

perror("can't set max speed hz");

exit(EXIT_FAILURE);

}

if (ioctl(spifd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)

{

perror("SPI max_speed_hz");

exit(EXIT_FAILURE);

}

printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n",argv[0], mode, bits, lsb ? "(lsb first) " : "", speed);

int miso_data;

miso_data = spi_write(0x03);

if(miso_data == -1)

{

printf("Fail to get MISO line data\r\n");

exit(EXIT_FAILURE);

}

else

{

printf("the miso line output is %x\r\n",miso_data);

}

return 0;

}

The above code is failed execute with "can't set spi mode" perror at SPI_IOC_WR_MODE command on Galileo Gen 2 Board

But the same code is execute without any error on Minnowboard MAX but on Minnowboard MAX the CS signal is remain same...not change to active HIGH.

I'm Using WindRiver IDP 2.0 build Linux. Do you think that there is driver limitation at myend?

Still I'm not able to get TC72 sensor ID.

asss
Valued Contributor II
371 Views

Hi,

1. are you sure that a 'mode' value was zero during initialization?

2. do you have the same error without SPI_CS_HIGH?

The driver limitation is possible. I did not check WindRiver IDP 2.0 build Linux as well as Minnowboard MAX.

BR,

xbolshe

bchav
Novice
371 Views

I just initialize the mode var to 0 and error will be gone but problem remain same still I get 0xFF from the MISO line on both boards.

As far as Galileo Gen 2 is concern I think there is one more dependency and that is PinMuxing.

For making PIN # 10,# 11,# 12 and # 13 as SPI module specific we have to configure Pin properly but from the PinMux table there is no any PinMux GPIO for MISO pin. Should I use https://intel-openport-v8.hosted.jivesoftware.com/thread/55920 Galileo Gen-2 Spidev1.0 |Intel Communities as PinConfig? actually it seems bit confusing script. Have you try it ever before that script or making your own?

And for Minnowboard Max, we did not have to perform PinMux, so PinMuxing dependency is not the cause of SPI data transfer fail.

I think I need to purchase CS active low line based SPI sensor and verify this thing. What you say?

asss
Valued Contributor II
371 Views

1. Have you tried with disabled intel_qrk_plat_galileo_gen2.gpio_cs=1 ?

2. Are you sure that all pins/pin mux were initialized correctly?

BR,

xbolshe

bchav
Novice
371 Views

Yup I have tried with disable gpio_cs also.

No I'm not sure about PinMux for all four Pins.

asss
Valued Contributor II
371 Views

1. What is happened when MOSI and MISO are connected with a wire without a sensor?

2. What is an output of a command below after your program execution?

cat /sys/kernel/debug/gpio

BR,

xbolshe

bchav
Novice
371 Views

hello xbolshe

Its been long time...

there are some another work I had in past days, but from today I'm working with this issue.

Can you tell me about this PinMuxing,

I have seen the source of mraa lib and make following script to make pinmuxing for SPI bus.

-------------------------------------------------------------------------------------------------------------------------------------

# !/bin/bash

# IO10 - CS Pin

# Total PinMuxing is 3

# Sysfs GPIOs are 27,74,26

echo 27 > /sys/class/gpio/export # pullup gpio

echo 74 > /sys/class/gpio/export # pinmux-1 gpio

echo 26 > /sys/class/gpio/export # level shifter gpio

echo in > /sys/class/gpio/gpio27/direction

echo 0 > /sys/class/gpio/gpio74/direction

echo out > /sys/class/gpio/gpio26/direction

echo 0 > /sys/class/gpio/gpio26/value

# IO11 - MOSI Pin

# Total PinMuxng is 4

# Sysfs GPIOs are 25,72,44,24

echo 25 > /sys/class/gpio/export # pullup gpio

echo 72 > /sys/class/gpio/export # pinmux-2 gpio

echo 44 > /sys/class/gpio/export # pinmux-1 gpio

echo 24 > /sys/class/gpio/export # level shifter gpio

echo in > /sys/class/gpio/gpio25/direction

echo 0 > /sys/class/gpio/gpio72/direction

echo out > /sys/class/gpio/gpio44/direction

echo 1 > /sys/class/gpio/gpio44/value

echo out > /sys/class/gpio/gpio24/direction

echo 0 > /sys/class/gpio/gpio24/value

# IO12 - MISO Pin

# Total PinMuxing is 2

# Sysfs GPIOs are 43,42

echo 43 > /sys/class/gpio/export # pullup gpio

echo 42 > /sys/class/gpio/export # level shifter gpio

echo in > /sys/class/gpio/gpio43/direction

echo out > /sys/class/gpio/gpio42/direction

echo 1 > /sys/class/gpio/gpio42/value

# IO13 - SCLK Pin

# Total PinMuxing is 3

# Sysfs GPIOs are 31,46,30

echo 31 > /sys/class/gpio/export # pullup gpio

echo 46 > /sys/class/gpio/export # pinmux-1 gpio

echo 30 > /sys/class/gpio/export # level shifter gpio

echo in > /sys/class/gpio/gpio31/direction

echo out > /sys/class/gpio/gpio46/direction

echo 1 > /sys/class/gpio/gpio46/value

echo out > /sys/class/gpio/gpio30/direction

echo 0 > /sys/class/gpio/gpio30/value

-------------------------------------------------------------------------------------------------------------------------------------

Warm Regards,

Bhoomil C.

asss
Valued Contributor II
371 Views

bchav
Novice
131 Views

xbolshe,

The pinMuxing and code are working perfectly at my end.

Thanks for your supoort.

We can say that the Topic is now closed.

Reply