/*---------------------------------------------------------------------------*/ PROCESS_THREAD(unicast_sender_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t global_ipaddr; PROCESS_BEGIN(); random_rand(); rpl_log_start(); if(node_id ==0) { NETSTACK_RDC.off(0); uint16_t mymac = linkaddr_node_addr.u8[7] << 8 | linkaddr_node_addr.u8[6]; printf("Node id unset, my mac is 0x%04x\n", mymac); PROCESS_EXIT(); } cc2420_set_txpower(RF_POWER); cc2420_set_cca_threshold(RSSI_THR); printf("App: %u starting\n", node_id); deployment_init(&global_ipaddr); //rpl_setup(node_id == ROOT_ID, node_id); simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); if(node_id == ROOT_ID) { uip_ipaddr_t my_ipaddr; set_ipaddr_from_id(&my_ipaddr, node_id); //NETSTACK_RDC.off(1); } else { etimer_set(&periodic_timer,1 * 30 * CLOCK_SECOND); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { etimer_set(&send_timer, random_rand() % (SEND_INTERVAL)); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); int rank = default_instance != NULL ? default_instance->current_dag->rank : 0xffff; if(rank != 0xffff){ app_send_to(ROOT_ID); } else{ printf("App: not in DODAG\n"); } PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_txpower_process, ev, data) { struct { uint16_t len; uint16_t txpower; } msg; const char *newptr; PROCESS_BEGIN(); msg.txpower = shell_strtolong(data, &newptr); /* If no transmission power was given on the command line, we print out the current txpower. */ if(newptr == data) { msg.txpower = cc2420_get_txpower(); } else { cc2420_set_txpower(msg.txpower); } msg.len = 1; shell_output(&txpower_command, &msg, sizeof(msg), "", 0); PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { PROCESS_BEGIN(); PROCESS_PAUSE(); cc2420_set_txpower(3); //cc2420 txpower can be set to {3,7,11,15,19,23,27,31} from lowest to highest set_global_address(); PRINTF("UDP client process started\n"); print_local_addresses(); /* new connection with remote host */ client_conn = udp_new(NULL, UIP_HTONS(UDP_SERVER_PORT), NULL); udp_bind(client_conn, UIP_HTONS(UDP_CLIENT_PORT)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); while(1) { PROCESS_YIELD(); if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
PROCESS_THREAD(interferer_example, ev, data) { PROCESS_EXITHANDLER() PROCESS_BEGIN(); // Initial configurations on CC2420: channel and tx power watchdog_stop(); cc2420_set_txpower(CC2420_TXPOWER_MAX); cc2420_set_channel(INTERFERED_CHANNEL); printf("HandyMote: interfering continuously with random power\n"); // Interfering continuously with random power CC2420_SPI_ENABLE(); set_jammer(CARRIER_TYPE); int randelay, randpower; while(1){ randpower = random_value(MIN_POWER,MAX_POWER); randelay = random_value(MIN_TIME,MAX_TIME); power_jammer(randpower); clock_delay(randelay); } CC2420_SPI_DISABLE(); PROCESS_WAIT_EVENT(); PROCESS_END(); }
// Initialize RDC layer static void init(void) { //get node id node_id_restore(); //set slot number sf_tdma_set_slot_num(node_id); //reset rime & radio address sf_tdma_set_mac_addr(); //reset packet number seq_num = 0; //check the if the number of time slot is large enough uint32_t min_segment_len = TS_period*total_slot_num + BS_period; if (min_segment_len > segment_period) { printf("min_segment_len > segment_period\n"); assert(1); } printf("Init RDC layer,packet size\n"); #ifdef SF_MOTE_TYPE_SENSOR incorrect_rx_counter = 0; #endif cc2420_set_channel(RF_CHANNEL_CONST); cc2420_set_txpower(CC2420_TXPOWER_MAX); on(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer periodic; static struct ctimer backoff_timer; #if WITH_COMPOWER static int print = 0; #endif PROCESS_BEGIN(); cc2420_set_txpower(11); PROCESS_PAUSE(); set_global_address(); PRINTF("UDP client process started\n"); print_local_addresses(); /* new connection with remote host */ client_conn = udp_new(NULL, UIP_HTONS(UDP_SERVER_PORT), NULL); if(client_conn == NULL) { PRINTF("No UDP connection available, exiting the process!\n"); PROCESS_EXIT(); } udp_bind(client_conn, UIP_HTONS(UDP_CLIENT_PORT)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); #if WITH_COMPOWER powertrace_sniff(POWERTRACE_ON); #endif etimer_set(&periodic, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(ev == tcpip_event) { tcpip_handler(); } if(etimer_expired(&periodic)) { etimer_reset(&periodic); ctimer_set(&backoff_timer, SEND_TIME, send_packet, NULL); #if WITH_COMPOWER if (print == 0) { powertrace_print("#P"); } if (++print == 3) { print = 0; } #endif } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ static void do_sending(void) { int i; cc2420_set_channel(11); cc2420_set_txpower(1); for(i = 0;i < send_amount; i++) { packetbuf_copyfrom(send_string, send_length); mac->on(); abc_send(&abc); mac->off(0); } cc2420_set_txpower(31); cc2420_set_channel(RF_CHANNEL); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(unicast_sender_process, ev, data) { static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t global_ipaddr; PROCESS_BEGIN(); if(node_id == 0) { NETSTACK_RDC.off(0); printf("Node id unset, my mac is "); uip_debug_lladdr_print(&rimeaddr_node_addr); printf("\n"); PROCESS_EXIT(); } cc2420_set_txpower(RF_POWER); cc2420_set_cca_threshold(RSSI_THR); orpl_log_start(); printf("App: %u starting\n", node_id); deployment_init(&global_ipaddr); #if WITH_ORPL orpl_init(node_id == ROOT_ID, 0); #endif /* WITH_ORPL */ simple_udp_register(&unicast_connection, UDP_PORT, NULL, UDP_PORT, receiver); if(node_id == ROOT_ID) { NETSTACK_RDC.off(1); etimer_set(&periodic_timer, 2 * 60 * CLOCK_SECOND); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { etimer_set(&send_timer, random_rand() % (SEND_INTERVAL)); PROCESS_WAIT_UNTIL(etimer_expired(&send_timer)); if(check_reachable_count()) { uip_ipaddr_t dest_ipaddr; static uint16_t target_id; static uint16_t i; do { target_id = get_node_id_from_index((random_rand()>>8)%get_n_nodes()); set_ipaddr_from_id(&dest_ipaddr, target_id); } while (target_id == ROOT_ID || !orpl_routing_set_contains(&dest_ipaddr)); app_send_to(target_id); } PROCESS_WAIT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); } }
PROCESS_THREAD(interferer_example, ev, data) { PROCESS_EXITHANDLER() PROCESS_BEGIN(); static struct etimer et; //powertrace_start(CLOCK_SECOND * 2); // Initial configurations on CC2420: channel and tx power watchdog_stop(); cc2420_set_txpower(power); cc2420_set_channel(26); //printf("interfering with periodic interference\n"); // Continuous Interference CC2420_SPI_ENABLE(); //SPI_SET_UNMODULATED(0x1800,0x0100,0x0508,0x0004); SPI_SET_MODULATED(0x050C); //powertrace_start(CLOCK_SECOND*2); while(1){ for(power=3; power<32; power+=4){ SPI_SET_TXPOWER((0xa0ff & 0xffe0) | (power & 0x1f)); etimer_set(&et, CLOCK_SECOND/1000); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); //printf ("transmit power = %d\n",power); } for(power=27; power>3; power-=4){ SPI_SET_TXPOWER((0xa0ff & 0xffe0) | (power & 0x1f)); etimer_set(&et, CLOCK_SECOND/1000); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); //printf ("transmit power = %d\n",power); } for(power=3; power<32; power+=2){ SPI_SET_TXPOWER((0xa0ff & 0xffe0) | (0 & 0x1f)); etimer_set(&et, CLOCK_SECOND/1000); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); //printf ("transmit power = %d\n",power); } } CC2420_SPI_DISABLE(); void powertrace_stop(void); PROCESS_WAIT_EVENT(); PROCESS_END(); }
/* --------------------------------- */ PROCESS_THREAD(radio_power_process, ev, data) { PROCESS_BEGIN(); uint8_t txp = (uint8_t) atoi((char *)data); static char shell_out[4]; if(txp > CC2420_TXPOWER_MAX) { txp = CC2420_TXPOWER_MAX; } cc2420_set_txpower(txp); snprintf(shell_out, sizeof(shell_out), "%d", txp); shell_output_str(&radio_power_command, "TX power: ", shell_out); PROCESS_END(); }
PROCESS_THREAD(simple_process, ev, data) { PROCESS_BEGIN(); cc2420_set_txpower(12); init_router(&message_received); static struct ctimer ct; ctimer_set(&ct, CLOCK_SECOND * 60 * 5, send_message_after_5_minutes, NULL); process_start(&neighbor_discovery_process, NULL); printf("K = %d, MAX_NODES = %d, MAX_EDGES = %d, LINKADDR = %d.%d\n", (int) K, (int) MAX_NODES, (int) MAX_EDGES, linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]); printf("NODE_MEM = %dB, EDGE_MEM = %dB\n", (int)(sizeof(p_node_t) * MAX_NODES), (int)(sizeof(p_edge_t) * MAX_EDGES)); printf("PACKETBUF_SIZE = %d, TXPOWER = %d, INTERVAL = %d - %d\n", PACKETBUF_SIZE, cc2420_get_txpower(), DISCOVERY_INTERVAL_MIN, DISCOVERY_INTERVAL_MAX); PROCESS_END(); }
PROCESS_THREAD(sensys_tx, ev, data) { PROCESS_EXITHANDLER() PROCESS_BEGIN(); // Initial configurations on CC2420 and resetting the timer leds_off(LEDS_ALL); cc2420_set_txpower(POWER); cc2420_set_channel(CHANNEL_SENDER); unicast_open(&uc, RIME_SENDER, &unicast_callbacks); ctimer_stop(&timer1); // Ready to send... leds_on(LEDS_BLUE); while(1) { PROCESS_WAIT_EVENT(); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(web_sense_process, ev, data) { static struct etimer timer; PROCESS_BEGIN(); cc2420_set_txpower(31); sensors_pos = 0; process_start(&webserver_nogui_process, NULL); etimer_set(&timer, CLOCK_SECOND * 2); SENSORS_ACTIVATE(battery_sensor); SENSORS_ACTIVATE(temperature_sensor); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer)); etimer_reset(&timer); battery1[sensors_pos] = get_mybatt()*1000; temperature[sensors_pos] = get_mytemp(); sensors_pos = (sensors_pos + 1) % HISTORY; } PROCESS_END(); }
void radio_set_txpower(int power) { cc2420_set_txpower(power); }
PROCESS_THREAD(markov_process, ev, data) { int new_state = b[0]; int i,j; PROCESS_BEGIN(); //etimer_set(&et, CLOCK_SECOND); //PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); // Initial configurations on CC2420 cc2420_set_txpower(MAX_POWER); cc2420_set_channel(INTERFERED_CHANNEL); watchdog_stop(); printf("RAND_MAX %d INTMAX %d\n", RANDOM_RAND_MAX, INT_MAX); random_init(19); /* XXX should use formula */ W[0]= 31; W[1]= 62; W[2]= 124; W[3]= 248; W[4]= 496; W[5]= 992; W[6]= 1023; W[7]= 1023; for(i=0; i<NRSTATES; i++) { b[i] = bSTATE_MIN+i; B[i] = BSTATE_MIN+i; bctr[i] = 0; Bctr[i] = 0; ALFA[i] = 2.0/((float)W[i]); BETA[i] = 2.0/((float)(W[i]-1)); } printf("a %d, b %d, c %d d %d BETA1 %d\n", 100*((1-P)*ALFA[0]), 100*((1-P)*(1-ALFA[0])), 100*(P*ALFA[1]), 100*(P*(1-ALFA[1])), 100*BETA[1]); /* this is the first "dry" run where we compute the state for the real run */ new_state = b[0]; for(i=0; i<SAVESTATES; i++) { sstate [i] = 0; visits[i] = 0; } while (cs<SAVESTATES) { new_state = compute_state(new_state); } new_state = b[0]; /* now we start the real run */ cur_state = 0; cur_visits = 0; CC2420_SPI_ENABLE(); #if 0 while (cs<) { ctr++; if (ctr % 100 == 0) { int ctr_sum = 0; printf("time %f\n", time); float b = 0; for(i=bSTATE_MIN; i<bSTATE_MIN+NRSTATES; i++) { printf("counter[%d]: %d\n", i, bctr[i]); ctr_sum += bctr[i]; b += timeb[i]; } for(i= 0; i<NRSTATES; i++) { printf("counter[%d]: %d\n", i, Bctr[i]); ctr_sum += Bctr[i]; } b = b / time; printf("counter %d percent in b: %f\n", ctr_sum, b); } if (ctr > MAX) { int ctr_sum = 0; float b = 0; for(i=bSTATE_MIN; i<bSTATE_MIN+NRSTATES; i++) { printf("counter[%d]: %d\n", i, bctr[i]); ctr_sum += bctr[i]; } for(i= 0; i<NRSTATES; i++) { printf("counter[%d]: %d\n", i, Bctr[i]); ctr_sum += Bctr[i]; } printf("counter %d percent in b: %f\n", ctr_sum, b); printf("ctrs: b0 %d B0 %d b1 %d B1 %d b5 %d B5 %d\n", bctr[0], Bctr[0], bctr[1], Bctr[1], bctr[5], Bctr[5]); exit(1); } update_state(); } #endif printf("after modelling\n"); j=0; for(i=0; i<SAVESTATES; i++) { //printf("state %d visits% d\n", sstate[i], visits[i]); j += visits[i]; } printf("sum visits %d\n", j); /* correct very first state */ if (visits[0] == 0) visits[0] = 1; ctr = 0; printf("--------- real run -------------\n"); update_state(); CC2420_SPI_DISABLE(); PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer periodic; static struct ctimer backoff_timer; //static struct etimer wait; //static int tx[] = { 31, 27, 23, 19, 15, 11, 7, 3}; #if WITH_COMPOWER static int print = 0; #endif PROCESS_BEGIN(); powertrace_start(CLOCK_SECOND * 1); //elnaz cc2420_set_txpower(23); printf(" Probe v\n Tx=%d\n", cc2420_get_txpower()); PROCESS_PAUSE(); set_global_address(); PRINTF("UDP client process started\n"); print_local_addresses(); /* new connection with remote host */ client_conn = udp_new(NULL, UIP_HTONS(UDP_SERVER_PORT), NULL); if(client_conn == NULL) { PRINTF("No UDP connection available, exiting the process!\n"); PROCESS_EXIT(); } udp_bind(client_conn, UIP_HTONS(UDP_CLIENT_PORT)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); #if WITH_COMPOWER powertrace_sniff(POWERTRACE_ON); #endif // etimer_set(&temp, 60*CLOCK_SECOND); // PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); etimer_set(&periodic, SEND_INTERVAL); while(1)//seq<150) { PROCESS_YIELD(); if(ev == tcpip_event) { tcpip_handler(); } if(etimer_expired(&periodic)) { etimer_reset(&periodic); ctimer_set(&backoff_timer, SEND_TIME, send_packet, NULL); } //end etimer_expired } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer periodic, start_sending_timer; static struct ctimer backoff_timer; int start_sending_flag = 0; PROCESS_BEGIN(); PROCESS_PAUSE(); set_global_address(); PRINTF("UDP client process started\n"); print_local_addresses(); cc2420_set_txpower(3); NETSTACK_MAC.off(1); /* new connection with remote host */ client_conn = udp_new(NULL, UIP_HTONS(UDP_SERVER_PORT), NULL); if(client_conn == NULL) { PRINTF("No UDP connection available, exiting the process!\n"); PROCESS_EXIT(); } udp_bind(client_conn, UIP_HTONS(UDP_CLIENT_PORT)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); /* new connection with MNs */ mn_conn = udp_new(NULL, UIP_HTONS(UDP_MN_PORT2), NULL); if(mn_conn == NULL) { PRINTF("No MN UDP connection available, exiting the process!\n"); PROCESS_EXIT(); } udp_bind(mn_conn, UIP_HTONS(UDP_MN_PORT1)); PRINTF("Created a connection with the client "); PRINT6ADDR(&mn_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(mn_conn->lport), UIP_HTONS(mn_conn->rport)); etimer_set(&start_sending_timer, START_SENDING_TIME); /*rpl_set_mode(RPL_MODE_LEAF);*/ etimer_set(&periodic, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(ev == tcpip_event) { tcpip_handler(); } if(etimer_expired(&start_sending_timer)){ start_sending_flag = 1; } if(etimer_expired(&periodic) && start_sending_flag == 1) { etimer_reset(&periodic); if(mobility_flag == 0) { ctimer_set(&backoff_timer, SEND_TIME, send_packet, NULL); } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ int cc2420_init(void) { uint16_t reg; { int s = splhigh(); cc2420_arch_init(); /* Initalize ports and SPI. */ CC2420_DISABLE_FIFOP_INT(); CC2420_FIFOP_INT_INIT(); splx(s); } /* Turn on voltage regulator and reset. */ SET_VREG_ACTIVE(); clock_delay(250); SET_RESET_ACTIVE(); clock_delay(127); SET_RESET_INACTIVE(); clock_delay(125); /* Turn on the crystal oscillator. */ strobe(CC2420_SXOSCON); /* Turn on/off automatic packet acknowledgment and address decoding. */ reg = getreg(CC2420_MDMCTRL0); #if CC2420_CONF_AUTOACK reg |= AUTOACK | ADR_DECODE; #else reg &= ~(AUTOACK | ADR_DECODE); #endif /* CC2420_CONF_AUTOACK */ setreg(CC2420_MDMCTRL0, reg); /* Set transmission turnaround time to the lower setting (8 symbols = 0.128 ms) instead of the default (12 symbols = 0.192 ms). */ /* reg = getreg(CC2420_TXCTRL); reg &= ~(1 << 13); setreg(CC2420_TXCTRL, reg);*/ /* Change default values as recomended in the data sheet, */ /* correlation threshold = 20, RX bandpass filter = 1.3uA. */ setreg(CC2420_MDMCTRL1, CORR_THR(20)); reg = getreg(CC2420_RXCTRL1); reg |= RXBPF_LOCUR; setreg(CC2420_RXCTRL1, reg); /* Set the FIFOP threshold to maximum. */ setreg(CC2420_IOCFG0, FIFOP_THR(127)); /* Turn off "Security enable" (page 32). */ reg = getreg(CC2420_SECCTRL0); reg &= ~RXFIFO_PROTECTION; setreg(CC2420_SECCTRL0, reg); cc2420_set_pan_addr(0xffff, 0x0000, NULL); cc2420_set_channel(26); cc2420_set_txpower(RF_POWER); cc2420_set_cca_threshold(-32 + RSSI_THR); flushrx(); process_start(&cc2420_process, NULL); return 1; }
void set_tx() { static float etx; static int i=4; static int power = { 3,7, 11, 15, 19, 23, 27, 31}; static float power_mw = {0.003, 0.03, 0.1, 0.2, 0.3, 0.5, 0.8, 1}; static float Es={100, 100, 100, 100, 100, 100, 100, 100 }; static char increase_tx=0; static char decrease_tx=0; static char minEs; static char minEs_index=4; static int tx_test_time; rpl_instance_t *instance=&instance_table[0]; rpl_parent_t *pref; static struct etimer tx_interval_timer; set_tx_flag=1; tx_test_time = (PROBE_NUM_THRESHOLD * PROBE_INTERVAL)+10; while(i>=0 && i<8) { cc2420_set_txpower(power[i]); rpl_pt_parents(1); etimer_set(&tx_interval_timer, tx_test_time * CLOCK_SECOND); WAIT_EVENT_UNTIL(etimer_expired(&tx_interval_timer)); pref = (instance->current_dag->preferred_parent); etx = (pref->retransmission_num) / (p->msg_num); if(increase_tx==decrease_tx) { ///Es[i] = power_mw[i]*ETX; if(etx<1.2) {decrease_tx=1;} else {increase_tx=1;} } Es[i] = power_mw[i]*ETX; if(Es[i]<minEs) { minEs_index = i; minEs = Es[i]; } if(decrease_tx==1) {i--; if(Es[i]==minEs) { minEs_index = i; minEs = Es[i]; } } else {i++;} } cc2420_set_txpower(power[minEs_index]); Tx = power[minEs_index]; }
/*---------------------------------------------------------------------------*/ int main(int argc, char **argv) { /* * Initalize hardware. */ msp430_cpu_init(); clock_init(); leds_init(); leds_toggle(LEDS_RED | LEDS_GREEN | LEDS_BLUE); #if WITH_UIP slip_arch_init(BAUD2UBR(115200)); /* Must come before first printf */ #else /* WITH_UIP */ uart1_init(BAUD2UBR(115200)); /* Must come before first printf */ #endif /* WITH_UIP */ printf("Starting %s " "($Id: contiki-sky-main.c,v 1.9 2009/11/20 10:45:07 nifi Exp $)\n", __FILE__); ds2411_init(); xmem_init(); leds_toggle(LEDS_RED | LEDS_GREEN | LEDS_BLUE); rtimer_init(); /* * Hardware initialization done! */ /* Restore node id if such has been stored in external mem */ // node_id_burn(3); node_id_restore(); printf("node_id : %hu\n", node_id); printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", ds2411_id[0], ds2411_id[1], ds2411_id[2], ds2411_id[3], ds2411_id[4], ds2411_id[5], ds2411_id[6], ds2411_id[7]); #if WITH_UIP uip_init(); uip_sethostaddr(&slipif.ipaddr); uip_setnetmask(&slipif.netmask); uip_fw_default(&slipif); /* Point2point, no default router. */ #endif /* WITH_UIP */ /* * Initialize Contiki and our processes. */ process_init(); process_start(&etimer_process, NULL); process_start(&sensors_process, NULL); /* * Initialize light and humidity/temp sensors. */ SENSORS_ACTIVATE(light_sensor); SENSORS_ACTIVATE(sht11_sensor); ctimer_init(); set_rime_addr(); cc2420_init(); cc2420_set_pan_addr(panId, 0 /*XXX*/, ds2411_id); cc2420_set_channel(RF_CHANNEL); cc2420_set_txpower(31); nullmac_init(&cc2420_driver); rime_init(&nullmac_driver); // xmac_init(&cc2420_driver); // rime_init(&xmac_driver); /* rimeaddr_set_node_addr*/ #if WITH_UIP process_start(&tcpip_process, NULL); process_start(&uip_fw_process, NULL); /* Start IP output */ process_start(&slip_process, NULL); #endif /* WITH_UIP */ SENSORS_ACTIVATE(button_sensor); print_processes(autostart_processes); autostart_start(autostart_processes); energest_init(); /* * This is the scheduler loop. */ printf("process_run()...\n"); ENERGEST_ON(ENERGEST_TYPE_CPU); while (1) { do { /* Reset watchdog. */ } while(process_run() > 0); /* * Idle processing. */ if(lpm_en) { int s = splhigh(); /* Disable interrupts. */ if(process_nevents() != 0) { splx(s); /* Re-enable interrupts. */ } else { static unsigned long irq_energest = 0; /* Re-enable interrupts and go to sleep atomically. */ ENERGEST_OFF(ENERGEST_TYPE_CPU); ENERGEST_ON(ENERGEST_TYPE_LPM); /* We only want to measure the processing done in IRQs when we are asleep, so we discard the processing time done when we were awake. */ energest_type_set(ENERGEST_TYPE_IRQ, irq_energest); _BIS_SR(GIE | SCG0 | /*SCG1 |*/ CPUOFF); /* LPM3 sleep. */ /* We get the current processing time for interrupts that was done during the LPM and store it for next time around. */ dint(); irq_energest = energest_type_time(ENERGEST_TYPE_IRQ); eint(); ENERGEST_OFF(ENERGEST_TYPE_LPM); ENERGEST_ON(ENERGEST_TYPE_CPU); } } } return 0; }
/** * The main function. */ int main( void ) { /* Stop the watchdog timer. */ WDTCTL = WDTPW + WDTHOLD; /* Setup MCLK 8MHz and SMCLK 1MHz */ set_mcu_speed_xt2_mclk_8MHz_smclk_1MHz(); /* Enable Interrupts */ eint(); LEDS_INIT(); LEDS_ON(); uart0_init(UART0_CONFIG_1MHZ_115200); printf("CC2420 TX test program with address recognition and acknowledge frames\r\n"); cc2420_init(); cc2420_io_sfd_register_cb(sfd_cb); cc2420_io_sfd_int_set_falling(); cc2420_io_sfd_int_clear(); cc2420_io_sfd_int_enable(); cc2420_set_txpower(CC2420_2_45GHz_TX_0dBm); uint8_t fcf[2] = {0x21, 0x88}; /* -> 00100001 10001000 -> reverse of bits for each byte -> 10000100 00010001 -> ack bit = 1 (6th bit), Frame type = 001 (don't forget to read from right to left) */ uint8_t seq_numb = 0x01; uint8_t dest_pan_id[2] = {0x22, 0x00}; uint8_t dest_addr[2] = {0x11, 0x11}; uint8_t src_pan_id[2] = {0x22, 0x01}; uint8_t src_addr[2] = {0x11, 0x12}; while ( (cc2420_get_status() & 0x40) == 0 ); // waiting for xosc being stable cc2420_set_panid(src_pan_id); // save pan id in ram cc2420_set_shortadr(src_addr); // save short address in ram printf("CC2420 initialized\r\n"); LEDS_OFF(); while (1) { cc2420_cmd_idle(); cc2420_cmd_flushtx(); txlength = sprintf((char *)txframe, "Hello World #%i", seq_numb); printf("Sent : %s of length %d\r\n", txframe,txlength); txlength += 13; cc2420_fifo_put(&txlength, 1); cc2420_fifo_put(fcf, 2); cc2420_fifo_put(&seq_numb, 1); cc2420_fifo_put(dest_pan_id, 2); cc2420_fifo_put(dest_addr, 2); cc2420_fifo_put(src_pan_id, 2); cc2420_fifo_put(src_addr, 2); cc2420_fifo_put(txframe, txlength-13); LED_BLUE_TOGGLE(); cc2420_cmd_tx(); micro_delay(0xFFFF); while (cc2420_io_sfd_read()); printf("Waiting for acknowledge frame...\n"); if (rx_ack()) { seq_numb ++; } else { printf("No Acknowledge frame received for frame number #%i - Retrying...\r\n\n", seq_numb); LED_RED_TOGGLE(); } micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); micro_delay(0xFFFF); } return 0; }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_server_process, ev, data) { uip_ipaddr_t ipaddr; struct uip_ds6_addr *root_if; PROCESS_BEGIN(); PROCESS_PAUSE(); SENSORS_ACTIVATE(button_sensor); PRINTF("UDP server started\n"); #if UIP_CONF_ROUTER /* The choice of server address determines its 6LoPAN header compression. * Obviously the choice made here must also be selected in udp-client.c. * * For correct Wireshark decoding using a sniffer, add the /64 prefix to the 6LowPAN protocol preferences, * e.g. set Context 0 to aaaa::. At present Wireshark copies Context/128 and then overwrites it. * (Setting Context 0 to aaaa::1111:2222:3333:4444 will report a 16 bit compressed address of aaaa::1111:22ff:fe33:xxxx) * Note Wireshark's IPCMV6 checksum verification depends on the correct uncompressed addresses. */ #if 0 /* Mode 1 - 64 bits inline */ uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 1); #elif 1 /* Mode 2 - 16 bits inline */ uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0x00ff, 0xfe00, 1); #else /* Mode 3 - derived from link local (MAC) address */ uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); #endif uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL); root_if = uip_ds6_addr_lookup(&ipaddr); if(root_if != NULL) { rpl_dag_t *dag; dag = rpl_set_root(RPL_DEFAULT_INSTANCE, (uip_ip6addr_t *)&ipaddr); uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); rpl_set_prefix(dag, &ipaddr, 64); PRINTF("created a new RPL dag\n"); } else { PRINTF("failed to create a new RPL DAG\n"); } #endif /* UIP_CONF_ROUTER */ print_local_addresses(); cc2420_set_txpower(3); /* The data sink runs with a 100% duty cycle in order to ensure high packet reception rates. */ NETSTACK_MAC.off(1); server_conn = udp_new(NULL, UIP_HTONS(UDP_CLIENT_PORT), NULL); if(server_conn == NULL) { PRINTF("No UDP connection available, exiting the process!\n"); PROCESS_EXIT(); } udp_bind(server_conn, UIP_HTONS(UDP_SERVER_PORT)); PRINTF("Created a server connection with remote address "); PRINT6ADDR(&server_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(server_conn->lport), UIP_HTONS(server_conn->rport)); while(1) { PROCESS_YIELD(); if(ev == tcpip_event) { tcpip_handler(); } else if(ev == sensors_event && data == &button_sensor) { PRINTF("Initiaing global repair\n"); rpl_repair_root(RPL_DEFAULT_INSTANCE); } } PROCESS_END(); }
int cc2420_init(cc2420_t *dev) { uint16_t reg; uint8_t addr[8]; /* reset options and sequence number */ dev->netdev.seq = 0; dev->netdev.flags = 0; /* set default address, channel, PAN ID, and TX power */ luid_get(addr, sizeof(addr)); /* make sure we mark the address as non-multicast and not globally unique */ addr[0] &= ~(0x01); addr[0] |= 0x02; cc2420_set_addr_short(dev, &addr[6]); cc2420_set_addr_long(dev, addr); cc2420_set_pan(dev, CC2420_PANID_DEFAULT); cc2420_set_chan(dev, CC2420_CHAN_DEFAULT); cc2420_set_txpower(dev, CC2420_TXPOWER_DEFAULT); /* set default options */ cc2420_set_option(dev, CC2420_OPT_AUTOACK, true); cc2420_set_option(dev, CC2420_OPT_CSMA, true); cc2420_set_option(dev, CC2420_OPT_TELL_TX_START, true); cc2420_set_option(dev, CC2420_OPT_TELL_RX_END, true); #ifdef MODULE_NETSTATS_L2 cc2420_set_option(dev, CC2420_OPT_TELL_RX_END, true); #endif /* set default protocol*/ #ifdef MODULE_GNRC_SIXLOWPAN dev->netdev.proto = GNRC_NETTYPE_SIXLOWPAN; #elif MODULE_GNRC dev->netdev.proto = GNRC_NETTYPE_UNDEF; #endif /* change default RX bandpass filter to 1.3uA (as recommended) */ reg = cc2420_reg_read(dev, CC2420_REG_RXCTRL1); reg |= CC2420_RXCTRL1_RXBPF_LOCUR; cc2420_reg_write(dev, CC2420_REG_RXCTRL1, reg); /* set the FIFOP threshold to maximum. */ cc2420_reg_write(dev, CC2420_REG_IOCFG0, CC2420_PKT_MAXLEN); /* turn off "Security enable" (page 33). */ reg = cc2420_reg_read(dev, CC2420_REG_SECCTRL0); reg &= ~CC2420_SECCTRL0_RXFIFO_PROT; cc2420_reg_write(dev, CC2420_REG_SECCTRL0, reg); /* set preamble length to 3 leading zero byte */ /* and turn on hardware CRC generation */ reg = cc2420_reg_read(dev, CC2420_REG_MDMCTRL0); reg &= ~(CC2420_MDMCTRL0_PREAMBLE_M); reg |= CC2420_MDMCTRL0_PREAMBLE_3B; reg |= CC2420_MDMCTRL0_AUTOCRC; cc2420_reg_write(dev, CC2420_REG_MDMCTRL0, reg); /* go into RX state */ cc2420_set_state(dev, CC2420_GOTO_RX); return 0; }
void component_radio_txpower_set(int8_t txpower) { cc2420_set_txpower(actual_txpower = txpower); }
static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t val_len) { if (netdev == NULL) { return -ENODEV; } cc2420_t *dev = (cc2420_t *)netdev; int ext = netdev_ieee802154_set(&dev->netdev, opt, val, val_len); switch (opt) { case NETOPT_ADDRESS: assert(val_len == 2); cc2420_set_addr_short(dev, val); return 2; case NETOPT_ADDRESS_LONG: assert(val_len == 8); cc2420_set_addr_long(dev, val); return 8; case NETOPT_NID: assert(val_len == sizeof(uint16_t)); cc2420_set_pan(dev, to_u16(val)); return sizeof(uint16_t); case NETOPT_CHANNEL: assert(val_len == sizeof(uint16_t)); return cc2420_set_chan(dev, to_u16(val)); case NETOPT_TX_POWER: assert(val_len == sizeof(int16_t)); cc2420_set_txpower(dev, to_i16(val)); return sizeof(int16_t); case NETOPT_STATE: assert(val_len == sizeof(netopt_state_t)); return cc2420_set_state(dev, *((netopt_state_t *)val)); case NETOPT_AUTOACK: return cc2420_set_option(dev, CC2420_OPT_AUTOACK, to_bool(val)); case NETOPT_CSMA: return cc2420_set_option(dev, CC2420_OPT_CSMA, to_bool(val)); case NETOPT_PRELOADING: return cc2420_set_option(dev, CC2420_OPT_PRELOADING, to_bool(val)); case NETOPT_PROMISCUOUSMODE: return cc2420_set_option(dev, CC2420_OPT_PROMISCUOUS, to_bool(val)); case NETOPT_RX_START_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_RX_START, to_bool(val)); case NETOPT_RX_END_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_RX_END, to_bool(val)); case NETOPT_TX_START_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_TX_START, to_bool(val)); case NETOPT_TX_END_IRQ: return cc2420_set_option(dev, CC2420_OPT_TELL_TX_END, to_bool(val)); default: return ext; } return 0; }