|
From: | elex S |
Subject: | Re: [Ltib] Read from /dev/spi |
Date: | Wed, 3 Nov 2010 19:01:09 +0530 |
Date: Mon, 01 Nov 2010 11:34:35 -0400
From: Peter Barada <address@hidden>
Subject: Re: [Ltib] Read from /dev/spi
To: address@hidden
Message-ID: <address@hidden>
Content-Type: text/plain; charset="iso-8859-1"
On 11/01/2010 10:57 AM, elex S wrote:
> Hello,
>
> I am using PHYTEC LPC3250 board. I want to write application that
> could do read/write over SPI bus. For that, I have enabled support for
> SPI while configuring LTIB.
> '' /proc/devices' shows SPI having major number '153'. Hence I have
> created the device nod '/dev/spi1' using following command:
>
> $ mknod -m 666 /dev/spi1 c `cat /proc/devices | grep spi | head
> -c 4` 0
>
> Now, to test this I have decided to read/write over "SPI EEPROM".
> Write to eeprom works fine.(I have confirmed it using
> "/sys/bus/spi/devices/spi0.0/eeprom".)
> However read in following program always returns 0xFF.
>
> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <string.h>
> #include <sys/ioctl.h>
> #include <sys/stat.h>
> #include <sys/time.h>
> #include <linux/types.h>
> #include <linux/spi/spidev.h>
> #include <sys/select.h>
> #include <errno.h>
>
> // Write Enable
> #define AT25_WREN 0x06
> // write disable
> #define AT25_WRDI 0x04
> //read status regs
> #define AT25_RDSR 0x05
> //write status regs
> #define AT25_WRSR 0x01
> //read data array
> #define AT25_READ 0x03
> //read data array
> #define AT25_WRITE 0x02
>
>
> int main(int argc, char* argv[])
> {
>
> int file;
> char filename[50] = {"/dev/spi1"};
>
> unsigned char wr_buf[50] = {0};
> unsigned char rd_buf[50] = {0};
>
>
> file = open(filename, O_RDWR);
> if (file < 0) {
> printf("error in opening SPI file %s\n", filename);
> /* ERROR HANDLING; you can check errno to see what went wrong */
> exit(1);
> }
>
> wr_buf[0] = AT25_WREN;
> if (write(file, wr_buf, 1) == -1) {
> printf("error in writing to AT25_WREN \n");
> }
>
> usleep(100);
>
> // write to EEPROM
> wr_buf[0] = AT25_WRITE;
> wr_buf[1] = 0x00;
> wr_buf[2] = 0x00;
> strcpy(&wr_buf[3],"Hello World");
> if (write(file, wr_buf, 25) == -1) {
> printf("error in writing \n");
> }
>
> usleep(100);
>
> // send command to set RD address
> wr_buf[0] = AT25_READ;
> wr_buf[1] = 0x00;
> wr_buf[2] = 0x00;
>
> if (write(file, wr_buf, 3) == -1) {
>
> printf("error in writing \n");
> }
> else
> {
> printf("no error in writing 2 \n");
> }
>
> usleep(100);
>
> if (read(file, rd_buf, 5) == -1) {
>
> printf("error inreading \n");
> }
>
> printf("\n rd buf = %s\n", rd_buf);
>
> close(file);
> return 0;
> }
>
>
>
> Could you please provide me pointer to solve the issue?
You really want to use spidev for this.
In your kernel source look at Documentation/spi/spidev for a description.
You'll have to modify the kernel source to add a "spidev" SPI device
which should cause udev (if you're running it) to create
"/dev/spidevX.Y" where "X" is the SPI bus and "Y" is the chip select on
that bus(both specified in the spidev SPI device). Then you can write a
userspace program that can access the spi device using spi messages.
>
> Regards,
> Elex
>
--
Peter Barada
address@hidden
[Prev in Thread] | Current Thread | [Next in Thread] |