Add config_set config_get to reconfiger MACs and Channel

This commit is contained in:
Stefan `Sec` Zehl 2011-07-16 22:02:33 +02:00
parent 6c0887c332
commit 2ee12e413e
2 changed files with 79 additions and 19 deletions

View File

@ -3,9 +3,7 @@
#include "core/ssp/ssp.h" #include "core/ssp/ssp.h"
#include "basic/xxtea.h" #include "basic/xxtea.h"
#define CHANNEL_BEACON 81
#define DEFAULT_SPEED R_RF_SETUP_DR_2M #define DEFAULT_SPEED R_RF_SETUP_DR_2M
#define MAC_BEACON "\x1\x2\x3\x2\1"
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Transmit a byte via SPI */ /* Transmit a byte via SPI */
@ -242,6 +240,63 @@ void nrf_set_channel(int channel){
nrf_write_reg(R_RF_CH, channel); nrf_write_reg(R_RF_CH, channel);
}; };
void nrf_config_set(nrfconfig config){
nrf_write_reg(R_SETUP_AW,R_SETUP_AW_5);
nrf_set_channel(config->channel);
for(int i=0;i<config->nrmacs;i++){
nrf_write_reg(R_RX_PW_P0+i,config->maclen[i]);
if(i==0){
nrf_write_reg_long(R_RX_ADDR_P0,5,config->mac0);
}else if(i==1){
nrf_write_reg_long(R_RX_ADDR_P1,5,config->mac1);
}else if(i>1){
nrf_write_reg_long(R_RX_ADDR_P0+i,1,config->mac2345+i-2);
};
};
nrf_write_reg_long(R_TX_ADDR,5,config->txmac);
nrf_write_reg(R_EN_RXADDR,(1<<config->nrmacs)-1);
};
void nrf_config_get(nrfconfig config){
// nrf_write_reg(R_SETUP_AW,R_SETUP_AW_5);
config->channel=nrf_read_reg(R_RF_CH);
config->nrmacs=nrf_read_reg(R_EN_RXADDR);
if(config->nrmacs & R_EN_RXADDR_ERX_P5 )
config->nrmacs=6;
else if(config->nrmacs & R_EN_RXADDR_ERX_P4 )
config->nrmacs=5;
else if(config->nrmacs & R_EN_RXADDR_ERX_P3 )
config->nrmacs=4;
else if(config->nrmacs & R_EN_RXADDR_ERX_P2 )
config->nrmacs=3;
else if(config->nrmacs & R_EN_RXADDR_ERX_P1 )
config->nrmacs=2;
else
config->nrmacs=1;
// config->nrmacs=6;
for(int i=0;i<config->nrmacs;i++){
config->maclen[i]=nrf_read_reg(R_RX_PW_P0+i);
if(i==0){
nrf_read_long(R_RX_ADDR_P0,5,config->mac0);
}else if(i==1){
nrf_read_long(R_RX_ADDR_P1,5,config->mac1);
}else if(i>1){
nrf_read_long(R_RX_ADDR_P0+i,1,config->mac2345+i-2);
};
};
nrf_read_long(R_TX_ADDR,5,config->txmac);
};
void nrf_init() { void nrf_init() {
// Enable SPI correctly // Enable SPI correctly
sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge); sspInit(0, sspClockPolarity_Low, sspClockPhase_RisingEdge);
@ -264,25 +319,8 @@ void nrf_init() {
nrf_write_reg(R_EN_AA, 0); // Disable Enhanced ShockBurst; nrf_write_reg(R_EN_AA, 0); // Disable Enhanced ShockBurst;
nrf_set_channel(CHANNEL_BEACON);
// enable receive pipes
nrf_write_reg(R_EN_RXADDR,R_EN_RXADDR_ERX_P0
// |R_EN_RXADDR_ERX_P1
);
nrf_write_reg(R_RX_PW_P0,16);
nrf_write_reg_long(R_RX_ADDR_P0,5,(uint8_t*)MAC_BEACON);
// nrf_write_reg(R_RX_PW_P1,16);
// nrf_write_reg_long(R_RX_ADDR_P1,5,"R0KET");
// OpenBeacon transmit address
nrf_write_reg_long(R_TX_ADDR,5,(uint8_t*)MAC_BEACON);
// Set speed / strength // Set speed / strength
nrf_write_reg(R_RF_SETUP,DEFAULT_SPEED|R_RF_SETUP_RF_PWR_3); nrf_write_reg(R_RF_SETUP,DEFAULT_SPEED|R_RF_SETUP_RF_PWR_3);
// XXX: or write R_CONFIG last?
}; };

View File

@ -98,6 +98,11 @@
#define R_RF_SETUP_DR_2M 0x08 #define R_RF_SETUP_DR_2M 0x08
#define R_RF_SETUP_DR_250K 0x20 #define R_RF_SETUP_DR_250K 0x20
//SETUP_AW register definitions
#define R_SETUP_AW_3 0x01
#define R_SETUP_AW_4 0x02
#define R_SETUP_AW_5 0x03
//STATUS register definitions //STATUS register definitions
#define R_STATUS_RX_DR 0x40 #define R_STATUS_RX_DR 0x40
#define R_STATUS_TX_DS 0x20 #define R_STATUS_TX_DS 0x20
@ -107,6 +112,20 @@
#define R_STATUS_RX_FIFO_EMPTY 0x0E #define R_STATUS_RX_FIFO_EMPTY 0x0E
#define R_STATUS_TX_FULL 0x01 #define R_STATUS_TX_FULL 0x01
/* config structure */
struct NRF_CFG {
uint8_t channel;
uint8_t txmac[5];
uint8_t mac0[5];
uint8_t mac1[5];
uint8_t mac2345[4];
uint8_t nrmacs;
uint8_t maclen[5];
};
typedef struct NRF_CFG * nrfconfig;
/* exported functions */ /* exported functions */
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt); int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt);
@ -129,6 +148,9 @@ void nrf_set_tx_mac(int maclen, uint8_t * mac);
void nrf_disable_pipe(int pipe); void nrf_disable_pipe(int pipe);
void nrf_set_channel(int channel); void nrf_set_channel(int channel);
void nrf_config_set(nrfconfig config);
void nrf_config_get(nrfconfig config);
/* END */ /* END */
#endif /* _NRF24L01P_H */ #endif /* _NRF24L01P_H */