/** \file maximPmod.h ******************************************************
 * 
 *             Project: Maxim Plug-in Peripheral Modules
 *            Filename: maximPMOD.h
 *         Description: This module contains the Main application for the
 *                      Avnet Zedboard board implementation of example
 *                      programs for the Maxim Plug-in Peripheral Modules
 *
 *    Revision History:
 *\n       7-20-12 Rev 1.4 Nathan Young    Initial release
 *\n       9-04-12 Rev 1.5 Nathan Young    Multiple PMOD port support
 *\n       10-15-12 Rev 1.6 Nathan Young	OLED display support
 * 
 *  --------------------------------------------------------------------
 * 
 *    This code follows the following naming conventions.
 * 
 * 	  char                   chPmodValue
 * 	  char (array)           sPmodString[16]
 * 	  float                  fPmodValue
 * 	  int                    nPmodValue
 * 	  int (array)            anPmodValue[16]
 * 	  u16                    uPmodValue
 * 	  u8                     uchPmodValue
 * 	  u8 (array)             auchPmodBuffer[16]
 * 	  unsigned int           unPmodValue
 * 	  int *                  punPmodValue
 * 
 * ------------------------------------------------------------------------- */
/*
 * Copyright (C) 2012 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 ***************************************************************************/

#ifndef MAXIMPMOD_H_
#define MAXIMPMOD_H_

#include "xgpio.h"
#include "xgpio_l.h"
#include "xparameters.h"
//#include "xuartlite.h"
#include "xspi_l.h"
#include "xspi.h"
#include "xiic_l.h"
#include "utilities.h"
#include <string.h>
#include <stdio.h>
#include "platform.h"
#include "math.h"

#define DEFAULT_HYPERTERMINAL_UART_ID XPAR_PS7_UART_1_DEVICE_ID  //!< macro used to abstract Physical Port of Hyperterminal UART
#define DEFAULT_HYPERTERMINAL_UART_ADDRESS XPAR_PS7_UART_1_BASEADDR

#define ABOUT_ONE_SECOND 74067512      //!< approx 1 second delay when used as argument with function delay(numberCyclesToDelay)
// Update this if uBlaze/Zynq CPU core frequency is changed, or if the external memory timing changes.
// Although emprirically tested to 1.0000003 seconds, it is not meant to be used for precise timing purposes

#define PMOD_TYPE_UART 0              //!< value which specifies UART in maximProduct structure
#define PMOD_TYPE_SPI 1               //!< value which specifies SPI in maximProduct structure
#define PMOD_TYPE_GPIO 2              //!< value which specifies GPIO in maximProduct structure
#define PMOD_TYPE_I2C 3               //!< value which specifies I2C in maximProduct structure

#define PMOD_PORT_TYPE_UART 0x00      //!< value to switch UART controller into a Pmod port in max_configure_PMOD_port
#define PMOD_PORT_TYPE_SPI  0x01      //!< value to switch SPI controller into a Pmod port in max_configure_PMOD_port
#define PMOD_PORT_TYPE_GPIO 0x02      //!< value to switch GPIO controller into a Pmod port in max_configure_PMOD_port
#define PMOD_PORT_TYPE_I2C	0x03      //!< value to switch I2C controller into a Pmod port in max_configure_PMOD_port

#define BIT_BANG_SPI_CLK 0x08         //!< bit mask for SPI CLK used in max_GPIO_bit_bang_SPI_write
#define BIT_BANG_SPI_CLRB 0x04        //!< bit mask for SPI /CLR used in max_GPIO_bit_bang_SPI_write
#define BIT_BANG_SPI_MISO 0x04        //!< bit mask for SPI MISO used in max_GPIO_bit_bang_SPI_write
#define BIT_BANG_SPI_MOSI 0x02        //!< bit mask for SPI MOSI used in max_GPIO_bit_bang_SPI_write
#define BIT_BANG_SPI_SSB 0x01         //!< bit mask for SPI SS used in max_GPIO_bit_bang_SPI_write

#define RE_B 0x00000001               //!< bit mask for RS-485 /RE used in menu_MAX14840
#define DI 0x00000002                 //!< bit mask for RS-485 DI used in menu_MAX14840
#define RO 0x00000004                 //!< bit mask for RS-485 RO used in menu_MAX14840
#define DE 0x00000008                 //!< bit mask for RS-485 DE used in menu_MAX14840

/*
 * The following constants defines the address of the IIC devices on the IIC bus.
 * For address, the Xilinx device drivers only use the upper 7 bits (ignoring the R/W#) bit
 * This means the Xilinx drivers need to use the real address bits bit-shifted one position right
 * In other words, the address divided by 2
 */
#define MAX44009_IIC_ADDRESS 0x4B     //!< The actual address is 8'b1001 011x
#define DS1077_IIC_ADDRESS 0x58       //!< The actual address is 8'b1011 000x
#define MAX9611_IIC_ADDRESS 0x7F      //!< A0 and A1 default to high, so actual address is 8'b1111 111x
#define MAX5380_IIC_ADDRESS 0x31      //!< Address of MAX5380 DAC with "M" suffix.  (Other options are 0x30 0x32, 0x33)
#define MAX5478_IIC_ADDRESS 0x29      //!< The actual address is 8'b0101 000x (0x52) with jumpers = 001 (J4, J3, J2)
#define MAX5825_IIC_ADDRESS 0x10      //!< The actual address is 8'b0010 000x (0x20)
#define MAX7304_IIC_ADDRESS 0x38      //!< The actual address is 8'b0111 000x (0x70)
#define MAX44000_IIC_ADDRESS 0x4A     //!< The actual address is 8'b1001 010x (0x94)
#define DS3231M_IIC_ADDRESS 0x68      //!< The actual address is 8'b1101 000x (0xD0)
#define DS1086L_IIC_ADDRESS 0x58      //!< The actual address is 8'b1011 000x (0xB0)

#define MAX9611_CSA_REGISTER 0x00     //!< I2C reg_addr for CSA data (MSB).  LSB = 0x01
#define MAX9611_RS_REGISTER 0x02      //!< I2C reg_addr for RS data (MSB).  LSB = 0x03
#define MAX9611_OUT_REGISTER 0x04     //!< I2C reg_addr for OUT data (MSB).  LSB = 0x05
#define MAX9611_SET_REGISTER 0x06     //!< I2C reg_addr for SET data (MSB).  LSB = 0x07

struct maximProduct                   //!< Structure holding info on each module for menus
{
	char sPartNumber[10];
	char sPartName[80];
	u8 uchPmodType;  // pmod:  i2c, spi, gpio, etc
	u8 uchKeypress;
};

struct maximDateTime                  //!< Structure to hold time, date, day of week
{
	u8 uchHour;
	u8 uchMinute;
	u8 uchSecond;
	u8 uchMonth;
	u8 uchDate;
	u8 uchYear;
	u8 uchDow;
};

struct maximOLEDDisplay
{
	XGpio xgpioPort;
	u8 portStatus;
	u8 writeBuffer[512];
	u8 flippedBuffer[512];
	u8 *font;
};

#define INPUT_STRING_MAX_SIZE 16
extern XGpio g_xGpioPmodPortA;
extern XGpio g_xGpioPmodPortB;
extern XGpio g_xGpioPmodPortC;
extern XGpio g_xGpioPmodPortD;
extern XGpio g_xGpioPmodPortMuxIO;
extern char g_sInputString[ INPUT_STRING_MAX_SIZE ];	//This string will hold user input from terminal.

extern int g_nActivePMODPort;
extern u32 g_unActivePeripheralAddressI2C;
extern u32 g_unActivePeripheralAddressSPI;
extern u32 g_unActivePeripheralAddressUart;
extern XGpio *g_pActiveGPIOPort;
extern struct maximOLEDDisplay g_structureOLED;
extern char g_tempString[32];

extern unsigned char g_auchPortType[4];

#endif /* MAXIMPMOD_H_ */
