//Fetch the next act payload //Return the payload length char NRF24L01_RxPacket(char *payload) { int len; int i; //Get the packet length RADIO_EN_CS(); SPI_SendByte(CMD_RX_PL_WID); len = SPI_ReceiveByte(); RADIO_DIS_CS(); if (len>0 && len<33) { //Read the packet from the RX buffer RADIO_EN_CS(); SPI_SendByte(CMD_R_RX_PAYLOAD); for(i=0;i<len;i++) payload[i] = SPI_ReceiveByte(); RADIO_DIS_CS(); } else { len=0; } //Pulse CE //CE_PULSE(); return len; }
//Fetch the next act payload //Return the payload length char radioRxPacket(__xdata char *payload) { int len; int i; //Get the packet length RADIO_EN_CS(); spiRadioSend(CMD_RX_PL_WID); len = spiRadioReceive(); RADIO_DIS_CS(); if (len>0 && len<33) { //Read the packet from the RX buffer RADIO_EN_CS(); spiRadioSend(CMD_R_RX_PAYLOAD); for(i=0;i<len;i++) payload[i] = spiRadioReceive(); RADIO_DIS_CS(); } else { len=0; } //Pulse CE //CE_PULSE(); return len; }
//Set the TX and RX address void radioSetAddress(__xdata char* address) { int i; RADIO_EN_CS(); spiRadioSend(CMD_W_REG | REG_TX_ADDR); for(i=0; i<5; i++) spiRadioSend(address[i]); RADIO_DIS_CS(); RADIO_EN_CS(); spiRadioSend(CMD_W_REG | REG_RX_ADDR_P0); for(i=0; i<5; i++) spiRadioSend(address[i]); RADIO_DIS_CS(); }
//Set the TX and RX address void NRF24L01_SetAddress(char* address) { int i; RADIO_EN_CS(); SPI_SendByte(CMD_W_REG | REG_TX_ADDR); for(i=0; i<5; i++) SPI_SendByte(address[i]); RADIO_DIS_CS(); RADIO_EN_CS(); SPI_SendByte(CMD_W_REG | REG_RX_ADDR_P0); for(i=0; i<5; i++) SPI_SendByte(address[i]); RADIO_DIS_CS(); }
//Nop command, permit to get the status byte char NRF24L01_Nop() { char status; RADIO_EN_CS(); status = SPI_SendByte(CMD_NOP); RADIO_DIS_CS(); return status; }
//Nop command, permit to get the status byte char radioNop() { char status; RADIO_EN_CS(); status = spiRadioSend(CMD_NOP); RADIO_DIS_CS(); return status; }
char NRF24L01_FlushRx() { char status; RADIO_EN_CS(); status = SPI_SendByte(CMD_FLUSH_RX); RADIO_DIS_CS(); return status; }
char radioFlushRx() { char status; RADIO_EN_CS(); status = spiRadioSend(CMD_FLUSH_RX); RADIO_DIS_CS(); return status; }
unsigned char nrfFlushTx() { unsigned char status; RADIO_EN_CS(); status = spiSendByte(CMD_FLUSH_TX); RADIO_DIS_CS(); return status; }
/* Sent the NOP command. Used to get the status byte */ unsigned char nrfNop() { unsigned char status; RADIO_EN_CS(); status = spiSendByte(CMD_NOP); RADIO_DIS_CS(); return status; }
char NRF24L01_WriteReg(char addr, char value) { char status; RADIO_EN_CS(); status = SPI_SendByte(CMD_W_REG | (addr&0x1F)); SPI_SendByte(value); RADIO_DIS_CS(); return value; }
char radioWriteReg(char addr, char value) { char status; RADIO_EN_CS(); status = spiRadioSend(CMD_W_REG | (addr&0x1F)); spiRadioSend(value); RADIO_DIS_CS(); return value; }
unsigned char nrfActivate() { unsigned char status; RADIO_EN_CS(); status = spiSendByte(CMD_ACTIVATE); spiSendByte(ACTIVATE_DATA); RADIO_DIS_CS(); return status; }
char radioReadReg(char addr) { char value; RADIO_EN_CS(); spiRadioSend(CMD_R_REG | (addr&0x1F)); value = spiRadioSend(0xA5); RADIO_DIS_CS(); return value; }
char NRF24L01_ReadReg(char addr) { char value; RADIO_EN_CS(); SPI_SendByte(CMD_R_REG | (addr&0x1F)); value = SPI_ReceiveByte(); RADIO_DIS_CS(); return value; }
// Return the payload length unsigned char nrfRxLength(unsigned int pipe) { unsigned char length; RADIO_EN_CS(); spiSendByte(CMD_RX_PL_WID); length = spiReceiveByte(); RADIO_DIS_CS(); return length; }
//Send a packet as acknowledgment payload void radioAckPacket(char pipe, __xdata char* payload, char len) { int i; RADIO_EN_CS(); /* Send the read command with the address */ spiRadioSend(CMD_W_ACK_PAYLOAD(pipe)); /* Read LEN bytes */ for(i=0; i<len; i++) spiRadioSend(payload[i]); RADIO_DIS_CS(); }
//Send a packed in no-ack mode void NRF24L01_TxPacketNoAck(char *payload, char len) { int i; //Send the packet in the TX buffer RADIO_EN_CS(); SPI_SendByte(CMD_W_PAYLOAD_NO_ACK); for(i=0;i<len;i++) SPI_SendByte(payload[i]); RADIO_DIS_CS(); //Pulse CE CE_PULSE(); return; }
//Send a packed in no-ack mode void radioTxPacketNoAck(__xdata char *payload, char len) { int i; //Send the packet in the TX buffer RADIO_EN_CS(); spiRadioSend(CMD_W_TX_PAYLOAD_NO_ACK); for(i=0;i<len;i++) spiRadioSend(payload[i]); RADIO_DIS_CS(); //Pulse CE CE_PULSE(); return; }
// Read the RX payload unsigned char nrfReadRX(char *buffer, int len) { unsigned char status; int i; RADIO_EN_CS(); /* Send the read command with the address */ status = spiSendByte(CMD_R_RX_PAYLOAD); /* Read LEN bytes */ for(i=0; i<len; i++) buffer[i]=spiReceiveByte(); RADIO_DIS_CS(); return status; }
/* Write len bytes a nRF24L register. 5 Bytes max */ unsigned char nrfWriteReg(unsigned char address, char *buffer, int len) { unsigned char status; int i; RADIO_EN_CS(); /* Send the write command with the address */ status = spiSendByte( CMD_W_REG | (address&0x1F) ); /* Write LEN bytes */ for(i=0; i<len; i++) spiSendByte(buffer[i]); RADIO_DIS_CS(); return status; }
// Write the ack payload of the pipe 0 unsigned char nrfWriteAck(unsigned int pipe, char *buffer, int len) { unsigned char status; int i; ASSERT(pipe<6); RADIO_EN_CS(); /* Send the read command with the address */ status = spiSendByte(CMD_W_ACK_PAYLOAD(pipe)); /* Read LEN bytes */ for(i=0; i<len; i++) spiSendByte(buffer[i]); RADIO_DIS_CS(); return status; }
void rt_hw_nrf24l01_init(void) { SPI_InitTypeDef SPI_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //if (isInit==TRUE) // return; /* Enable the EXTI interrupt router */ // extiInit(); /* Enable SPI and GPIO clocks */ RCC_APB2PeriphClockCmd(RADIO_SPI_CLK | RADIO_GPIO_SPI_CLK | RADIO_GPIO_CS_PERIF | RADIO_GPIO_CE_PERIF | RADIO_GPIO_IRQ_PERIF, ENABLE); /* Disable JTAG */ GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); /* Remap SPI1 */ GPIO_PinRemapConfig(GPIO_Remap_SPI1 , ENABLE); /* Configure SPI pins: SCK, MISO and MOSI */ GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_SPI_SCK | RADIO_GPIO_SPI_MOSI | RADIO_GPIO_SPI_MISO; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RADIO_GPIO_SPI_PORT, &GPIO_InitStructure); /* Configure MISO */ // GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_SPI_MISO; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // GPIO_Init(RADIO_GPIO_SPI_PORT, &GPIO_InitStructure); /* Configure I/O for the Chip select */ GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_CS; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(RADIO_GPIO_CS_PORT, &GPIO_InitStructure); /* Configure the interruption (EXTI Source) */ GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_IRQ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(RADIO_GPIO_IRQ_PORT, &GPIO_InitStructure); GPIO_EXTILineConfig(RADIO_GPIO_IRQ_SRC_PORT, RADIO_GPIO_IRQ_SRC); EXTI_InitStructure.EXTI_Line = RADIO_GPIO_IRQ_LINE; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* disable the chip select */ RADIO_DIS_CS(); /* Configure I/O for the Chip Enable */ GPIO_InitStructure.GPIO_Pin = RADIO_GPIO_CE; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(RADIO_GPIO_CE_PORT, &GPIO_InitStructure); /* disable the chip enable */ RADIO_DIS_CE(); /* SPI configuration */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; 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_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(RADIO_SPI, &SPI_InitStructure); /* Enable the SPI */ SPI_Cmd(RADIO_SPI, ENABLE); //isInit = TRUE; }