Exemplo n.º 1
0
void SPIClass::begin() {
    int ret =0;
    int max_speed = 0, default_mode=0;
    int mode;
 
    hw_pinMode(SPI_CS, IO_SPI_FUNC); //CS
    hw_pinMode(SPI_MOSI, IO_SPI_FUNC); //MOSI
    hw_pinMode(SPI_MISO, IO_SPI_FUNC); //MISO
    hw_pinMode(SPI_CLK, IO_SPI_FUNC); //CLK
		
    if (!_fd)
   		_fd = open(spi_name, O_RDWR);
    if (_fd < 0)
        pabort("can't open device");
	
	_speed = 500000;
    ret = ioctl(_fd, SPI_IOC_RD_MODE, &default_mode);
    if (ret == -1)
        pabort("can't get spi mode");
    mode = default_mode;

    ret = ioctl(_fd, SPI_IOC_RD_MAX_SPEED_HZ, &max_speed);
    if (ret == -1)
        pabort("can't get max speed hz");
    _speed = max_speed;

    printf("spi mode: 0x%x\n", mode);
    printf("bits per word: %d\n", _bits_per_word);
    printf("max speed: %d Hz (%d KHz)\n", _speed, _speed/1000);
}
Exemplo n.º 2
0
static void transfer_file(int fd, char *filename)
{
    ssize_t bytes;
    struct stat sb;
    int tx_fd;
    uint8_t *tx;
    uint8_t *rx;

    if (stat(filename, &sb) == -1)
        pabort("can't stat input file");

    tx_fd = open(filename, O_RDONLY);
    if (tx_fd < 0)
        pabort("can't open input file");

    tx = malloc(sb.st_size);
    if (!tx)
        pabort("can't allocate tx buffer");

    rx = malloc(sb.st_size);
    if (!rx)
        pabort("can't allocate rx buffer");

    bytes = read(tx_fd, tx, sb.st_size);
    if (bytes != sb.st_size)
        pabort("failed to read input file");

    transfer(fd, tx, rx, sb.st_size);
    free(rx);
    free(tx);
    close(tx_fd);
}
Exemplo n.º 3
0
static void transfer_buf(int fd, int len)
{
	uint8_t *tx;
	uint8_t *rx;
	int i;

	tx = malloc(len);
	if (!tx)
		pabort("can't allocate tx buffer");
	for (i = 0; i < len; i++)
		tx[i] = random();

	rx = malloc(len);
	if (!rx)
		pabort("can't allocate rx buffer");

	transfer(fd, tx, rx, len);

	_write_count += len;
	_read_count += len;

	if (mode & SPI_LOOP) {
		if (memcmp(tx, rx, len)) {
			fprintf(stderr, "transfer error !\n");
			hex_dump(tx, len, 32, "TX");
			hex_dump(rx, len, 32, "RX");
			exit(1);
		}
	}

	free(rx);
	free(tx);
}
Exemplo n.º 4
0
static PyObject* directWrite(PyObject* self, PyObject* arg)
{
	PyObject* transferTuple;
	unsigned int offset, returnVal ;

	if(!PyArg_ParseTuple(arg, "lO", &offset, &transferTuple))		
		return NULL;					

	if(!PyTuple_Check(transferTuple))			
		pabort("Only accepts a single tuple as an argument\n");


	uint32_t tupleSize = PyTuple_Size(transferTuple);
	uint8_t tx[tupleSize];
	PyObject* tempItem;
	uint32_t i=0;
	while(i < tupleSize)
	{
		tempItem = PyTuple_GetItem(transferTuple, i);		//
		if(!PyInt_Check(tempItem))
		{
			pabort("non-integer contained in tuple\n");
		}
		tx[i] = (uint8_t)PyInt_AsSsize_t(tempItem);
		i++;

	}
	returnVal = direct_write(offset, tx, tupleSize);
	return Py_BuildValue("l", returnVal) ;
}
Exemplo n.º 5
0
int open_spidev(const char *device, uint8_t mode, uint32_t speed)
{
   int spifd, ret;

   spifd = open(device, O_RDWR);
   if (spifd < 0)
      pabort("can't open device");

   /*
    * spi mode
    */
   ret = ioctl(spifd, SPI_IOC_WR_MODE, &mode);
   if (ret == -1)
      pabort("can't set spi mode");

   ret = ioctl(spifd, SPI_IOC_RD_MODE, &mode);
   if (ret == -1)
      pabort("can't get spi mode");

   /*
    * max speed hz
    */
   ret = ioctl(spifd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
   if (ret == -1)
      pabort("can't set max speed hz");

   ret = ioctl(spifd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
   if (ret == -1)
      pabort("can't get max speed hz");

   return spifd;
}
Exemplo n.º 6
0
static PyObject* transfer(PyObject* self, PyObject* arg)
{
	PyObject* transferTuple;

	if(!PyArg_ParseTuple(arg, "O", &transferTuple))		// "O" - Gets non-NULL borrowed reference to Python argument.
		return NULL;					// As far as I can tell, it's mostly just copying arg[0] into transferTuple
								// and making sure at least one arg has been passed (I think)

	if(!PyTuple_Check(transferTuple))			// The only argument we support is a single tuple.
		pabort("Only accepts a single tuple as an argument\n");


	uint32_t tupleSize = PyTuple_Size(transferTuple);

	uint8_t tx[tupleSize];
	uint8_t rx[tupleSize];
	PyObject* tempItem;

	uint16_t i=0;

	while(i < tupleSize)
	{
		tempItem = PyTuple_GetItem(transferTuple, i);		//
		if(!PyInt_Check(tempItem))
		{
			pabort("non-integer contained in tuple\n");
		}
		tx[i] = (uint8_t)PyInt_AsSsize_t(tempItem);

		i++;

	}

	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)tx,
		.rx_buf = (unsigned long)rx,
		.len = tupleSize,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
                .cs_change = 1,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");

	transferTuple = PyTuple_New(tupleSize);
	for(i=0;i<tupleSize;i++)
		PyTuple_SetItem(transferTuple, i, Py_BuildValue("i",rx[i]));

	return transferTuple;
}


static PyObject* closeSPI(PyObject* self,PyObject* args)
{
	close(fd);
	Py_RETURN_NONE;
}
Exemplo n.º 7
0
/**
 * @brief  spi read operation function.
 *
 * @param[in]  fd                 spi file descriptor
 * @param[in]  reg                spi slave register address to be read
 * @param[out] value              read back data
 * @retval                        void
 *
 */
void snowleo_spi_custom_read(int fd, uint8_t *reg, uint8_t *data, uint8_t data_size)
{

	int ret;
#if 0
	struct spi_ioc_transfer tr[2];
	memset(tr, 0, sizeof(tr));
	tr[0].tx_buf = (unsigned long)reg;
	tr[0].len = data_size;

	tr[1].rx_buf = (unsigned long)data;
	tr[1].len = data_size;

	ret = ioctl(fd, SPI_IOC_MESSAGE(2), &tr);
	if (ret < 1)
		pabort("can't send spi message");
#else
	struct spi_ioc_transfer tr1[2];
	memset(tr1, 0, sizeof(tr1));

	int i = 0;
	for(i = 0; i< data_size; i++){
		tr1[0].tx_buf = (unsigned long)&reg[i];
		tr1[0].len = 1;
		tr1[1].rx_buf = (unsigned long)&data[i];
		tr1[1].len = 1;
		ret = ioctl(fd, SPI_IOC_MESSAGE(2), tr1);
		if (ret < 1)
			pabort("can't send spi message");

		//printf("reg[0x%x] = 0x%x\n", reg[i], data[i]);
	}
#endif
}
Exemplo n.º 8
0
char SPIClass::transfer(char val, int transferMode)
{
    int ret=0;
    char tx = val;
    char rx = 0;
	unsigned short delay_usecs = 0;

#ifndef SPI_FULL_DUPLEX
    struct spi_ioc_transfer tr[2]={0};

    if (transferMode == SPI_CONTINUE) 
	   delay_usecs = 0;
	else if (transferMode == SPI_LAST)  
	   delay_usecs = 0xAA55;	

	memset(tr, 0x0, sizeof(tr));
	tr[0].tx_buf = (unsigned long)&tx;
	tr[0].len = 1;
	tr[0].speed_hz = _speed;
	tr[0].bits_per_word = _bits_per_word;
	tr[0].delay_usecs = delay_usecs;
	tr[1].rx_buf = (unsigned long)&rx;
	tr[1].len = 1;
	tr[1].speed_hz = _speed;
	tr[1].bits_per_word = _bits_per_word;
	tr[1].delay_usecs = delay_usecs;

    ret = ioctl(_fd, SPI_IOC_MESSAGE(2), tr);
    if (ret < 1)
        pabort("can't send spi message");
#else
    struct spi_ioc_transfer tr;

    if (transferMode == SPI_CONTINUE) 
	   delay_usecs = 0;
	else if (transferMode == SPI_LAST)  
	   delay_usecs = 0xAA55;	

	memset(&tr, 0x0, sizeof(spi_ioc_transfer));
	tr.tx_buf = (unsigned long)&tx;
	tr.rx_buf = (unsigned long)&rx;
	tr.len = 1;
	tr.speed_hz = _speed;
	tr.bits_per_word = _bits_per_word;
	tr.delay_usecs = delay_usecs;

    ret = ioctl(_fd, SPI_IOC_MESSAGE(1), &tr);
    if (ret < 1)
        pabort("can't send spi message");
#endif

    //printf("rx = %.2X \r\n", rx);

    return rx;

}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
	int ret = 0,cnt =0;
	int fd,fds,i,diff =0;
	unsigned int dist,prevdist = 0,prev_direction;
        struct sigaction act;
	fd = open(display, O_RDWR);
	if (fd < 0)
		pabort("can't open device");
	printf("\n initialising....:");
	ioctl(fd, SPI_LED_INITIALISE, 0);
	transfer(fd);
	fds = open(sensor, O_RDWR);
	if (fds < 0)
		pabort("can't open sensor");

        memset (&act, '\0', sizeof(act));
        act.sa_sigaction = &terminatehdl;
        act.sa_flags = SA_SIGINFO;
        if (sigaction(SIGINT, &act,NULL) < 0) {
                perror ("sigaction");
                return 1;
        }

	while((!terminate)&&(cnt < 120)){	
	  dist = read(fds,NULL,0);
	  write(fds,NULL,0);
	  printf("\ndistance is %lu \n",dist);
	  diff  = dist - prevdist;
	  if(diff<0)
	  {
	    diff = prevdist - dist;
	  }
	  if(dist < 40)
	  {
	      move_left(fd);
          }
	  else 
	  {
	      move_right(fd);
          }
	  prevdist = dist;
	  cnt++;
	}
	write(fd,NULL,0);
	/* time to displat some patterns */
	transfer_moves(fd);
	anime(fd);	
	write(fd,NULL,0);
	usleep(100000);
	close(fd);
	close(fds);
	return 0;
}
Exemplo n.º 10
0
static void transfer(int fd)
{
	int ret;
	ret = ioctl(fd, SPI_IOC_MESSAGE(ntransfers), spi_xfrs);
	if (ret < 1)
		pabort("can't send spi message");
}
Exemplo n.º 11
0
static void transfer(int fd, uint8_t *tx, uint8_t *rx, int len)
{
   int ret;
   struct spi_ioc_transfer tr = {
      .len = len,
   };

   printf("\n%s(len=%d):\n", __func__);

   if (tx) {
      tr.tx_buf = (unsigned long)tx;
      printf("  TX: ");
      print_hex(tx, len);
   }
   if (rx)
      tr.rx_buf = (unsigned long)rx;

   ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
   if (ret < 1)
      pabort("can't send spi message");

   if (rx) {
      printf("  RX: ");
      print_hex(rx, len);
   }
}
Exemplo n.º 12
0
void P_CCONV pLog(PLogVerbosityEnum verbosity, 
    PLogChannelEnum channel,
    const pchar *file, 
    puint32 line, 
    const pchar *format, 
    ...)
{
    // The global verbosity value controls which kinds of log can be 
    // printed.
    if (verbosity < g_logVerbosity)
    {
        return ;
    }

    static const pchar *verbosityText[] = 
    {
        "(debug)",
        "(info)",
        "(warning)",
        "(error)",
        "(fatal)",
    };

    va_list arguments;

    va_start(arguments, format);

    if (pstrlen(format) > 200)
    {
        pchar message[1024];
        puint32 nchars = psprintf(message, 1024, 
                "(warning)%s:%d,log buffer for formatting data is too small.",
                __FILE__, __LINE__);
        pLogOutput(P_LOG_CHANNEL2, message, nchars);
    }
    else
    {
        pchar msg[3000]; 
        puint32 nchars;
        if (verbosity == P_LOG_ERROR || verbosity == P_LOG_WARNING)
        {
            pchar fmt[256];
            psprintf(fmt, 256, "%s%s:%d,%s", 
                verbosityText[verbosity], file, line, format);
            nchars = pvsprintf(msg, 3000, fmt, arguments);
        }
        else 
        {
            nchars = pvsprintf(msg, 3000, format, arguments);
        }

        pLogOutput(channel, msg, nchars);
    }

    if (verbosity == P_LOG_FATAL)
    {
        // FIXME: make it more polite
        pabort();
    }
}
Exemplo n.º 13
0
void spi_close(spi_context* spi)
{
  if(close(spi->fd) < 0) {
    pabort("can't close SPI device");
  }
  delete spi;
}
Exemplo n.º 14
0
static void transfer(int fd)
{
	int ret,i,k;
	uint8_t tr[160];
	uint8_t tx0[] = {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
        };/* Digit 1  */
	uint8_t tx1[] = {
        0x19,0xFB,0xEC,0x08,0x08,0x0F,0x09,0x10
        };/* Digit 1  */
	uint8_t tx2[] = {
        0x18,0xFF,0xE9,0x08,0x0B,0x0E,0x08,0x04
        };/* Digit 1  */
	uint8_t tx3[] = {
        0x10,0x09,0x0f,0x08,0x08,0xEC,0xFB,0x19
        };/* Digit 1  */
	uint8_t tx4[] = {
        0x04,0x08,0x0E,0x0B,0x08,0xE9,0xFF,0x18
        };/* Digit 1  */
	memset(tr,0,80);
	memcpy(tr,tx0,8);
	memcpy((tr+8),tx1,8);
	memcpy((tr+16),tx2,8);
	memcpy((tr+24),tx3,8);
	memcpy((tr+32),tx4,8);
	ret = ioctl(fd, SPI_LED_TX_PATTERN, &tr);
	if (ret < -1)
		pabort("can't send spi message");
        sleep(1);
}
Exemplo n.º 15
0
static void transfer(int fd)
{
	int ret;
	uint8_t tx[] = {
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
		0xF0, 0x0D,
	};
	uint8_t rx[ARRAY_SIZE(tx)] = {0, };
	struct spi_ioc_transfer tr;
	tr.tx_buf = (unsigned long)tx;
	tr.rx_buf = (unsigned long)rx;
	tr.len = ARRAY_SIZE(tx);
	tr.interbyte_usecs = 10;
	tr.delay_usecs = delay;
	tr.speed_hz = speed;
	tr.bits_per_word = bits;

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");

	for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
		if (!(ret % 6))
			puts("");
		printf("%.2X ", rx[ret]);
	}
	puts("");
}
Exemplo n.º 16
0
Arquivo: spi2.c Projeto: heye/PiScope
static void transfer(int fd)
{
	int ret;
	uint8_t tx[] = {
		0x01, 0x02, 0x03
	};
	
	uint8_t rx[ARRAY_SIZE(tx)] = {0, };
	
	struct spi_ioc_transfer tr;
		tr.tx_buf = (unsigned long)tx;
		tr.rx_buf = (unsigned long)rx;
		tr.len = ARRAY_SIZE(tx);
		tr.delay_usecs = 0;
		tr.speed_hz = speed;
		tr.bits_per_word = bits;
	

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	
	if (ret < 1)
		pabort("can't send spi message");

	for (ret = 0; ret < ARRAY_SIZE(tx); ret+=3) {
		
		//printbits((rx[ret]<<16) + (rx[ret+1]<<8) + rx[ret+2],  24);
		printf("CHA: %d \n", (((rx[ret]<<16) + (rx[ret+1]<<8) + rx[ret+2])&0b000000000000001111111111));
		
		//printf("%.2X ", rx[ret]);
	}
	//puts("");
}
Exemplo n.º 17
0
Arquivo: spi.c Projeto: sjhx/etrv
void spi_transfern(char *buf, uint32_t len) {
	int ret;
        //dump("TR tx", (uint8_t *)buf, len);
	
#ifdef BCM
	bcm2835_spi_transfern(buf, len);
#else
	uint8_t *rx = malloc(len);
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)buf,
		.rx_buf = (unsigned long)rx,
		.len = len,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1) {
		free(rx);
		pabort("can't send spi message");
	}
	memcpy(buf, rx, len);
	free(rx);
#endif
}

void spi_writenb(char * buf, uint32_t len) {
	int ret;
	dump("WR tx", (uint8_t *)buf, len);
#ifdef BCM
	bcm2835_spi_writenb(buf, len);
#else
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)buf,
		.rx_buf = (unsigned long)0,
		.len = len,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");
#endif
}
Exemplo n.º 18
0
static PyObject* logiWrite(PyObject* self, PyObject* arg)
{
	PyObject* transferTuple;
	unsigned int offset, returnVal ;
	unsigned char type_size = 1 ;

	if(!PyArg_ParseTuple(arg, "lO|b", &offset, &transferTuple, &type_size))
		return NULL;

	if(!PyTuple_Check(transferTuple))
		pabort("Only accepts a single tuple as an argument\n");

	if(type_size != 1 && type_size != 2 && type_size != 4)
		pabort("type_size argument can only be 1, 2, 4\n");
	uint32_t tupleSize = PyTuple_Size(transferTuple);
	uint8_t tx[tupleSize*type_size];
	PyObject* tempItem;
	uint32_t i=0;
	while(i < tupleSize)
	{
		tempItem = PyTuple_GetItem(transferTuple, i);		//
		if(!PyInt_Check(tempItem))
		{
			pabort("non-integer contained in tuple\n");
		}
		switch(type_size){
			case 1:
				tx[i] = (uint8_t)PyInt_AsSsize_t(tempItem);
				break ;
			case 2:
				((uint16_t*)tx)[i] = (uint16_t)PyInt_AsSsize_t(tempItem);
				break ;
			case 4:
				((uint32_t*)tx)[i] = (uint32_t)PyInt_AsSsize_t(tempItem);
				break ;
			default:
				break ;

			
		}
		i++;

	}
	returnVal = logi_write(tx, tupleSize*type_size, offset);
	return Py_BuildValue("l", returnVal) ;
}
Exemplo n.º 19
0
void make_bulk_transfer(int len)
{
	uint8_t *buf = malloc(len);
	if (NULL == buf)
		pabort("malloc failed");
	memset(buf, 0xCB, len);
	add_transfer(buf, len);
}
static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
{
	int ret;

	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)tx,
		.rx_buf = (unsigned long)rx,
		.len = len,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	if (mode & SPI_TX_QUAD)
		tr.tx_nbits = 4;
	else if (mode & SPI_TX_DUAL)
		tr.tx_nbits = 2;
	if (mode & SPI_RX_QUAD)
		tr.rx_nbits = 4;
	else if (mode & SPI_RX_DUAL)
		tr.rx_nbits = 2;
	if (!(mode & SPI_LOOP)) {
		if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
			tr.rx_buf = 0;
		else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
			tr.tx_buf = 0;
	}

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");

	if (verbose)
		hex_dump(tx, len, 32768, "TX");
	hex_dump(rx, len, 32768, "RX");
}

static void print_usage(const char *prog)
{
	printf("Usage: %s [-DsbdlHOLC3]\n", prog);
	puts("  -D --device   device to use (default /dev/spidev1.1)\n"
	     "  -s --speed    max speed (Hz)\n"
	     "  -d --delay    delay (usec)\n"
	     "  -b --bpw      bits per word \n"
	     "  -l --loop     loopback\n"
	     "  -H --cpha     clock phase\n"
	     "  -O --cpol     clock polarity\n"
	     "  -L --lsb      least significant bit first\n"
	     "  -C --cs-high  chip select active high\n"
	     "  -3 --3wire    SI/SO signals shared\n"
	     "  -v --verbose  Verbose (show tx buffer)\n"
	     "  -p            Send data (e.g. \"1234\\xde\\xad\")\n"
	     "  -N --no-cs    no chip select\n"
	     "  -R --ready    slave pulls low to pause\n"
	     "  -2 --dual     dual transfer\n"
	     "  -4 --quad     quad transfer\n");
	exit(1);
}
Exemplo n.º 21
0
static PyObject* transfer(PyObject* self, PyObject* args)
{
	PyObject* pyObj;

	if(!PyArg_ParseTuple(args, "O", &pyObj))
		return NULL;

	uint8_t tupleSize = PyTuple_Size(pyObj);

	uint8_t tx[bytesPerMessage];
	uint8_t rx[bytesPerMessage];
	PyObject* item;

	uint8_t i=0;

	while(i<bytesPerMessage){
		if(i<tupleSize){
			item = PyTuple_GetItem(pyObj, i);
			if(!PyInt_Check(item)){
				printf("non-integer contained in tuple");
				exit(1);
			}
			tx[i] = PyFloat_AsDouble(item);
		}
		else
			tx[i] = 0;

		i++; // printf("%d ", tx[i++]);
	}

	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)tx,
		.rx_buf = (unsigned long)rx,
		.len = bytesPerMessage,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");

	pyObj = PyTuple_New(bytesPerMessage);
	 
	for(i=0;i<bytesPerMessage;i++){
		PyTuple_SetItem(pyObj, i, Py_BuildValue("i",rx[i]));
	}

	return pyObj;
}


static PyObject* end(PyObject* self,PyObject* args)
{
	close(fd);
	Py_RETURN_NONE;
}
Exemplo n.º 22
0
int main(int argc, char *argv[])
{
    int ret = 0;
    int fd;

    parse_opts(argc, argv);

    fd = open(device, O_RDWR);
    if (fd < 0)
        pabort("can't open device");

    /*
     * spi mode
     */
    printf("mode=%d\n",mode);
    //    mode = 3;
    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1)
        pabort("can't set spi mode");

    ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
    if (ret == -1)
        pabort("can't get spi mode");

    /*
     * bits per word
     */
    ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret == -1)
        pabort("can't set bits per word");

    ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    if (ret == -1)
        pabort("can't get bits per word");

    /*
     * max speed hz
     */
    speed = 8000000;
    ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't set max speed hz");

    ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
    if (ret == -1)
        pabort("can't get max speed hz");

    printf("spi mode: %d\n", mode);
    printf("bits per word: %d\n", bits);
    printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
    while(1)
    {
//        sleep(1);
        spi_write_read(fd);
//       printf("\n");
    }
    close(fd);

    return ret;
}
Exemplo n.º 23
0
// bus freq range 10kHz-400kHz
void TwoWire::setBusFreq(unsigned int speed_hz)
{
  int libi2cdev;

  if ((speed_hz > 400000) || (speed_hz < 10000))
  {
     printf("invalid bus freq, range[10000,400000]\r\n");
     return;
  }

  if ((libi2cdev = open("/dev/hwi2c", O_RDWR)) < 0) 
    pabort("can't open device");       
  
  if (ioctl(libi2cdev, I2CCLOCK_CHANGE, &speed_hz) < 0) 
    pabort("change I2C bus freq fail");

  close(libi2cdev);
}
Exemplo n.º 24
0
static void transfer(int fd)
{
/* ioctl is the function that activates the transmit and receive. The way to
 * properly use it is to create a SPI transaction structure like "tr", which
 * sets aside memory space for the data that is about to be transmitted or
 * received. ioctl starts the SPI clock and transmits whatever is in the TX
 * buffer. At the same time, the RX buffer is filled with whatever comes in
 * on the RX line while the clock is active. SPI_IOC_MESSAGE(n) tells ioctl
 * that it is sending a SPI messages. The n tells it how many messages to send.
 *
 * The spi_ioc_transfer function contains the parameters for the transaction to
 * take place. Transactions can be linked together in arrays. See the function
 * definition for more information.
 */

	int ret;
	uint8_t tx[] = {
		0x10, 0x01, 0xe0, 0x01, 0x00, 0x00
	};
	uint8_t rx[ARRAY_SIZE(tx)] = {0, };
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)tx,
		.rx_buf = (unsigned long)rx,
		.len = ARRAY_SIZE(tx),
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
		.cs_change = 1,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");

	for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
		if (!(ret % 6))
			puts("");
		printf("%.2X ", rx[ret]);
	}
	puts("");
}

static void print_usage(const char *prog)
{
	printf("Usage: %s [-DsbdlHOLC3]\n", prog);
	puts("  -D --device   device to use (default /dev/spidev1.1)\n"
	     "  -s --speed    max speed (Hz)\n"
	     "  -d --delay    delay (usec)\n"
	     "  -b --bpw      bits per word \n"
	     "  -l --loop     loopback\n"
	     "  -H --cpha     clock phase\n"
	     "  -O --cpol     clock polarity\n"
	     "  -L --lsb      least significant bit first\n"
	     "  -C --cs-high  chip select active high\n"
	     "  -3 --3wire    SI/SO signals shared\n");
	exit(1);
}
Exemplo n.º 25
0
int main(int argc, char *argv[])
{
    int ret = 0;

    fd = open(device, O_RDWR);
    if (fd < 0)
        pabort("can't open device");

    parse_opts(argc, argv);
    if(argc <= 1) {
        /*
         * spi mode
         */
        ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
        if (ret == -1)
            pabort("can't set spi mode");

        ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
        if (ret == -1)
            pabort("can't get spi mode");

        uint8_t msb_first = 0;
        ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &msb_first);
        if(ret == -1)
            pabort("can't set msb first");
        /*
         * bits per word
         */
        ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
        if (ret == -1)
            pabort("can't set bits per word");

        ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
        if (ret == -1)
            pabort("can't get bits per word");

        /*
         * max speed hz
         */
        ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
        if (ret == -1)
            pabort("can't set max speed hz");

        ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
        if (ret == -1)
            pabort("can't get max speed hz");

        ymu836_audio_init(); 
    }

    close(fd);

    return ret;
}
Exemplo n.º 26
0
Arquivo: spi.c Projeto: sjhx/etrv
int spi_init() {
	int ret = 0;

	mode = 0;
	delay = 0;

#ifdef BCM
	if (!bcm2835_init())
		return 0;
	bcm2835_spi_begin();	
	bcm2835_spi_setClockDivider(SPI_CLOCK_DIVIDER); 			// 250MHz / 26 = 9.6MHz
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); 				// CPOL = 0, CPHA = 0
	bcm2835_spi_chipSelect(BCM2835_SPI_CS1);					// chip select 1
	return 1;
#else
	fd = open(device, O_RDWR);
	if (fd < 0)
		pabort("can't open device");

	/*
	 * spi mode
	 */
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret == -1)
		pabort("can't set spi mode");

	ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
	if (ret == -1)
		pabort("can't get spi mode");

	/*
	 * bits per word
	 */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't set bits per word");

	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't get bits per word");

	/*
	 * max speed hz
	 */
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't set max speed hz");

	ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't get max speed hz");

	printf("spi mode: %d\n", mode);
	printf("bits per word: %d\n", bits);
	printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);

	return 1;
#endif
}
Exemplo n.º 27
0
Arquivo: dsp.c Projeto: lsyslsy/SEMG
static void transfer(int fd)
{
	int ret;
	uint8_t tx[8] = {
		0x08, 0x02, 0x03, 0x04,
                0x05, 0x06, 0x07, 0x08
	};
	uint8_t rx[8] = {0};
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)tx,
		.rx_buf = (unsigned long)rx,
		.len = 8,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};
        while(1)
{
	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
        ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");
	printf("read:%x,%x,%x,%x\n",rx[0],rx[1],rx[2],rx[7]);
        usleep(10);
}
}



static void do_msg(int fd)
{
	unsigned char tbuf[8];
        unsigned char rbuf[8];
        unsigned char *bp;
        struct spi_ioc_transfer	xfer[2];
	int status;
        tbuf[0]=1;
        tbuf[1]=2;
        tbuf[2]=3;
        tbuf[3]=4;
        tbuf[4]=1;
        //memset(xfer, 0, sizeof(xfer));
	xfer[0].tx_buf = (unsigned long) tbuf;
        xfer[0].rx_buf = (unsigned long) rbuf;
	xfer[0].len = 5;
       while(1){
	status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
        printf("%d,write\n",status);
	if (status < 0) {
		printf("write wrong!\n");
		return;
	}
        usleep(10);
        }
	
}
Exemplo n.º 28
0
static void transfer_escaped_string(int fd, char *str)
{
    size_t size = strlen(str);
    uint8_t *tx;
    uint8_t *rx;

    tx = malloc(size);
    if (!tx)
        pabort("can't allocate tx buffer");

    rx = malloc(size);
    if (!rx)
        pabort("can't allocate rx buffer");

    size = unescape((char *)tx, str, size);
    transfer(fd, tx, rx, size);
    free(rx);
    free(tx);
}
Exemplo n.º 29
0
void detachInterrupt(uint8_t interruptNum) 
{
   int ret = -1, fd = -1;

   if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {

      fd = open(swirq_dev, O_RDONLY);
      if ( fd < 0 )
         pabort("open swirq device fail");

      ret = ioctl(fd, SWIRQ_STOP, &interruptNum);
      if (ret < 0)
         pabort("can't set SWIRQ_STOP");

      if (fd) 
         close(fd);
   } 

}
Exemplo n.º 30
0
int main(int argc, char *argv[])
{
	int ret = 0;
	int fd;
        //mode |= SPI_MSB_FIRST;
	parse_opts(argc, argv);

	fd = open(device, O_RDWR);
	if (fd < 0)
		pabort("can't open device");

	
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret == -1)
		pabort("can't set spi mode");

	
	/*
	 * bits per word
	 */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't set bits per word");


	/*
	 * max speed hz
	 */
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't set max speed hz");


	//printf("spi mode: %d\n", mode);
	//printf("bits per word: %d\n", bits);
	//printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);

	transfer(fd);

	close(fd);

	return ret;
}