/****************************************************************************/
/* Copyright 2015 MBARI                                                     */
/****************************************************************************/
/* Summary  : Serial I/O for OASIS5 on PIC32MX470F512L                      */
/* Filename : serial.h                                                      */
/* Author   : Robert Herlien (rah)                                          */
/* Project  : OASIS Mooring Replacement (OASIS5)                            */
/* Revision: 1.0                                                            */
/* Created  : 07/13/2015                                                    */
/*                                                                          */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*                                                                          */
/****************************************************************************/
/* Modification History:                                                    */
/* 13jul2015 rah - created                                                  */
/****************************************************************************/

#ifndef SERIAL_H
#define SERIAL_H

#include "sys_timer.h"


/********************************************/
/*      Typedefs and Constants              */
/********************************************/

#define PIC_UARTS       5
//#define DB_UARTS        12
#define DB_UARTS        0
#define SER_PORT_TOTAL  (PIC_UARTS + DB_UARTS)

#define RX_BUFF_SIZE    256     //Double these for final code
#define TX_BUFF_SIZE    128

#define UxMODE          0x8800
#define UxSTA           0x1400

#define FastBRG(baud)   (unsigned int)((PBCLK/(4*baud)) - 1)
#define StdBRG(baud)    (unsigned int)((PBCLK/(16*baud)) - 1)

#define SER_PORT_0     0
#define SER_PORT_1     1
#define SER_PORT_2     2
#define SER_PORT_3     3
#define SER_PORT_4     4

#define SER_CONSOLE    0
#define STD_BAUD       9600

/* data/parity settings */
#define D8_NONE         0
#define D8_EVEN         1
#define D8_ODD          2
#define D9_NONE         3

/* stop bits */
#define S_1             1
#define S_2             2


/********************************/
/*    Macros                    */
/********************************/

#define isSerPortValid(port)  ((port >= SER_PORT_0) && (port < SER_PORT_TOTAL))


/********************************************/
/*      Function Declarations               */
/********************************************/

void serInit(); 

int serGetByte(int port, unsigned char* b);
int serPutByte(int port, unsigned char b);
int serPutString(int port, char* buff);

void serSetConfig(int port, unsigned long baud, int data_par, int stop);
void serGetConfig(int port, unsigned long *baud, int *data_par, int *stop);

int  serRxAvail(int port);
void serRxFlush(int port);
void serTxFlush(int port);

#endif
