void nrf_cmd(uint8_t cmd, uint8_t data) { RF1_WriteRegister(cmd, data); #if 0 cbi(PORTB, PIN_nCS); spi_byte(cmd); spi_byte(data); sbi(PORTB, PIN_nCS); //Deselect chip #endif }
/*! * \brief Radio power-on initialization. * \return Error code, ERR_OK if everything is ok. */ uint8_t RADIO_PowerUp(void) { WAIT1_WaitOSms(100); /* the transceiver needs 100 ms power up time */ RF1_Init(); /* set CE and CSN to initialization value */ RF1_WriteRegister(RF1_RF_SETUP, RF1_RF_SETUP_RF_PWR_0|RF1_RF_SETUP_RF_DR_250); #if NRF24_DYNAMIC_PAYLOAD /* enable dynamic payload */ RF1_WriteFeature(RF1_FEATURE_EN_DPL|RF1_FEATURE_EN_ACK_PAY|RF1_FEATURE_EN_DYN_PAY); /* set EN_DPL for dynamic payload */ RF1_EnableDynanicPayloadLength(RF1_DYNPD_DPL_P0); /* set DYNPD register for dynamic payload for pipe0 */ #else RF1_SetStaticPipePayload(0, RPHY_PAYLOAD_SIZE); /* static number of payload bytes we want to send and receive */ #endif (void)RADIO_SetChannel(RADIO_CHANNEL_DEFAULT); /* Set RADDR and TADDR as the transmit address since we also enable auto acknowledgment */ RF1_WriteRegisterData(RF1_RX_ADDR_P0, (uint8_t*)TADDR, sizeof(TADDR)); RF1_WriteRegisterData(RF1_TX_ADDR, (uint8_t*)TADDR, sizeof(TADDR)); /* Enable RX_ADDR_P0 address matching */ RF1_WriteRegister(RF1_EN_RXADDR, RF1_EN_RXADDR_ERX_P0); /* enable data pipe 0 */ /* clear interrupt flags */ RF1_ResetStatusIRQ(RF1_STATUS_RX_DR|RF1_STATUS_TX_DS|RF1_STATUS_MAX_RT); /* rx/tx mode */ RF1_EnableAutoAck(RF1_EN_AA_ENAA_P0); /* enable auto acknowledge on pipe 0. RX_ADDR_P0 needs to be equal to TX_ADDR! */ RF1_WriteRegister(RF1_SETUP_RETR, RF1_SETUP_RETR_ARD_750|RF1_SETUP_RETR_ARC_15); /* Important: need 750 us delay between every retry */ RX_POWERUP(); /* Power up in receiving mode */ RF1_StartRxTx(); /* Listening for packets */ RADIO_AppStatus = RADIO_INITIAL_STATE; /* init Rx descriptor */ radioRx.phyData = &radioRxBuf[0]; radioRx.phySize = sizeof(radioRxBuf); radioRx.rxtx = &RPHY_BUF_SIZE(radioRx.phyData); /* we transmit the size too */ return ERR_OK; }
void APP_Run(void) { static const uint8_t chRf[] = {2, 26,80}; static const uint8_t chLe[] = {37,38,39}; uint8_t i, L, ch = 0; uint8_t buf[32]; nrf_cmd(0x20, 0x12); //on, no crc, int on RX/TX done nrf_cmd(0x21, 0x00); //no auto-acknowledge nrf_cmd(0x22, 0x00); //no RX nrf_cmd(0x23, 0x02); //5-byte address nrf_cmd(0x24, 0x00); //no auto-retransmit nrf_cmd(0x26, 0x06); //1MBps at 0dBm nrf_cmd(0x27, 0x3E); //clear various flags nrf_cmd(0x3C, 0x00); //no dynamic payloads nrf_cmd(0x3D, 0x00); //no features nrf_cmd(0x31, 32); //always RX 32 bytes nrf_cmd(0x22, 0x01); //RX on pipe 0 buf[0] = 0x30; //set addresses buf[1] = swapbits(0x8E); buf[2] = swapbits(0x89); buf[3] = swapbits(0xBE); buf[4] = swapbits(0xD6); #if 0 nrf_manybytes(buf, 5); #else RF1_WriteRegisterData(RF1_TX_ADDR, &buf[1], 4); #endif buf[0] = 0x2A; #if 0 nrf_manybytes(buf, 5); #else RF1_WriteRegisterData(RF1_RX_ADDR_P0, &buf[1], 4); #endif while(1) { L = 0; buf[L++] = 0x40; //PDU type, given address is random buf[L++] = 11; //17 bytes of payload buf[L++] = MY_MAC_0; buf[L++] = MY_MAC_1; buf[L++] = MY_MAC_2; buf[L++] = MY_MAC_3; buf[L++] = MY_MAC_4; buf[L++] = MY_MAC_5; buf[L++] = 2; //flags (LE-only, limited discovery mode) buf[L++] = 0x01; buf[L++] = 0x05; buf[L++] = 7; buf[L++] = 0x08; buf[L++] = 'n'; buf[L++] = 'R'; buf[L++] = 'F'; buf[L++] = ' '; buf[L++] = 'L'; buf[L++] = 'E'; buf[L++] = 0x55; //CRC start value: 0x555555 buf[L++] = 0x55; buf[L++] = 0x55; if(++ch == sizeof(chRf)) { /* channel hopping */ ch = 0; } //nrf_cmd(0x25, chRf[ch]); (void)RF1_SetChannel(chRf[ch]); //nrf_cmd(0x27, 0x6E); //clear flags RF1_WriteRegister(RF1_STATUS, RF1_STATUS_RX_DR|RF1_STATUS_TX_DS|RF1_STATUS_RX_P_NO_RX_FIFO_EMPTY); /* clear flags */ btLePacketEncode(buf, L, chLe[ch]); //nrf_simplebyte(0xE2); //Clear RX Fifo RF1_Write(RF1_FLUSH_RX); /* flush old data */ //nrf_simplebyte(0xE1); //Clear TX Fifo //RF1_Write(RF1_FLUSH_TX); /* flush old data */ /* done in RF1_TxPayload() */ RF1_TxPayload(buf, L); #if 0 cbi(PORTB, PIN_nCS); spi_byte(0xA0); for(i = 0 ; i < L ; i++) { spi_byte(buf[i]); } sbi(PORTB, PIN_nCS); nrf_cmd(0x20, 0x12); //tx on sbi(PORTB, PIN_CE); //do tx delay_ms(10); cbi(PORTB, PIN_CE); (in preparation of switching to RX quickly); #endif LED1_Neg(); WAIT1_Waitms(10); } /* for */ }