Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-04-2012, 08:25 AM
|
#1
|
|
Member
Registered: Sep 2007
Location: Bangalore,India
Distribution: Fedora,Mandriva,Debian
Posts: 178
Rep:
|
FlashROM interface with Embedded board
Hi All,
I am having an embedded SBC(master) and slave a 8051 based RF module having 32kbs of Internal ROM. I am having SPI bus to access that internal ROM.When i am sending some data from my master to slave using SPI bus, i am able see some data on MOSI line and after that data, i need to get some response from the slave.I am not confident that data is written properly on 00,01,02,03 address of flash ROM.I have added the code for your reference, please let me know whats wrong i am doing.
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/ioctl.h>
#include <linux/spi/spidev.h>
void run_test(int fd)
{
int size,l,size1;
unsigned char buf[100];
printf("\nrun test 1\n");
for(l=0;l<=32;l++)
{
size1 = snprintf(buf,100,"AC");
write(fd,(char *)buf,sizeof(size1));
printf("\n%x\n",buf);
size1 = snprintf(buf,100,"53");
write(fd,(char *)buf,sizeof(size1));
printf("\n%x\n",buf);
size1 = snprintf(buf,100,"AA");
write(fd,(char *)buf,sizeof(size1));
printf("\n%x\n",buf);
usleep(4.5);
size1 = snprintf(buf,100,"55");
write(fd,(char *)buf,sizeof(size1));
printf("All the 4 bytes are sucessfully written\n");
read(fd,(char *)buf,sizeof(size1));
printf("\n\n%x\n\n",buf);
if(strcmp(buf, "53") == 0)
{
printf("successfull!\n");
} else {
printf("failed.\n");
}
}
}
/* size = snprintf(buf, 100, "Invalid data \n");
read_test(fd, 0, buf, size);
buf[size] = 0;
print_hex(buf, size);
size = snprintf(buf, 100, "Hello world!");
printf("Writing \"%s\"\n", buf);
write_test(fd, 0, buf, size);
snprintf(buf, 100, "Invalid data \n");
// Give the EEPROM enough time to commit
usleep(5000);
read_test(fd, 0, buf, size);
buf[size] = 0;
print_hex(buf, size);
write_disable(fd);
if(strcmp(buf, "Hello world!") == 0)
{
printf("SPI EEPROM Read/Write test successfull!\n");
} else {
printf("SPI EEPROM Read/Write failed.\n");
}*/
#define SPI_MSB 0
#define SPI_LSB 1
int initialize_spi(const char *dev, char mode, char msb, char bpw, __u32 max_speed)
{
char fd;
fd = open(dev, O_RDWR);
if(fd < 0)
{
perror("open");
return -1;
}
if(ioctl(fd, SPI_IOC_WR_MODE, &mode))
{
perror("SPI_IOC_WR_MODE");
return -1;
}
if(ioctl(fd, SPI_IOC_WR_LSB_FIRST, &msb))
{
perror("SPI_IOC_WR_LSB_FIRST");
return -1;
}
if(ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bpw))
{
perror("SPI_IOC_WR_BITS_PER_WORD");
return -1;
}
if(ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &max_speed))
{
perror("SPI_IOC_WR_MAX_SPEED_HZ");
return -1;
}
return fd;
}
int main(int argc, char **argv)
{
const char *dev = "/dev/spidev1.2";
int fd, opt;
while ((opt = getopt(argc, argv, "01")) != -1)
{
switch(opt)
{
case '0':
dev = "/dev/spidev1.0";
break;
case '1':
dev = "/dev/spidev1.2";
break;
default:
fprintf(stderr, "%s: Pass -0 for McSPI3.CS0 test, or -1 for McSPI3.CS1 test\n", argv[0]);
exit(-1);
}
}
if(optind < argc)
{
printf("Opening \"%s\" for SPI communication\n", argv[optind]);
fd = initialize_spi(argv[optind], SPI_MODE_0, SPI_MSB, 8, 800000);
} else {
printf("Opening \"%s\" for SPI communication\n", dev);
fd = initialize_spi(dev, SPI_MODE_0, SPI_MSB, 8, 800000);
}
if(fd < 0)
{
fprintf(stderr, "initialize_spi failed\n");
return -1;
}
/* {
struct at25080a_status_register reg;
get_status(fd, ®);
printf("Status is: 0x%02hhx\n", (*((char *)®) & 0xff));
}*/
run_test(fd);
close(fd);
return 0;
}
Regards,
Ravi
Last edited by onebuck; 12-22-2012 at 11:21 AM.
Reason: cleanup & use proper VBcode tags
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:41 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|