/*****************************************************************************/
/** @file       esp_spi.h
*
*   @brief      ESP32 SPI interface definitions and functions
*   @ingroup    esp_wrapper Processor Layer Driver Wrapper
*
*   @copyright  Copyright(C) 2022 by Access Business Group (ABG)
*
*   @par Software License Agreement
*
*   @par
*   The software supplied herewith by ABG.
*   (the "Company") is intended and supplied to you, the Company's
*   supplier, for use solely and exclusively on ABG
*   products. The software is owned by the Company and is
*   protected under applicable copyright laws. All rights are reserved.
*   Any use in violation of the foregoing restrictions may subject the
*   user to criminal sanctions under applicable laws, as well as to
*   civil liability for the breach of the terms and conditions of this
*   license.
*
*   @par
*   THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
*   WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
*   TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
*   PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
*   IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
*   CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
*   @par
*   Copyright(C) 2022 by ABG.
*   All rights reserved.  No part of this software may be disclosed or
*   distributed in any form or by any means without the prior written
*   consent of ABG.
*
******************************************************************************/

#ifndef ESP_SPI_H_INCLUDED
#define ESP_SPI_H_INCLUDED

#include "uController.h"
#include "esp_err.h"
#include "esp_intr_alloc.h"
#include "driver/spi_master.h"
#include "driver/spi_slave.h"
//#include "soc/spi_common.h"
//#include "soc/spi_reg.h"
#include "soc/dport_reg.h"
#include "soc/spi_struct.h"
#include "procConfig.h"

/** @ingroup    esp_wrapper
*   @{
*/


/******************************************************************************
*   DEFINITIONS
******************************************************************************/

#define SPI_MAX_LOCAL_BUFFER_LENGTH     32



#if !defined(SPI3_ENABLED) || !defined(SPI3_NUM_DEVICES)
/**< The SPI peripheral is disabled by default. */
#define SPI3_ENABLED                false
#define SPI3_NUM_DEVICES            0
#endif

#ifndef SPI3_CONFIGURE_AT_BOOT
#define SPI3_CONFIGURE_AT_BOOT      false
#endif


/** Status codes used by the SPI driver. */
typedef enum
{
    SPI_ERROR = -1,
    SPI_OK = 0,
    SPI_ERROR_TIMEOUT = 1,
    SPI_ERROR_ARGUMENT,
    SPI_ERROR_OVERRUN,
    SPI_ERROR_MODE_FAULT,
    SPI_ERROR_OVERRUN_AND_MODE_FAULT
} spi_status_t;


typedef struct
{
    union
    {
        uint32_t    val;
        struct
        {
            uint32_t    size        : 12;
            uint32_t    length      : 12;
            uint32_t    reserved    : 6;
            uint32_t    eof         : 1;
            uint32_t    owner       : 1;
        } bits;
    } DW0;
    uint32_t    pu32BuffAddr;
    uint32_t    pu32NextDesc;
        
} dma_desc_t;


/* Type definition for slave transfer callback - called when SPI transfer is completed by master */
typedef void (*slaveTransCb)( void* pvRxBuffer, size_t rxLength );


/******************************************************************************
*   FUNCTION PROTOTYPES
******************************************************************************/

void SpiInit( void );

void Spi3Init( void );
esp_err_t Spi3SlaveQueueXfer( uint8_t* pu8Data, size_t* pLength, slaveTransCb callback );
void Spi3TimeSlice( void );

/** @} */ /* end of group */

#endif /* ESP_SPI_H_INCLUDED */

