/*********************************************************************
* spi_nrf24l01.c
*********************************************************************/
#include
#include
#include "inc/hw_memmap.h"
#include "driverlib/ssi.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "nrf24l01.h"
#define SSI_BitRate 1000000
// Define a static TX address
uint8_t TX_ADDRESS[TX_ADR_WIDTH] = [0x34,0x43,0x10,0x10,0x01];
uint8_t RX_ADDRESS[RX_ADR_WIDTH] = [0x34,0x43,0x10,0x10,0x01];
uint8_t TX_Buf[TX_PLOAD_WIDTH]=['s','p','i','s'];
uint8_t RX_Buf[TX_PLOAD_WIDTH] = ['0','1','2','3'];
uint8_t sta;
//*********************************************************************
//SPI initialization,??TM4C123GH6PM?SPI??
void SPI_TM4C123_init(void)
[
uint32_t ui32Data;
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0RX);
GPIOPinConfigure(GPIO_PA5_SSI0TX);
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_2);
SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,SSI_MODE_MASTER,SSI_BitRate,8);
SSIEnable(SSI0_BASE);
while(SSIDataGetNonBlocking(SSI0_BASE, &ui32Data))[]
]
//*********************************************************************************
//UART???
void InitConsole(void)
[
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
//UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 115200, 50000000);
]
//**********************************************************************
//
uint8_t SPI_RW(uint8_t value)
[
uint32_t ui32Data;
uint8_t ui8Data;
SSIDataPut(SSI0_BASE,value);
while (SSIBusy(SSI0_BASE))[]
SSIDataGet(SSI0_BASE, &ui32Data);
ui8Data = ui32Data & 0xff;
return(ui8Data);
]
//********************************************************************
//ÓÃSPIÅäÖÃnrf24l01µÄ¼Ä´æÆ÷º¯Êý
uint8_t SPI_Write_register(uint8_t WriteReg,uint8_t Value)//WriteRegҪдµÄ¼Ä´æÆ÷µØÖ·,ValueҪдµÄ¼Ä´æÆ÷Öµ
[
uint8_t status;
//UARTprintf("nSPI_Wreg ->n");
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);
status=SPI_RW(WriteReg);
SPI_RW(Value);
//UARTprintf("'%x' ", status);
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
return(status);
]
//**********************************************************************
uint8_t SPI_Write_Buf(uint8_t WriteReg, uint8_t *pBuf, uint8_t bytes)
[
uint8_t i,status;
//UARTprintf("nSPI_WBuf ->n");
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);
status=SPI_RW(WriteReg);
for(i=0; i
[
SPI_RW(*pBuf++);
]
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
return(status);
]
//*************************************************************************//
uint8_t SPI_Red_Buf(uint8_t WriteReg, uint8_t *pBuf, uint8_t bytes)
[
uint8_t i,status;
//UARTprintf("nSPI_ RBuf ->n");
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);
status=SPI_RW(WriteReg);
for(i=0; i
[
pBuf = SPI_RW(0);
]
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
return(status);
]
//*************************************************************************
uint8_t SPIReadStatus(uint8_t reg)
[
uint8_t status;
UARTprintf("nRead Status ->n");
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);
SPI_RW(reg);
status=SPI_RW(0);
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
UARTprintf("'%x' ", status);
return(status);
]
//***********************************************************************
void nrf24l01init(void)
[
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,0); // ´¦ÓÚ´ý»úģʽһ£¬ËùÒÔ¿ÉÒÔд¼Ä´æÆ÷
SPI_Write_register(FLUSH_TX,0x00);
SPI_Write_register(WRITE_REG+EN_AA,0X00);//ʹÄÜͨµÀ×Ô¶¯Ó¦´ð£¬ ·½±ãµ¥¶Àµ÷ÊÔ
SPI_Write_register(WRITE_REG+EN_RXADDR,0X00);//ʧÄܽÓÊÜͨµÀ0£¬·½±ãµ¥¶Àµ÷ÊÔ·¢ËÍÄ£¿é
SPI_Write_register(WRITE_REG+SETUP_RETR,0x00);//ʧÄÜ×Ô¶¯ÖØ·¢
SPI_Write_register(WRITE_REG + RF_CH, 0x02); // Select RF channel 40
SPI_Write_register(WRITE_REG +RX_PW_P0,RX_PLOAD_WIDTH);
SPI_Write_register(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:1Mbps, LNA:HCURR
SPI_Write_register(WRITE_REG+STATUS,0xff);
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,GPIO_PIN_6);
SysCtlDelay(235);//
]
//*************************************************************************
void nrf24l01TxMode(void)//·¢ËÍģʽ
[
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,0);
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH);
SPI_Write_register(FLUSH_TX,0x00);
SPI_Write_Buf(WR_TX_PLOAD, TX_Buf, TX_PLOAD_WIDTH); // Writes dat
SPI_Write_register(WRITE_REG + CONFIG, 0x0e);
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,GPIO_PIN_6);
SysCtlDelay(185);//10us
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,0);
]
void nrf24l01RxMode(void)//½ÓÊÜģʽ
[
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,0); // ´¦ÓÚ´ý»úģʽһ£¬ËùÒÔ¿ÉÒÔд¼Ä´æÆ÷
SPI_Write_register(FLUSH_TX,0x00);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_register(WRITE_REG+EN_AA,0X01);//ʹÄÜͨµÀ×Ô¶¯Ó¦´ð£¬ ·½±ãµ¥¶Àµ÷ÊÔ
SPI_Write_register(WRITE_REG+EN_RXADDR,0X01);//ʧÄܽÓÊÜͨµÀ0£¬·½±ãµ¥¶Àµ÷ÊÔ·¢ËÍ
SPI_Write_register(WRITE_REG + RF_CH, 0x40); // Select RF channel 40
SPI_Write_register(WRITE_REG +RX_PW_P0,TX_PLOAD_WIDTH);
SPI_Write_register(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:1Mbps, LNA:HCURR
SPI_Write_register(WRITE_REG+STATUS,0xff);
SPI_Write_register(WRITE_REG + CONFIG, 0x0f);
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,GPIO_PIN_6);
SysCtlDelay(185);//10usÒÔÉÏ
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,0);
]
//***************************************************************************//
uint8_t NRFRevDate(uint8_t *RevDate)
[
uint8_t RevFlags=0;
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,GPIO_PIN_6);
SysCtlDelay(185);//10usÒÔÉÏ
sta=SPIReadStatus(R_REGISTER+STATUS);
if(sta&0x40) // ?????????
[
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,0);
SPI_Red_Buf(R_RX_PAYLOAD,RevDate,RX_PLOAD_WIDTH);// ?RXFIFO????
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0);
SPI_RW(FLUSH_RX);//ÓÃÓÚÇå¿ÕFIFO ¹Ø¼ü£¡£¡£¡£¡£¡£¡£¡£¡
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,GPIO_PIN_3);
RevFlags=1; //????????
]
SPI_Write_register(WRITE_REG+STATUS,0xff); //??????RX_DR,TX_DS,MAX_PT????1,???1??????
return(RevFlags); //RevFlags
]
//***************************************************************************
//????????,?????????
void testTXWin(void)
[
uint32_t ui32Loop;
uint8_t status,FIFOStatus;
status=SPIReadStatus(STATUS);
FIFOStatus=SPIReadStatus(FIFO_STATUS);
if(status==0x0e&&FIFOStatus==0x11)//????
[
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1);
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,GPIO_PIN_1);
for(ui32Loop = 0; ui32Loop < 2000000; ui32Loop++)[]
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,0);
for(ui32Loop = 0; ui32Loop < 2000000; ui32Loop++)[]
]
]
//***************************************************************************
int main(void)
[
uint8_t temp;
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
InitConsole();
UARTprintf("SSI ->n");
UARTprintf(" Mode: SPIn");
UARTprintf(" Data: 8-bitnn");
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_6);
GPIOPinTypeGPIOInput(GPIO_PORTA_BASE,GPIO_PIN_7);
GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_6,0);
SPI_TM4C123_init();
//nrf24l01init();
//????????
nrf24l01RxMode();
while(1)
[
//nrf24l01TxMode();
testTXWin();
while(NRFRevDate(RX_Buf))[;]
//UARTprintf("'%c' '%c' '%c' '%c'n",RX_Buf[0],RX_Buf[1],RX_Buf[2],RX_Buf[3]);
SPI_Write_register(WRITE_REG+STATUS,0xff);
]
]
发送程序前面帖子有。
2
|
|
|
|