/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2022. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

/******************************************************************************
 * @version: V1.0.0  demo
 ******************************************************************************/

/*!
* @file spi.c
*
* @brief This file contains the user spi function.
*
*/

/* ===========================================  Includes  =========================================== */
#include "spi.h"
#include "gpio.h"
#include "string.h"

/* ============================================  Define  ============================================ */


/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
SPI_ConfigType spiConfig; /*! SPI配置结构体*/

/*! SPI发送和接收数组*/
uint8_t g_spiTxBuff[DATA_SIZE] = {1, 1, 3, 4, 5, 6, 7, 8};
uint8_t g_spiRxBuff[DATA_SIZE] = {0};

/* ====================================  Functions declaration  ===================================== */

/*!
* @brief   user SPI0 callback function
*
* @param   device:
* @param   wpara: SPI0->STATUS;
* @param   lpara: SPI0->CFG1;
*
* @return  void
*/
void SPI0_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    if (wpara & SPI_STATUS_TXUF_Msk)
    {
        /*! TX下溢处理*/
    }
    else if (wpara & SPI_STATUS_RXOF_Msk)
    {
        /*! RX溢出处理*/
    }
}
/*!
 * @brief   SPI initialization
 *
 * @param   none
 * @return  none
 */
void SPI_MasterInit(void)
{
    /*! 初始化SPI引脚,功能复用选择.*/
    GPIO_SetFunc(GPIOB, GPIO_PIN5, GPIO_FUN3);      /*! SCK */
    GPIO_SetFunc(GPIOA, GPIO_PIN7, GPIO_FUN3);      /*! MOSI */
    GPIO_SetFunc(GPIOB, GPIO_PIN4, GPIO_FUN3);      /*! MISO */
    GPIO_SetFunc(GPIOA, GPIO_PIN6, GPIO_FUN3);      /*! CS */

    /*! 清零配置结构体变量.*/
    memset(&spiConfig, 0x00, sizeof(spiConfig));
    /*! 初始化SPI参数,波特率 = 2Mbps = (F_BCLK / (SCK_LOW+1 + SCK_HIGH+1)).*/
    spiConfig.csSetup = 4;                          /*! 片选建立时间  = (CS_SETUP + 1) * CLK_PERIOD.*/
    spiConfig.csHold  = 4;                          /*! 片选保持时间  = (CS_HOLD + 1) * CLK_PERIOD.*/
    spiConfig.sckHigh = 3;                          /*! SCK高电平时间 = (SCK_HIGH + 1) * CLK_PERIOD.*/
    spiConfig.sckLow  = 3;                          /*! SCK低电平时间 = (SCK_LOW + 1) * CLK_PERIOD.*/
    spiConfig.csIdle  = 4;                          /*! 两条数据间最短时间间隔 = (CS_IDLE + 1) * CLK_PERIOD.*/
    spiConfig.mode          = SPI_MASTER;           /*! 设置为主机模式*/
    spiConfig.cpha          = SPI_CPHA_2EDGE;       /*! 设置数据采样相位,第2个边沿采样数据*/
    spiConfig.cpol          = SPI_CPOL_HIGH;        /*! 设置SCK空闲时极性,空闲时SCK为高*/
    spiConfig.frmSize       = SPI_FRAME_SIZE_8BITS;
    spiConfig.rxMsbFirstEn  = ENABLE;               /*! 选择从最高位开始接收*/
    spiConfig.txMsbFirstEn  = ENABLE;               /*! 选择从最高位开始发送*/
    spiConfig.csOutputEn    = ENABLE;               /*! CS由SPI硬件控制*/
    spiConfig.continuousCSEn = ENABLE;              /*! 片选连续模式*/
    spiConfig.modeFaultEn   = DISABLE;              /*! 模式故障禁止*/
    spiConfig.wakeUpEn      = DISABLE;              /*! 主机模式不支持唤醒功能*/
    spiConfig.spiEn         = ENABLE;
    spiConfig.callBack      = SPI0_Callback;
    spiConfig.interruptEn       = ENABLE;           /*! 使能NVIC中断*/
    spiConfig.txUFInterruptEn   = ENABLE;           /*! 打开TXUF中断,可禁止*/
    spiConfig.rxOFInterruptEn   = ENABLE;           /*! 打开RXOF中断,可禁止*/
    spiConfig.modeFaultInterruptEn = DISABLE;       /*! 关闭模式故障中断*/
    SPI_Init(SPI0, &spiConfig);
}

/*!
 * @brief   SPI TransmitReceivePoll Test
 *
 * @param   none
 * @return  none
 */
void SPI_MasterTransmitReceivePolling(void)
{
    while (g_spiTxBuff[0] == g_spiTxBuff[1])        /*! 等待按键按下*/
    {
        ;
    }
    g_spiTxBuff[0]++;

    SPI_TransmitReceivePoll(SPI0, g_spiRxBuff, g_spiTxBuff, DATA_SIZE);

    g_spiTxBuff[1] = g_spiTxBuff[0];
}
/* ======================================  Functions define  ======================================== */


/* =============================================  EOF  ============================================== */
