本文整理汇总了C++中SPI_IOC_MESSAGE函数的典型用法代码示例。如果您正苦于以下问题:C++ SPI_IOC_MESSAGE函数的具体用法?C++ SPI_IOC_MESSAGE怎么用?C++ SPI_IOC_MESSAGE使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SPI_IOC_MESSAGE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: lcd_writedata
void
lcd_writedata(unsigned char c) //write data
{
int ret;
GPIO_SET = 1<<LCD_RS_GPIO; //set RS high for writing data
txbuf[0]=c;
spi.len=1;
spi.delay_usecs=0;
spi.speed_hz=spi_speed;
spi.bits_per_word=spi_bits;
spi.cs_change=0;
spi.tx_buf=(unsigned long)txbuf;
spi.rx_buf=(unsigned long)rxbuf;
ret=ioctl(lcd_fd, SPI_IOC_MESSAGE(1), &spi);
if (ret<0)
{
fprintf(stderr, "Error performing SPI exchange: %s\n", strerror(errno));
exit(1);
}
}
示例2: ARRAY_SIZE
int SPI::writeByte(uint8_t byteSent)
{
int ret;
uint8_t tx[1] = {byteSent};
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 = _delay;
tr.speed_hz = _speed;
tr.bits_per_word = _bits;
ret = ioctl(_fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
printf("SPI writeByte : can't send spi message\n");
}
return ret;
}
示例3: memset
uint8_t SPI::transfer(uint8_t tx)
{
struct spi_ioc_transfer tr;
memset(&tr, 0, sizeof(tr));
tr.tx_buf = (unsigned long)&tx;
uint8_t rx;
tr.rx_buf = (unsigned long)℞
tr.len = sizeof(tx);
tr.speed_hz = _spi_speed; //RF24_SPIDEV_SPEED;
tr.delay_usecs = 0;
tr.bits_per_word = RF24_SPIDEV_BITS;
tr.cs_change = 0;
int ret;
ret = ioctl(this->fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) throw SPIException("can't send spi message");
/*{
perror("can't send spi message");
abort();
}*/
return rx;
}
示例4: spi_write
boolean
spi_write(SpiDevice *spidev, int demux_BA, uint8_t *data, int length)
{
struct spi_ioc_transfer spi;
if (!spi_demux_select(spidev, demux_BA))
return FALSE;
memset (&spi, 0, sizeof(spi)); /* clear padding bytes?? */
spi.tx_buf = (unsigned long) data;
spi.rx_buf = 0;
spi.len = length;
spi.delay_usecs = spi_delay;
spi.speed_hz = spidev->speed[demux_BA];
spi.bits_per_word = spi_bpw;
if (ioctl(spidev->fd, SPI_IOC_MESSAGE(1), &spi) < 0)
{
printf("spi_write() ioctl() failed: %s\n", strerror(errno));
return FALSE;
}
return TRUE;
}
示例5: ioctl
bool CDeviceSPI::WriteBuffer()
{
spi_ioc_transfer spi = {};
spi.tx_buf = (__u64)m_buff;
spi.len = m_buffsize;
int returnv = ioctl(m_fd, SPI_IOC_MESSAGE(1), &spi);
if (returnv == -1)
{
LogError("%s: %s %s", m_name.c_str(), m_output.c_str(), GetErrno().c_str());
return false;
}
if (m_debug)
{
for (int i = 0; i < m_buffsize; i++)
printf("%x ", m_buff[i]);
printf("\n");
}
return true;
}
示例6: ioctl
int SPI::xfer1(uint8_t wbuf[], uint8_t rbuf[], int len)
{
struct spi_ioc_transfer txinfo;
txinfo.tx_buf = (__u64 ) wbuf;
txinfo.rx_buf = (__u64 ) rbuf;
txinfo.len = len;
txinfo.delay_usecs = 0;
txinfo.speed_hz = speed;
txinfo.bits_per_word = bpw;
txinfo.cs_change = 1;
int r = ioctl(fd, SPI_IOC_MESSAGE(1), &txinfo);
if (r < 0)
{
printf("ioctl(fd, SPI_IOC_MESSAGE(1), &txinfo): %s (len=%d)\n",
strerror(r), len);
return r;
}
//deactivate CS line
//uint8_t tmp;
//::read(fd, &tmp, 0);
return len;
}
示例7: __u64
int ProviderSpi::writeBytes(const unsigned size, const uint8_t * data)
{
if (_fid < 0)
{
return -1;
}
_spi.tx_buf = __u64(data);
_spi.len = __u32(size);
if (_spiDataInvert)
{
uint8_t * newdata = (uint8_t *)malloc(size);
for (unsigned i = 0; i<size; i++) {
newdata[i] = data[i] ^ 0xff;
}
_spi.tx_buf = __u64(newdata);
}
int retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi);
ErrorIf((retVal < 0), _log, "SPI failed to write. errno: %d, %s", errno, strerror(errno) );
return retVal;
}
示例8:
uint8_t BlackSPI::transfer(uint8_t writeByte, uint16_t wait_us)
{
uint8_t tempReadByte = 0x00;
if( ! this->isOpenFlag )
{
this->spiErrors->openError = true;
this->spiErrors->transferError = true;
return tempReadByte;
}
this->spiErrors->openError = false;
spi_ioc_transfer package;
package.tx_buf = (unsigned long)&writeByte;
package.rx_buf = (unsigned long)&tempReadByte;
package.len = 1;
package.delay_usecs = wait_us;
package.speed_hz = this->currentProperties.spiSpeed;
package.bits_per_word = this->currentProperties.spiBitsPerWord;
if( ::ioctl(this->spiFD, SPI_IOC_MESSAGE(1), &package) >= 0)
{
this->spiErrors->transferError = false;
return tempReadByte;
}
else
{
this->spiErrors->transferError = true;
return tempReadByte;
}
}
示例9: memset
bool BlackSPI::transfer(uint8_t *writeBuffer, uint8_t *readBuffer, size_t bufferSize, uint16_t wait_us)
{
if( ! this->isOpenFlag )
{
this->spiErrors->openError = true;
this->spiErrors->transferError = true;
return false;
}
this->spiErrors->openError = false;
uint8_t tempReadBuffer[ bufferSize ];
memset( tempReadBuffer, 0, bufferSize);
spi_ioc_transfer package;
package.tx_buf = (unsigned long)writeBuffer;
package.rx_buf = (unsigned long)tempReadBuffer;
package.len = bufferSize;
package.delay_usecs = wait_us;
package.speed_hz = this->currentProperties.spiSpeed;
package.bits_per_word = this->currentProperties.spiBitsPerWord;
if( ::ioctl(this->spiFD, SPI_IOC_MESSAGE(1), &package) >= 0)
{
this->spiErrors->transferError = false;
memcpy(readBuffer, tempReadBuffer, bufferSize);
return true;
}
else
{
this->spiErrors->transferError = true;
return false;
}
}
示例10: raspberrypiSPIDataRW
int raspberrypiSPIDataRW(int channel, unsigned char *data, int len) {
struct spi_ioc_transfer spi;
memset(&spi, 0, sizeof(spi)); // found at http://www.raspberrypi.org/forums/viewtopic.php?p=680665#p680665
channel &= 1;
spi.tx_buf = (unsigned long)data;
spi.rx_buf = (unsigned long)data;
spi.len = len;
spi.delay_usecs = spiDelay;
spi.speed_hz = spiSpeeds[channel];
spi.bits_per_word = spiBPW;
#ifdef SPI_IOC_WR_MODE32
spi.tx_nbits = 0;
#endif
#ifdef SPI_IOC_RD_MODE32
spi.rx_nbits = 0;
#endif
if(ioctl(spiFds[channel], SPI_IOC_MESSAGE(1), &spi) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPIDataRW: Unable to read/write from channel %d: %s", channel, strerror(errno));
return -1;
}
return 0;
}
示例11: assert
bool SPIDevice::transfer(const uint8_t *send, uint32_t send_len,
uint8_t *recv, uint32_t recv_len)
{
struct spi_ioc_transfer msgs[2] = { };
unsigned nmsgs = 0;
assert(_bus.fd >= 0);
if (send && send_len != 0) {
msgs[nmsgs].tx_buf = (uint64_t) send;
msgs[nmsgs].rx_buf = 0;
msgs[nmsgs].len = send_len;
msgs[nmsgs].speed_hz = _speed;
msgs[nmsgs].delay_usecs = 0;
msgs[nmsgs].bits_per_word = _desc.bits_per_word;
msgs[nmsgs].cs_change = 0;
nmsgs++;
}
if (recv && recv_len != 0) {
msgs[nmsgs].tx_buf = 0;
msgs[nmsgs].rx_buf = (uint64_t) recv;
msgs[nmsgs].len = recv_len;
msgs[nmsgs].speed_hz = _speed;
msgs[nmsgs].delay_usecs = 0;
msgs[nmsgs].bits_per_word = _desc.bits_per_word;
msgs[nmsgs].cs_change = 0;
nmsgs++;
}
if (!nmsgs) {
return false;
}
int r;
if (_bus.last_mode == _desc.mode) {
/*
the mode in the kernel is not tied to the file descriptor,
so there is a chance some other process has changed it since
we last used the bus. We want to report when this happens so
the user has a chance of figuring out when there is
conflicted use of the SPI bus. Unfortunately this costs us
an extra syscall per transfer.
*/
uint8_t current_mode;
if (ioctl(_bus.fd, SPI_IOC_RD_MODE, ¤t_mode) < 0) {
hal.console->printf("SPIDevice: error on getting mode fd=%d (%s)\n",
_bus.fd, strerror(errno));
_bus.last_mode = -1;
} else if (current_mode != _bus.last_mode) {
hal.console->printf("SPIDevice: bus mode conflict fd=%d mode=%u/%u\n",
_bus.fd, (unsigned)_bus.last_mode, (unsigned)current_mode);
_bus.last_mode = -1;
}
}
if (_desc.mode != _bus.last_mode) {
r = ioctl(_bus.fd, SPI_IOC_WR_MODE, &_desc.mode);
if (r < 0) {
hal.console->printf("SPIDevice: error on setting mode fd=%d (%s)\n",
_bus.fd, strerror(errno));
return false;
}
_bus.last_mode = _desc.mode;
}
_cs_assert();
r = ioctl(_bus.fd, SPI_IOC_MESSAGE(nmsgs), &msgs);
_cs_release();
if (r == -1) {
hal.console->printf("SPIDevice: error transferring data fd=%d (%s)\n",
_bus.fd, strerror(errno));
return false;
}
return true;
}
示例12: transfer
static int transfer(int fd, char *tbuf, char *rbuf, int bytes)
{
int ret;
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tbuf,
.rx_buf = (unsigned long)rbuf,
.len = bytes,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret == 1)
printf("can't send spi message");
return ret;
}
int execute_buffer_test(int spi_id, int len, char *buffer)
{
char *rbuf;
int res = 0;
int fd = -1;
if (spi_id == 0) {
fd = open(DEV_SPI1, O_RDWR);
} else if (spi_id == 1) {
fd = open(DEV_SPI2, O_RDWR);
} else if (spi_id == 2) {
fd = open(DEV_SPI3, O_RDWR);
}
if (fd < 0) {
printf("Error:cannot open device "
"(Maybe not present in your board?)\n");
return -1;
}
res = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (res == -1) {
printf("can't set spi mode");
goto exit;
}
res = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (res == -1) {
printf("can't set spi mode");
goto exit;
}
/*
* bits per word
*/
res = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word);
if (res == -1) {
printf("can't set bits per word");
goto exit;
}
res = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits_per_word);
if (res == -1) {
printf("can't get bits per word");
goto exit;
}
/*
* max speed hz
*/
res = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (res == -1) {
printf("can't set max speed hz");
goto exit;
}
res = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (res == -1) {
printf("can't get max speed hz");
goto exit;
}
printf("spi mode: %d\n", mode);
printf("bits per word: %d\n", bits_per_word);
printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
rbuf = malloc(len);
memset(rbuf, 0, len);
res = transfer(fd, buffer, rbuf, len);
if (res < 0) {
printf("Failed transferring data: %d\n", errno);
return -1;
}
res = check_data_integrity(buffer, rbuf, len);
printf("Data sent : %s\n", (char *)buffer);
printf("Data received : %s\n", (char *)rbuf);
if (res != 0) {
printf("Test FAILED.\n");
} else {
printf("Test PASSED.\n");
}
free(rbuf);
//.........这里部分代码省略.........
示例13: transfer
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);
// printf("size is %d\n", tupleSize);
uint8_t tx[tupleSize];
uint8_t rx[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++;
}
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;
}
示例14: snowleo_sdr_set_freq
/**
* @brief configure lms6002's Frequency.
*
* @param[in] fd spi file descriptor
* @param[in] freq frequency to be configured
* @param[in] channel RF channel
* @retval void
*
*/
int snowleo_sdr_set_freq(int fd, unsigned int freq, uint8_t channel)
{
int ret;
unsigned char value;
unsigned int value2, net_data = freq;
unsigned int NINT,NFRAC;
double x = (double)net_data/(double)1000000000.0;
printf("x = %f\n", x);
if(x >= 3.72)
return -1;
if(x>0.2325&&x<0.285625)
value = 0x27;//100111
else if(x>0.285625&&x<0.336875)
value = 0x2f;//101111
else if(x>0.336875&&x<0.405)
value = 0x37;//110111
else if(x>0.405&&x<0.465)
value = 0x3f;//111111
else if(x>0.465&&x<0.57125)
value = 0x26;//100110
else if(x>0.57125&&x<0.67375)
value = 0x2e;//101110
else if(x>0.67375&&x<0.81)
value = 0x36;//110110
else if(x>0.81&&x<0.93)
value = 0x3e;//111110
else if(x>0.93&&x<1.1425)
value = 0x25;//100101
else if(x>1.1425&&x<1.3475)
value = 0x2d;//101101
else if(x>1.3475&&x<1.62)
value = 0x35;//110101
else if(x>1.62&&x<1.86)
value = 0x3d;//111101
else if(x>1.86&&x<2.285)
value = 0x24;//100100
else if(x>2.285&&x<2.695)
value = 0x2c;//101100
else if(x>2.695&&x<3.24)
value = 0x34;//110100
else if(x>3.24&&x<3.72)
value = 0x3c;//111100
else
value = 0;
value2 = 2<<((value&0x07)-4);
//printf("value = 0x%x,value2 = %d,(value&0x07)-3 = %d\n",value, value2,(value&0x07)-3);
NINT= (double)value2*(double)net_data/30.72/1000000.0;
NFRAC = (2<<22)*((double)value2*(double)net_data/30.72/1000000.0-NINT);
printf("NINT = %d; NFRAC = %d\n", NINT,NFRAC);
uint8_t rf_en = 0;
snowleo_spi_read(fd, 0x05, &rf_en);
if(channel == RF_TX_CHANNEL)
rf_en = rf_en | 0x08;
else
rf_en = rf_en | 0x04;
uint8_t tx[] = {
0x05|(0x1<<7), rf_en,
0x09|(0x1<<7), 0x85, //
(0x10+channel)|(0x1<<7), (uint8_t)(NINT>>1),
(0x11+channel)|(0x1<<7), (((uint8_t)NINT&0x01)<<7)|((uint8_t)(NFRAC>>16)&0x7f),
(0x12+channel)|(0x1<<7), (uint8_t)(NFRAC>>8),
(0x13+channel)|(0x1<<7), (uint8_t)(NFRAC),
(0x15+channel)|(0x1<<7), (value<<2)|(0x01),
//(0x15+channel)|(0x1<<7), (value<<2)|(0x10),
(0x19+channel)|(0x1<<7), (0x2<<6)|64,
(0x5a)|(0x1<<7), 0xb0,
};
struct spi_ioc_transfer tr_freq = {
.tx_buf = (unsigned long)tx,
.rx_buf = 0,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr_freq);
if (ret < 1)
pabort("can't send spi message");
if(channel == RF_RX_CHANNEL) {
//.........这里部分代码省略.........
示例15: main
//.........这里部分代码省略.........
exit(1);
}
// clean receive buffer
for (i=0; i<20; i++)
{
rxbuf[i]=0;
}
// send to tag 0x03
txbuf[0]=0x03;
txbuf[1]=0x0f;
txbuf[2]=0xa0;
spi.len=4003;
nb=4003;
if (cmd==3) // request trace
{
txbuf[0]=0x03;
txbuf[1]=0x00;
txbuf[2]=0x01;
spi.len=4;
nb=4;
}
else if (cmd==0) // retrieve trace
{
//delay_ms(99);
txbuf[0]=0x00;
txbuf[1]=0x03;
txbuf[2]=0xe8;
spi.len=1003;
nb=1003;
}
else if (cmd==4) // set timebase
{
txbuf[0]=0x04;
txbuf[1]=0x00;
txbuf[2]=0x01;
txbuf[3]=level;
spi.len=4;
nb=4;
}
else if (cmd==6) // set triglevel
{
if (level>255)
level=255;
txbuf[0]=0x06;
txbuf[1]=0x00;
txbuf[2]=0x01;
txbuf[3]=level;
spi.len=4;
nb=4;
}
else if (cmd==8) // set trigdir
{
txbuf[0]=0x08;
txbuf[1]=0x00;
txbuf[2]=0x01;
txbuf[3]=level;
spi.len=4;
nb=4;
}
else if (cmd==10) // set trigmode (cont/norm/single)
{
txbuf[0]=0x0a;
txbuf[1]=0x00;
txbuf[2]=0x01;
txbuf[3]=level;
spi.len=4;
nb=4;
}
spi.delay_usecs=0;
spi.speed_hz=spi_speed;
spi.bits_per_word=spi_bits;
spi.cs_change=0;
spi.tx_buf=(unsigned long)txbuf;
spi.rx_buf=(unsigned long)rxbuf;
ret=ioctl(fd, SPI_IOC_MESSAGE(1), &spi);
if (ret<0)
{
fprintf(stderr, "Error performing SPI exchange: %s\n", strerror(errno));
exit(1);
}
fprintf(stdout, "%02x", rxbuf[0]);
for (i=1; i<(nb-3); i++)
{
fprintf(stdout, ",%02x", rxbuf[i]);
}
fprintf(stdout, "\n");
close(fd);
return(0);
}