/****************************************************************************/
/* Copyright 2009-2013 MBARI.                                               */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Written by Mike Risi for Respirometer project in 2009                    */
/* Modified by Bob Herlien for SensorNode for xFOCE, May 2013               */
/****************************************************************************/
#include "periph.h"

void periphInit()
{
    /* Disable Watch Dog Timer */
    RCONbits.SWDTEN = 0;

    /*************************** Map Pins UART 1 ****************************/
    RPINR18bits.U1RXR = 22;	/* Make Pin RP22 U1RX */
    RPOR10bits.RP20R = 3;	/* Make Pin RP20 U1TX */

    /*************************** Map Pins UART 2 ****************************/
    RPINR19bits.U2RXR = 23;	/* Make Pin RP23 U2RX */
    RPOR12bits.RP25R = 5;	/* Make Pin RP25 U2TX */

    /*************************** Map Pins UART 3 ****************************/
    RPINR17bits.U3RXR = 24;	/* Make Pin RP24 U3RX */
    RPOR5bits.RP11R = 28;	/* Make Pin RP11 U3TX */

    /************************** Map SPI1 Bus Pins ***************************/
    RPINR20bits.SDI1R = 19;	/* SDI1 (MISO) RP19  */
    RPOR13bits.RP26R = 7;	/* SDO1 (MOSI) RP26  */
    RPOR13bits.RP27R = 8;	/* SCK1OUT (SCK) RP27*/

    /* Lock the IOLOCK bit so that the IO is not accidentally changed. */
    __builtin_write_OSCCONL(OSCCON | 0x0040);
    
    /* delay 1 cycle before set output bits */
    Nop();

    /********************** Disable Unused Peripherals **********************/
    /* PMD1 */
    _ADC1MD = 1;	/* A/D module off	*/
    _SPI1MD = 0;	/* SPI1 on		*/
    _SPI2MD = 1;	/* SPI2 off		*/
    _U1MD = 0;		/* UART 1-3 on		*/
    _U2MD = 0;
    _I2C1MD = 1;	/* I2C modules off	*/
    
    _T1MD = 0;		/* Timers 1-3 on	*/
    _T2MD = 0;
    _T3MD = 0;
    _T4MD = 1;		/* Timers 4-5 off	*/
    _T5MD = 1;
    
    /* PMD2 */
    _OC1MD = 1;		/* Output comparators off*/
    _OC2MD = 1;
    _OC3MD = 1;
    _OC4MD = 1;
    _OC5MD = 1;
    _OC6MD = 1;
    _OC7MD = 1;
    _OC8MD = 1;
    _IC1MD = 1;		/* Input comparators off*/
    _IC2MD = 1;
    _IC3MD = 1;
    _IC4MD = 1;
    _IC5MD = 1;
    _IC6MD = 1;
    _IC7MD = 1;
    _IC8MD = 1;

    /* PMD3 */
    _I2C2MD = 1;	/* I2C modules off	*/
    _I2C3MD = 1;
    _U3MD = 0;		/* UART 3 on		*/
    _PMPMD = 1;
    _RTCCMD = 1;	/* RTCC off		*/
    _CMPMD = 1;
    _CRCMD = 1;

    /* PMD4 */
    _LVDMD = 1;
    _CTMUMD = 1;
    _REFOMD = 1;
    _U4MD = 1;		/* UART4 off		*/
    _I2C2MD = 1;

    /* PMD5 */
    _OC9MD = 1;		/* Output comparators off*/
    _IC9MD = 1;		/* Input comparators off*/
    
    /* PMD6 */
    _SPI3MD = 1;	/* SPI3 bus off		*/
}

