critical uint16_t mac_send(uint8_t packet[], uint16_t length, uint16_t dst_addr) { // check length if (length>PACKET_LENGTH_MAX) { PRINTF("mac_send length error\n"); return 2; } // check state if (txframe.length != 0) { PRINTF("mac_send already sending\n"); // already sending, can't do anything return 1; } // prepare header txframe.length = length + HEADER_LENGTH; txframe.type = TYPE_DATA; txframe.dst_addr[0] = dst_addr>>8; txframe.dst_addr[1] = dst_addr & 0xFF; txframe.src_addr[0] = node_addr>>8; txframe.src_addr[1] = node_addr & 0xFF; // copy packet to the local variable memcpy(txframe.payload, packet, length); // try to send delay_count = 0; tx_delay(); return 0; }
static uint16_t retest(void) { stat_add(STAT_MAC_RETRY_ACK_TIMEOUT); count_as_retry = 1; tx_delay(); return 0; }
static uint16_t tx_try(void) { uint8_t status; if (txframe.length == 0) { PRINTF("tx_try no packet error\n"); return rx_set(); } // if radio not in RX, delay status = cc1101_status() & 0x70; if ( status != 0x10) { PRINTF("try sending radio state error (%x)\n", status); tx_delay(); return rx_set(); } // if there are some weird bytes in TX FIFO, flush everything if (cc1101_status_txbytes()!=0) { PRINTF("mac had to flush\n"); cc1101_cmd_idle(); cc1101_cmd_flush_tx(); cc1101_cmd_flush_rx(); } // try to send cc1101_cmd_tx(); // get chip status status = cc1101_status() & 0x70; // if status is not RX if ( status != 0x10) { // put data in fifo cc1101_fifo_put((uint8_t*)&txframe, txframe.length+1); cc1101_gdo0_register_callback(tx_done); } else { tx_delay(); rx_set(); } return 0; }