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 (;;) { status = nRFAPI_GetFifoStatus (); /* 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)) { // Sende Daten an die Zwischenschicht, um zu sehen ob was empfangen wird memset(packet,0,100); sprintf((portCHAR*)(packet+sizeof(struct packet_header) + 3), "Hallo ich höre!"); u_int8_t length = strlen( (portCHAR*)(packet+sizeof(struct packet_header) + 3) ) + 3 + 1; usb_send_buffer_zero_copy(packet, length+sizeof(struct packet_header), NULL, NULL, portMAX_DELAY); rf_rec++; } } while ((nRFAPI_GetFifoStatus () & FIFO_RX_EMPTY) == 0); vLedSetGreen (1); } vTaskDelay (5 / portTICK_RATE_MS); /* did I already mention the paranoid world thing? */ nRFAPI_ClearIRQ (MASK_IRQ_FLAGS); } }
static void PtInternalTransmit (BRFPacket * pkg) { /* update the sequence */ if (sequence_seed == 0) return; /* turn on redLED for TX indication */ vLedSetRed (1); /* disable receive mode */ nRFCMD_CE (0); /* wait in case a packet is currently received */ vTaskDelay (3 / portTICK_RATE_MS); /* set TX mode */ nRFAPI_SetRxMode (0); if (pkg->mac == 0xffff) rf_sent_broadcast++; else rf_sent_unicast++; pkg->sequence = sequence_seed + (xTaskGetTickCount () / portTICK_RATE_MS); /* update crc */ pkg->crc = PtSwapLong (crc32 ((unsigned char *) pkg, sizeof (*pkg) - sizeof (pkg->crc))); /* encrypt the data */ shuffle_tx_byteorder ((unsigned long *) pkg, sizeof (*pkg) / sizeof (long)); xxtea_encode ((long *) pkg, sizeof (*pkg) / sizeof (long)); shuffle_tx_byteorder ((unsigned long *) pkg, sizeof (*pkg) / sizeof (long)); /* upload data to nRF24L01 */ //hex_dump((unsigned char *) pkg, 0, sizeof(*pkg)); nRFAPI_TX ((unsigned char *) pkg, sizeof (*pkg)); /* transmit data */ nRFCMD_CE (1); /* wait till packet is transmitted */ vTaskDelay (3 / portTICK_RATE_MS); /* switch to RX mode again */ nRFAPI_SetRxMode (1); /* turn off red TX indication LED */ vLedSetRed (0); }
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); } }