LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 12-04-2012, 08:25 AM   #1
ravi_chobey
Member
 
Registered: Sep 2007
Location: Bangalore,India
Distribution: Fedora,Mandriva,Debian
Posts: 180

Rep: Reputation: 30
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, &reg);
		printf("Status is: 0x%02hhx\n", (*((char *)&reg) & 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded board question elico Linux - Embedded & Single-board computer 4 03-17-2012 09:49 AM
embedded board question elico Linux - Software 8 02-19-2012 02:47 PM
Kernel Testing using LTP on Beagle board (ARM based Embedded Dev Board) Rajender.G Linux - Newbie 0 07-27-2011 05:19 AM
gigabit embedded board taltman Linux - Embedded & Single-board computer 2 02-23-2008 10:06 PM
power down on an embedded board knobby67 Debian 1 03-27-2006 06:35 AM


All times are GMT -5. The time now is 12:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration