/*
 * Init.c
 *
 *  Created on: Jan 23, 2014
 *      Author: rob
 *  Modified starting Mar 2018 by Thom Maughan (TM)
 *    Added MFET/MCAP board support
 */

#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 "pwm.h"
#include "board_util.h"
#include "sample.h"
#include "bme280.h"

int microSD_flg;



extern void AUX1_init(void);
extern void optode_init(void);
//extern void microCat_init(void);
extern void ctd_init(void);
extern void led_init(void);

extern void profile2_toggle(int target);
extern void ADS1248_gpio_init(void);

extern void init_TM4C123GH6PGE_144pin(void);
extern void init_TM4C123GH6PM_64pin(void);

/**********************************************************************************
 * Init()
 * Initialize pins and ports upon reset or wake from hybernation. Set unused pins low.
 **********************************************************************************/
void init(void)
{

    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of2
    // extra stack usage.
    ROM_FPULazyStackingEnable();

    //enable floats
    ROM_FPUEnable();

    //set the system clock...50MHZ
    // THOM TODO, experiment with slower clock settings to balance compute vs power consumption
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);


#if BOARD_MFET >= 1 || BOARD_SEAPHOX == 1 || BOARD_MPHOX == 1
    init_TM4C123GH6PGE_144pin();
#endif

#if BOARD_NANOFET >= 1
//#define BOARD_DAG       1
    init_TM4C123GH6PM_64pin();
#endif


    /*** Configure SysTick for a 100Hz interrupt.  The FatFs driver and 10 ms timer share a 10 ms tick ***/
    /*** Configure SysTick for a 1000Hz / 1msec interrupt.  General purpose timing while maintaining the
     * The FatFs driver and 10 ms timer share a 10 ms tick ***/
    // void SysTickHandler(void) is in system.c
    //ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);     // 10msec tick (100 Hz)
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 1000);      // 1msec tick (1K Hz)
    ROM_SysTickEnable();
    ROM_SysTickIntEnable();


#if BOARD_MPHOX >= 1
    pwm_init();
#endif


//#if BOARD_MFET == 2 || BOARD_MPHOX == 1
    // if i2c does not have a device, the fram_probe hangs (on MFET v1)
    fram_init();        // i2c0_init(TRUE);     // fast mode 400kpbs, defd in i2c_fram.c

#if BOARD_NANOFET >= 1
    bme280_init();
#endif

//#endif

    /*** Retrieve saved sys_data variables ***/
    retrieveSysDataVariables();         // defd in system.c, moved down in init until after i2c init (in fram)

    verifySysDataVariables();

    // default is 115200 and this is set in the init() above - if sys_data.baud_rate is not 115200, then re(set) here
    if(sys_data.baud_rate == 4800L)
    {
        UARTStdioConfig(0, 4800, 16000000);       // 4800 bits per second
    }
    if(sys_data.baud_rate == 9600L)
    {
        UARTStdioConfig(0, 9600, 16000000);       // 9600 bits per second
    }
    if(sys_data.baud_rate == 19200L)
    {
        UARTStdioConfig(0, 19200, 16000000);       // 19200 bits per second
    }
    if(sys_data.baud_rate == 38400L)
    {
        UARTStdioConfig(0, 38400, 16000000);       // 38400 bits per second
    }
    if(sys_data.baud_rate == 57600L)
    {
        UARTStdioConfig(0, 57600, 16000000);       // 57600 bits per second
    }

    // Find out what woke us and decide what to do
    //sleep_handler();  Move to main after init() 25 May 2021

    microSD_flg = 0;
#if BOARD_MFET == 2 || BOARD_MPHOX == 1
    // Initialize MicroSD card and FatFs (relies on retrieveSysDataVariables)
    if(sys_data.app_cfg[APPCFG_MICROSD_ENABLE] != 0)
    {
        // TODO: check SDpresent
        if( uSD_present() )
        {
            // uprintf("\r\nMicroSD card is Initialized....\r\n");
            Init_SDCard();
            microSD_flg = 1;
        }
        else
        {
            uprintf("\r\nError: MicroSD card is not detected\r\n");
            microSD_flg = 0;
        }
    }
    else
    {
        //uprintf("MicroSD card is Disabled, to change it: 3 -- Config, A -- App Config....\r\n");
    }

#endif

    pump_var_init();
}


