/*
 * $LastChangedRevision: 1603 $
 * $LastChangedDate:: 2019-01-21 19:02:13 +0100#$
 *
 * This file is part of X2C. http://x2c.lcm.at/
 *
 * Copyright (c) 2016, Linz Center of Mechatronics GmbH (LCM) http://www.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 "main.h"
#include "Target.h"
#include "Hardware.h"

extern UART_HandleTypeDef huart2;

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

#define PWM_FREQUENCY_10KHZ ((uint16)2400)
#define PWM_FREQUENCY_20KHZ ((uint16)1200)
#define PWM_FREQUENCY_30KHZ ((uint16)800)
#define PWM_FREQUENCY_40KHZ ((uint16)600)
#define PWM_FREQUENCY_50KHZ ((uint16)480)

#define PWM_FREQUENCY PWM_FREQUENCY_10KHZ

/**
 * @brief Initialization of serial interface.
 */
void initSerial(tSerial* serial)
{
  //LL_LPUART_Enable(LPUART1);
  //HAL_UART_Init(&hlpuart1);
  __HAL_UART_ENABLE(&huart2);

	serial->send = (void (*)(tInterface*, uint8))sendSerial;
	serial->receive = (uint8 (*)(tInterface*))receiveSerial;
	serial->isReceiveDataAvailable = (uint8 (*)(tInterface*))isReceiveDataAvailable;
	serial->isSendReady = (uint8 (*)(tInterface*))isSendReady;
    serial->getTxFifoFree = (uint8 (*)(tInterface*))getTxFifoFree;
    serial->flush = (void (*)(tInterface*))flush;
}


/**
 * @brief Routine to send data over serial interface.
 * 
 * @param serial Serial interface object.
 * @param data Data byte to send.
 */
static void sendSerial(tSerial* serial, uint8 data)
{
  //LL_LPUART_TransmitData8(LPUART1, data);
  //LPUART1->TDR = data;
  USART2->TDR = data;
}


/**
 * @brief Routine to fetch data from the serial interface.
 * 
 * @param serial Serial interface object.
 * @return Received data byte.
 */
static uint8 receiveSerial(tSerial* serial)
{
	//uint8 data;
	//data = LL_LPUART_ReceiveData8(LPUART1);
	return (uint8_t)(READ_BIT(USART2->RDR, USART_RDR_RDR) & 0xFFU);
}


/**
 * @brief Routine to check if data from the serial interface is available.
 * 
 * @param serial Serial interface object.
 * @return 1 when data is available, 0 otherwise.
 */
static uint8 isReceiveDataAvailable(tSerial* serial)
{
	return ((READ_BIT(USART2->ISR, USART_ISR_RXNE) == (USART_ISR_RXNE)) ? 1UL : 0UL);
}


/**
 * @brief Routine to check if data can be send over the serial interface.
 *  
 * @param serial Serial interface object.
 * @return 1 when data can be send, 0 otherwise.
 */
static uint8 isSendReady(tSerial* serial)
{
	return ((READ_BIT(USART2->ISR, USART_ISR_TXE) == (USART_ISR_TXE)) ? 1UL : 0UL);
}

static uint8 getTxFifoFree(tSerial* serialP)
{
    return ((uint8)0);
}

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