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){ 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; };
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){ if(size > MAX_PKT) size=MAX_PKT; nrf_write_reg(R_CONFIG, R_CONFIG_PWR_UP| // Power on R_CONFIG_EN_CRC // CRC on, single byte ); // nrf_write_long(C_W_TX_PAYLOAD,size,pkt); uint16_t crc=crc16(pkt,size-2); pkt[size-2]=(crc >>8) & 0xff; pkt[size-1]=crc & 0xff; if(key !=NULL) xxtea_encode_words((uint32_t*)pkt,size/4,key); CS_LOW(); xmit_spi(C_W_TX_PAYLOAD); sspSend(0,pkt,size); CS_HIGH(); CE_HIGH(); delayms(1); // Send it. (only needs >10ys, i think) CE_LOW(); return nrf_cmd_status(C_NOP); };
void f_init(void){ nrf_init(); struct NRF_CFG config = { .channel= BEACON_CHANNEL, .txmac= BEACON_MAC, .nrmacs=1, .mac0= BEACON_MAC, .maclen ="\x10", }; nrf_config_set(&config); lcdPrintln("Done."); }; void f_status(void){ int dx=0; int dy=8; uint8_t buf[4]; buf[0]=C_R_REGISTER | R_CONFIG; buf[1]=0; buf[2]=0; buf[3]=0; dx=DoString(0,dy,"S:"); dx=DoCharX(dx,dy,buf[0]); dx=DoCharX(dx,dy,buf[1]); dx=DoCharX(dx,dy,buf[2]); dx=DoCharX(dx,dy,buf[3]); dy+=8; nrf_cmd_rw_long(buf,2); dx=DoString(0,dy,"R:"); dx=DoCharX(dx,dy,buf[0]); dx=DoCharX(dx,dy,buf[1]); dx=DoCharX(dx,dy,buf[2]); dx=DoCharX(dx,dy,buf[3]); dy+=8; int status=nrf_cmd_status(C_NOP); dx=DoString(0,dy,"St:"); DoCharX(dx,dy,status);dy+=8; };
char snd_pkt_no_crc(int size, uint8_t * pkt) { if(size > MAX_PKT) size=MAX_PKT; nrf_write_reg(R_CONFIG, R_CONFIG_PWR_UP| // Power on R_CONFIG_EN_CRC // CRC on, single byte ); CS_LOW(); xmit_spi(C_W_TX_PAYLOAD); sspSend(0,pkt,size); CS_HIGH(); CE_HIGH(); delayms(1); // Send it. (only needs >10ys, i think) CE_LOW(); return nrf_cmd_status(C_NOP); }
int nrf_receive_poll (uint8_t * frame) { uint8_t len; uint8_t status = 0; if (mode != MODE_RECEIVE_POLL) return (-1); 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) { 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) return -2; if (len < 1) return -1; /* UART2PutHex (status); UART2PutStr (" "); UART2PutHex (len); UART2PutStr ("\r\n"); */ mLED_2_On (); nrf_read_pkt (32, frame); mLED_2_Off (); return len; }
void nrf_send_frame (uint8_t * frame) { int ret; nrf_write_reg (R_CONFIG, R_CONFIG_PWR_UP | R_CONFIG_EN_CRC); for (ret = 0; ret < 32; ret++) { UART2PutHex (frame[ret]); UART2PutStr (" "); } UART2PutStr ("\n\r"); NRF_CS_LOW (); SPI2_xmit (C_W_TX_PAYLOAD); SPI2_transmit (frame, 32); NRF_CS_HIGH (); NRF_CE_HIGH (); while (1) { ret = nrf_read_reg (R_FIFO_STATUS); if ((ret & R_FIFO_STATUS_TX_EMPTY) == R_FIFO_STATUS_TX_EMPTY) break; } NRF_CE_LOW (); nrf_write_reg (R_STATUS, R_CONFIG_MASK_RX_DR | R_CONFIG_MASK_TX_DS | R_CONFIG_MASK_MAX_RT); ret = nrf_cmd_status (C_NOP); }
void nrf_check_reset(void){ if(nrf_cmd_status(C_NOP) & R_STATUS_MAX_RT){ _nrfresets++; nrf_init(); }; };
// High-Level: int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){ uint8_t len; uint8_t status=0; uint16_t cmpcrc; 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(); for(int i=0;i<maxsize;i++) pkt[i] = 0x00; // Sanity: clear packet buffer #define LOOPY 10 for (;maxtime >= LOOPY;maxtime-=LOOPY){ delayms(LOOPY); status =nrf_cmd_status(C_NOP); 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{ // Get/Check packet... nrf_read_long(C_R_RX_PL_WID,1,&len); if(len>32 || len==0){ continue; return -2; // no packet error }; if(len>maxsize){ continue; return -1; // packet too large }; nrf_read_pkt(len,pkt); if(key != NULL) xxtea_decode_words((uint32_t*)pkt,len/4,key); cmpcrc=crc16(pkt,len-2); if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) { continue; return -3; // CRC failed }; break; }; }; }; CE_LOW(); CS_HIGH(); if(maxtime<LOOPY) return 0; // timeout return len; };