/*******************************************************************************
** Document: FW_gpio_api.c
** Copyright (c) 2004 R&D Center, Tailyn Communication Company, Ltd.  
** Created by: Chien Chung Lo
** Date: Nov. 25, 2004
** Description: Firmware interface API.
** Version: 0.1
*******************************************************************************/
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <FW_gpio/FW_gpio_api.h>

#include <umap_type.h>

//----------------------------------------------------------------------
UMAP_S32 FW_ReadGpio(UMAP_U8* value, UMAP_U8 addr)
{
	UMAP_S32 gpio_file;
	UMAP_S32 result;
	
	gpio_file = open(GPIO_DEVICE_FILE_NAME, O_RDONLY);
	if(gpio_file < 0)
	{
		result = gpio_file;
		return result; // open fail!!
	}
	
	result = lseek(gpio_file, addr, SEEK_SET);
	if(result < 0)
	{	
		close(gpio_file);
		return result; // lseek fail!!
	}
	
	result = read(gpio_file, value, 1);
	if(result < 0)
	{	
		close(gpio_file);
		return result; // read fail!!
	}

	*value = *value & 0x1;
	result = close(gpio_file);
		
	return 0; // succeed
}

//----------------------------------------------------------------------
UMAP_S32 FW_WriteGpio(UMAP_U8 value, UMAP_U8 addr)
{
	UMAP_S32 gpio_file;
	UMAP_S32 result;
	
	value = value & 0x1;
	
	gpio_file = open(GPIO_DEVICE_FILE_NAME, O_WRONLY);
	if(gpio_file < 0)
	{
		result = gpio_file;
		return result; // open fail!!
	}
	
	result = lseek(gpio_file, addr, SEEK_SET);
	if(result < 0)
	{	
		close(gpio_file);
		return result; // lseek fail!!
	}	
	
	result = write(gpio_file, &value, 1);
	if(result < 0)
	{	
		close(gpio_file);
		return result; // write fail!!
	}
	
	result = close(gpio_file);
	
	return 0; // succeed
}



