#include "ws2812b_spi.h"
#include "user_ws2812b.h"

/*******************************************************************************
  * @函数名称   user_spi_dma_init
  * @函数说明   SPI DMA初始化
  * @输入参数  	txData：数据指针
               txSize：数据长度
  * @输出参数   无
  * @返回参数   无
*******************************************************************************/
void Ws2812b_spi_dma_init(uint8_t *txData, uint32_t txSize)
{
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    DMA_DeInit(USER_WS2812B_DMA_CHANNEL);  // Channel 3 for SPI1_TX
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USER_WS2812B_SPI->DATAR;

    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)txData;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = txSize;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    DMA_Init(USER_WS2812B_DMA_CHANNEL, &DMA_InitStructure);
    // 启用 DMA 传输完成中断
    DMA_ITConfig(USER_WS2812B_DMA_CHANNEL, DMA_IT_TC, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = USER_WS2812B_DMA_IRQn;

    NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
  * @函数名称   SPI_FullDuplex_Init
  * @函数说明   SPI初始化
  * @输入参数  	无
  * @输出参数   无
  * @返回参数   无
*******************************************************************************/
void ws2812b_spi_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    SPI_InitTypeDef  SPI_InitStructure = {0};

    USER_WS2812B_GPIO_RCC_ENABLE;
    USER_WS2812B_SPI_RCC_ENABLE;
    USER_WS2812B_DMA_RCC_ENABLE;

    GPIO_InitStructure.GPIO_Pin = USER_WS2812B_SPI_MOSI_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(USER_WS2812B_SPI_MOSI_GPIO, &GPIO_InitStructure);

    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(USER_WS2812B_SPI, &SPI_InitStructure);

    SPI_CalculateCRC(USER_WS2812B_SPI, DISABLE);

    SPI_Cmd(USER_WS2812B_SPI, ENABLE);
}

/*******************************************************************************
* Function Name  : bUser_spi_WriteByte
* Description    : SPI写字节
* Input          : unsigned char TxData 写入字节
* Output         : None
* Return         : 结果
Flash_ReadWriteByte
*******************************************************************************/
bool bWs2812b_spi_WriteByte(volatile uint8_t *TxData, uint32_t TxSize)
{
    // 清除传输完成标志
    vUser_ws2812b_set_tx_stat(true);
    // 配置 DMA
    DMA_SetCurrDataCounter(USER_WS2812B_DMA_CHANNEL, TxSize);
    // 启用 SPI DMA 请求
    SPI_I2S_DMACmd(USER_WS2812B_SPI, SPI_I2S_DMAReq_Tx, ENABLE);
    // 启用 DMA 通道
    DMA_Cmd(USER_WS2812B_DMA_CHANNEL, ENABLE);

    return true;
}
