/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.1
 *
 * @date        2025-04-31
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  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 Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

#include "Port.h"
#include "CDD_Usart.h"
#include "Mcu.h"
#include "Spi.h"

#if (STD_ON == SPI_DMA_SUPPORT)
#include "CDD_Pdl.h"
#endif

/*******************************************************************************
**                        GLOBAL VARIABLES                                    **
*******************************************************************************/  

/* Prepare the receive buffer */
Spi_DataBufferType g_dataBufferReceive[100];

/* Prepare the send buffer */
uint8 g_sendDataM[SPI_TRANS_ONCE_LENGTH]={0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,\
                    0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30};
uint8 g_sendDataS[SPI_TRANS_ONCE_LENGTH]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,\
                    0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20};

/*******************************************************************************
**                     GLOBAL FUNCTION                                        **
*******************************************************************************/
                    
void SysDelay(int cnt)
{
  while(cnt--);
}

//#define SPI_POLLINGT_TEST
#define SPI_INTERRUPT_TEST

/*              
    * Pin number        | Function       
    * ----------------- |------------    
    * PTA 15             | LPSPI1_NSS     
    * PTB  3             | LPSPI1_SCK     
    * PTB  4             | LPSPI1_MISO    
    * PTB  5             | LPSPI1_MOSI    
*/

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    int i = 0;
    boolean status = TRUE;
    
    Mcu_Init(&g_mcuConfig);
    Mcu_InitClock(McuClockSettingConfig_0);

    Port_Init(&g_portConfig);

    NVIC_EnableIRQ(USART2_IRQn);
    NVIC_EnableIRQ(SPI_IRQn);
    
#if (SPI_DMA_SUPPORT == STD_ON)
    Pdl_Init(&g_pdlConfig);

    NVIC_SetPriority(DMA_CH2_3_IRQn, 0x0f);
    NVIC_EnableIRQ(DMA_CH2_3_IRQn);
#endif
    
    Spi_Init(&g_spiConfig);
        
    Usart_Init(g_usartConfg);

#if (SPI_MASTER_SUPPORT == STD_ON)
    printf("\r\n---------------- SPI_ITA Master mode Start ----------------\r\n");
    /* Write the IB channel data. */
    Spi_WriteIB(SPI_CHANNELCFG_0,g_sendDataM);
    printf("\nSpi tx data : \r\n ");
    for(i = 0;i < SPI_TRANS_ONCE_LENGTH;i++)
    {
        printf("0x%02X " , g_sendDataM[i]);
    }
    printf("\r\n");
#else
    printf("\r\n---------------- SPI_ITA Slave mode Start ----------------\r\n");
    /* Write the IB channel data. */
    Spi_WriteIB(SPI_CHANNELCFG_0,g_sendDataS);
    printf("\nSpi tx data : \r\n ");
    for(i = 0;i < SPI_TRANS_ONCE_LENGTH;i++)
    {
        printf("0x%02X " , g_sendDataS[i]);
    }
    printf("\r\n");
#endif

#ifdef SPI_INTERRUPT_TEST
    #if (SPI_ASYNC_MODE_ENABLE == STD_ON)
        /* Set Async Mode */
        Spi_SetAsyncMode(SPI_INTERRUPT_MODE);
        /* Async Transmit. */
        Spi_AsyncTransmit(SPI_SEQUENCECFG_0);
        #if (SPI_DMA_SUPPORT == STD_OFF)
            while(SPI_IDLE != Spi_GetStatus())
            {
                
            }
        #endif
    #else
        Spi_SyncTransmit(SPI_SEQUENCECFG_0);
    #endif
#elif defined SPI_POLLINGT_TEST
    #if (SPI_ASYNC_MODE_ENABLE == STD_ON)
        /* Set Async Mode */
        Spi_SetAsyncMode(SPI_POLLING_MODE);

        /* Async Transmit. */
        Spi_AsyncTransmit(SPI_SEQUENCECFG_0);
        while(SPI_IDLE != Spi_GetStatus())
        {
            Spi_MainFunction_Handling();
            SysDelay(10);
        } 
    #else
        Spi_SyncTransmit(SPI_SEQUENCECFG_0);
    #endif
#endif
#ifdef SPI_INTERRUPT_TEST
    /* Read IB data. */
    #if (SPI_DMA_SUPPORT == STD_ON)
        while(0 == g_dataBufferReceive[SPI_TRANS_ONCE_LENGTH-1])
        {
            Spi_ReadIB(SPI_CHANNELCFG_0,g_dataBufferReceive);
        }
    #else
        Spi_ReadIB(SPI_CHANNELCFG_0,g_dataBufferReceive);
    #endif
#else
        Spi_ReadIB(SPI_CHANNELCFG_0,g_dataBufferReceive);
#endif 
    printf("\nSpi rx data : \r\n ");
    for(i = 0;i < SPI_TRANS_ONCE_LENGTH;i++)
    {
        printf("0x%02X " , g_dataBufferReceive[i]);
    #if (SPI_MASTER_SUPPORT == STD_ON)
        /* Check the data from SLAVE */
        if(g_sendDataS[i] != g_dataBufferReceive[i])
        {
            status = FALSE;
        }
     #else
        /* Check the data from SLAVE */
        if(g_sendDataM[i] != g_dataBufferReceive[i])
        {
            status = FALSE;
        }
     #endif
    }
    printf("\r\n");

    Spi_DeInit();
   
    if(TRUE == status)
    {
        printf("\nSpi ITA test pass! \r\n ");
    }
    else
    {
        printf("\nSpi ITA test fail! \r\n ");
    }
    
    while(1)
    {
    }

}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    int reVal = 0;
    reVal = Usart_PutRedefinition(USART1_ID,&ch);
    
    return reVal;
}

/* If configuring the full library using versions after IAR 9.30,
   it is necessary to implement the _write function for printf redirection.*/
size_t __write(int handle,const unsigned char *buf,size_t bufSize)
{
    int32 tempCh = 0;
    for(int i=0; i<bufSize; i++)
    {
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return bufSize;
}

#elif defined (__GNUC__)

/* For Arm Cross GCC: Eclipse */
int _write(int fd,const unsigned char *buf,int len)
{
    int32 tempCh = 0;
    for(int i=0; i<len; i++)
    {
        int32 tempCh = (int32)buf[i];
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return len;
}

#endif

