static void vnRFtaskRxTx (void *parameter) { u_int32_t t; u_int8_t status; portTickType last_ticks = 0, jam_ticks = 0; if (!PtInitNRF ()) return; for (;;) { // sprintf(msg, "Senden ...\n"); /* check if TX strength changed */ if (nrf_powerlevel_current != nrf_powerlevel_last) { nRFAPI_SetTxPower (nrf_powerlevel_current); nrf_powerlevel_last = nrf_powerlevel_current; } status = nRFAPI_GetFifoStatus (); /* living in a paranoid world ;-) */ if (status & FIFO_TX_FULL) nRFAPI_FlushTX (); /* transmit current lamp value */ // schickt regelmässig ein Packet broadcast if (env.e.mcu_id && ((xTaskGetTickCount () - last_ticks) > jam_ticks)) { memset (&rfpkg, 0, sizeof (rfpkg)); rfpkg.cmd = RF_CMD_SET_VALUES; rfpkg.wmcu_id = env.e.mcu_id; rfpkg.mac = 0xffff; /* send to all MACs */ for (t = 0; t < RF_PAYLOAD_SIZE; t++) rfpkg.payload[t] = (last_lamp_val[t * 2] & 0xf) | (last_lamp_val[(t * 2) + 1] << 4); // random delay to avoid collisions PtInternalTransmit (&rfpkg); // prepare next jam transmission last_ticks = xTaskGetTickCount (); jam_ticks = (RndNumber () % (jam_density_ms * 2)) / portTICK_RATE_MS; } vTaskDelay (5 / portTICK_RATE_MS); /* did I already mention the paranoid world thing? */ nRFAPI_ClearIRQ (MASK_IRQ_FLAGS); } }
unsigned char nRFAPI_Init( unsigned char channel, const unsigned char *mac, unsigned char mac_size, unsigned char features ) { unsigned char i; // init lower layer nRFCMD_Init(); // check validity if( mac_size<3 || mac_size>5 || !nRFAPI_DetectChip() ) return 0; // update mac nRFAPI_SetSizeMac(mac_size); nRFAPI_SetTxMAC(mac,mac_size); // enables pipe nRFAPI_SetRxMAC(mac,mac_size,0); nRFAPI_PipesEnable(ERX_P0); nRFAPI_PipesAck(0); // set payload sizes for(i=0; i<=5; i++) nRFAPI_SetPipeSizeRX(i,2); // set TX retry count nRFAPI_TxRetries(0); // set selected channel nRFAPI_SetChannel(channel); // set Tx power nRFAPI_SetTxPower(3); // flush FIFOs nRFAPI_FlushRX(); nRFAPI_FlushTX(); nRFAPI_SetRxMode(0); if(features != 0) nRFAPI_SetFeatures(features); return 1; }
uint8_t nRFAPI_Init (uint8_t channel, const uint8_t * mac, uint8_t mac_size, uint8_t features) { uint8_t i; // init IO layer of nRF24L01 nRFCMD_Init (); /* wait for nRF to boot */ pmu_sleep_ms(10); // check validity if (mac_size < 3 || mac_size > 5 || !nRFAPI_DetectChip ()) return 0; // update mac nRFAPI_SetSizeMac (mac_size); nRFAPI_SetTxMAC (mac, mac_size); // enables pipe nRFAPI_SetRxMAC (mac, mac_size, 0); nRFAPI_PipesEnable (ERX_P0); nRFAPI_PipesAck (0); // set payload sizes for (i = 0; i <= 5; i++) nRFAPI_SetPipeSizeRX (i, 16); // set TX retry count nRFAPI_TxRetries (0); // set selected channel nRFAPI_SetChannel (channel); // set Tx power nRFAPI_SetTxPower (3); // flush FIFOs nRFAPI_FlushRX (); nRFAPI_FlushTX (); if (features != 0) nRFAPI_SetFeatures (features); return 1; }
static void vnRFtaskRxTx (void *parameter) { u_int32_t crc, t; u_int8_t status; portTickType last_ticks = 0, jam_ticks = 0; if (!PtInitNRF ()) return; for (;;) { /* check if TX strength changed */ if (nrf_powerlevel_current != nrf_powerlevel_last) { nRFAPI_SetTxPower (nrf_powerlevel_current); nrf_powerlevel_last = nrf_powerlevel_current; } status = nRFAPI_GetFifoStatus (); /* living in a paranoid world ;-) */ if (status & FIFO_TX_FULL) nRFAPI_FlushTX (); /* check for received packets */ if ((status & FIFO_RX_FULL) || nRFCMD_WaitRx (0)) { vLedSetGreen (0); do { /* read packet from nRF chip */ nRFCMD_RegReadBuf (RD_RX_PLOAD, (unsigned char *) &rfpkg, sizeof (rfpkg)); /* adjust byte order and decode */ shuffle_tx_byteorder ((unsigned long *) &rfpkg, sizeof (rfpkg) / sizeof (long)); xxtea_decode ((long *) &rfpkg, sizeof (rfpkg) / sizeof (long)); shuffle_tx_byteorder ((unsigned long *) &rfpkg, sizeof (rfpkg) / sizeof (long)); /* verify the crc checksum */ crc = crc32 ((unsigned char *) &rfpkg, sizeof (rfpkg) - sizeof (rfpkg.crc)); if ((crc == PtSwapLong (rfpkg.crc)) && (rfpkg.wmcu_id == env.e.mcu_id) && (rfpkg.cmd & RF_PKG_SENT_BY_DIMMER)) { //debug_printf("dumping received packet:\n"); //hex_dump((unsigned char *) &rfpkg, 0, sizeof(rfpkg)); b_parse_rfrx_pkg (&rfpkg); rf_rec++; } } while ((nRFAPI_GetFifoStatus () & FIFO_RX_EMPTY) == 0); vLedSetGreen (1); } /* transmit current lamp value */ if (env.e.mcu_id && ((xTaskGetTickCount () - last_ticks) > jam_ticks)) { memset (&rfpkg, 0, sizeof (rfpkg)); rfpkg.cmd = RF_CMD_SET_VALUES; rfpkg.wmcu_id = env.e.mcu_id; rfpkg.mac = 0xffff; /* send to all MACs */ for (t = 0; t < RF_PAYLOAD_SIZE; t++) rfpkg.payload[t] = (last_lamp_val[t * 2] & 0xf) | (last_lamp_val[(t * 2) + 1] << 4); // random delay to avoid collisions PtInternalTransmit (&rfpkg); // prepare next jam transmission last_ticks = xTaskGetTickCount (); jam_ticks = (RndNumber () % (jam_density_ms * 2)) / portTICK_RATE_MS; } vTaskDelay (5 / portTICK_RATE_MS); /* did I already mention the paranoid world thing? */ nRFAPI_ClearIRQ (MASK_IRQ_FLAGS); } }