SUB_20 to SUB_20 through SPI interface transfer issue

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
alexander
Posts: 1
Joined: Sun Apr 26, 2015 5:37 am

SUB_20 to SUB_20 through SPI interface transfer issue

Post by alexander »

Hello.
I'm trying to transfer one Byte data from SUB_20 Master to SUB_20 Slave through SPI interface.
I plugged it in the next way: https://www.dropbox.com/s/g0thfwj46nvxs ... 8.jpg?dl=0
The master sends a number (1-to 255 ) to slave each cycle. The number is increased by one at end of each cycle. The slave receives a number and sends it back to a master.
I.e it must be in this way:
Cycle 0: _MASTER: writing to slave 0 ----> _SLAVE: reading from master 0 ----> _SLAVE: writing to master 0 ----> _MASTER: reading from input buffer 0
Cycle 1: _MASTER: writing to slave 1 ----> _SLAVE: reading from master 1 ----> _SLAVE: writing to master 1 ----> _MASTER: reading from input buffer 1
Cycle 2: _MASTER: writing to slave 2 ----> _SLAVE: reading from master 2 ----> _SLAVE: writing to master 2 ----> _MASTER: reading from input buffer 2
e.t.c.

The one direction (master to slave) transfer is perfect. BUT an issue is in the second direction.
Three first cycles a master receives back form slave a number 0. On fourth cycle a master receives back form slave a number 1, and after it continues to receive the numbers with "skip" of 3 numbers, i.e.:

Cycle 0: _MASTER: writing to slave 0 ----> _SLAVE: reading from master 0 ----> _SLAVE: writing to master 0 ----> _MASTER: reading from input buffer 0
Cycle 1: _MASTER: writing to slave 1 ----> _SLAVE: reading from master 1 ----> _SLAVE: writing to master 1 ----> _MASTER: reading from input buffer 0
Cycle 2: _MASTER: writing to slave 2 ----> _SLAVE: reading from master 2 ----> _SLAVE: writing to master 2 ----> _MASTER: reading from input buffer 0
Cycle 3: _MASTER: writing to slave 3 ----> _SLAVE: reading from master 3 ----> _SLAVE: writing to master 3 ----> _MASTER: reading from input buffer 0
Cycle 4: _MASTER: writing to slave 4 ----> _SLAVE: reading from master 4 ----> _SLAVE: writing to master 4 ----> _MASTER: reading from input buffer 1
Cycle 5: _MASTER: writing to slave 5 ----> _SLAVE: reading from master 5 ----> _SLAVE: writing to master 5 ----> _MASTER: reading from input buffer 2
Cycle 6: _MASTER: writing to slave 6 ----> _SLAVE: reading from master 6 ----> _SLAVE: writing to master 6 ----> _MASTER: reading from input buffer 3
e.t.c...

The sources of my code for master and slave are attached to the post.

Please help me to solve the problem.

Thank you in advance.
Alexander.
Attachments
SUB_20-to_SUB_20.rar
The sources of my code for master and slave
(11.2 KiB) Downloaded 1574 times

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: SUB_20 to SUB_20 through SPI interface transfer issue

Post by serg »

Hello Alexander,

Sorry for the delay in reply, for some reason I didn't get an email notification to your post.
A while ago, one of our customers Ravi MK had prepared a C test application which uses two SUB-20 boards to establish an SPI communication between them. Could you please try this application on your setup and let me know how it goes.

Code: Select all

/**
 * 	sub20_spi_test.c
 *
 *	Test application to test SPI device on SUB20 board
 *
 * 	Created on: 27-Nov-2012
 *      Authors: Ravi MK, Vikram N
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libsub.h"
#define SZ 1
#define SZ_MAX 25
#define TST_DATA "spi_test"
#define MSTR 0
#define SLAVE 1


int main(void)
{
  sub_device dev = 0;
  sub_handle hndl[2] = { 0 };
  int i = 0;
  int rc = 0;
  char rx_buf[SZ_MAX] = { 0 };
  char tx_buf[] = TST_DATA;
  char tmp_buf[SZ_MAX] = { 0 };

  /** scan for sub20 devices attached and open each device */
  while((dev = sub_find_devices(dev)) && (i < 2))
  {
    printf("Found a %d sub20 device\n", i + 1);
    hndl[i] = sub_open(dev);
    if(!hndl[i])
    {
      printf("Opening of %d sub20 device failed errno: %d\n", i + 1, sub_errno);
      exit(1);
    }
    i++;
  }

  if(i != 2)
  {
    printf("Please connect two sub20 boards and start test\n");
    exit(2);
  }

  /** Configure the second SPI device as Master */
  rc = sub_spi_config(hndl[MSTR], SPI_ENABLE | SPI_CPOL_RISE | SPI_MSB_FIRST
      | SPI_CLK_125KHZ | SPI_SMPL_SETUP, 0);
  if(rc != 0)
  {
    printf("SPI master configuration failed errno: %d\n", sub_errno);
    exit(3);
  }
  printf("Sub20 configured for SPI master \n");

  /** Configure the first SPI device as slave */
  rc = sub_spi_config(hndl[SLAVE], SPI_ENABLE | SPI_SLAVE | SPI_CPOL_RISE
      | SPI_SMPL_SETUP, 0);
  if(rc != 0)
  {
    printf("I2c slave configuration failed errno: %d\n", sub_errno);
    exit(3);
  }
  printf("Sub20 configured for SPI Slave \n");

  /** Configure FIFO of slave the devices */
  rc = sub_fifo_config(hndl[SLAVE], FIFO_SELECT_SPI | FIFO_CLEAR);
  if(rc != 0)
  {
    printf("SPI FIFO configuration failed errno: %d\n", sub_errno);
    exit(5);
  }
  printf("SPI FIFOs configured\n");

  char slave_data[]="data written by slave";
  rc = sub_fifo_write(hndl[SLAVE],slave_data,(int)sizeof(slave_data) ,10000);
  if(rc < 0)
  {
    printf("SPI FIFO write failed errno: %d\n", sub_errno);
    exit(6);
  }

  char master_receive[64];
  char slave_receive[64];

  /** Transfer 8 bytes from the master and receive from slave */
  char master_data[]="data written by master";
  rc = sub_spi_transfer(hndl[MSTR], master_data, master_receive, sizeof(master_data), SS_CONF(0, SS_LO));
  if(rc != 0)
  {
    printf("data transfer to SPI slave failed errno %d \n", sub_errno);
    exit(7);
  }

  /** compare transmitted bytes */
  rc = sub_fifo_read(hndl[SLAVE],slave_receive,sizeof(slave_receive), 1000 );
  if(rc < 0)
    {
      printf("SPI FIFO read failed errno: %d\n", sub_errno);
      exit(60);
    }

  /** compare received bytes */
  rc = strncmp(slave_data, master_receive, sizeof(slave_data));
  if(rc != 0)
  {
    printf(" Received data on master is not equal to sent \n");
    printf(" SUB20 test failed\n");
    exit(9);
  }

  rc = strncmp(master_data, slave_receive, sizeof(slave_data));
  if(rc != 0)
  {
    printf(" Received data on slave is not equal to sent \n");
    printf(" SUB20 test failed\n");
    exit(9);
  }

  printf(" Received data is equal to sent \n");
  printf(" SUB20 SPI test Passed\n");

  /** close sub20 SPI devices */
  for(i = 0; i < 2; i++)
  {
    (void)sub_close(hndl[i]);
  }
  exit(0);
  return 0;
}


Post Reply