/* ---------------------------------------------------------------------------
 * bus.c - v0.1                                       (c) 2007 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: RS-485 bus driver
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, 28-11-2007, fvds.
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */
#include "bus.h"
#include "protocolfunctions.h"


/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
#define BAUDRATE B38400
#define BUS1_DEVICE "/dev/ttyPSC1"


/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
static int sigio_interrupt = 0;

/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */


void   busInit (t_bus_devices device)
{
	struct termios newtio;

    
    bus1Handle = -1;
    bus2Handle = -1;
	
	if (device == BUS1)
	{        
        bus1Handle = open(BUS1_DEVICE, (O_RDWR | O_NOCTTY | O_NONBLOCK));
        if (bus1Handle == -1)
            {
            printf ("Failed to initialise bus interface\n");
            }
		//bus1Handle = open(BUS1_DEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
        if (bus1Handle < 0) 
		{
			perror(BUS1_DEVICE); 
		}

	}
	
	if (bus1Handle > 0)
	{
		bzero(&newtio, sizeof(newtio));
		
        newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR;
        newtio.c_oflag = 0;
        
        /* set input mode (non-canonical, no echo,...) */
        newtio.c_lflag = 0;
         
        newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
        newtio.c_cc[VMIN]     = 5;   /* blocking read until 5 chars received */
       

        tcflush(bus1Handle, TCIFLUSH);
        tcsetattr(bus1Handle,TCSANOW,&newtio);		
	}
}

/** \brief Write data of a certain length to a serial port.*/  
void busWrite (
        t_bus_devices device, 
        UINT16 length, /**< Lengh of data in bytes */ 
        UINT8 *data   /**< Pointer to data */
)
{
	int handle = (device == BUS1 ? bus1Handle: bus2Handle);
	
	write(handle, data, length);
}

/** \brief Reads data from serial port.
    \retval Length of received data in bytes*/  
UINT16 busRead (
        t_bus_devices device, 
        UINT8 * data /**< Pointer to data */
)
{
	int handle = (device == BUS1 ? bus1Handle: bus2Handle);
	
	return read(handle, data, 255);
}

/** \brief Get byte from serial port.
    \retval bool Returns true if there was data */
BOOLEAN busGet(
        t_bus_devices device,
        UINT8 * byte /**< Pointer to byte to return data*/
)
{
	int handle = (device == BUS1 ? bus1Handle: bus2Handle);
	
	return read(handle, byte, 1);
}

/** \brief Send byte to serial port. */
void busPut(
        t_bus_devices device, 
        UINT8 value /**< Byte to send*/
)
{
	int handle = (device == BUS1 ? bus1Handle: bus2Handle);
	
	write(handle, &value, 1);
}

/** \brief Flush serial port buffers. */
void   busFlush(
        t_bus_devices device
)
{
	int handle = (device == BUS1 ? bus1Handle: bus2Handle);

	tcflush(handle, TCIOFLUSH);  // Flushes both input as output
}

/** \brief Check if receive buffers is empty.
    \retval bool Returns true if recieve buffer is empty */
BOOLEAN busEmpty(
        t_bus_devices device
)
{
	return FALSE;
}



