1 2 Previous Next 20 Replies Latest reply on Jul 10, 2016 11:45 PM by BhoomsC

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

    BhoomsC

      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);
      }
      
        • 1. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          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_mcp4261.c
           
          Peter.

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

            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?

            • 3. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
              xbolshe

              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

              • 4. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                BhoomsC

                Hi xbolshe,

                 

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

                 

                #include <stdio.h>
                #include <sys/types.h>
                #include <sys/stat.h>
                #include <fcntl.h>
                #include <unistd.h>
                #include <sys/ioctl.h>
                #include <linux/types.h>
                #include <linux/spi/spidev.h>
                #include <stdint.h>
                #include <stdio.h>
                #include <stdlib.h>
                #include <string.h>
                #include <errno.h>
                
                
                #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.

                register.PNG

                 

                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

                • 5. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                  xbolshe

                  Hi,

                   

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

                  May you provide a scheme?

                   

                  BR,

                  xbolshe

                  • 6. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                    BhoomsC

                    tc72.PNG

                     

                    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

                    • 7. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                      xbolshe

                         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

                      • 8. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                        BhoomsC

                        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

                        • 10. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                          BhoomsC

                          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.

                          • 11. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                            xbolshe

                            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

                            • 12. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                              BhoomsC
                              #include <stdio.h>
                              #include <sys/types.h>
                              #include <sys/stat.h>
                              #include <fcntl.h>
                              #include <unistd.h>
                              #include <sys/ioctl.h>
                              #include <linux/types.h>
                              #include <linux/spi/spidev.h>
                              #include <stdint.h>
                              #include <stdio.h>
                              #include <string.h>
                              #include <errno.h>
                              #include <stdlib.h>
                              
                              #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.

                              • 13. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                                xbolshe

                                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

                                • 14. Re: Can anybody get the success to activate the SPI interface of Galileo Gen 2 using Linux application/spidev based?
                                  BhoomsC

                                  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 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?

                                  1 2 Previous Next