/*!
 * @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 SPI_FullDuplex
  @{
*/

/** @defgroup SPI_FullDuplex_Macros Macros
  @{
*/

#define DATA_BUF_SIZE       32

/**@} end of group SPI_FullDuplex_Macros */

/** @defgroup SPI_FullDuplex_Variables Variables
  @{
*/

uint8_t rxDataBufSPI[DATA_BUF_SIZE] = {0};
uint8_t rxDataBufUSART[DATA_BUF_SIZE] = {0};

/**@} end of group SPI_FullDuplex_Variables */

/** @defgroup SPI_FullDuplex_Functions Functions
  @{
*/

/* SPI Init */
void SPI_Init(void);
/* USART Init */
void USART_Init(void);

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

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

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);

    SPI_Init();
    USART_Init();

    /* send data */
    for(i = 0; i < DATA_BUF_SIZE; i++)
    {
        while(SPI_ReadStatusFlag(SPI_FLAG_TXBE) == RESET);
        /* Write data in SPI transmit data register */
        SPI_TxData(i);

        /* Write data in USART1 transmit data register */
        USART_TxData8(USART1, ~i);

        /* Wait data is received by UART1 */
        while(USART_ReadStatusFlag(USART1, USART_FLAG_RXBNE) == RESET);
        rxDataBufUSART[i] = USART_RxData8(USART1);

        /* Wait data is received by SPI */
        while(SPI_ReadStatusFlag(SPI_FLAG_RXBNE) == RESET);
        rxDataBufSPI[i] = SPI_RxData();
    }

    /* Verify data */
    for(i = 0; i < DATA_BUF_SIZE; i++)
    {
        if(rxDataBufUSART[i] != i)
        {
            state = FALSE;
            break;
        }

        if(rxDataBufSPI[i] != (uint8_t)(~i))
        {
            state = FALSE;
            break;
        }
    }

    if(state == TRUE)
    {
        APM_MINI_LEDOn(LED2);
    }
    else
    {
        APM_MINI_LEDOff(LED2);
    }

    while(1)
    {

        APM_MINI_LEDToggle(LED3);
        APM_Delay(500);
    }
}

/*!
 * @brief       SPI Initialization
 *
 * @param       None
 *
 * @retval      None
 */
void SPI_Init(void)
{
    SPI_Config_T spiConfig;

    /* Baudrate divider is 32  */
    spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_32;
    /* 2 line full duplex  */
    spiConfig.direction = SPI_DIRECTION_2LINES_FULLDUPLEX;
    /* LSB first  */
    spiConfig.firstBit = SPI_FIRST_BIT_LSB;
    /* No interrupt  */
    spiConfig.interrupt = SPI_INT_NONE;
    /* Slave mode  */
    spiConfig.mode = SPI_MODE_SLAVE;
    /* Polarity is high  */
    spiConfig.polarity = SPI_CLKPOL_HIGH;
    /* Software select slave enable  */
    spiConfig.slaveSelect = SPI_SSC_ENABLE;
    /* Phase is 2 edge  */
    spiConfig.phase = SPI_CLKPHA_2EDGE;
    /* SPI config  */
    SPI_Config(&spiConfig);

    SPI_Enable();
}

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

    /* Enable USART Clock  */
    usartSyncClockConfig.enable = USART_CLKEN_ENABLE;
    /* Enable last bit clock  */
    usartSyncClockConfig.lastBitClock = USART_LBCP_ENABLE;
    /* Clock phase is 2 edge */
    usartSyncClockConfig.phase = USART_CLKPHA_2EDGE;
    /* Clock polarity is high */
    usartSyncClockConfig.polarity = USART_CLKPOL_HIGH;
    /* Synchronous communication clock configuration */
    USART_ConfigSyncClock(USART1, &usartSyncClockConfig);

    /* BaudRate is 9600 */
    usartConfig.baudRate = 9600;
    /* No interrupt */
    usartConfig.interrupt = USART_INT_NONE;
    /* Enable RX and TX */
    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 USART1 */
    USART_Enable(USART1);
}

/**@} end of group SPI_FullDuplex_Functions */
/**@} end of group SPI_FullDuplex */
/**@} end of group Examples */
