
#include "../include/hal_lksmcu_reg.h"
#include "../include/hal_lksmcu_spi.h"
#include "hal_lks32mc09x.h"

/**
 * @brief 模块复位
 * @param spi SPI模块号
 */
void HAL_SPI_Reset(HAL_SPI_x spi)
{
    HAL_REG_SYS_UNLOCK();
    HAL_REG_SET(SYS_SFT_RST, BIT10);
    HAL_REG_RESET(SYS_SFT_RST, BIT10);
    HAL_REG_SYS_LOCK();
}

/**
 * @brief 模块打开
 * @param spi SPI模块号
 */
void HAL_SPI_Enable(HAL_SPI_x spi)
{
    HAL_REG_SYS_UNLOCK();
    HAL_REG_SET(SYS_CLK_FEN, BIT10);
    HAL_REG_SYS_LOCK();
}

/**
 * @brief 模块关闭
 * @param spi SPI模块号
 */
void HAL_SPI_Disable(HAL_SPI_x spi)
{
    HAL_REG_SYS_UNLOCK();
    HAL_REG_RESET(SYS_CLK_FEN, BIT10);
    HAL_REG_SYS_LOCK();
}

/**
 * @brief 设置波特率
 * @param spi SPI模块号
 */
void HAL_SPI_SetBaud(HAL_SPI_x spi, uint32_t baud)
{
    uint32_t mclk = HAL_CLK_GetMcuFreq();
    uint32_t cnt  = (mclk / (2 * baud)) - 1;
    HAL_REG_WRITE_BIT(HAL_SPI[spi]->DIV, 0x1f, cnt);
}

/**
 * @brief 发送数据
 * @param spi SPI模块号
 * @param data 要发送的数据
 * @param len 数据长度
 * @return 发送状态
 */
uint8_t HAL_SPI_TxData(HAL_SPI_x spi, uint8_t *data, uint32_t len)
{
    int i = 0;
    HAL_REG_WRITE(HAL_SPI[spi]->SIZE, 8);
    HAL_REG_SET(HAL_SPI[spi]->CFG, BIT7 | BIT4 | BIT0);
    for (i = 0; i < len; i++)
    {
        HAL_REG_WRITE_BIT(HAL_SPI[spi]->IE, 0x07, BIT2);
        HAL_REG_WRITE(HAL_SPI[spi]->TX_DATA, data[i]);
        while (HAL_REG_READ(HAL_SPI[spi]->IE & BIT2) == 0)
        {
        }
    }
    return 0;
}

/**
 * @brief 接收数据
 * @param spi SPI模块号
 * @param data 接收缓冲区
 * @param len 数据长度
 * @return 接收状态
 */
uint8_t HAL_SPI_RxData(HAL_SPI_x spi, uint8_t *data, uint32_t len)
{
    int i = 0;
    HAL_REG_WRITE(HAL_SPI[spi]->SIZE, 8);
    HAL_REG_SET(HAL_SPI[spi]->CFG, BIT7 | BIT6 | BIT4 | BIT0);
    for (i = 0; i < len; i++)
    {
        HAL_REG_WRITE_BIT(HAL_SPI[spi]->IE, 0x07, BIT2);
        HAL_REG_WRITE(HAL_SPI[spi]->TX_DATA, 0); // 发送一个空字节，以触发接收
        while (HAL_REG_READ(HAL_SPI[spi]->IE & BIT2) == 0)
        {
        }
        data[i] = HAL_REG_READ(HAL_SPI[spi]->RX_DATA);
    }
    // 实现接收数据的代码
    return 0; // 返回接收状态
}

/**
 * @brief 发送和接收数据
 * @param spi SPI模块号
 * @param rxdata 接收缓冲区
 * @param txdata 要发送的数据
 * @param len 数据长度
 * @return 发送和接收状态
 */
uint8_t HAL_SPI_RxTxData(HAL_SPI_x spi, uint8_t *rxdata, uint8_t *txdata, uint32_t len)
{
    int i = 0;
    HAL_REG_WRITE(HAL_SPI[spi]->SIZE, 8);
    HAL_REG_SET(HAL_SPI[spi]->CFG, BIT4 | BIT0);
    for (i = 0; i < len; i++)
    {
        HAL_REG_WRITE_BIT(HAL_SPI[spi]->IE, 0x07, BIT2);
        HAL_REG_WRITE(HAL_SPI[spi]->TX_DATA, txdata[i]);
        while (HAL_REG_READ(HAL_SPI[spi]->IE & BIT2) == 0)
        {
        }
        rxdata[i] = HAL_REG_READ(HAL_SPI[spi]->RX_DATA);
    }
    return 0; // 返回发送和接收状态
}

/**
 * @brief 发送数据
 * @param spi SPI模块号
 * @param data 要发送的数据
 * @param len 数据长度
 * @return 发送状态
 */
uint8_t HAL_SPI_TxDataWidth16(HAL_SPI_x spi, uint16_t *data, uint32_t len)
{
    int i = 0;
    HAL_REG_WRITE(HAL_SPI[spi]->SIZE, 16);
    HAL_REG_SET(HAL_SPI[spi]->CFG, BIT7 | BIT4 | BIT0);
    for (i = 0; i < len; i++)
    {
        HAL_REG_WRITE_BIT(HAL_SPI[spi]->IE, 0x07, BIT2);
        HAL_REG_WRITE(HAL_SPI[spi]->TX_DATA, data[i]);
        while (HAL_REG_READ(HAL_SPI[spi]->IE & BIT2) == 0)
        {
        }
    }
    return 0;
}
/**
 * @brief 接收数据
 * @param spi SPI模块号
 * @param data 接收缓冲区
 * @param len 数据长度
 * @return 接收状态
 */
uint8_t HAL_SPI_RxDataWidth16(HAL_SPI_x spi, uint16_t *data, uint32_t len)
{
    int i = 0;
    HAL_REG_WRITE(HAL_SPI[spi]->SIZE, 16);
    HAL_REG_SET(HAL_SPI[spi]->CFG, BIT7 | BIT6 | BIT4 | BIT0);
    for (i = 0; i < len; i++)
    {
        HAL_REG_WRITE_BIT(HAL_SPI[spi]->IE, 0x07, BIT2);
        HAL_REG_WRITE(HAL_SPI[spi]->TX_DATA, 0); // 发送一个空字节，以触发接收
        while (HAL_REG_READ(HAL_SPI[spi]->IE & BIT2) == 0)
        {
        }
        data[i] = HAL_REG_READ(HAL_SPI[spi]->RX_DATA);
    }
    return 0; // 返回接收状态
}
/**
 * @brief 发送和接收数据
 * @param spi SPI模块号
 * @param rxdata 接收缓冲区
 * @param txdata 要发送的数据
 * @param len 数据长度
 * @return 发送和接收状态
 */
uint8_t HAL_SPI_RxTxDataWidth16(HAL_SPI_x spi, uint16_t *rxdata, uint16_t *txdata, uint32_t len)
{
    int i = 0;
    HAL_REG_WRITE(HAL_SPI[spi]->SIZE, 16);
    HAL_REG_SET(HAL_SPI[spi]->CFG, BIT4 | BIT0);
    for (i = 0; i < len; i++)
    {
        HAL_REG_WRITE_BIT(HAL_SPI[spi]->IE, 0x07, BIT2);
        HAL_REG_WRITE(HAL_SPI[spi]->TX_DATA, txdata[i]);
        while (HAL_REG_READ(HAL_SPI[spi]->IE & BIT2) == 0)
        {
        }
        rxdata[i] = HAL_REG_READ(HAL_SPI[spi]->RX_DATA);
    }
    return 0; // 返回发送和接收状态
}
