/*
 *  Copyright (C) 2018-2025 Texas Instruments Incorporated
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *    Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the
 *    distribution.
 *
 *    Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include "ti_drivers_config.h"
#include "ti_board_config.h"
#include "ti_drivers_open_close.h"
#include "ti_board_open_close.h"
#include <drivers/pinmux.h>
#include <drivers/gpio.h>


#define CONFIG_MCU_GPIO0_4_PIN (4)
#define CONFIG_MCU_GPIO0_4_DIR (GPIO_DIRECTION_OUTPUT)
#define CONFIG_MCU_GPIO0_4_TRIG_TYPE (GPIO_TRIG_TYPE_NONE)
#define CONFIG_MCU_GPIO0_NUM_INSTANCES (1U)
#define CONFIG_MCU_GPIO0_BASE_ADDR (0x4201000)

static Pinmux_PerCfg_t gPinMuxMcuCfg[] = {
    /* MCU_GPIO0 pin config MCU_GPIO0_4 -> MCU_SPI0_D1 */
    {
        PIN_MCU_SPI0_D1,
        ( PIN_MODE(7) | PIN_INPUT_ENABLE )
    },
    {PINMUX_END, PINMUX_END}
};


void empty_main(void *args);
extern void Pinmux_init();
extern void Drivers_uartInit();

void mod_system_init()
{
    /* DPL init sets up address transalation unit, on some CPUs this is needed
     * to access SCICLIENT services, hence this needs to happen first
     */
    Dpl_init();
    /* Now we can do pinmux */
    Pinmux_init();
    /* finally we initialize all peripheral drivers */
    Drivers_uartInit();
    I2C_init();
}

int main()
{
    Pinmux_config(gPinMuxMcuCfg, PINMUX_DOMAIN_ID_MCU); // Configure PinMux
    GPIO_setDirMode(CONFIG_MCU_GPIO0_BASE_ADDR, CONFIG_MCU_GPIO0_4_PIN, CONFIG_MCU_GPIO0_4_DIR);    //Set GPIO direction
    GPIO_pinWriteHigh(CONFIG_MCU_GPIO0_BASE_ADDR, CONFIG_MCU_GPIO0_4_PIN); // Set GPIO state to HIGH
    int32_t status = SystemP_SUCCESS;

    // System_init();
    mod_system_init();
    Board_init();

    /* Open drivers */
    Drivers_open();
    /* Open flash and board drivers */
    status = Board_driversOpen();
    DebugP_assert(status==SystemP_SUCCESS);

    uint8_t targetAddr = 0x48;
    status = I2C_probe(gI2cHandle[0], targetAddr); // probing the I2C device at target address 0x48
    if(status == SystemP_SUCCESS)
    {
        DebugP_log("I2C device found at address %X.\r\n", targetAddr); 
    }
    else
    {
        DebugP_log("I2C device not found.\r\n");
    }
    
    uint8_t val = 'A';
    UART_Transaction trans;
    UART_Transaction_init(&trans);
    trans.buf = &val;
    trans.count = 1;
    status = UART_write(gUartHandle[0], &trans);
    
    if(status == SystemP_SUCCESS)
    {
        DebugP_log("All test Passed.\r\n"); 
    }
    else
    {
        DebugP_log("Some test Failed.\r\n");
    }

    /* Close board and flash drivers */
    Board_driversClose();
    /* Close drivers */
    Drivers_close();

    Board_deinit();
    System_deinit();

    return 0;
}
