void bindRX(bool timeout) { uint32_t start = millis(); printStrLn("waiting bind..."); init_rfm(1); RF_Mode = Receive; to_rx_mode(); while(!timeout || ((millis() - start) < 500)) { if (RF_Mode == Received) { printStrLn("Got pkt\n"); spiSendAddress(0x7f); // Send the package read command uint8_t rxb = spiReadData(); if (rxb == 'b') { for (uint8_t i = 0; i < sizeof(bind_data); i++) { *(((uint8_t*) &bind_data) + i) = spiReadData(); } if (bind_data.version == BINDING_VERSION) { printStrLn("data good\n"); rxb = 'B'; tx_packet(&rxb, 1); // ACK that we got bound bindWriteEeprom(); Red_LED_ON; //signal we got bound on LED:s Green_LED_ON; //signal we got bound on LED:s return; } } } } }
/* USB (low priority) interrupt */ void usb_lp_isr(void) { u16 mask; u16 status; int ep_id; u16 trans; /* Interrupt mask */ mask = usbdevfs_get_interrupt_mask(USBDEVFS_CORRECT_TRANSFER | USBDEVFS_ERROR | USBDEVFS_RESET | USBDEVFS_SOF); if (!mask) return; /* Interrupt status */ status = usbdevfs_get_interrupt_status(USBDEVFS_CORRECT_TRANSFER | USBDEVFS_ERROR | USBDEVFS_RESET | USBDEVFS_SOF | USBDEVFS_DIR | USBDEVFS_EP_ID); if (!(status & (USBDEVFS_CORRECT_TRANSFER | USBDEVFS_ERROR | USBDEVFS_RESET | USBDEVFS_SOF))) return; /* Start of frame */ if (mask & status & USBDEVFS_SOF) { sof(); /* Clear interrupt. */ usbdevfs_clear_interrupt(USBDEVFS_SOF); } /* Correct transfer */ if (mask & status & USBDEVFS_CORRECT_TRANSFER) { ep_id = status & USBDEVFS_EP_ID; trans = usbdevfs_get_ep_status(ep_id, USBDEVFS_RX | USBDEVFS_TX | USBDEVFS_SETUP); /* Rx (OUT/SETUP transaction) */ if ((status & USBDEVFS_DIR) && (trans & USBDEVFS_RX)) rx_packet(ep_id, trans & USBDEVFS_SETUP); /* Tx (IN transaction) */ if (!(status & USBDEVFS_DIR) && (trans & USBDEVFS_TX)) tx_packet(ep_id); } /* Error */ if (mask & status & USBDEVFS_ERROR) { usb_error++; /* Clear interrupt. */ usbdevfs_clear_interrupt(USBDEVFS_ERROR); } /* USB RESET */ if (mask & status & USBDEVFS_RESET) { usb_reset(); /* Clear interrupt. */ usbdevfs_clear_interrupt(USBDEVFS_RESET); } }
/*------------------------------------------------------------------------ * radioWrite - write a packet from an Radioernet device *------------------------------------------------------------------------ */ devcall radioWrite ( struct dentry *devptr, /* entry in device switch table */ void *buf, /* buffer to hold packet */ uint32 len /* length of buffer */ ) { volatile packet_t *p; struct radio *rptr; uint32 i; char *from, *to; rptr = &radiotab[devptr->dvminor]; /* * Return immediately if a buffer is available */ p = get_free_packet(); if(p) { kprintf("radioWrite preparing 0x%08x\n", p); p->length = len; p->offset = 0; for (from = buf, to = (char *)p->data, i = 0; i < len; i++) *to++ = *from++; //kprintf("radioWrite semcount %d\n", semcount(rptr->osem)); //wait(rptr->osem); tx_packet(p); return len; } else { kprintf("radioWrite: can't get free packet\n"); return SYSERR; } }
// Send a packet, the very first byte of which will be read by the testbench // and used to indicate which test we'll use. void send_ethmac_rxtx_test_init_packet(char test) { char cmd_tx_buffer[40]; cmd_tx_buffer[0] = test; tx_packet(cmd_tx_buffer, 40); // Smallest packet that can be sent (I think) }
void sendECHO() { unsigned char buf[500]; unsigned int sum; pp_printf("> sending ECHO packet\n"); // ------------- Ethernet ------------ // MAC address memcpy(buf+ETH_DEST, hisMAC, 6); memcpy(buf+ETH_SOURCE, myMAC, 6); // ethertype IP buf[ETH_TYPE+0] = 0x08; buf[ETH_TYPE+1] = 0x00; // ------------ IP -------------- buf[IP_VERSION] = 0x45; buf[IP_TOS] = 0; buf[IP_LEN+0] = (hisBodyLen+24) >> 8; buf[IP_LEN+1] = (hisBodyLen+24) & 0xff; buf[IP_ID+0] = 0; buf[IP_ID+1] = 0; buf[IP_FLAGS+0] = 0; buf[IP_FLAGS+1] = 0; buf[IP_TTL] = 63; buf[IP_PROTOCOL] = 1; /* ICMP */ buf[IP_CHECKSUM+0] = 0; buf[IP_CHECKSUM+1] = 0; memcpy(buf+IP_SOURCE, myIP, 4); memcpy(buf+IP_DEST, hisIP, 4); // ------------ ICMP --------- buf[ICMP_TYPE] = 0x0; // echo reply buf[ICMP_CODE] = 0; buf[ICMP_CHECKSUM+0] = 0; buf[ICMP_CHECKSUM+1] = 0; memcpy(buf+ICMP_QUENCH, hisBody, hisBodyLen); if ((hisBodyLen & 1) != 0) buf[ICMP_QUENCH+hisBodyLen] = 0; sum = ipv4_checksum((unsigned short*)(buf+ICMP_TYPE), (hisBodyLen+4+1)/2); buf[ICMP_CHECKSUM+0] = sum >> 8; buf[ICMP_CHECKSUM+1] = sum & 0xff; sum = ipv4_checksum((unsigned short*)(buf+IP_VERSION), 10); buf[IP_CHECKSUM+0] = sum >> 8; buf[IP_CHECKSUM+1] = sum & 0xff; tx_packet(buf, hisBodyLen+ICMP_QUENCH); sawPING = 0; }
void fill_and_tx_call_packet(int size, int response_time) { int i; volatile oeth_regs *regs; regs = (oeth_regs *)(OETH_REG_BASE); volatile oeth_bd *tx_bd; tx_bd = (volatile oeth_bd *)OETH_BD_BASE; tx_bd = (volatile oeth_bd*) &tx_bd[next_tx_buf_num]; // If it's in use - wait while ((tx_bd->len_status & OETH_TX_BD_IRQ)); // Use rand() function to generate data for transmission // Assumption: ethernet buffer descriptors are 4byte aligned char* data_b = (char*) tx_bd->addr; // We will fill with words until there' less than a word to go int words_to_fill = size / sizeof(unsigned int); unsigned int* data_w = (unsigned int*) data_b; // Put first word as size of packet, second as response time data_w[0] = size; data_w[1] = response_time; for(i=2; i<words_to_fill; i++) data_w[i] = rand(); // Point data_b to offset wher word fills ended data_b += (words_to_fill * sizeof(unsigned int)); int leftover_size = size - (words_to_fill * sizeof(unsigned int)); for(i=0; i<leftover_size; i++) { data_b[i] = rand() & 0xff; } tx_packet((void*)0, size); }
int main () { /* Initialise handler vector */ int_init(); /* Install ethernet interrupt handler, it is enabled here too */ int_add(ETH0_IRQ, oeth_interrupt, 0); ethmac_setup(ETH0_PHY, ETH0_BUF); /* Configure MAC, TX/RX BDs and enable RX and TX in MODER */ /* clear tx_done, the tx interrupt handler will set it when it's been transmitted */ while (1) { char buf[120]; memcpy(buf, "Hello world!\n", 12); tx_packet(buf, sizeof(buf)); } exit(0x8000000d); }
int cmp_send_job_list(int sock,struct tx_struct *data) { int i; char temp[1000]; char buf[10000]; strcpy(buf,""); if (cmpstr_min(data->id,"gpvdm_send_job_list")==0) { struct tx_struct packet; tx_struct_init(&packet); tx_set_id(&packet,"gpvdm_job_list"); gen_job_list(buf); tx_set_size(&packet,strlen(buf)); tx_packet(sock,&packet,buf); return 0; } return -1; }
void main(void) { volatile packet_t *p; char c; uint16_t r=30; /* start reception 100us before ack should arrive */ uint16_t end=180; /* 750 us receive window*/ /* trim the reference osc. to 24MHz */ trim_xtal(); uart_init(UART1, 115200); vreg_init(); maca_init(); set_channel(0); /* channel 11 */ // set_power(0x0f); /* 0xf = -1dbm, see 3-22 */ // set_power(0x11); /* 0x11 = 3dbm, see 3-22 */ set_power(0x12); /* 0x12 is the highest, not documented */ /* sets up tx_on, should be a board specific item */ GPIO->FUNC_SEL_44 = 1; GPIO->PAD_DIR_SET_44 = 1; GPIO->FUNC_SEL_45 = 2; GPIO->PAD_DIR_SET_45 = 1; *MACA_RXACKDELAY = r; printf("rx warmup: %d\n\r", (int)(*MACA_WARMUP & 0xfff)); *MACA_RXEND = end; printf("rx end: %d\n\r", (int)(*MACA_RXEND & 0xfff)); set_prm_mode(AUTOACK); print_welcome("rftest-tx"); while(1) { /* call check_maca() periodically --- this works around */ /* a few lockup conditions */ check_maca(); while((p = rx_packet())) { if(p) { printf("RX: "); print_packet(p); free_packet(p); } } if(uart1_can_get()) { c = uart1_getc(); switch(c) { case 'z': r++; if(r > 4095) { r = 0; } *MACA_RXACKDELAY = r; printf("rx ack delay: %d\n\r", r); break; case 'x': if(r == 0) { r = 4095; } else { r--; } *MACA_RXACKDELAY = r; printf("rx ack delay: %d\n\r", r); break; case 'q': end++; if(r > 4095) { r = 0; } *MACA_RXEND = end; printf("rx end: %d\n\r", end); break; case 'w': end--; if(r == 0) { r = 4095; } else { r--; } *MACA_RXEND = end; printf("rx end: %d\n\r", end); break; default: p = get_free_packet(); if(p) { fill_packet(p); printf("autoack-tx --- "); print_packet(p); tx_packet(p); } break; } } } }
void main(void) { volatile packet_t *p; #ifdef CARRIER_SENSE volatile uint32_t i; #endif uint16_t r=30; /* start reception 100us before ack should arrive */ uint16_t end=180; /* 750 us receive window*/ /* trim the reference osc. to 24MHz */ trim_xtal(); uart_init(INC, MOD, SAMP); vreg_init(); maca_init(); ///* Setup the timer */ *TMR_ENBL = 0; /* tmrs reset to enabled */ *TMR0_SCTRL = 0; *TMR0_LOAD = 0; /* reload to zero */ *TMR0_COMP_UP = 18750; /* trigger a reload at the end */ *TMR0_CMPLD1 = 18750; /* compare 1 triggered reload level, 10HZ maybe? */ *TMR0_CNTR = 0; /* reset count register */ *TMR0_CTRL = (COUNT_MODE<<13) | (PRIME_SRC<<9) | (SEC_SRC<<7) | (ONCE<<6) | (LEN<<5) | (DIR<<4) | (CO_INIT<<3) | (OUT_MODE); *TMR_ENBL = 0xf; /* enable all the timers --- why not? */ set_channel(CHANNEL); /* channel 11 */ set_power(0x12); /* 0x12 is the highest, not documented */ /* sets up tx_on, should be a board specific item */ GPIO->FUNC_SEL_44 = 1; GPIO->PAD_DIR_SET_44 = 1; GPIO->FUNC_SEL_45 = 2; GPIO->PAD_DIR_SET_45 = 1; *MACA_RXACKDELAY = r; *MACA_RXEND = end; set_prm_mode(AUTOACK); while(1) { if((*TMR0_SCTRL >> 15) != 0) tick(); /* call check_maca() periodically --- this works around */ /* a few lockup conditions */ check_maca(); while((p = rx_packet())) { if(p) free_packet(p); } p = get_free_packet(); if(p) { fill_packet(p); #ifdef CARRIER_SENSE for(i=0; i<POWER_DELAY; i++) {continue;} while(get_power()>74) {} #endif #ifdef BLOCKING_TX blocking_tx_packet(p); #else tx_packet(p); #endif current_pkts++; #if defined(RANDOM_WAIT_TIME) || defined(FIXED_WAIT) random_wait(); #endif } } }
void blocking_tx_packet(volatile packet_t *p) { transmitting=1; tx_packet(p); while(transmitting) {} }
void bindMode(void) { uint32_t prevsend = millis(); uint8_t tx_buf[sizeof(bind_data) + 1]; bool sendBinds = 1; init_rfm(1); while (serialAvailable()) { serialRead(); // flush serial } Red_LED_OFF; while (1) { if (sendBinds & (millis() - prevsend > 200)) { prevsend = millis(); Green_LED_ON; buzzerOn(BZ_FREQ); tx_buf[0] = 'b'; memcpy(tx_buf + 1, &bind_data, sizeof(bind_data)); tx_packet(tx_buf, sizeof(bind_data) + 1); Green_LED_OFF; buzzerOff(); RF_Mode = Receive; rx_reset(); delay(50); if (RF_Mode == Received) { RF_Mode = Receive; spiSendAddress(0x7f); // Send the package read command if ('B' == spiReadData()) { sendBinds = 0; } } } if (!digitalRead(BTN)) { sendBinds = 1; } while (serialAvailable()) { Red_LED_ON; Green_LED_ON; switch (serialRead()) { case '\n': case '\r': printStrLn("Enter menu..."); handleCLI(); init_rfm(1); break; case '#': scannerMode(); break; default: break; } Red_LED_OFF; Green_LED_OFF; } } }