/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-05     Mexico       the first version
 */
#include "nrf24.h"
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "drv_soft_spi.h"

#define W25Q_SPI_DEVICE_NAME "spi10"
static struct rt_spi_device *spi_dev_w25q;

rt_uint8_t w25x_device_id[4] = {0x90,0x00,0x00,0x00};
rt_uint8_t id[5]={0};
const rt_uint8_t RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //发送地址

rt_uint8_t NRF24L01_Init_Check(void)
{
    rt_uint8_t buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
    rt_uint8_t i;
    char name[RT_NAME_MAX];

    rt_uint8_t pin2 = rt_pin_get("PB.0"); // csn
    rt_pin_mode(pin2, PIN_MODE_OUTPUT);
    rt_soft_spi_device_attach("spi0","spi10",RT_NULL,8);
    rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
    spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);
    if(!spi_dev_w25q)
    {
        rt_kprintf("spi sample run failed\n");
    }
    else {
        rt_uint8_t pin = rt_pin_get("PE.6"); // ce
        rt_pin_mode(pin, PIN_MODE_OUTPUT);
        rt_pin_write(pin,PIN_LOW);

        rt_uint8_t pin2 = rt_pin_get("PB.0"); // csn
        rt_pin_mode(pin2, PIN_MODE_OUTPUT);
        rt_pin_write(pin2,PIN_HIGH);

        struct rt_spi_configuration cfg;
        cfg.data_width = 8;
        cfg.mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_0;// 修改
        cfg.max_hz = 1*1000; //1.92 M
        rt_spi_configure(spi_dev_w25q, &cfg);

        NRF24L01_Write_Buf(WRITE_REG_NRF+TX_ADDR,buf,5);//写入5个字节的地址.
        NRF24L01_Read_Buf(TX_ADDR,buf,5); //读出写入的地址
        for(rt_uint8_t i=0;i<100;i++)
        {
            rt_kprintf("%x ",buf[i]);
            if(buf[i] != 0XA5)
                return 1;
        }
    }
}

void NRF24L01_RX_Mode(void)
{
    uint8_t pin = rt_pin_get("PE.6");
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    rt_pin_write(pin, PIN_LOW);

    NRF24L01_Write_Buf(WRITE_REG_NRF+RX_ADDR_P0,(rt_uint8_t*)RX_ADDRESS,RX_ADR_WIDTH);//写RX节点地址

    NRF24L01_Write_Reg(WRITE_REG_NRF+EN_AA,0x01);    //使能通道0的自动应答
    NRF24L01_Write_Reg(WRITE_REG_NRF+EN_RXADDR,0x01);//使能通道0的接收地址
    NRF24L01_Write_Reg(WRITE_REG_NRF+RF_CH,40);      //设置RF通信频率
    NRF24L01_Write_Reg(WRITE_REG_NRF+RX_PW_P0,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
    NRF24L01_Write_Reg(WRITE_REG_NRF+RF_SETUP,0x0f);//设置TX发射参数,0db增益,2Mbps,低噪声增益开启
    NRF24L01_Write_Reg(WRITE_REG_NRF+CONFIG, 0x0f);//配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式

    rt_kprintf("rx mode success\r\n");
    rt_pin_write(pin, PIN_HIGH);
}

//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
//返回值:0，接收完成；其他，错误代码
rt_uint8_t NRF24L01_RxPacket(rt_uint8_t *rxbuf)
{
    rt_uint8_t sta;
   // SPI2_SetSpeed(SPI_BaudRatePrescaler_8); //spi速度为9Mhz（24L01的最大SPI时钟为10Mhz）
    sta=NRF24L01_Read_Reg(STATUS);  //读取状态寄存器的值
    NRF24L01_Write_Reg(WRITE_REG_NRF+STATUS,sta); //清除TX_DS或MAX_RT中断标志
    if(sta&RX_OK)//接收到数据
    {
        NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
        NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器
        return 0;
    }
    return 1;//没收到任何数据
}

rt_uint8_t NRF24L01_Write_Buf(rt_uint8_t reg, rt_uint8_t *pBuf, rt_uint8_t len)
{
    rt_uint8_t status,u8_ctr;
   // NRF24L01_CSN = 0;          //使能SPI传输

    uint8_t pin = rt_pin_get("PB.0");
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    rt_pin_write(pin, PIN_LOW);

    status = SPI2_ReadWriteByte(reg);//发送寄存器值(位置),并读取状态值
    for(u8_ctr=0; u8_ctr<len; u8_ctr++)SPI2_ReadWriteByte(*pBuf++); //写入数据

    rt_pin_write(pin, PIN_HIGH);
   // NRF24L01_CSN = 1;       //关闭SPI传输
    return status;          //返回读到的状态值
}

rt_uint8_t NRF24L01_Read_Buf(rt_uint8_t reg,rt_uint8_t *pBuf,rt_uint8_t len)
{
    rt_uint8_t status,u8_ctr;
  //  NRF24L01_CSN = 0;           //使能SPI传输

    uint8_t pin = rt_pin_get("PB.0");
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    rt_pin_write(pin, PIN_LOW);

    status=SPI2_ReadWriteByte(reg);//发送寄存器值(位置),并读取状态值
    for(u8_ctr=0;u8_ctr<len;u8_ctr++)pBuf[u8_ctr]=SPI2_ReadWriteByte(0XFF);//读出数据
    rt_pin_write(pin, PIN_HIGH);
    //NRF24L01_CSN=1;       //关闭SPI传输
    return status;        //返回读到的状态值
}

//SPI写寄存器
//reg:指定寄存器地址
//value:写入的值
rt_uint8_t NRF24L01_Write_Reg(rt_uint8_t reg,rt_uint8_t value)
{
    rt_uint8_t status;
   // NRF24L01_CSN=0;                 //使能SPI传输
    uint8_t pin = rt_pin_get("PB.0");
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    rt_pin_write(pin, PIN_LOW);

    status =SPI2_ReadWriteByte(reg);//发送寄存器号
    SPI2_ReadWriteByte(value);      //写入寄存器的值

    rt_pin_write(pin, PIN_HIGH);
   // NRF24L01_CSN=1;                 //禁止SPI传输
    return(status);                 //返回状态值
}

//读取SPI寄存器值
//reg:要读的寄存器
rt_uint8_t NRF24L01_Read_Reg(rt_uint8_t reg)
{
    rt_uint8_t reg_val;
//    NRF24L01_CSN = 0;          //使能SPI传输
    uint8_t pin = rt_pin_get("PB.0");
    rt_pin_mode(pin, PIN_MODE_OUTPUT);
    rt_pin_write(pin, PIN_LOW);

    SPI2_ReadWriteByte(reg);   //发送寄存器号
    reg_val=SPI2_ReadWriteByte(0XFF);//读取寄存器内容

    rt_pin_write(pin, PIN_HIGH);
    //NRF24L01_CSN = 1;          //禁止SPI传输
    return(reg_val);           //返回状态值
}

void SPI2_ReadWriteByte(rt_uint8_t reg)
{
    struct rt_spi_message msg3;
    msg3.send_buf = reg;
    msg3.recv_buf = RT_NULL;
    msg3.length = 1;
    msg3.next = RT_NULL;
    msg3.cs_take = 1;
    msg3.cs_release = 1;
    rt_spi_transfer_message(spi_dev_w25q, &msg3);

}
