[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Ltib] Read from /dev/spi

From: Peter Barada
Subject: Re: [Ltib] Read from /dev/spi
Date: Mon, 01 Nov 2010 11:34:35 -0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20101027 Thunderbird/3.0.10

On 11/01/2010 10:57 AM, elex S wrote:

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 */
    wr_buf[0] = AT25_WREN;
    if (write(file, wr_buf, 1) == -1) {
      printf("error in writing to AT25_WREN \n");

    // 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");

    // 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");
      printf("no error in writing 2 \n");

    if (read(file, rd_buf, 5) == -1) {
        printf("error inreading \n");

    printf("\n rd buf = %s\n", rd_buf);

    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.


Peter Barada

reply via email to

[Prev in Thread] Current Thread [Next in Thread]