/*
 *            Copyright (c) 1998-2000 by NETsilicon Inc.
 *
 *  This software is copyrighted by and is the sole property of
 *  NETsilicon.  All rights, title, ownership, or other interests
 *  in the software remain the property of NETsilicon.  This
 *  software may only be used in accordance with the corresponding
 *  license agreement.  Any unauthorized use, duplication, transmission,
 *  distribution, or disclosure of this software is expressly forbidden.
 *
 *  This Copyright notice may not be removed or modified without prior
 *  written consent of NETsilicon.
 *
 *  NETsilicon, reserves the right to modify this software
 *  without notice.
 *
 *  NETsilicon
 *  411 Waverley Oaks Road                  USA 781.647.1234
 *  Suite 227                               http://www.netsilicon.com
 *  Waltham, MA 02452                       AmericaSales@netsilicon.com
 *
 *************************************************************************
 *
 *     Module Name: netos_serl.h
 *         Version: 1.00
 *   Original Date: 03/27/01
 *          Author: Naum Gurevich
 *        Language: Ansi C
 * Compile Options:
 * Compile defines:
 *       Libraries:
 *    Link Options:
 *
 *    Entry Points:
 *
 * Description.
 * =======================================================================
 * This header file contains declarations and constants used by the
 * NET+OS SPI driver.
 * 
 * Edit Date/Ver   Edit Description
 * ==============  ===================================================
 * LZ   10/14/02   Changed writeAddr & readAddr to WORD16. Fairchild's
 *                 EEPROM is 64k bit, address is beyond one byte.
 * JZW  05/16/03   Replaced c++-style comments with c-style comments
 * JZW  06/24/04   Removed references to O_SPI_CLOCK_HI. The option does not exist
 */

/* netos_spi.h */

/*
 * Header file for netos SPI driver.
*/


#ifndef _NETOS_SPI_H_
#define _NETOS_SPI_H_
#include <spiDev.h>
#include "bsp_api.h"
#include <bool.h>
#include <semaphore.h>
#include <gpio_def.h>


#include "../serial/netos_serl.h"

#define PORTAB_MASK         (0x67670067U)
#define PORTAB_MASTER_VALUE (0x98900000U)
#define PORTAB_SLAVE_VALUE  (0x98800000U)
#define PORTAB_BLICKSEL_VALUE  (0x88800000U)

#define PORTC_CHA_MASK         (0x7F7F007FU)
#define PORTC_CHA_MASTER_VALUE (0x80800000U) 
#define PORTC_CHA_SLAVE_VALUE  (0x80000000U)

#define PORTC_CHB_MASK         (0xDFDF00CFU)
#define PORTC_CHB_MASTER_VALUE (0x20200000U) 
#define PORTC_CHB_SLAVE_VALUE  (0x20000000U)

#define SPI_20UM_PORTA_MASTER	(GPIO_TXD |GPIO_RXD |GPIO_SPI_M_CLK_OUT |GPIO_20UM_SPI_M_ENABLE_A)
#define SPI_20UM_PORTA_SLAVE	(GPIO_TXD |GPIO_RXD |GPIO_SPI_S_CLK_IN |GPIO_20UM_SPI_S_ENABLE_A)

#define SPI_20UM_PORTC_MASTER   (GPIO_20UM_TXDB |GPIO_20UM_RXDB |GPIO_20UM_SPI_M_CLK_B_OUT |GPIO_20UM_SPI_M_ENABLE_B_OUT)
#define SPI_20UM_PORTC_SLAVE    (GPIO_20UM_TXDB |GPIO_20UM_RXDB |GPIO_20UM_SPI_S_CLK_B_IN |GPIO_20UM_SPI_S_ENABLE_B_IN)

#define SPI_MASTER_MODE_VALUE  (0x00200000)
#define SPI_SLAVE_MODE_VALUE   (0x00300000)

#define SPI_LSB_FIRST_VALUE    (0x00000000)
#define SPI_MSB_FIRST_VALUE    (0x00080000)

#define DEFAULT_SPI_BAUD_RATE       (57600)  /* default baud rate */

/* command codes for EEPROM XICOR X25020 */

#define XICOR_WRITE  ((char)0X02)
#define XICOR_READ   ((char)0X03)
#define XICOR_WRITEN ((char)0X06)
#define XICOR_RDSR   ((char)0X05)
#define XICOR_WRSR   ((char)0X01)


typedef struct           /* ONSPI_CHAN */
{
    /* must be first */
    /* SIO_CHAN sio; */        /* standard SIO_CHAN element */
    UINT8   rxlevel;       /* Rx interrupt level for this device */
    UINT8   txlevel;       /* Tx interrupt level for this device */
    UINT8   rxDmalevel;    /* Rx DMA interrupt level for this device */
    UINT8   txDmalevel;    /* TX DMA interrupt level for this device */
    int     baudRate;        /* the current baud rate */
    int     port;            /* port number for this channel */
    bool    opened;         /* whether the serial port is already in use */
    unsigned openmode;    /* RD, WR, DMA, MASTER, O_SPI_BLOCKSEL  */
	int     hwConfiguration; /* Direct, XICOR EEPROM */
	bool    slaveMode;      /* true when using a slave mode of operation */
    bool    LSBfirst;       /* true when sending LSB first */
    bool    readInProgres;  /* set to true when read operation is in progress */
  	sem_t   *spi_int_sem;	/* semaphore for the ISR routines */
   	WORD16  writeAddr;       /* Starting address for the next write operation */
   	WORD16  readAddr;        /* Starting address for the next read operation */
   	bool    initSeq;         /* Driver is in the initialization sequence     */
   	bool    blockSEL;        /* SEL signal is blocked                        */
   	unsigned long * SSport ;  /* GPIO port for generating  "Chip Select" signal */
   	unsigned long SSsetHiValue; /* Bit to set "Slave Select" signal "Inactive Hign" */
   	unsigned long SSsetLowMask; /* Mask to set "Slave Select" signal "actve low" */
   	int     ISRlevel;               /* ISR level for the interrupt on the "End of Block".*/
   	int     ISRbit;                 /* Bit in the PORTC used to generate and interrupt on the "End of Block". */
   	int     slaveTimeOutValue;      /* Value for the slave time out period in ticks*/
   	bool    startClockHigh;        /* true if initial value for the CLK signal high */
   	bool    TXCINVset;             /* true if  TXD to be driven on rising edge of TX clock  */
   	bool    RXCINVset;             /* true if RRXD to be sampled on falling edge of RX clock */
   	bool    IOinProgress;          /* true if driver performs read or write operation */
} ONSPI_CHAN;


    /* structure for storing input and output characters */
typedef union 
{
    int dummyInt[2];
    char bufferData[2*4];
} SERVICEBUFFER;
    
typedef struct 
{
    int countServData;          /* number of char in the  service buffer */
    int countForBufferData;     /* number of char in the data buffer */
    char *ptrDataBuffer ;       /* pointer to the data buffer */
    char *ptrSerData ;          /* pointer to the service buffer */
    char *ptrDmaBuffer;        /* pointer to the DMA buffer. This buffer is needed
				  only because we have problems with the 2 DMA
			          buffers. When this problem will be corrected
			          we can do without it
			          */
    SERVICEBUFFER serBufferData; /* server buffer out */
} SPIBUFFER;



   
   extern int InstallSpiDevice(char *port);     /* This function search for the device table for  */
                                                /* for empty slot to install the serial driver */

#endif   /* netos_spi.h */
