bool RHReliableDatagram::available() { bool ret = nrf24_rx_fifo_data(); if (!ret) nrf24_rx_mode(); return ret; }
void loop(void) { static uint8_t tx_cnt; /* Consecutive Tx packets counter */ /* * Note: all nrf24 calls are serialised in this function so as * to avoid any concurrency issues. */ if (nrf24_rx_fifo_data()) { uint8_t pkt_len, pkt_buf[32], i; #ifdef SEQN static uint8_t seqn = 0xff; #endif #ifdef FLASH_TOOL_MODE flasher_rx_handle(); #endif nrf24_rx_read(pkt_buf, &pkt_len); #ifdef SEQN if (pkt_buf[0] != seqn) { seqn = pkt_buf[0]; for (i = 1; i < pkt_len; i ++) serial_write1(pkt_buf[i]); } #else for (i = 0; i < pkt_len; i ++) serial_write1(pkt_buf[i]); #endif tx_cnt = 0; } if (tx_fifo.len) { /* .len access should be atomic */ uint8_t pkt_len, pkt_buf[MAX_PKT_SIZE], split; #ifdef SEQN static uint8_t seqn = 0x00; uint8_t count = 128; #define MAX_PLD_SIZE (MAX_PKT_SIZE - 1) pkt_buf[0] = seqn ++; #else uint8_t count = 2; #define MAX_PLD_SIZE MAX_PKT_SIZE #endif #ifdef FLASH_TOOL_MODE flasher_tx_handle(); #endif tx_cnt ++; cli(); pkt_len = min(tx_fifo.len, MAX_PLD_SIZE); sei(); /* HACK */ if (tx_cnt == 2 && MAX_PLD_SIZE > 2 && pkt_len == MAX_PLD_SIZE) pkt_len = MAX_PLD_SIZE - 2; else if (MAX_PLD_SIZE > 2 && tx_cnt == 3) pkt_len = 1; split = min(pkt_len, (uint16_t) (~tx_fifo.start & FIFO_MASK) + 1); #define START (MAX_PKT_SIZE - MAX_PLD_SIZE) memcpy(pkt_buf + START, tx_fifo.data + (tx_fifo.start & FIFO_MASK), split); memcpy(pkt_buf + START + split, tx_fifo.data, pkt_len - split); /* * Or we could just do pkt_buf = tx_fifo.data + ...; * pkt_len = split; */ cli(); tx_fifo.len -= pkt_len; tx_fifo.start += pkt_len; sei(); while (-- count) { /* Don't flood the remote end with the comms */ my_delay(4); nrf24_tx(pkt_buf, pkt_len + START); if (!nrf24_tx_result_wait()) break; } } }