/**
 * 与单片机平台有关
 */
#include "gpio.h"
#include "spi.h"


#include	"bc3603.h"

/**
 * @brief Transmission 1-byte strobe command.
 * @param cmd: 1-byte strobe command
 * @retval None
 */
void BC3603_StrobeCommand(uint8_t Cmd)
{
    /* SPI CS(NSS) 引脚先拉低 选中 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);

    /* SPI 先发送命令 */
    HAL_SPI_Transmit( &hspi1, (uint8_t*)&Cmd, 1, 0xFFFF );

    /* SPI CS(NSS) 引脚再拉高 结束 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET );
}

/**
 * @brief read data from RF register.
 * @param addr: register address.
 * @retval register data(uint8_t)
 */
uint8_t BC3603_ReadRegister(uint8_t Addr)
{

    Addr &= REGSADDR_MASK;										/* mask register address */
    Addr |= READ_REGS_CMD;                             /* set read register command */

    uint8_t data;

    /* SPI CS(NSS) 引脚先拉低 选中 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);

    /* SPI 先发送地址 */
    HAL_SPI_Transmit( &hspi1, (uint8_t*)&Addr, 1, 0xFFFF );

    /* SPI 再读取响应数据 */
    HAL_SPI_Receive( &hspi1, (uint8_t*)&data, 1, 0xFFFF );

    /* SPI CS(NSS) 引脚再拉高 结束 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET );

    return data;
}

/**
 * @brief write data to RF register.
 * @param addr: register address.
 * @param data: write data.
 * @retval None
 */
void BC3603_WriteRegister(uint8_t Addr,uint8_t Data)
{
    Addr &= REGSADDR_MASK;										/* mask register address */
    Addr |= WRITE_REGS_CMD;                            /* set write register command */

    /* SPI CS(NSS) 引脚先拉低 选中 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);

    /* SPI 先发送地址 */
    HAL_SPI_Transmit( &hspi1, (uint8_t*)&Addr, 1, 0xFFFF );

    /* SPI 再发送数据 */
    HAL_SPI_Transmit( &hspi1, (uint8_t*)&Data, 1, 0xFFFF );

    /* SPI CS(NSS) 引脚再拉高 结束 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET );
}

/**
 * @brief read multiple data from RF register.
 * @param cmd: strobe command.
 * @param pbuf: data buffer.
 * @param length: read length.
 * @retval None
 */
void BC3603_ReadMultipleRegister(uint8_t Cmd,uint8_t *DstAddr,uint16_t Length)
{
    /* SPI CS(NSS) 引脚先拉低 选中 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);

    /* SPI 先发送命令 */
    HAL_SPI_Transmit( &hspi1, (uint8_t*)&Cmd, 1, 0xFFFF );

    /* SPI 再读取响应数据 */
    HAL_SPI_Receive( &hspi1, DstAddr, Length, 0xFFFF );

    /* SPI CS(NSS) 引脚再拉高 结束 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET );
}

/**
 * @brief write multiple data to RF register.
 * @param cmd: strobe command.
 * @param pbuf: data buffer.
 * @param length: read length.
 * @retval None
 */
void BC3603_WriteMultipleRegister(uint8_t Cmd,uint8_t *ScrAddr,uint16_t Length)
{
    /* SPI CS(NSS) 引脚先拉低 选中 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);

    /* SPI 先发送命令 */
    HAL_SPI_Transmit( &hspi1, (uint8_t*)&Cmd, 1, 0xFFFF );

    /* SPI 再发送数据 */
    HAL_SPI_Transmit( &hspi1, ScrAddr, Length, 0xFFFF );

    /* SPI CS(NSS) 引脚再拉高 结束 */
    HAL_GPIO_WritePin( SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET );
}
