ltib
[Top][All Lists]
Advanced

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

[Ltib] Read from /dev/spi


From: elex S
Subject: [Ltib] Read from /dev/spi
Date: Mon, 1 Nov 2010 20:27:55 +0530

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?

Regards,
Elex


reply via email to

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