/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @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_MultiProcessor
  @{
*/

/** @defgroup USART_MultiProcessor_Macros Macros
  @{
*/

#define DATA_BUF_SIZE       (16)
#define USART_MASTER        USART2
#define USART_SLAVE         USART1
#define USART_SLAVE_IRQn    USART1_RX_IRQn
#define SLAVE_ADDR          0x0A

/**@} end of group USART_MultiProcessor_Macros */

/** @defgroup USART_MultiProcessor_Variables Variables
  @{
*/

uint8_t txDataPt = 0;
uint8_t rxDataPt = 0;

uint8_t rxDataBuf[DATA_BUF_SIZE] = {0};
uint8_t txDataBuf[DATA_BUF_SIZE] = {'h','e','l','l','o',',','w','o','r','l','d','!'};

/**@} end of group USART_MultiProcessor_Variables */

/** @defgroup USART_MultiProcessor_Functions Functions
  @{
*/

/* USART Init */
void USART_Init(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)
{
    BOOL state = FALSE;

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDOff(LED2);

    APM_DelayInit(APM_DELAY_MS);

    USART_Init();

    while(USART_ReadStatusFlag(USART_MASTER, USART_FLAG_TXBE) == RESET);
    USART_TxData8(USART_MASTER, (0x80 | SLAVE_ADDR));
    /* transmission */
    while (txDataPt < DATA_BUF_SIZE)
    {
        while(USART_ReadStatusFlag(USART_MASTER, USART_FLAG_TXBE) == RESET);
        USART_TxData8(USART_MASTER, txDataBuf[txDataPt++]);
    }

    /* Wait until end of reception */
    while (rxDataPt < DATA_BUF_SIZE);

    /* Verify data */
    state = BufferCompare(rxDataBuf, txDataBuf, 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_Delay(500);
        }
    }
}

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

    usartConfig.baudRate = 9600;                /*!< BaudRate is 9600 */
    usartConfig.wordLength = USART_WORD_LEN_8B; /*!< Word length is 8bit */
    usartConfig.stopBits = USART_STOP_BIT_1;    /*!< One stop bit */
    usartConfig.parity = USART_PARITY_NONE;     /*!< Parity disable */
    usartConfig.mode = USART_MODE_RX;           /*!< Enable receiver */
    usartConfig.interrupt = USART_INT_RX;       /*!< Receiver interrupt */
    /*  USART_SLAVE configuration */
    USART_Config(USART_SLAVE, &usartConfig);

    usartConfig.interrupt = USART_INT_NONE; /*!< None interrupt */
    usartConfig.mode = USART_MODE_TX;       /*!< Enable transmitter */
    /*  USART_MASTER configuration */
    USART_Config(USART_MASTER, &usartConfig);

    /*  Enable USART_SLAVE */
    USART_Enable(USART_SLAVE);
    /*  Enable USART_MASTER */
    USART_Enable(USART_MASTER);

    /*  Enable USART_SLAVE mute mode */
    USART_Address(USART_SLAVE, SLAVE_ADDR);
    USART_ConfigMuteModeWakeUp(USART_SLAVE, USART_WAKEUP_ADDRESS_MARK);
    USART_EnableMuteMode(USART_SLAVE);

    /*  Enable USART_SLAVE Receiver IRQ request */
    NVIC_EnableIRQRequest(USART_SLAVE_IRQn, 0x02);
}

/*!
 * @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;
}

/*!
 * @brief       USART_SLAVE receiver interrupt service routine
 *
 * @param       None
 *
 * @retval      None
 */
void USART_SLAVE_RxIsr(void)
{
    if (USART_ReadIntFlag(USART_SLAVE, USART_INT_FLAG_RX) == SET)
    {
        if (rxDataPt < DATA_BUF_SIZE)
        {
            rxDataBuf[rxDataPt++] = USART_RxData8(USART_SLAVE);
        }
    }
}

/**@} end of group USART_MultiProcessor_Functions */
/**@} end of group USART_MultiProcessor */
/**@} end of group Examples */
