/*
 * init_TM4C123GH6PM_64pin.c
 *
 *  Created on: Apr 18, 2021
 *      Author: thomm
 */

#include "system.h"
#include "init.h"

#include "microsd.h"
#include "uartstdio.h"  // User local version with larger RX buffer
#include "sleep.h"
#include "i2c_fram.h"
#include "board_util.h"

extern int microSD_flg;
extern struct systemSamplingData sys_samp;

// TODO, use #include for these...
extern void profile2_toggle(int target);
extern void ADS1248_gpio_init(void);
extern void AUX1_init(void);
extern void optode_init(void);
extern void led_init(void);

void init_TM4C123GH6PM_64pin(void);


void init_TM4C123GH6PM_64pin(void)
{

    //TM4C123GH6PM = 64   (43 GPIO)
    //PA0 - PA7       8
    //PB0 - PB7       8
    //PC0 - PC7       8
    //PD0 - PD7       8
    //PE0 - PE5       6
    //PF0 - PF4       5

    /*** Enable the GPIO ports used ***/
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //PA0 - PA7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //PB0 - PB7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); //PC0 - PC7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); //PD0 - PD7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); //PE0 - PE5       6
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //PF0 - PF4       5

    /*** Initialize Green and Red LED ***/
    // This is done early in init to enable oscope timing
    led_init();     // defd in board_util.c
    led_power_on();  //green led

#ifdef EXPERIMENT
    /*** Initialize UART0 for use as the console, printf, etc ADDED 19May2022 - turn on rs232 xlator and in a bad spot.   Make sure interrupts are off???***/
    //17       PA0/U0RX      PA0_CONSOLE_RX0
    //18       PA1/U0TX      PA1_CONSOLE_TX0
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 115200, 16000000);       // 115200 bits per second
    UARTEchoSet(true);  // Enable console echo
#endif


#if BOARD_NANOFET >= 1
    // Set PA1 high to avoid RS232 glitch, later in init(), this will be setup as console serial port
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_1); // PA1 is console TX
    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, 0xFF);

    ROM_SysCtlDelay(MILLISECOND*2);       // fast startup (2msec, was 100msec)

    //1        PB6           PB6_ADC_START        // PB6_ADC_START
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_6); // 1        PB6           PB6_ADC_START
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_6, 0xFF);  // ADC_START=1 is inactive

    //+5        PF4           PF4_ADC_RESET        // PF4_ADC_RESET
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4); // 5        PF4           PF4_ADC_RESET
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0xFF);    // ADC_RESET to ADS1284 active low reset (init to 1)

    //called above
    //led_init();     // defd in board_util.c

    /*** Initialize UART0 for use as the console, printf, etc ***/
    //17       PA0/U0RX      PA0_CONSOLE_RX0
    //18       PA1/U0TX      PA1_CONSOLE_TX0
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 115200, 16000000);       // 115200 bits per second
    UARTEchoSet(true);  // Enable console echo

    // THOM TODO, figure out best way to manage I2C for low power, default is to set as GPIO high so to not pull down resistors for now
    // 47       PB2/I2C0SCL    I2C_SCL
    // 48       PB3/I2C0SDA    I2C_SDA
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2); // 47       PB2/I2C0SCL    I2C_SCL
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0xFF);    // Set high (maybe should float?)
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_3); // 48       PB3/I2C0SDA    I2C_SDA
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xFF);    // Set

    // FRAM init calls i2c_init - need to figure out peripheral init backplane policy (recommend calling i2c_init here)
    // void i2c0_init(bool bFast)

    // DRDY input pin for ADS1284
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_6);      // Input   53       PD6           PD6_DRDY

    // NC on both DAG and nanoFET, DAG has named the pins
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);     // 57       PB5           PB5_ENV_ENABLE (no connect on DAG)
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0x00);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_4);     // 58       PB4           PB4_NC           //PB4_VENABLE_NC (no connect on DAG)
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, 0x00);

    /*** Initialize ADS1248 pins as GPIO to reduce power consumption, SPI config happens in ADS1284_xxx.c ***/
    // THOM TODO on nanoFET and DAG - ADS1284 routines need tweaking
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0);     // SSI3CLK  61       PD0           PD0_SSI3_CLK
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_1);     // SSI3Fss  62       PD1           PD1_SSI3_SS_CS   TODO, FIX BUG (CS=0 is active, should be inactive)  18 Jul 2024
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0x00);

    ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_2);      // SSI3Rx   63       PD2           PD2_SSI3_RX_MISO

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3);     // SSI3Tx   64       PD3           PD3_SSI3_TX_MOSI
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0x00);
#endif

#if BOARD_MSC == 3
    // Set PA1 high to avoid RS232 glitch, later in init(), this will be setup as console serial port later in init
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_1); // PA1 is console TX (uart0)
    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, 0xFF);

    ROM_SysCtlDelay(MILLISECOND*2);       // fast startup (2msec, was 100msec)

    // PE2 ADC_START (pH_Start)
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_2); // 1    PE2_ADC_START (pH)
    ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, 0xFF);  // ADC_START=1 is inactive

    // PE3 ADC_RESET (pH_ADC_Reset)
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_3); // PE3_ADC_RESET
    ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xFF);    // ADC_RESET to ADS1284 active low reset (init to 1)

    // PE1 ADC_DRDY (pH_DRDY) input pin for ADS1284
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_1);      // Input PE1_DRDY

    // ADC_SHUTDOWN (pH_SHUTDOWN)  PE0
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_0); // PE0 pH adc shutdown
    ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0, 0x00);

    /*** Initialize UART0 for use as the console, printf, etc ***/
    //17       PA0/U0RX      PA0_CONSOLE_RX0
    //18       PA1/U0TX      PA1_CONSOLE_TX0
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 115200, 16000000);       // 115200 bits per second
    UARTEchoSet(true);  // Enable console echo

    // 47       PB2/I2C0SCL    I2C_SCL
    // 48       PB3/I2C0SDA    I2C_SDA
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2); // 47       PB2/I2C0SCL    I2C_SCL
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0xFF);    // Set high (maybe should float?)
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_3); // 48       PB3/I2C0SDA    I2C_SDA
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xFF);    // Set

    //MMS Spec CTS (input) PD6, MMS Spec RTS (output) PD7
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_6);      // PD6 MMS spectrometer CTS as input PD6
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_7);     // PC5 system/float RTS output
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, 0x00);      // RTS=0 is inactive ???  THOM TODO  1 Jul 2024

    //APF PC4 (SYS) CTS / PC5 (SYS) RTS
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4);      // PC4 system/float CTS input
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_5);     // PC5 system/float RTS output
    ROM_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5, 0x00);      // RTS=0 is inactive ???  THOM TODO  1 Jul 2024


    //NAND flash chip select PB5 (FSS) is CS1, PE5 is CS2 (SCHchecked)  Done in SSI
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);     // PB5 is Flash_CS1 (SSI2Fss)
    //ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0xFF);        // active lo
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_5);     // PE5 is Flash_CS2
    //ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, 0xFF);        // active lo

    //NAND flash power supply enables
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0);     //PF0 is Flash_EN1
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0xFF);        // active lo
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);     //PF1 is Flash_EN2
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0xFF);        // active lo

    //NAND flash hold1 (PF4) and hold2 (PE4)
    // NOT NEEDED, BUT PROGRAM AS INPUTS on RevA - remove for RevB
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);      // PF4
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_4);      // PE4
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4);     //PF4 is Flash hold1
    //ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x00);        // active hi
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_4);     //PE4 is Flash hold1
    //ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_4, 0x00);        // active hi

    //I2C Reset
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_7);     //PA7 is I2C_RST
    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0xFF);        // active lo

    // uSD detect (input) PA6
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_6);      // uSD card detect

    /*** Initialize ADS1248 pins as GPIO to reduce power consumption, SPI config happens in ADS1284_xxx.c ***/
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0);     // SSI3CLK  61       PD0           PD0_SSI3_CLK
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_1);     // SSI3Fss  62       PD1           PD1_SSI3_SS_CS
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0xFF);        // CS=inactive  THOM TODO: Check MPHOX, NanoFET, MFET for this error (CS is active lo)

    ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_2);      // SSI3Rx   63       PD2           PD2_SSI3_RX_MISO

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3);     // SSI3Tx   64       PD3           PD3_SSI3_TX_MOSI
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0x00);


    // GPIO init for SSI2 (NAND flash) PB4=CLK, PB5=CS1, PE5=CS2, PB6=MISO, PB7=MOSI
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_4);     // SSI2CLK         PB4
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, 0x00);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);     // SSI2Fss CS1        PB5           PB5  CS1 is active lo
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0xFF);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_5);     // CS2        PE5           PE5  CS2 is active lo
    ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, 0xFF);

    ROM_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_6);      // SSI2MISO       PB6           PB6_SSI2_RX_MISO

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_7);     // SSI2MOSI       PB7           PB7_SSI2_TX_MOSI
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_7, 0x00);

    //and SSI0 (uSD)  PA2=CLK,  PA3=CS,  PA4=MISO,  PA5=MOSI
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_2);     // SSI0CLK         PA2
    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, 0x00);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3);     // SSI0Fss CS        PA3      CS is active lo
    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, 0xFF);

    ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_4);      // SSI0MISO       PA4           PA4_SSI0_RX_MISO

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_5);     // SSI0MOSI       PA5           PA5_SSI0_TX_MOSI
    ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_5, 0x00);

#endif
    //---------- Done with 'pin' init --------------------------


}


#ifdef NOCODE_DEFS_FOR_PINS_DAG_NANOFET
    // + means checked, * means different between nanoFET and DAG, nanoFET is subset of DAG
                            NANOFET             //       DAG
    +1        PB6           PB6_ADC_START        // PB6_ADC_START
    +4*        PB7           NC                   // PB7_SHUTDOWN_ISO (not connected in DAG or nFET)
    +5        PF4           PF4_ADC_RESET        // PF4_ADC_RESET
    +6*        PE3           NC                   // PE3_AIN0_VOLTAGE
    +7*        PE2           NC                   // PE2_AIN1_HUMIDITY
    +8*        PE1           NC                   // PE1_AIN2_TMP_CS3
    +9*        PE0           NC                   // PE0_MUX_X6_A1
    +10*       PD7           NC                   // PD7_MUX_X6_A0
    +13       PC7           PC7_LED_RED_WDOG     // PC7_LED_RED_WDOG
    +14       PC6           PC6_GREEN_LED        // PC6_GREEN_LED
    +15*       PC5           PC5_NC               // PC5_SPARE_TX4
    +16*       PC4           PC4_NC               // PC4_SPARE_RX4

    +17       PA0           PA0_CONSOLE_RX       // PA0_CONSOLE_RX0
    +18       PA1           PA1_CONSOLE_TX       // PA1_CONSOLE_TX0
    +19       PA2           PA2_FLASH_CLK        // PA2_FLASH_CLK
    +20       PA3/SSI0FSS   PA3_FLASH_CS0        // PA3_FLASH_CS0
    +21       PA4           PA4_FLASH_MISO       // PA4_FLASH_MISO
    +22       PA5           PA5_FLASH_MOSI       // PA5_FLASH_MOSI
    +23*       PA6           PA6_NC               // PA6_FLASH_CS1
    +24       PA7           PA7_FLASH_HOLD       // PA7_FLASH_CS2   (TODO, FLASH_HOLD vs Chip select?)
    +28*       PF0           PF0_NC               // PF0_CONSOLE_PS
    +29*       PF1           PF1_NC               // PF1_CONSOLE_MODE
    +30*       PF2           PF2_NC               // PF2_DRIVER_MODE
    +31*       PF3           PF3_NC               // PF3_DRIVER_PS

    +43*       PD4           PD4_NC               // PD4_SPARE_RX6
    +44*       PD5           PD5_NC               // PD5_SPARE_TX6
    +45*       PB0/USB0ID    PB0_NC               // PB0_SPARE_RX1
    +46*       PB1/USB0VBUS  PB1_NC               // PB1_SPARE_TX1
    +47       PB2/I2C0SCL   I2C_SCL              // I2C_SCL
    +48       PB3/I2C0SDA   I2C_SDA              // I2C_SDA

    +53       PD6           PD6_DRDY             // PD6_DRDY
    +57       PB5           PB5_ENV_ENABLE       // PB5_NC
    +58       PB4           PB4_NC               // PB4_VENABLE_NC
    +59       PE4           PE4_NC              // PE4_SPARE_RX5
    +60       PE5           PE5_NC              // PE5_SPARE_TX5
    +61       PD0           PD0_SSI3_CLK         // PD0_SSI3_CLK
    +62       PD1           PD1_SSI3_SS_CS       // PD1_SSI3_SS_CS
    +63       PD2           PD2_SSI3_RX_MISO     // PD2_SSI3_RX_MISO
    +64       PD3           PD3_SSI3_TX_MOSI     // PD3_SSI3_TX_MOSI

#endif
