/*
 * $LastChangedRevision: 3952 $
 * $LastChangedDate:: 2025-11-27 21:04:55 +0100#$
 *
 * This file is part of X2C. web: x2c.lcm.at
 *
 * Copyright (c) 2024, Linz Center of Mechatronics GmbH (LCM), web: x2c.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 *
 * 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 the "Linz Center of Mechatronics GmbH" and "LCM" 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 "Linz Center of Mechatronics GmbH" 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 <DAVE.h>
#include "GlobalDefines.h"
#include "Hardware.h"

/* private prototypes */
static void sendSerial(tSerial* serialXMC, uint8 data);
static uint8 receiveSerial(tSerial* serialXMC);
static uint8 isReceiveDataAvailable(tSerial* serialXMC);
static uint8 isSendReady(tSerial* serialXMC);
static uint8 getTxFifoFree(tSerial* serialXMC);
static void flush(tSerial* serialXMC);


/**
 * @brief Initialization of hardware.
 */
void initHardware(void)
{
    /*------------------------------------------------------------------------*/
    /** - Configuration of IO ports */
    /*------------------------------------------------------------------------*/
	/* Initialize LED output pin P5.8 */
	XMC_GPIO_SetMode(P5_8, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);

	/* Initialize LED output pin P5.9 */
	XMC_GPIO_SetMode(P5_9, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);

	/* Initialize button input pin P15.12 */
	/* Note: done with APP PORT15_12 */

	/* Initialize button input pin P15.13 */
	/* Note: done with APP PORT15_13 */

    /*------------------------------------------------------------------------*/
    /** - Start of watchdog timer */
    /*------------------------------------------------------------------------*/
	WATCHDOG_Start();

    /*------------------------------------------------------------------------*/
    /** - Start of timer for X2C update */
    /*------------------------------------------------------------------------*/
	TIMER_Start(&TIMER_0);
}


/**
 * @brief Initialization of serial interface.
 *
 * @param serialXMC Serial object
 * @param UART_t UART interface
 */
void initSerial(tSerial* serialXMC, const UART_t* interface)
{
	serialXMC->send = (void (*)(tInterface*, uint8))sendSerial;
	serialXMC->receive = (uint8 (*)(tInterface*))receiveSerial;
	serialXMC->isReceiveDataAvailable = (uint8 (*)(tInterface*))isReceiveDataAvailable;
	serialXMC->isSendReady = (uint8 (*)(tInterface*))isSendReady;
	serialXMC->getTxFifoFree = (uint8 (*)(tInterface*))getTxFifoFree;
	serialXMC->flush = (void (*)(tInterface*))flush;

	serialXMC->uartObject = interface;
}


/**
 * @brief Linking of serial interface with LNet protocol.
 *
 * @param tProtocol Protocol object
 * @param serialXMC Serial object
 */
void linkSerial(tProtocol* lnkProtocol, tSerial* serialXMC)
{
	lnkProtocol->hwInterface = (tInterface*)serialXMC;
}


/* send byte */
static void sendSerial(tSerial* serialXMC, uint8 data)
{
	serialXMC->uartObject->channel->IN[0] = (uint32)data;
}


/* receive byte */
static uint8 receiveSerial(tSerial* serialXMC)
{
	return (uint8)(serialXMC->uartObject->channel->OUTR);
}


/* receive data check function */
static uint8 isReceiveDataAvailable(tSerial* serialXMC)
{
	return (uint8)(!((serialXMC->uartObject->channel->TRBSR)&0x00000008));
}

static uint8 isSendReady(tSerial* serialXMC)
{
	return (uint8)((serialXMC->uartObject->channel->TRBSR)&(1<<11)&&1);
}

/* No FIFO or software buffer -> always returns 0 */
static uint8 getTxFifoFree(tSerial* serialXMC)
{
	return ((uint8)0);
}


static void flush(tSerial* serialXMC)
{
	/* do nothing because 'send' immediately sends byte */
}
