/*!
 * @file        soft_spi.h
 *
 * @brief       This file contains all the functions prototypes for the soft spi
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */
#ifndef SOFT_SPI_H
#define SOFT_SPI_H

#include <stdint.h>
#include "stdbool.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @addtogroup SPI_Driver
  @{
*/

/** @addtogroup SPI_Soft_Master
  @{
*/

/** @defgroup SPI_Soft_Master_Enumerations Enumerations
  @{
*/
typedef enum
{
    SPI_MODE_0 = 0,  //!< CPOL=0, CPHA=0
    SPI_MODE_1 = 1,  //!< CPOL=0, CPHA=1
    SPI_MODE_2 = 2,  //!< CPOL=1, CPHA=0
    SPI_MODE_3 = 3   //!< CPOL=1, CPHA=1
} SoftSpi_ModeType;

/**@} end of group SPI_Soft_Master_Enumerations */


/** @defgroup SPI_Soft_Master_Structures Structures
  @{
*/

typedef void (*SoftSpi_PinSetPtr)(uint8_t Val);
typedef uint8_t (*SoftSpi_PinReadPtr)(void);

typedef struct
{
    SoftSpi_ModeType mode;
    uint8_t cpol;
    uint8_t cpha;
    SoftSpi_PinSetPtr set_mosi;
    SoftSpi_PinReadPtr read_miso;
    SoftSpi_PinSetPtr set_miso;
    SoftSpi_PinSetPtr set_miso_output_type;
    SoftSpi_PinReadPtr read_mosi;
    SoftSpi_PinSetPtr set_sck;
    SoftSpi_PinReadPtr read_sck;
    SoftSpi_PinSetPtr set_cs;
    SoftSpi_PinReadPtr read_cs;
    uint32_t half_clock_delay_us;
} SoftSpi_ConfigType;

typedef struct
{
    volatile uint8_t rx_byte;
    volatile uint8_t tx_byte;
    volatile uint8_t next_tx_byte;
    volatile uint8_t bit_idx;
    volatile uint8_t edge_phase;
    volatile uint8_t rx_ready;
    volatile uint8_t cs_active;
} SoftSpi_SlaveCtxType;

/**@} end of group SPI_Soft_Master_Structures */

/*******************************************************************************
                            FUNCTION DECLARATIONS
*******************************************************************************/
/** @defgroup SPI_Soft_Master_Functions Functions
  @{
*/

void SoftSpi_Init(SoftSpi_ConfigType *CfgPtr);
uint8_t SoftSpi_MasterTransferByte(const SoftSpi_ConfigType *CfgPtr, uint8_t Tx_data);
void SoftSpi_SlaveInit(const SoftSpi_ConfigType *CfgPtr, SoftSpi_SlaveCtxType *CtxPtr);
void SoftSpi_SlaveSckIsr(const SoftSpi_ConfigType *CfgPtr, SoftSpi_SlaveCtxType *CtxPtr);

#endif /* SOFT_SPI_H */

/**@} end of group SPI_Soft_Master_Functions */
/**@} end of group SPI_Soft_Master */
/**@} end of group SPI_Examples */
/**@} end of group Examples */
