Hello 
I am new to BBB and C programming. i Wrote this program to interface SPI to 
UART ic SC16IS750.
When i run this program this is my output.

ubuntu@arm:~$ ./Hello
 0  0  0  0  0  0  0  0  0  255  255  255  0  � ubuntu@arm:~$ 

Please help.

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to beagleboard+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <NXCTRL.h>

#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>

const uint16_t FCR  = 16;
const uint16_t LCR = 24;
const uint16_t SPR  =56;
const uint16_t DLL  =0;
const uint16_t DLM = 8;
const uint16_t EFR = 16;

static int
__SPI_read (int nFD, uint16_t nAddr, uint16_t *pnD)
{
  int nStatus;
  struct spi_ioc_transfer xfer[2];
  char rchTXData[4];
  char rchRXData[4];
  int i;

  memset(xfer, 0, sizeof(xfer));

 // memcpy(rchTXData, &INSTREAD, sizeof(uint8_t));
  memcpy(rchTXData , &nAddr, sizeof(uint16_t));
  memcpy(rchTXData + 2, pnD, sizeof(uint16_t));

  xfer[0].tx_buf = (unsigned int)rchTXData;
  xfer[0].rx_buf = (unsigned int)rchRXData;
  xfer[0].len = 3;
  xfer[0].delay_usecs = 200;

  nStatus = ioctl(nFD, SPI_IOC_MESSAGE(1), xfer);

  if (nStatus < 0)
  {
  	perror("SPI_IOC_MESSAGE");
    return -1;
  }

  for (i = 0 ; i <= 3 ; i++)
	  printf (" %d " , rchRXData[i]);

	  //*pnD = rchRXData[3];
  return 0;

}


static int
__SPI_write (int nFD, uint16_t nAddr, uint16_t nD)
{
  int nStatus;
  struct spi_ioc_transfer xfer[2];
  char rchTXData[4];
  char rchRXData[4];

  memset(xfer, 0, sizeof(xfer));

  //memcpy(rchTXData, &INSTWRITE, sizeof(uint8_t));
  memcpy(rchTXData, &nAddr, sizeof(uint8_t));
  memcpy(rchTXData + 1, &nD, sizeof(uint16_t));

  xfer[0].tx_buf = (unsigned int)rchTXData;
  xfer[0].rx_buf = (unsigned int)rchRXData;
  xfer[0].len = 2;
  xfer[0].delay_usecs = 200;

  nStatus = ioctl(nFD, SPI_IOC_MESSAGE(1), xfer);

  if (nStatus < 0)
  {
    perror("SPI_IOC_MESSAGE");
    return -1;
  }

  return 0;

}

void SPI_UART_Init(int nFD)
{
	int x = 0;
	char rd;
	x = __SPI_write (nFD , LCR , 128 );
	printf (" %d " , x);
	x = __SPI_write (nFD , DLL , 96 );
	printf (" %d " , x);
	x = __SPI_write (nFD , DLM , 0 );
	printf (" %d " , x);
	x = __SPI_write (nFD , LCR , 191 );
	printf (" %d " , x);
	x = __SPI_write (nFD , EFR , 16 );
	printf (" %d " , x);
	x = __SPI_write (nFD , LCR , 3 );
	printf (" %d " , x);
	x = __SPI_write (nFD , FCR , 6 );
	printf (" %d " , x);
	x = __SPI_write (nFD , FCR , 1 );
	printf (" %d " , x);
	x = __SPI_write (nFD , SPR , 97 );
	printf (" %d " , x);
	__SPI_read (nFD , SPR ,&rd );
	printf (" %c " , rd );

}

int main (void) {

  int nFD, i;
  uint8_t nLSB, nD ,rD;
  uint32_t nSpeed, nSPIMode;
  uint16_t nAddr;


  nFD = open("/dev/spidev1.0", O_RDWR);



  nLSB = 0;

  ioctl(nFD, SPI_IOC_WR_LSB_FIRST, &nLSB);

  nSpeed = 500000;

  ioctl(nFD, SPI_IOC_WR_MAX_SPEED_HZ, &nSpeed);

  nSPIMode = SPI_MODE_0;

  ioctl(nFD, SPI_IOC_WR_MODE, &nSPIMode);



  nAddr = 305;

  nD = -1;



  for (i = 0; i < 1; i++)

	  {
		  SPI_UART_Init(nFD);
	 	 usleep(900);
	  }




     //  NXCTRLSleep(5, 0);



  close(nFD);

return(1);
}

Reply via email to