/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup USART_HalfDuplex
  @{
*/

/** @defgroup USART_HalfDuplex_Macros Macros
  @{
*/

#define DATA_BUF_SIZE       (32)
/* Enable transmit and disable receive */
#define WRITE_ONLY(x)        x->CTRL2_B.RXEN = RESET; x->CTRL2_B.TXEN = SET
/* Enable receive and disable transmit */
#define READ_ONLY(x)         x->CTRL2_B.TXEN = RESET; x->CTRL2_B.RXEN = SET

/**@} end of group USART_HalfDuplex_Macros */

/** @defgroup USART_HalfDuplex_Variables Variables
  @{
*/

uint8_t rxDataBufUSART1[DATA_BUF_SIZE] = {0};
uint8_t txDataBufUSART2[DATA_BUF_SIZE] = {0};

/**@} end of group USART_HalfDuplex_Variables */

/** @defgroup USART_HalfDuplex_Functions Functions
  @{
*/

/* USART Init */
void USART_Init(void);
/* USART Send Byte*/
void USART_SendByte(USART_T *usart,uint8_t data);
/* USART Receive Byte**/
uint8_t USART_ReceiveByte(USART_T *usart);
/* Delay */
void Delay(uint32_t count);
/* Data buffer init */
void DataBufInit(void);
/* Compares two buffers */
BOOL BufferCompare(uint8_t *buf1, uint8_t *buf2, uint8_t size);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    uint8_t i;
    BOOL state = TRUE;

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);
    APM_DelayInit(APM_DELAY_MS);

    USART_Init();

    DataBufInit();

    for(i = 0; i < DATA_BUF_SIZE; i++)
    {
        /* Wait until end of transmission */
        USART_SendByte(USART2, txDataBufUSART2[i]);

        rxDataBufUSART1[i] = USART_ReceiveByte(USART1);
    }

    /* Verify data */
    state = BufferCompare(rxDataBufUSART1, txDataBufUSART2, DATA_BUF_SIZE);

    while(1)
    {
        /* Data is ok then turn off LED2 */
        if(state == TRUE)
        {
            APM_MINI_LEDOn(LED2);
        }
        else
        {
            APM_MINI_LEDToggle(LED2);
            APM_MINI_LEDToggle(LED3);
            APM_Delay(500);
        }
    }
}

/*!
 * @brief       USART Init
 *
 * @param       None
 *
 * @retval      None
 */
void USART_Init(void)
{
    USART_Config_T usartConfig;
    GPIO_Config_T gpioConfig;

    gpioConfig.mode = GPIO_MODE_IN_FLOATING;
    gpioConfig.speed = GPIO_SPEED_10MHz;
    gpioConfig.pin = USART1_TX_PIN;

    GPIO_Config(USART1_TX_GPIO_PORT, &gpioConfig);

    gpioConfig.pin = USART2_TX_PIN;

    GPIO_Config(USART2_TX_GPIO_PORT, &gpioConfig);

    RCM_EnableAPBPeriphClock(RCM_PERIPH_USART1);
    RCM_EnableAPBPeriphClock(RCM_PERIPH_USART2);

    /*  BaudRate */
    usartConfig.baudRate = USART_BAUDRATE;
    /*  No interrupt */
    usartConfig.interrupt = USART_INT_NONE;
    /*  Enable receiver */
    usartConfig.mode = USART_MODE_TX_RX;
    /*  Parity disable */
    usartConfig.parity = USART_PARITY_NONE;
    /*  One stop bit */
    usartConfig.stopBits = USART_STOP_BIT_1;
    /*  Word length is 8bit */
    usartConfig.wordLength = USART_WORD_LEN_8B;
    /*  USART1 configuration */
    USART_Config(USART1, &usartConfig);

    /*  Enable transmitter */
    usartConfig.mode = USART_MODE_TX_RX;
    /*  USART2 configuration */
    USART_Config(USART2, &usartConfig);

    /*  Clear other function */
    USART1->CTRL3_B.CLKEN = BIT_RESET;
    USART1->CTRL3_B.LINEN = BIT_RESET;
    USART1->CTRL5_B.IRDAEN = BIT_RESET;
    USART1->CTRL5_B.SMEN = BIT_RESET;

    USART2->CTRL3_B.CLKEN = BIT_RESET;
    USART2->CTRL3_B.LINEN = BIT_RESET;
    USART2->CTRL5_B.IRDAEN = BIT_RESET;
    USART2->CTRL5_B.SMEN = BIT_RESET;

    /*  Enable Half Duplex */
    USART_EnableHalfDuplex(USART1);
    USART_EnableHalfDuplex(USART2);

    /*  Enable USART1 */
    USART_Enable(USART1);
    /*  Enable USART2 */
    USART_Enable(USART2);
}

/*!
 * @brief       USART Send byte
 *
 * @param       usart
 *
 * @param       data
 *
 * @retval      None
 */
void USART_SendByte(USART_T *usart, uint8_t data)
{
    /* Enable transmit and disable receive */
    WRITE_ONLY(usart);

    USART_TxData8(usart, data);
    while(USART_ReadStatusFlag(usart, USART_FLAG_TXBE) == RESET)
    {
    }
}

/*!
 * @brief       USART Receive byte
 *
 * @param       usart
 *
 * @retval      receive data
 */
uint8_t USART_ReceiveByte(USART_T *usart)
{
    uint8_t revData;

    while(USART_ReadStatusFlag(usart, USART_FLAG_TXC) == RESET)
    {
    }
    /* Enable receive and disable transmit */
    READ_ONLY(usart);

    while(USART_ReadStatusFlag(usart, USART_FLAG_RXBNE) == RESET)
    {
    }
    revData = USART_RxData8(usart);

    return revData;
}

/*!
 * @brief       Data buffer init
 *
 * @param       None
 *
 * @retval      None
 */
void DataBufInit(void)
{
    uint8_t i;

    for(i = 0; i < DATA_BUF_SIZE; i++)
    {
        txDataBufUSART2[i] = 0xa0 + i;
        rxDataBufUSART1[i] = 0;
    }
}

/*!
 * @brief       Compares two buffers
 *
 * @param       buf1:    First buffer to be compared
 *
 * @param       buf1:    Second buffer to be compared
 *
 * @param       size:    Buffer size
 *
 * @retval      Return TRUE if buf1 = buf2 else return FALSE
 */
BOOL BufferCompare(uint8_t *buf1, uint8_t *buf2, uint8_t size)
{
    uint8_t i;

    for(i = 0; i < size; i++)
    {
        if(buf1[i] != buf2[i])
        {
            return FALSE;
        }
    }

    return TRUE;
}

/**@} end of group USART_HalfDuplex_Functions */
/**@} end of group USART_HalfDuplex */
/**@} end of group Examples */
