void nrfSetDatarate(int datarate)
{
  switch(datarate)
  {
    case RADIO_RATE_250K:
      nrfWrite1Reg(REG_RF_SETUP, VAL_RF_SETUP_250K);
      break;
    case RADIO_RATE_1M:
      nrfWrite1Reg(REG_RF_SETUP, VAL_RF_SETUP_1M);
      break;
    case RADIO_RATE_2M:
      nrfWrite1Reg(REG_RF_SETUP, VAL_RF_SETUP_2M);
      break;
  }  
}
static void eskylinkInitPairing(void)
{
  int i;
  
  //Power the radio, Enable the DR interruption, set the radio in PRX mode with 2bytes CRC
  nrfWrite1Reg(REG_CONFIG, 0x3F);
  vTaskDelay(M2T(2)); //Wait for the chip to be ready
  
   //Set the radio channel, pairing channel is 50
  nrfSetChannel(50);
  //Set the radio data rate
  nrfSetDatarate(RADIO_RATE_1M);

  nrfWrite1Reg(REG_SETUP_AW, VAL_SETUP_AW_3B); // 3 bytes address
  address[0] = address[1] = address[2] = 0;
  nrfWriteReg(REG_RX_ADDR_P0, address, 3);     // Pipe address == 0
  nrfWrite1Reg(REG_EN_RXADDR, 0x01);
  nrfWrite1Reg(REG_FEATURE, 0x00);             // No dynamic size payload
  nrfWrite1Reg(REG_DYNPD, 0x00);
  nrfWrite1Reg(REG_RX_PW_P0, 13);              //13 bytes payload
  nrfWrite1Reg(REG_EN_AA, 0);                  //Disable shockburst

  //Flush RX
  for(i=0;i<3;i++)
    nrfFlushRx();
  //Flush TX
  for(i=0;i<3;i++)
    nrfFlushTx();
}
static int eskylinkFetchData(char * packet, int dataLen)
{
  nrfSetEnable(false);

  //Fetch the data
  nrfReadRX(packet, dataLen);

  //clear the interruptions flags
  nrfWrite1Reg(REG_STATUS, 0x70);
  
  nrfSetEnable(true);
  
  return dataLen;
}
static void eskylinkInitPaired(int channel)
{
  nrfSetChannel(channel);
  nrfSetDatarate(RADIO_RATE_1M);

  nrfWrite1Reg(REG_SETUP_AW, VAL_SETUP_AW_4B); // 4 bytes address
  nrfWriteReg(REG_RX_ADDR_P0, address, 4);     // Pipe address == from pairing packet
  nrfWrite1Reg(REG_EN_RXADDR, 0x01);
  nrfWrite1Reg(REG_FEATURE, 0x00);             // No dynamic size payload
  nrfWrite1Reg(REG_DYNPD, 0x00);
  nrfWrite1Reg(REG_RX_PW_P0, 13);              //13 bytes payload
  nrfWrite1Reg(REG_EN_AA, 0);                  //Disable shockburst
}
void nrfSetChannel(unsigned int channel)
{
  if (channel<126)
    nrfWrite1Reg(REG_RF_CH, channel);
}