fram_test.c 1.68 KB
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ftw.h>
#include <string.h>

#define RW_SIZE		32

#define DEV_NAME    "/sys/bus/spi/devices/spi0.0/fram"

unsigned char *cWriteBuffer = NULL;
unsigned char *cReadBuffer = NULL;


int RWDateCompare(void)
{
	int i;

	for(i=0; i<RW_SIZE; i++)
	{
		if( cWriteBuffer[i] != cReadBuffer[i] )
			return -1;
	}

	return 0;
}

int Fram_Write(int fd)
{
	int ret;

	lseek(fd, 0, SEEK_SET);
	ret = write(fd, cWriteBuffer, RW_SIZE);

	if(ret != RW_SIZE) 
	{
		fputs("Error Write()\n",stderr);
		free(cWriteBuffer);
		free(cReadBuffer);
		exit(-EIO);
	}

	return 0;
}

int Fram_Read(int fd)
{
	int ret;

	lseek(fd, 0, SEEK_SET);
	ret = read(fd, cReadBuffer, RW_SIZE);

	if(ret != RW_SIZE)
	{
		fputs("Error Read()\n",stderr);
		free(cWriteBuffer);
		free(cReadBuffer);
		exit(-EIO);
	}

	return 0;
}


int main(int argc, char **argv)
{

	int ret, i;
	unsigned int fd;

	printf("### Fram Read/Write Test App Start!! ###\n");
	cWriteBuffer = (unsigned char *)malloc( sizeof(unsigned char)*RW_SIZE);
	if( cWriteBuffer == NULL)
		return -ENOMEM;

	cReadBuffer = (unsigned char *)malloc( sizeof(unsigned char)*RW_SIZE);
	if( cReadBuffer == NULL)
		return -ENOMEM;

	memset(cWriteBuffer, 0, RW_SIZE);
	memset(cReadBuffer, 0, RW_SIZE);

	fd = open (DEV_NAME, O_RDWR);
	if(fd < 0) {
		fputs("Error open()\n",stderr);
		free(cWriteBuffer);
		free(cReadBuffer);
		return -ENODEV;
	}

	// Init 0 (32Byte)
	Fram_Write(fd);

	// Set Value
	for(i=0; i<RW_SIZE; i++)
		cWriteBuffer[i] = i;


	Fram_Write(fd);

	Fram_Read(fd);

	ret = RWDateCompare();

	printf("### Fram Read/Write Test App End!! ###\n");
	return ret;
}