/* 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) 2023. 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 eio_spi_master_interrupt_sample.c
*
* @brief This file contains the user EIO spi function.
*
*/

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "ac7803x.h"
#include "ac780x_eio_spi_drv.h"
#include "ac780x_gpio.h"
#include "stdlib.h"
#include "eio_spi_master_interrupt_sample.h"

/* ============================================  Define  ============================================ */
#define EIO_SPI_DATA_LEN  10        /*spi transfer length*/
/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
uint8_t g_spiTxBuffer[EIO_SPI_DATA_LEN] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};   /*SPI send buffer*/
uint8_t g_spiRxBuffer[EIO_SPI_DATA_LEN] = {0};                               /*SPI receive buffer*/
uint8_t g_spi_Flag = 0;                                                      /*SPI transfer end flag*/

eio_spi_master_user_config_t g_eio_spi_MasterConfig = {0};
eio_device_state_t g_eio_device_state;
eio_spi_master_state_t   SPIMasterState;

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


/* ======================================  Functions define  ======================================== */
/*!
* @brief  EIO_SPI_Callback
*         user EIO spi callback
* @param  driverState: EIO SPI state
* @param  event：the events which can trigger spi callback
* @param  userData：userdata 
* @return none
*/
static void EIO_SPI_Callback(void *driverState, eio_spi_event_t event, void *userData)
{
    if(event == EIO_SPI_EVENT_END_TRANSFER)
    {
        g_spi_Flag = 1;
    }
}

/*!
* @brief EIO SPI Init
*
* @param none
* @return none
*/
void EIO_SPI_Master_Init(void)
{
    /* GPIO config for EIO function */
    GPIO_SetFunc(GPIOB, GPIO_PIN12, GPIO_FUN5);  /*PB12:EIO_D0 */
    GPIO_SetFunc(GPIOB, GPIO_PIN11, GPIO_FUN5);  /*PB11:EIO_D1 */
    GPIO_SetFunc(GPIOA, GPIO_PIN11, GPIO_FUN4);  /*PA11:EIO_D2 */
    GPIO_SetFunc(GPIOA, GPIO_PIN10, GPIO_FUN4);  /*PA10:EIO_D3 */

    /*EIO SPI Master configure parameter */
    g_eio_spi_MasterConfig.driverType = EIO_DRIVER_TYPE_INTERRUPTS,  /*interrupt type*/
    g_eio_spi_MasterConfig.clockPolarity = 0U,                       /*Set the polarity of SCK operation, with SCK set to high during operation*/
    g_eio_spi_MasterConfig.clockPhase = 0U,                          /*Set data sampling phase, first edge sampling*/
    g_eio_spi_MasterConfig.baudRate = 2000000U,                      /*set spi baudrate*/
    g_eio_spi_MasterConfig.bitOrder = EIO_SPI_TRANSFER_MSB_FIRST,    /*Bit order: MSB-first*/
    g_eio_spi_MasterConfig.transferSize = EIO_SPI_TRANSFER_1BYTE,    /*Transfer size in bytes: 1*/
    g_eio_spi_MasterConfig.mosiPin = 0U,                             /*SPI MOSI PIN*/
    g_eio_spi_MasterConfig.misoPin = 1U,                             /*SPI MISO PIN*/
    g_eio_spi_MasterConfig.sckPin = 2U,                              /*SPI SCK PIN*/
    g_eio_spi_MasterConfig.ssPin = 3U,                               /*SPI ss PIN*/
    g_eio_spi_MasterConfig.callback = EIO_SPI_Callback,              /*user callback*/
    g_eio_spi_MasterConfig.callbackParam = NULL,                     /*param[in]eter for the callback function*/
    g_eio_spi_MasterConfig.txDMAChannel = 0U,                        /*Tx DMA channel number. Only used in DMA mode*/
    g_eio_spi_MasterConfig.rxDMAChannel = 0U,                        /*Rx DMA channel number. Only used in DMA mode*/

    EIO_DRV_InitDevice(0, &g_eio_device_state);
    EIO_SPI_DRV_MasterInit(0, &g_eio_spi_MasterConfig, &SPIMasterState);
}

/*!
* @brief EIO_SPIMasterTestTask
*        EIO SPI send and receive data with interrupt mode 
* @param none
* @return none
*/
void EIO_SPIMasterTestTask(void)
{
    status_t status;
    status = EIO_SPI_DRV_MasterTransfer(&SPIMasterState, g_spiTxBuffer, g_spiRxBuffer, EIO_SPI_DATA_LEN);

    while(0 == g_spi_Flag);
    g_spi_Flag = 0;
    if(STATUS_SUCCESS != status)
    {
        printf("spi transfer fail,status %d\r\n", status);
    }
    else
    {
        printf("master rece data:\r\n");
        for(uint8_t i = 0; i < EIO_SPI_DATA_LEN; i++)
        {
            printf("%d ", g_spiRxBuffer[i]);
        }
        printf("\r\n");
    }
}

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