PDA

توجه ! این یک نسخه آرشیو شده میباشد و در این حالت شما عکسی را مشاهده نمیکنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : راه اندازی spi کوبی برد



saeedhadi
15-09-2013, 17:32
دوستان کسی رابط 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 در کرنل هم تنظیم کردم

saeedhadi
17-09-2013, 15:24
مشکل مربوط به تنظیمات پایه در اسکریپ تنظیمات بود.در ضمن یادتون باشه این رابط spi به صورت halfduplex می باشد و من با دستور read ,write تونستم با میکروی lpc1768 ارتباط برقرار کنم با سرعت حداکثر 8mhz
مشکل داشتید مطرح کنید جواب بدم.

silentshot
17-09-2013, 20:47
با تشکر اگه راه اندازی این رابط رو تحت یک اموزش بزارید ممنون می شیم
در ضمن دوستان هم به اسم شما در بخش ویکی قرار می دن

با تشکر

saeedhadi
20-09-2013, 01:02
این اموزش تکمیلی تری هست که به صورت یک فایل برای استفاده دوستان اپلود کردم امیدوارم به درد بخورد.

jalil_211
19-12-2014, 10:16
مشکلی که من دارم اینه که بعد از کامپل کرنل با وجود استفاده از دستور modeprob هیچ چیزی به عنوان spi تو پوشه ی dev نمی یاد.

jalil_211
28-12-2014, 09:13
مشکل dev حل شد. حالا برنامه تستی که در pdf i هست رو اجرا که می کنم هیچ دیتایی روی اسکوپ تو پایه های spi نمی بینم.لطفا کمک کنید.fex فایل هم اینه

ahravan
29-12-2014, 14:06
ابتدا سعی کنید از آخرین نسخه ی کرنل به روز شده استفاده کنید،با توجه به مسیر توسعه ی چیپ های فوق (You can see links before reply) - spi برای تمامی چیپ ها تکمیل شده است.
دسترسی به اخرین نسخه ای که از کوبین انتشار یافت . (You can see links before reply) میتونی config کرنل رو هم برای Spi چک کنی تا مطمئن بشی.

دستور
modeprob برای ماژل هاست،اگه اشتباه نکنم، همچنین spi به عنوان درایور ثابت کرنل اضافه شده باشه نیازی به modeprob نیست.
دسترسی به سند اصلی spi (You can see links before reply)

سایت حمایت از کوبی و برد های لینوکس (در اینده ای نزدیک مثال های spi و I2c رو قرار خواهیم داد) :
bir-robotic.ir

bir-robotic.ir (You can see links before reply)

jalil_211
31-12-2014, 09:36
من modprobe رو انجام دادم.تو dev هم میاد تو نرم افزار هم ظاهرا همه چی درسته و داده ها دارن ارسال میشن ولی با اسکوپ چیزی رو پایه های کارت نمیبینم.
نکنه باید حتما به یه جایی وصلش کنم؟