Example #1
0
int main( void )
{

	// Stop the watchdog timer.
	WDTCTL = WDTPW + WDTHOLD;
    
	// Clock settings
	set_mcu_speed_xt2_mclk_8MHz_smclk_8MHz();	// used by CDMA
	set_aclk_div(1); // ACKL is at 32768Hz		// used for clock synchronization
    
	// Initialize the UART0
	uart0_init(UART0_CONFIG_8MHZ_115200);	// 115kbaud, SMCLK is at 8MHz
	uart0_register_callback(char_rx);	// Set the UART callback function

	// Initialize random number
	ds2411_init();
	rnd = (((uint16_t)ds2411_id.serial0) << 8) + (uint16_t)ds2411_id.serial1;

	// Timer settings
	time_1w = 0;
	timerA_init();
	timerA_start_ACLK_div(TIMERA_DIV_1);			// timerA period = 2s
	timerA_register_cb(TIMERA_ALARM_OVER, timer_overflow);	// timerA overflow event
	timerA_register_cb(TIMERA_ALARM_CCR0, run_algorithm);	// run algorithm at CCR0
	timerA_register_cb(TIMERA_ALARM_CCR1, skew_correction); // compensate skew error at CCR1
	timerA_set_alarm_from_now(TIMERA_ALARM_CCR0, rnd, 54983);	// same period 1.678s, different phase
	timerA_set_alarm_from_now(TIMERA_ALARM_CCR1, 35000, skew);	// skew compensation happens every 'skew' ticks

	// Initialize the MAC layer (radio)
	mac_init(11);
	mac_set_rx_cb(frame_rx);
	mac_set_error_cb(frame_error);
	mac_set_sent_cb(frame_sent);

	// Enable Interrupts
	eint();
    
 	while (1) {
	}

	return 0;
}
Example #2
0
int main(void)
{
    WDTCTL = WDTPW+WDTHOLD;

    set_mcu_speed_xt2_mclk_8MHz_smclk_1MHz();
    set_aclk_div(1);

    LEDS_INIT();
    LEDS_OFF();


    ds2411_init();
    nodeaddr = (((uint16_t)ds2411_id.serial1)<<8) + (ds2411_id.serial0);

    uart0_init(UART0_CONFIG_1MHZ_115200);
    uart0_register_callback(char_rx);
    eint();

    printf("[APP];BOOTING;%.4x\n",nodeaddr);


    //check if this node is the sink
    if (nodeaddr == sink_nodes)
    {
        type = SINK;
        level = DEFAULT_LEVEL;
    }
    else
    {
        //retrieve father
        for (idx=0; idx<NUMBER_NODES; idx++)
        {
            if (list_nodes[idx] == nodeaddr)
            {
                if(father_nodes1[idx] != 0x0000)
                {
                    parent_id = father_nodes1[idx];
                    level = 12;
                    break;
                }
            }
        }
    }
    
    //hack for mobile
    /*if(nodeaddr == 0x1f5d)
    {
        parent_id = 0x0000;
        mac_set_mobile(1);
        level = 12;
    }*/

    mac_init(10);
    mac_set_rx_cb(packet_received);
    mac_set_error_cb(packet_error);
    mac_set_sent_cb(packet_sent);

    timerB_set_alarm_from_now(TIMERB_ALARM_CCR6, 32768, 32768);
    timerB_register_cb(TIMERB_ALARM_CCR6, inc_clock);

    while (1)
    {
        LPM1;

        if (state == SM_TX)
        {
            if (level != UNDEF_LEVEL && type != SINK)
            {
                seq_max = NUM_SEQ_MAX;
                delay = rand();
                delay &= 0xCFFF;
                delay += 12000; //(369ms < delay < 1991ms)
                timerB_set_alarm_from_now(TIMERB_ALARM_CCR5, delay, 0);
                timerB_register_cb(TIMERB_ALARM_CCR5, next_send);
            }
            else
            {
                printf("[APP];NOROUTE\n");
            }

            state = SM_IDLE;
        }
        else if (state == SM_LOOP_TX)
        {
            if (level != UNDEF_LEVEL)
            {
                sprintf(sourceaddr,"%.4x",nodeaddr);
                data_txframe[0] = DATA;
                data_txframe[1] = level-1;
                data_txframe[2] = sourceaddr[0];
                data_txframe[3] = sourceaddr[1];
                data_txframe[4] = sourceaddr[2];
                data_txframe[5] = sourceaddr[3];
                data_txframe[6] = seq;	//sequence
                data_txframe[7] = 1;    //hops
                txlength = 8;

                stat_add(STAT_APP_TX);
                printf("[APP];NODE_TX;%.4x;%.4x;%u;%u-%u\n", nodeaddr, parent_id, seq, global_clock, timerB_time()/32);

                seq++;

                mac_send(data_txframe, txlength, parent_id);

                if (DEBUG_LEDS == 1)
                {
                    LED_GREEN_ON();
                }

                if (seq < seq_max)
                {
                    timerB_set_alarm_from_now(TIMERB_ALARM_CCR5, SEND_DATA_PERIOD, 0);
                    timerB_register_cb(TIMERB_ALARM_CCR5, next_send);
                }
            }
            state = SM_IDLE;
        }
     
    }

    return 0;
}