Fix receive so that it returns as soon as a packet arrives.

This commit is contained in:
Stefan `Sec` Zehl 2011-07-09 14:51:47 +02:00
parent 6ebb783b1e
commit 0abca10597
2 changed files with 39 additions and 3 deletions

View File

@ -108,22 +108,46 @@ void nrf_init() {
}; };
int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){ int nrf_rcv_pkt_time(int maxtime, int maxsize, uint8_t * pkt){
char buf; uint8_t buf;
int len; int len;
uint8_t status=0;
nrf_write_reg(R_CONFIG, nrf_write_reg(R_CONFIG,
R_CONFIG_PRIM_RX| // Receive mode R_CONFIG_PRIM_RX| // Receive mode
R_CONFIG_PWR_UP| // Power on R_CONFIG_PWR_UP| // Power on
R_CONFIG_CRCO // 2-byte CRC R_CONFIG_CRCO // 2-byte CRC
); );
nrf_cmd(C_FLUSH_RX);
nrf_write_reg(R_STATUS,0);
CE_HIGH(); CE_HIGH();
delayms(maxtime); // XXX: check interrupt?
#define LOOPY 10
for (;maxtime >= LOOPY;maxtime-=LOOPY){
delayms(LOOPY);
// status =nrf_cmd_status(C_NOP);
CS_LOW(); status=C_NOP; sspSendReceive(0, &status, 1); CS_HIGH();
if( (status & R_STATUS_RX_DR) == R_STATUS_RX_DR){
if( (status & R_STATUS_RX_P_NO) == R_STATUS_RX_FIFO_EMPTY){
nrf_cmd(C_FLUSH_RX);
delayms(1);
nrf_write_reg(R_STATUS,0);
continue;
}else{
break;
};
};
};
CE_LOW(); CE_LOW();
if(maxtime<LOOPY)
return 0; // timeout
len=1; len=1;
nrf_read_long(C_R_RX_PL_WID,len,&buf); nrf_read_long(C_R_RX_PL_WID,len,&buf);
len=buf; len=buf;
if(len>32 || len==0){ if(len>32 || len==0){
return 0; // no packet return -2; // no packet error
}; };
if(len>maxsize){ if(len>maxsize){
return -1; // packet too large return -1; // packet too large
@ -148,3 +172,4 @@ char nrf_snd_pkt_crc(int size, uint8_t * pkt){
return nrf_cmd_status(C_NOP); return nrf_cmd_status(C_NOP);
}; };

View File

@ -92,6 +92,16 @@
#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
//STATUS register definitions
#define R_STATUS_RX_DR 0x40
#define R_STATUS_TX_DS 0x20
#define R_STATUS_MAX_RT 0x10
#define R_STATUS_RX_P_NO 0x0E
#define R_STATUS_GET_RX_P_NO(x) ((x&R_STATUS_RX_P_NO)>>1)
#define R_STATUS_RX_FIFO_EMPTY 0x0E
#define R_STATUS_TX_FULL 0x01
/* 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);
void nrf_init() ; void nrf_init() ;
@ -106,3 +116,4 @@ void nrf_write_reg_long(const uint8_t reg, int len, uint8_t* data);
/* END */ /* END */
#endif /* _NRF24L01P_H */ #endif /* _NRF24L01P_H */