SPI Read User Space Issue

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI Read User Space Issue

773 Views
Dhevan
Contributor IV

Hi,

     Board = IMX8MM Custom Board

     In our user space code spi accelerometer reading 14k data per second.

     SPI Clock Speed 3.5MHZ.

     Our issue is every second we not able to read 14k fixed data it will be varying (13k to 20k)

     We want to get stable 14k data per second.

     Please suggest to get stable data count in user space code.

     Note : Microcontroller (imxrt 1064) we can able to read stable count data

Regards,

 Vasu

 

Labels (1)
0 Kudos
Reply
2 Replies

765 Views
Zhiming_Liu
NXP TechSupport
NXP TechSupport

Hi

 

You can refer thsi spi test code https://source.codeaurora.org/external/imx/imx-test/tree/test/mxc_spi_test/mxc_spi_test1.c?h=imx_5.4...

 

 

static const char   *spiDev0  = "/dev/spidev0.0" ;
static const char   *spiDev1  = "/dev/spidev0.1" ;
static  uint8_t     spiBPW   = 8 ;
static  uint16_t    spiDelay = 0 ;
 
static uint32_t     spiSpeeds [2] ;
static int          spiFds [2] ;
/*
* SPIDataRW:
*    Write and Read a block of data over the SPI bus.
*    Note the data ia being read into the transmit buffer, so will
*    overwrite it!
*    This is also a full-duplex operation.
*********************************************************************************
*********************************************************************************/
int SPIDataRW (int channel, unsigned char *tx_data, unsigned char *rx_data, int len)
{
  int i = 0;
  struct spi_ioc_transfer spi ;
  channel &= 1 ;
  memset (&spi, 0, sizeof (spi)) ;
  spi.tx_buf        = (unsigned long)tx_data ;
  spi.rx_buf        = (unsigned long)rx_data ;
  spi.len           = len ;
  spi.delay_usecs   = spiDelay ;
  spi.speed_hz      = spiSpeeds [channel] ;
  spi.bits_per_word = spiBPW ;
return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ; 
}
/*
* SPISetupMode:
*    Open the SPI device, and set it up, with the mode, etc.
*********************************************************************************
*********************************************************************************/
 
int SPISetupMode (int channel, int speed, int mode)
{
  int fd ;
 
  if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
  {
    printf("Unable to open SPI device: %s\n", strerror (errno)) ;
    return -1;
  }
  spiSpeeds [channel] = speed ;
  spiFds    [channel] = fd ;
  
  if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 0)                     
  {                                                               
    printf("Can't set spi mode: %s\n", strerror (errno)) ;         
    return -1;                                                    
  }                                                               
  if (ioctl (fd, SPI_IOC_RD_MODE, &mode) < 0)                     
  {                                                               
    printf("Can't get spi mode: %s\n", strerror (errno)) ;        
    return -1;                                                 
  }    
  

  if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)          
  {                                                               
    printf("Can't set bits per word: %s\n", strerror (errno))  ;  
    return -1;                                                    
  }                                                              
  
  if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0)          
  {                                                               
    printf("Can't get bits per word: %s\n", strerror (errno))  ;  
    return -1;                                                   
  }   
  
/*
* Set SPI RW speed*********************************************************************************
*/
  if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
  {
    printf("Can't set max speed hz: %s\n", strerror (errno));
    return -1;
  }
  
  if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
  {
    printf("Can't get max speed hz: %s\n", strerror (errno));
    return -1;
  }
  
  return fd ;
}
/*
* SPISetup:
*    Open the SPI device, and set it up, etc. in the default MODE 0
*********************************************************************************
*********************************************************************************/
int SPISetup (int channel, int speed)
{
  return SPISetupMode (channel, speed, 0) ;
}

BR

Zhiming

0 Kudos
Reply

758 Views
Dhevan
Contributor IV

Hi @Zhiming_Liu,

      SPI user space code working fine.

      Our issue reading 14k data per second.

      In parallel we are reading two spi interface at same time 14k data one spi and one more spi 1k data.

      If we read two interface parallel data loss is happening and reading data count also different (14k to 20k) but its not stable count.

      Our industrial processor contains 2 core

      Did you have any solution to avoid data loss ?

      Possible M4 to read one spi data linux user space read one more spi interface to avoid data loss ?

0 Kudos
Reply