关于 RTOS SDK HSPI驱动NRF24L01的问题

xcm_gz
Posts: 1
Joined: Mon Jan 09, 2017 11:32 pm

关于 RTOS SDK HSPI驱动NRF24L01的问题

Postby xcm_gz » Mon Jan 09, 2017 11:58 pm

你好,我刚接触 ESP8266 TROS SDK,想用ESP8266的HSPI驱动NRF24L01模块,自己模拟SPI时序可以正常驱动NRF24L01,但通信速率只有350K左右。请问如何使用RTOS SDK才能正确驱动NRF24L模块呢?关键在于要如何用RTOS SDK里面的函数来替换我的SPI2_ReadWriteByte函数(全双工)。
RTOS SDK HSPI 代码

void MySPI_Init(void)
{
SpiAttr hSpiAttr;
hSpiAttr.bitOrder = SpiBitOrder_MSBFirst;
hSpiAttr.speed = SpiSpeed_2MHz;
hSpiAttr.mode = SpiMode_Master;

Code: Select all

[code][/code]

hSpiAttr.subMode = SpiSubMode_0;

// Init HSPI GPIO
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);//configure io to spi mode
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode

SPIInit(SpiNum_HSPI, &hSpiAttr);
}

我发现RTOS SPI 里面主要由spi_transaction函数处理接收和发送。
还有以下宏和定义
uint32 spi_transaction(uint8 spi_no, uint8 cmd_bits, uint16 cmd_data, uint32 addr_bits, uint32 addr_data, uint32 dout_bits, uint32 dout_data, uint32 din_bits, uint32 dummy_bits);

#define spi_busy(spi_no) READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR

#define spi_txd(spi_no, bits, data) spi_transaction(spi_no, 0, 0, 0, 0, bits, (uint32) data, 0, 0)
#define spi_tx8(spi_no, data) spi_transaction(spi_no, 0, 0, 0, 0, 8, (uint32) data, 0, 0)
#define spi_tx16(spi_no, data) spi_transaction(spi_no, 0, 0, 0, 0, 16, (uint32) data, 0, 0)
#define spi_tx32(spi_no, data) spi_transaction(spi_no, 0, 0, 0, 0, 32, (uint32) data, 0, 0)

#define spi_rxd(spi_no, bits) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, bits, 0)
#define spi_rx8(spi_no) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, 8, 0)
#define spi_rx16(spi_no) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, 16, 0)
#define spi_rx32(spi_no) spi_transaction(spi_no, 0, 0, 0, 0, 0, 0, 32, 0)


我的模拟SPI代码
const u8 TX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //发送地址
const u8 RX_ADDRESS[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; //发送地址
//MISO->GPIO12
//MOSI->GPIO13
//SCK->GPIO14
//CSN->GPIO15

//IRQ->GPIO2
//CE->GPIO5
u8 NRF24L01_MISO()//输入
{
u8 result;
result = GPIO_INPUT_GET(GPIO_ID_PIN(12));
return result;
}

void NRF24L01_MOSI(u8 level)
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(13), level);
}

void NRF24L01_SCK(u8 level)
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(14), level);
}


void NRF24L01_CSN(u8 level)
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(15), level);
}

void NRF24L01_CE(u8 level)
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(5), level);
}

u8 NRF24L01_IRQ() //输入
{
u8 result;
result = GPIO_INPUT_GET(GPIO_ID_PIN(2));
return result;
}

void NRF24L01_Init(void)
{
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); //输入

PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15);

PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); //输入

NRF24L01_CE(0);
NRF24L01_CSN(1);
}

uint8 SPI2_ReadWriteByte(uint8 u8_writedata)
{
uint8 i;
uint8 u8_readdata = 0x00;

for(i=0;i<8;i++)
{
u8_readdata <<= 1; //读取MISO 8次输入的值,存入u8_readdata。

if(u8_writedata & 0x80) //判断最高位,总是写最高位(输出最高位)
{
NRF24L01_MOSI(1); //MOSI输出1,数据总线准备数据1
}
else
{
NRF24L01_MOSI(0); //MOSI输出0,数据总线准备数据0
}
u8_writedata <<= 1; //左移抛弃已经输出的最高位

NRF24L01_SCK(1); //上升沿来了(SCK从0-->1),数据总线上的数据写入器件
if(NRF24L01_MISO()) //读取最高位,保存至最末尾,通过左移位完成读整个字节
{
u8_readdata |= 0x01;
}
else
{
u8_readdata &= ~0x01;
}

NRF24L01_SCK(0); //下降沿来了(SCK从1-->0),MISO上将产生新的数据,读取存入u8——readdata
}
return u8_readdata;
}

u8 NRF24L01_Write_Reg(u8 reg,u8 value)
{
u8 status;
NRF24L01_CSN(0);
status =SPI2_ReadWriteByte(reg);
SPI2_ReadWriteByte(value);
NRF24L01_CSN(1);
return(status);
}
u8 NRF24L01_Read_Reg(u8 reg)
{
u8 reg_val;
NRF24L01_CSN(0);
SPI2_ReadWriteByte(reg);
reg_val=SPI2_ReadWriteByte(0XFF);
NRF24L01_CSN(1);
return(reg_val);
}


u8 NRF24L01_Read_Buf(u8 reg,u8 *pBuf,u8 len)
{
u8 status,u8_ctr;
NRF24L01_CSN(0);
status=SPI2_ReadWriteByte(reg);
for(u8_ctr=0;u8_ctr<len;u8_ctr++)pBuf[u8_ctr]=SPI2_ReadWriteByte(0XFF);
NRF24L01_CSN(1);
return status;
}
u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 len)
{
u8 status,u8_ctr;
NRF24L01_CSN(0);
status = SPI2_ReadWriteByte(reg);
for(u8_ctr=0; u8_ctr<len; u8_ctr++)SPI2_ReadWriteByte(*pBuf++);
NRF24L01_CSN(1);
return status;
}

u8 NRF24L01_Check(void)
{
u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
u8 i;

NRF24L01_Write_Buf(WRITE_REG_NRF+TX_ADDR,buf,5);
NRF24L01_Read_Buf(TX_ADDR,buf,5);
for(i=0;i<5;i++)if(buf[i]!=0XA5)break;
if(i!=5)return 1;
return 0;
}

u8 NRF24L01_TxPacket(u8 *txbuf)
{
u8 sta;
NRF24L01_CE(0);
NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);
NRF24L01_CE(1);
while(NRF24L01_IRQ()!=0);
sta=NRF24L01_Read_Reg(STATUS);
NRF24L01_Write_Reg(WRITE_REG_NRF+STATUS,sta);
if(sta&MAX_TX)
{
NRF24L01_Write_Reg(FLUSH_TX,0xff);
return MAX_TX;
}
if(sta&TX_OK)
{
return TX_OK;
}
return 0xff;
}
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
u8 sta;
sta=NRF24L01_Read_Reg(STATUS);
NRF24L01_Write_Reg(WRITE_REG_NRF+STATUS,sta);
if(sta&RX_OK)
{
NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);
NRF24L01_Write_Reg(FLUSH_RX,0xff);
return 0;
}
return 1;
}
void NRF24L01_RX_Mode(void)
{
NRF24L01_CE(0);
NRF24L01_Write_Buf(WRITE_REG_NRF+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH);

NRF24L01_Write_Reg(WRITE_REG_NRF+EN_AA,0x01);
NRF24L01_Write_Reg(WRITE_REG_NRF+EN_RXADDR,0x01);
NRF24L01_Write_Reg(WRITE_REG_NRF+RF_CH,40);
NRF24L01_Write_Reg(WRITE_REG_NRF+RX_PW_P0,RX_PLOAD_WIDTH);
NRF24L01_Write_Reg(WRITE_REG_NRF+RF_SETUP,0x0f);
NRF24L01_Write_Reg(WRITE_REG_NRF+CONFIG, 0x0f);
NRF24L01_CE(1);
}
void NRF24L01_TX_Mode(void)
{
NRF24L01_CE(0);
NRF24L01_Write_Buf(WRITE_REG_NRF+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);
NRF24L01_Write_Buf(WRITE_REG_NRF+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH);

NRF24L01_Write_Reg(WRITE_REG_NRF+EN_AA,0x01);
NRF24L01_Write_Reg(WRITE_REG_NRF+EN_RXADDR,0x01);
NRF24L01_Write_Reg(WRITE_REG_NRF+SETUP_RETR,0x1a);
NRF24L01_Write_Reg(WRITE_REG_NRF+RF_CH,40);
NRF24L01_Write_Reg(WRITE_REG_NRF+RF_SETUP,0x0f);
NRF24L01_Write_Reg(WRITE_REG_NRF+CONFIG,0x0e);
NRF24L01_CE(1);
}



#ifndef __24L01_H
#define __24L01_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "c_types.h"
#include "gpio.h"
#include "esp8266/eagle_soc.h"
#include "esp8266/pin_mux_register.h"
#include "esp8266/gpio_register.h"

#include "spi_interface.h"
#include "spi_test.h"
#include "espressif/esp_libc.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//NRF24L01寄存器操作命令
#define READ_REG_NRF 0x00 //读配置寄存器,低5位为寄存器地址
#define WRITE_REG_NRF 0x20 //写配置寄存器,低5位为寄存器地址
#define RD_RX_PLOAD 0x61 //读RX有效数据,1~32字节
#define WR_TX_PLOAD 0xA0 //写TX有效数据,1~32字节
#define FLUSH_TX 0xE1 //清除TX FIFO寄存器.发射模式下用
#define FLUSH_RX 0xE2 //清除RX FIFO寄存器.接收模式下用
#define REUSE_TX_PL 0xE3 //重新使用上一包数据,CE为高,数据包被不断发送.
#define NOP 0xFF //空操作,可以用来读状态寄存器
//SPI(NRF24L01)寄存器地址
#define CONFIG 0x00 //配置寄存器地址;bit0:1接收模式,0发射模式;bit1:电选择;bit2:CRC模式;bit3:CRC使能;
//bit4:中断MAX_RT(达到最大重发次数中断)使能;bit5:中断TX_DS使能;bit6:中断RX_DR使能
#define EN_AA 0x01 //使能自动应答功能 bit0~5,对应通道0~5
#define EN_RXADDR 0x02 //接收地址允许,bit0~5,对应通道0~5
#define SETUP_AW 0x03 //设置地址宽度(所有数据通道):bit1,0:00,3字节;01,4字节;02,5字节;
#define SETUP_RETR 0x04 //建立自动重发;bit3:0,自动重发计数器;bit7:4,自动重发延时 250*x+86us
#define RF_CH 0x05 //RF通道,bit6:0,工作通道频率;
#define RF_SETUP 0x06 //RF寄存器;bit3:传输速率(0:1Mbps,1:2Mbps);bit2:1,发射功率;bit0:低噪声放大器增益
#define STATUS 0x07 //状态寄存器;bit0:TX FIFO满标志;bit3:1,接收数据通道号(最大:6);bit4,达到最多次重发
//bit5:数据发送完成中断;bit6:接收数据中断;
#define MAX_TX 0x10 //达到最大发送次数中断
#define TX_OK 0x20 //TX发送完成中断
#define RX_OK 0x40 //接收到数据中断

#define OBSERVE_TX 0x08 //发送检测寄存器,bit7:4,数据包丢失计数器;bit3:0,重发计数器
#define CD 0x09 //载波检测寄存器,bit0,载波检测;
#define RX_ADDR_P0 0x0A //数据通道0接收地址,最大长度5个字节,低字节在前
#define RX_ADDR_P1 0x0B //数据通道1接收地址,最大长度5个字节,低字节在前
#define RX_ADDR_P2 0x0C //数据通道2接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P3 0x0D //数据通道3接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P4 0x0E //数据通道4接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P5 0x0F //数据通道5接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define TX_ADDR 0x10 //发送地址(低字节在前),ShockBurstTM模式下,RX_ADDR_P0与此地址相等
#define RX_PW_P0 0x11 //接收数据通道0有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P1 0x12 //接收数据通道1有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P2 0x13 //接收数据通道2有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P3 0x14 //接收数据通道3有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P4 0x15 //接收数据通道4有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P5 0x16 //接收数据通道5有效数据宽度(1~32字节),设置为0则非法
#define NRF_FIFO_STATUS 0x17 //FIFO状态寄存器;bit0,RX FIFO寄存器空标志;bit1,RX FIFO满标志;bit2,3,保留
//bit4,TX FIFO空标志;bit5,TX FIFO满标志;bit6,1,循环发送上一数据包.0,不循环;
//24L01发送接收数据宽度定义
#define TX_ADR_WIDTH 5 //5字节的地址宽度
#define RX_ADR_WIDTH 5 //5字节的地址宽度
#define TX_PLOAD_WIDTH 32 //32字节的用户数据宽度
#define RX_PLOAD_WIDTH 32 //32字节的用户数据宽度

void NRF24L01_CSN(uint8 level);
void NRF24L01_CE(uint8 level);
u8 NRF24L01_IRQ();
void NRF24L01_SCK(uint8 level);
void NRF24L01_MOSI(uint8 level);
u8 NRF24L01_MISO();

uint8 SPI2_ReadWriteByte(uint8 u8_writedata);

void NRF24L01_Init(void);//初始化
void NRF24L01_RX_Mode(void);//配置为接收模式
void NRF24L01_TX_Mode(void);//配置为发送模式
u8 NRF24L01_Write_Buf(u8 reg, u8 *pBuf, u8 u8s);//写数据区
u8 NRF24L01_Read_Buf(u8 reg, u8 *pBuf, u8 u8s);//读数据区
u8 NRF24L01_Read_Reg(u8 reg); //读寄存器
u8 NRF24L01_Write_Reg(u8 reg, u8 value);//写寄存器
u8 NRF24L01_Check(void);//检查24L01是否存在
u8 NRF24L01_TxPacket(u8 *txbuf);//发送一个包的数据
u8 NRF24L01_RxPacket(u8 *rxbuf);//接收一个包的数据

void spi_mast_byte_write(uint8 spi_no,uint8 data);
uint32 spi_transaction(uint8 spi_no, uint8 cmd_bits, uint16 cmd_data, uint32 addr_bits, uint32 addr_data, uint32 dout_bits, uint32 dout_data, uint32 din_bits, uint32 dummy_bits);


#ifdef __cplusplus
}
#endif

#endif

achao1104
Posts: 6
Joined: Thu May 26, 2016 12:31 pm

Re: 关于 RTOS SDK HSPI驱动NRF24L01的问题

Postby achao1104 » Wed Jan 11, 2017 12:38 pm

你好,我最近也在研究他们的spi,也碰到不少困难,我的qq:931296,可以加下,互相探讨。

Who is online

Users browsing this forum: No registered users and 2 guests