/* 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) 2024. 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: V3.1.0  AC7840x  Demo
 ******************************************************************************/

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

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "ac7840x.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "port_hw.h"
#include "eio_spi_drv.h"
#include "stdlib.h"
#include "eio_spi_slave_polling_sample.h"

/* ============================================  Define  ============================================ */
#define EIO_SPI_DATA_LEN  10

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

/* ==========================================  Variables  =========================================== */
/*! SPI发送和接收数组*/
uint8_t g_spiRxBuffer[EIO_SPI_DATA_LEN] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
uint8_t g_spiTxBuffer[EIO_SPI_DATA_LEN] = {0xaa, 0xaa, 0xaa, 7, 6, 5, 4, 3, 0x55, 0x55};

/*! @brief EIO SPI Slave configure parameter */
eio_spi_slave_user_config_t g_eio_spi_SlaveConfig =
{
    .driverType =  EIO_DRIVER_TYPE_POLLING,     /*! 选择通信类型为阻塞方式*/
    .clockPolarity = 0U,                        /*! 设置SCK工作时极性,工作时SCK为高*/
    .clockPhase = 0U,                           /*! 设置数据采样相位,第1个边沿采样数据*/
    .bitOrder = EIO_SPI_TRANSFER_MSB_FIRST,     /*! Bit order: LSB-first / MSB-first*/
    .transferSize = EIO_SPI_TRANSFER_1BYTE,     /*! Transfer size in bytes: 1/2/4*/
    .mosiPin = 0U,
    .misoPin = 1U,
    .sckPin = 2U,
    .ssPin = 3U,
    .callback = NULL,
    .callbackParam = NULL,
    .txDMAChannel = 0U,                         /*! 设置SPI DMA发送通道，若未使用DMA方式传输，则该配置可忽略*/
    .rxDMAChannel = 0U,                         /*! 设置SPI DMA发送通道，若未使用DMA方式传输，则该配置可忽略*/
};

eio_device_state_t g_eio_device_state;
eio_spi_slave_state_t   SPISlaveState;

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

/*!
* @brief EIO SPI Init
*
* @param none
* @return none
*/
void EIO_SPI_Slave_Init(void)
{
    /* GPIO config for EIO function */
    GPIO_DRV_SetMuxModeSel(PORTD, (gpio_channel_type_t)0U, PORT_MUX_ALT6);   /* PD0：EIO_D0 */
    GPIO_DRV_SetMuxModeSel(PORTD, (gpio_channel_type_t)1U, PORT_MUX_ALT6);   /* PD1：EIO_D1 */
    GPIO_DRV_SetMuxModeSel(PORTA, (gpio_channel_type_t)0U, PORT_MUX_ALT4);   /* PA0：EIO_D2 */
    GPIO_DRV_SetMuxModeSel(PORTA, (gpio_channel_type_t)1U, PORT_MUX_ALT4);   /* PA1：EIO_D3 */

    EIO_DRV_InitDevice(0, &g_eio_device_state);
    EIO_SPI_DRV_SlaveInit(0, &g_eio_spi_SlaveConfig, &SPISlaveState);

}

/*!
* @brief EIO SPI send and receive data
*
* @param none
* @return none
*/
uint8_t EIO_SPISlaveTestTask(void)
{
    EIO_SPI_DRV_SlaveTransferBlocking(&SPISlaveState, g_spiTxBuffer, g_spiRxBuffer, EIO_SPI_DATA_LEN, 3000);

    return 0;
}

/* ======================================  Functions define  ======================================== */


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