/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include<stdio.h>
#include<stdlib.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

#define                  DEBUG_INFO                      1                  /*debug information flag*/
#define                  T_BUFFER_SIZE                   128                /*data size of receive and transmit*/
#define                  DataWidth_8bit                  8                  /*data width size of SSP*/
#define                  SSPBaudrate_1M                  1000000            /*SSP baud rate*/


/*
 * If you use the 1168 and 1158 development boards, you cannot download using JLink
 * because GPIO38 and GPIO39 are already in use.
 */
#if defined(SPD1148) || defined(SPD1178) || defined(SPD1188)
    #define                  MOSI_GPIO                         GPIO_10
    #define                  MISO_GPIO                         GPIO_11
    #define                  MOSI_GPIO_FUNC                    GPIO10_SPI_MOSI
    #define                  MISO_GPIO_FUNC                    GPIO11_SPI_MISO
    #define                  SCLK_GPIO                         GPIO_8
    #define                  SFRM_GPIO                         GPIO_9
    #define                  SCLK_GPIO_FUNC                    GPIO8_SPI_SCLK
    #define                  SFRM_GPIO_FUNC                    GPIO9_SPI_SFRM
#else
    #define                  MOSI_GPIO                         GPIO_38
    #define                  MISO_GPIO                         GPIO_39
    #define                  MOSI_GPIO_FUNC                    GPIO38_SPI_MOSI
    #define                  MISO_GPIO_FUNC                    GPIO39_SPI_MISO
    #define                  SCLK_GPIO                         GPIO_8
    #define                  SFRM_GPIO                         GPIO_9
    #define                  SCLK_GPIO_FUNC                    GPIO8_SPI_SCLK
    #define                  SFRM_GPIO_FUNC                    GPIO9_SPI_SFRM
#endif

uint32_t                 gu32BuffSize                    = T_BUFFER_SIZE;
uint32_t                 gau32TxBuf[T_BUFFER_SIZE];
uint32_t                 gau32RxBuf[T_BUFFER_SIZE];
ErrorStatus              estatus;

static ErrorStatus Check_Receive_Data(void)
{
    int i;
    uint8_t u8Data = 0;

    /*To check the datum sent and received are the same*/
    for(i = 0; i < gu32BuffSize; i++)
    {
        if (gau32RxBuf[i] != u8Data)
        {
            #if DEBUG_INFO
            printf("[Error]@%4d: TX(0x%02X) != RX(0x%02X)\n", __LINE__, u8Data, gau32RxBuf[i]);
            #endif

            return ERROR;
        }
        else
        {
            #if DEBUG_INFO
            printf("%3d: TX(0x%02X) == RX(0x%02X)\n", i, u8Data, gau32RxBuf[i]);
            #endif
        }

        u8Data++;
    }

    return SUCCESS;
}


void SSP_TxRx(void)
{
    int i;
    uint8_t u8Data = 0;

    /* Prepare data to be send */
    for (i = 0; i < (int)(gu32BuffSize); i++)
    {
        gau32TxBuf[i] = u8Data++;
    }

    printf("Master TxRx data...\n");

    estatus = SSP_MasterB2BTransceive(SSP, gau32TxBuf, gau32RxBuf, gu32BuffSize);
    if (estatus == ERROR)
    {
        printf("SSP_MasterB2BTransceive fail\n");
        return;
    }

    estatus = Check_Receive_Data();
    if (estatus == SUCCESS)
    {
        printf("Success\n");
    }
}


/*************************************************************************************************************************
*
* @brief      In this case, Master sent datum to slave and then read these datum back with Back-to-Back mode. There two ways
*             to trigger Back-to-Back mode:
*                  1).TX FIFO is not empty while 'SFRM' signal is valid period.
*                  2).If the data width is less then 16bits, and when the Back-to-Back mode is enable. what's more, there is
*                     a key point : The max speed of Back-to-Back mode is 4M, because of the limit of MCU can cause the TX
*                     FIFO empty to breakout the Back-to-Back mode.
*
*             This demo code couple with another named 'SSP_Slave_B2B_TxRx_Polling'.
*
*************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(200000000UL);
#endif

    Delay_Init();

    /*
     * Init the UART
     *
     * 1.Set the GPIO34/35 as UART FUNC
     *
     * 2.Enable the UART CLK
     *
     * 3.Set the rest para
     */
    GPIO_SetPinChannel(GPIO_34, GPIO34_UART_TXD);
    GPIO_SetPinChannel(GPIO_35, GPIO35_UART_RXD);
    CLOCK_EnableModule(UART_MODULE);
    UART_Init(UART, 38400);

    /* Set GPIOs as SSP function */
    GPIO_SetPinChannel(SCLK_GPIO, SCLK_GPIO_FUNC);
    GPIO_SetPinChannel(SFRM_GPIO, SFRM_GPIO_FUNC);
    GPIO_SetPinChannel(MOSI_GPIO, MOSI_GPIO_FUNC);
    GPIO_SetPinChannel(MISO_GPIO, MISO_GPIO_FUNC);

    /* Set Output Strength(for high speed) */
    GPIO_SetOutStrength(SCLK_GPIO, GPIO_OUT_STRENGTH_20MA);
    GPIO_SetOutStrength(SFRM_GPIO, GPIO_OUT_STRENGTH_20MA);
    GPIO_SetOutStrength(MOSI_GPIO, GPIO_OUT_STRENGTH_20MA);
    GPIO_SetOutStrength(MISO_GPIO, GPIO_OUT_STRENGTH_20MA);

    /*
     * Init SSP Baud Rate, Frame Format
     * 1) Set master mode
     * 2) Set frame data 8-bits width
     * 3) Set transfer speed 30Mbps
     * 4) Set pin SFRM active low
     * 5) Set SCLK Polarity: SCLK is low when bus idle
     * 6) Set SCLK Phase: sample data on SCLK rising edge
     */
    SSP_Init(SSP, SSP_MASTER_MODE, DataWidth_8bit, SSPBaudrate_1M, SSP_FRM_ACTIVE_LOW,
             SSP_CLK_IDLE_LOW, SSP_SEND_ON_FALLING_EDGE);

    /* Disable FIFO Packed Mode */
    SSP_DisableFIFOPackMode(SSP);

    /* Enable SSP */
    SSP_Enable(SSP);

    /* SSP Master Tx/Rx datum */
    SSP_TxRx();

    while (1)
    {

    }
}
