/****************************************************************************/
/* Copyright 2015 MBARI                                                     */
/****************************************************************************/
/* Summary  : Module to control peripheral modules on PIC24FJ128GA406       */
/* Filename : periph.c                                                      */
/* Author   : Robert Herlien (rah)                                          */
/* Project  : OASIS Mooring Replacement (OASIS5)                            */
/* Revision: 1.0                                                            */
/* Created  : 10/27/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:                                                    */
/* 27oct2015 rah - created                                                  */
/****************************************************************************/

#include "xc.h"
#include "periph.h"


void periphInit()
{
    /* Disable Watch Dog Timer */
    _SWDTEN = 0;

    /* UART Setup - Map the PPS pins to UART 1-4                              */
    /* Note that UART 5 & 6 are on fixed pins so don't need PPS mapping       */
    /* Note also that dig_io.c sets all the Tx bits to output 0 state         */
    /* This will cause the port to go into break when the UART is disabled    */

    _U1RXR = 23;                /* RXD1 = RP23          */
    _RP22R = 3;                 /* RP22 = TXD1          */

    _U2RXR = 10;                /* RXD2 = RP10          */
    _RP17R = 5;                 /* RP17 = TXD2          */

    _U3RXR = 26;                /* RXD3 = RP26          */
    _RP19R = 19;                /* RP19 = TXD3          */

    _U4RXR = 4;                 /* RXD4 = RP4           */
    _RP3R = 21;                 /* RP3 = TXD4           */

    /*  Map the SPI pins        */
    _SCK1R = 18;                /* SCK1 in, RP18, during slave mode*/
    _RP18R = 8;                 /* SCK1 out, RP18, during mstr mode*/
    _SDI1R = 28;                /* SDI1 is on pin 13, RP28      */
    _RP13R = 7;                 /* SDO1 out on pin 14, RP13     */
#ifdef PROTO
    _SS1R = 6;                  /* Slave select on pin 17, RP6  */
#else
    _SS1R = 27;                 /* Slave select on pin 8, RP27  */
#endif

    /* Lock the IOLOCK bit so that the IO is not accidentally changed. */
    __builtin_write_OSCCONL((OSCCON | 0x0040) & 0xfffd);

    /* delay 1 cycle before set output bits */
    Nop();

    /********************** Disable Unused Peripherals **********************/
    /* PMD1 & similar */
    _ADC1MD = 1;        /* A/D module off       */
    _SPI1MD = 0;        /* SPI1 on              */
    _SPI2MD = 1;        /* SPI2 off             */
    _SPI3MD = 1;        /* SPI3 bus off         */
    _SPI4MD = 1;        /* SPI4 bus off         */

    _U1MD = 0;          /* UART1 on             */
    _U2MD = 0;          /* UART2 on             */
    _U3MD = 0;          /* UART3 on             */
    _U4MD = 0;          /* UART4 on             */
    _U5MD = 0;          /* UART5 on             */
    _U6MD = 0;          /* UART6 on             */

    _I2C1MD = 1;        /* I2C modules off      */
    _I2C2MD = 1;
    _I2C3MD = 1;
    
    _T1MD = 0;          /* Timers 1-4 on        */
    _T2MD = 0;
    _T3MD = 0;
    _T4MD = 0;
    _T5MD = 1;          /* Timer 5 off          */
    
    /* PMD2 */
    _OC1MD = 1;         /* Output comparators off*/
    _OC2MD = 1;
    _OC3MD = 1;
    _OC4MD = 1;
    _OC5MD = 1;
    _OC6MD = 1;
    _IC1MD = 1;         /* Input comparators off*/
    _IC2MD = 1;
    _IC3MD = 1;
    _IC4MD = 1;
    _IC5MD = 1;
    _IC6MD = 1;

    /* PMD3 */
    _PMPMD = 1;
    _DAC1MD = 1;
    _RTCMD = 1;
    _CMPMD = 1;
    _CRCMD = 1;
    _CRCPMD = 1;

    /* PMD4 */
    _USB1MD = 1;
    _USBMD = 1;
    _LVDMD = 1;
    _CTMUMD = 1;
    _REFOMD = 1;
    _I2C2MD = 1;
    _HLVDMD = 1;

    /* PMD5 */
    _CCP1MD = 1;
    _CCP2MD = 1;
    _CCP3MD = 1;
    _CCP4MD = 1;
    _CCP5MD = 1;
    _CCP6MD = 1;
    _CCP7MD = 1;

    /* PMD6, 7, 8 */
    _LCDMD = 1;
    _DMA0MD = 1;
    _DMA1MD = 1;
    _CRYMD = 1;
    _CLC1MD = 1;
    _CLC2MD = 1;
    _CLC3MD = 1;
    _CLC4MD = 1;
}
