/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. 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"spc1168.h"
#include<stdio.h>
#include<stdlib.h>

#define                  DEBUG_INFO                      1                  /*debug infomation flag*/
#define                  T_BUFFER_SIZE                   128                /*data size of recieve and tansmit*/
#define                  DataWidth_8bit                  8                  /*data width size of SSP*/
#define                  SSPBaudrate_1M                  1000000            /*SSP baudrate*/

uint32_t                 gu32BuffSize                    = T_BUFFER_SIZE;
uint32_t                 gau32TxBuf[T_BUFFER_SIZE];
uint32_t                 gau32RxBuf[T_BUFFER_SIZE];
uint32_t                 gau32Buf[T_BUFFER_SIZE];

void SSP_TxRx(void)
{
  int i;

  while(1)
  {
    /* Prepare data to be send */
    for(i=0; i < (int)(gu32BuffSize); i++)
        gau32TxBuf[i] = rand() & 0xFF;

    printf("Master Tx data...\n");
    /* Wait Slave Prepare data */
    Delay_Ms(4000);
    SSP_MasterB2BTransceive(SSP, gau32TxBuf, gau32Buf, gu32BuffSize);

    printf("Master Rx data...\n");
    /* Wait Slave Prepare data */
    Delay_Ms(4000);
    SSP_MasterB2BTransceive(SSP, gau32Buf, gau32RxBuf, gu32BuffSize);

    for(i=0; i < (int)(gu32BuffSize); i++)
    {
      if( gau32TxBuf[i] != gau32RxBuf[i])
        printf("[Error]@%4d: %3d: TX(%p,0x%02X) != RX(%p,0x%02X)\n", __LINE__, 
                              i, &gau32TxBuf[i], gau32TxBuf[i], &gau32RxBuf[i], gau32RxBuf[i] );
      else
        printf("%3d: TX(%p,0x%02X) == RX(%p,0x%02X)\n", i, &gau32TxBuf[i],
                              gau32TxBuf[i], &gau32RxBuf[i], gau32RxBuf[i] );
    }
  }

}


 /*************************************************************************************************************************
 *
 * @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 'SFRAM' 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 reakout the Back-to-Back mode.
 *
 *             This demo code couple with another named 'SSP_Slave_B2B_TxRx_Polling'.
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  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(GPIO_40,GPIO40_SPI_SCLK);
  GPIO_SetPinChannel(GPIO_37,GPIO37_SPI_SFRM);
  GPIO_SetPinChannel(GPIO_38,GPIO38_SPI_MOSI);
  GPIO_SetPinChannel(GPIO_39,GPIO39_SPI_MISO);

  /* Set Output Strength(for high speed) */
  GPIO_SetOutStrength(GPIO_40, GPIO_OUT_STRENGTH_20MA);
  GPIO_SetOutStrength(GPIO_37, GPIO_OUT_STRENGTH_20MA);
  GPIO_SetOutStrength(GPIO_38, GPIO_OUT_STRENGTH_20MA);
  GPIO_SetOutStrength(GPIO_39, 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);

  /*
   * We need delay some time to wait for GPIOs ready,
   * if not, there may be problem if the master and slave
   * is not the same Chip. etc, Master is SPD1178, Slave is SPC1068.
   */
  Delay_Ms(500);

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

  while(1)
  {

  }
}
