New polling-based funk receive functions.

This commit is contained in:
Stefan `Sec` Zehl 2011-07-27 23:20:55 +02:00
parent 196ac680e9
commit 625af67f3f
2 changed files with 86 additions and 0 deletions

View File

@ -91,6 +91,85 @@ void nrf_write_long(const uint8_t cmd, int len, const uint8_t* data){
#define nrf_write_reg_long(reg, len, data) \
nrf_write_long(C_W_REGISTER|(reg), len, data)
// High-Level:
void nrf_rcv_pkt_start(void){
nrf_write_reg(R_CONFIG,
R_CONFIG_PRIM_RX| // Receive mode
R_CONFIG_PWR_UP| // Power on
R_CONFIG_EN_CRC // CRC on, single byte
);
nrf_cmd(C_FLUSH_RX);
nrf_write_reg(R_STATUS,0);
CE_HIGH();
};
int nrf_rcv_pkt_poll(int maxsize, uint8_t * pkt){
uint8_t len;
uint8_t status=0;
for(int i=0;i<maxsize;i++) pkt[i] = 0x00; // Sanity: clear packet buffer
status =nrf_cmd_status(C_NOP);
if((status & R_STATUS_RX_P_NO) == R_STATUS_RX_FIFO_EMPTY){
if( (status & R_STATUS_RX_DR) == R_STATUS_RX_DR){
#ifdef USB_CDC
puts("FIFO empty, but RX?\r\n");
#endif
nrf_write_reg(R_STATUS,R_STATUS_RX_DR);
};
return 0;
};
nrf_read_long(C_R_RX_PL_WID,1,&len);
nrf_write_reg(R_STATUS,R_STATUS_RX_DR);
if(len>32 || len==0){
return -2; // no packet error
};
if(len>maxsize){
return -1; // packet too large
};
nrf_read_pkt(len,pkt);
return len;
};
int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]){
int len;
uint16_t cmpcrc;
len=nrf_rcv_pkt_poll(maxsize,pkt);
if(len <=0)
return len;
if(key==NULL)
return len;
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
xxtea_decode_words((uint32_t*)pkt,len/4,key);
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
return -3; // CRC failed
};
};
return len;
};
void nrf_rcv_pkt_end(void){
CE_LOW();
nrf_cmd(C_FLUSH_RX);
nrf_write_reg(R_STATUS,R_STATUS_RX_DR);
};
// High-Level:
int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){
uint8_t len;
@ -156,6 +235,7 @@ int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t cons
return len;
};
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){
if(size > MAX_PKT)

View File

@ -157,6 +157,12 @@ void nrf_config_get(nrfconfig config);
void nrf_set_strength(unsigned char strength);
// new receive IF
void nrf_rcv_pkt_start(void);
int nrf_rcv_pkt_poll(int maxsize, uint8_t * pkt);
int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]);
void nrf_rcv_pkt_end(void);
/* END */
#endif /* _NRF24L01P_H */