/*-----------------------------------------------------------------------------
 * main.c               startup file for ti rtos application
 * Copyright            acontis technologies GmbH, Weingarten, Germany
 * Response             Mikhail Ledyaev
 * Description          starts EcMainDemo function.
 *---------------------------------------------------------------------------*/

#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/timers/dmtimer/Timer.h>
#include <ti/board/board.h>
#include <ti/drv/uart/UART_stdio.h>

#include <EcVersion.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

Timer_Handle g_auxClocksTimerHandle = 0;

//extern int EcMasterDemo(void);
extern int CatMasterMain(void);
extern void pcieMmuInitial(void);

#define BOARD_IDKAM571x     1
#define BOARD_IDKAM572x     2


void InitAuxClockTimer()
{
    const UInt auxClockTimerInstance = 1; // instance number defined in EcMaster.cfg
    g_auxClocksTimerHandle = Timer_getHandle(auxClockTimerInstance);
}

void TimerEmptyISR()
{
}
/**
 * 起動後の初期タスクメイン
 */
void TaskFxn(UArg a0, UArg a1)
{
    InitAuxClockTimer();
    UART_printf("\n\rSYS/BIOS EcMasterDemo ICSS Sample application\n\r");
//    EcMasterDemo();
    // EtherCatメインループ処理。
    CatMasterMain();
}
volatile int  dbg_flg = 1;
int main()
{
    Board_IDInfo info;
    Error_Block eb;
    uint8_t board_type = 0;
    Task_Params taskParams;

    while(dbg_flg);

    // PCIeに関連する、MMU設定
//    pcieMmuInitial();


    Board_initCfg boardCfg = BOARD_INIT_UNLOCK_MMR | BOARD_INIT_PINMUX_CONFIG |
            BOARD_INIT_MODULE_CLOCK | BOARD_INIT_UART_STDIO;
    Board_init(boardCfg);

    memset(&info, 0,sizeof(Board_IDInfo));
    Board_getIDInfo(&info);
    UART_printf("boardName: %s\n", info.boardName);

    if (!(strcmp(info.boardName, "AM571IDK")))
    {
        board_type =BOARD_IDKAM571x;
        UART_printf("board type is AM571IDK\n");
    }
    else
    {
        board_type =BOARD_IDKAM572x;
        UART_printf("board type is AM572IDK\n");
    }
    Task_Params_init(&taskParams);
    taskParams.priority = 1;
    taskParams.stackSize = 0x1400;
    taskParams.instance->name = "MainTask";
    Error_init(&eb);
    Task_create (TaskFxn, &taskParams, &eb);

    BIOS_start();

    return(0);
}

