دوستان کسی رابط spi مربوط به کوبی را راه اندازی کرده یا نه؟؟؟
من یک کد با زبان c < یک کد به زبان پایتون زدم ولی کار نمی کنند.کسی میدونه مشکلش چیه؟
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
static const char *spi_name = "/dev/spidev0.0";
int main(void)
{
int res = 0; // We can use this to monitor the results of any operation.
int j=0;
int i = 0;
// As usual, we begin the relationship by establishing a file object which
// points to the SPI device.
int spiDev = open(spi_name, O_RDWR);
if(spiDev < 1)
{
printf("can not open spi device\n");
return -1;
}
// We'll want to configure our SPI hardware before we do anything else. To do
// this, we use the ioctl() function. Calls to this function take the form
// of a file descriptor, a "command", and a value. The returned value is
// always the result of the operation; pass it a pointer to receive a value
// requested from the SPI peripheral.
// Start by setting the mode. If we wanted to *get* the mode, we could
// use SPI_IOC_RD_MODE instead. In general, the "WR" can be replaced by
// "RD" to fetch rather than write. Also note the somewhat awkward
// setting a variable rather than passing the constant. *All* data sent
// via ioctl() must be passed by reference!
int mode = 0;//SPI_MODE0;
ioctl(spiDev, SPI_IOC_WR_MODE, &mode);
ioctl(spiDev,SPI_IOC_RD_MODE,&mode);
printf("spi mode set to:%d\n",mode);
// The maximum speed of the SPI bus can be fetched. You'll find that, on the
// pcDuino, it's 12MHz.
int maxSpeed = 0;
ioctl(spiDev, SPI_IOC_RD_MAX_SPEED_HZ, &maxSpeed);
printf("Max speed: %dHz\n", maxSpeed);
// In rare cases, you may find that a device expects data least significant
// bit first; in that case, you'll need to set that up. Writing a 0
// indicates MSb first; anything else indicates LSb first.
int lsb_setting = 0;
ioctl(spiDev, SPI_IOC_WR_LSB_FIRST, &lsb_setting);
// Some devices may require more than 8 bits of data per transfer word. The
// SPI_IOC_WR_BITS_PER_WORD command allows you to change this; the default,
// 0, corresponds to 8 bits per word.
int bits_per_word = 8;
ioctl(spiDev, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word);
// Okay, now that we're all set up, we can start thinking about transferring
// data. This, too, is done through ioctl(); in this case, there's a special
// struct (spi_ioc_transfer) defined in spidev.h which holds the needful
// info for completing a transfer. Its members are:
// * tx_buf - a pointer to the data to be transferred
// * rx_buf - a pointer to storage for received data
// * len - length in bytes of tx and rx buffers
// * speed_hz - the clock speed, in Hz
// * delay_usecs - delay between last bit and deassertion of CS
// * bits_per_word - override global word length for this transfer
// * cs_change - strobe chip select between transfers?
// * pad - ??? leave it alone.
// For this example, we'll be reading the address location of an ADXL362
// accelerometer, then writing a value to a register and reading it back.
// We'll do two transfers, for ease of data handling: the first will
// transfer the "read register" command (0x0B) and the address (0x02), the
// second will dump the response back into the same buffer.
struct spi_ioc_transfer xfer;
memset(&xfer, 0, sizeof(xfer));
char dataBuffer[3];
char rxBuffer[3];
dataBuffer[0] = 0x0B;
dataBuffer[1] = 0x02;
dataBuffer[2] = 0x00;
xfer.tx_buf = (unsigned long)dataBuffer;
xfer.rx_buf = 0;//(unsigned long)rxBuffer;
xfer.len = 3;
xfer.speed_hz = 500000;
xfer.cs_change = 1;
xfer.bits_per_word = 8;
while(1)
{
res = write(spiDev,dataBuffer,3);
// res = ioctl(spiDev, SPI_IOC_MESSAGE(1), &xfer);
if(res<3)
{
printf("SPI can not send result: %d\n", res);
// return -1;
}
else{
printf("read Device ID: %d - %d - %d\n", rxBuffer[2], rxBuffer[1], rxBuffer[0]);
}
for(i=0;i<0xffffff;i++);
}
}
و این هم کد پایتون:
import spidev
import time
spi = spidev.SpiDev()
spi.open(0,0)
while 1:
spi.writebytes([0,0,0])
time.sleep(1)
print("send char\n")
در ضمن تنظیمات مربوط به spi در کرنل هم تنظیم کردم