Beispiel #1
0
void bmac_nw_task ()
{
int8_t v;
int8_t e;
uint8_t backoff;
nrk_sig_mask_t event;

while(bmac_started()==0) nrk_wait_until_next_period();

//register the signal after bmac_init has been called
v=nrk_signal_register(bmac_enable_signal); 
if(v==NRK_ERROR) nrk_kprintf( PSTR("Failed to register signal\r\n"));
backoff=0;
    while (1) {
    #ifdef NRK_SW_WDT
	#ifdef BMAC_SW_WDT_ID
	nrk_sw_wdt_update(BMAC_SW_WDT_ID);
	#endif
    #endif
	if(is_enabled ) { 
	v=1;
	if(rx_buf_empty==1) v=_bmac_channel_check();
	// If the buffer is full, signal the receiving task again.
	else e=nrk_event_signal (bmac_rx_pkt_signal);
	// bmac_channel check turns on radio, don't turn off if
	// data is coming.
		if(v==0)
			{
			if(_bmac_rx()==1)
			  {
				e=nrk_event_signal (bmac_rx_pkt_signal);
				//if(e==NRK_ERROR) {
				//	nrk_kprintf( PSTR("bmac rx pkt signal failed\r\n"));
				//	printf( "errno: %u \r\n",nrk_errno_get() );
				//}
			  }
			  //else nrk_kprintf( PSTR("Pkt failed, buf could be corrupt\r\n" ));
			} 
		if(/*rx_buf_empty==1 &&*/ tx_data_ready==1)
			{
				rf_rx_off(); 
				_bmac_tx();
			}
	//do {
		nrk_wait(_bmac_check_period); 
	//	if(rx_buf_empty!=1)  nrk_event_signal (bmac_rx_pkt_signal);
	//} while(rx_buf_empty!=1);
	} else {
		event=0;
		do {
		v=nrk_signal_register(bmac_enable_signal); 
    		event=nrk_event_wait (SIG(bmac_enable_signal));
		} while((event & SIG(bmac_enable_signal))==0);
	}
	//nrk_wait_until_next_period();
	}

}
Beispiel #2
0
// This function will put the node into a low-duty checking mode to save power
void tdma_snooze()
{
    int8_t v;
    uint8_t i;

// stop the software watchdog timer so it doesn't interrupt
    nrk_sw_wdt_stop(0);
// This flag is cleared only by the button interrupt
    snoozing=1;
// Setup the button interrupt
    nrk_ext_int_configure( NRK_EXT_INT_1,NRK_LEVEL_TRIGGER, &wakeup_func);
// Clear it so it doesn't fire instantly
    EIFR=0xff;
    nrk_ext_int_enable( NRK_EXT_INT_1);
// Now loop and periodically check for packets
    while(1)
    {
        nrk_led_clr(RED_LED);
        nrk_led_clr(GREEN_LED);
        rf_power_up();
        rf_rx_on ();
// check return from button interrupt on next cycle
        if(snoozing==0 ) return;
        tmp_time.secs=0;
        tmp_time.nano_secs=10*NANOS_PER_MS;
        nrk_led_set(RED_LED);
        // Leave radio on for two loops of 10ms for a total of 20ms every 10 seconds
        for(i=0; i<2; i++ )
        {
            v = _tdma_rx ();
            if(v==NRK_OK) {
                nrk_led_set(RED_LED);
                nrk_ext_int_disable( NRK_EXT_INT_1);
                nrk_sw_wdt_update(0);
                nrk_sw_wdt_start(0);
                return NRK_OK;
            }
            nrk_wait(tmp_time);
        }
        nrk_led_clr(RED_LED);
        rf_power_down();
        tmp_time.secs=10;
        tmp_time.nano_secs=0;
        nrk_sw_wdt_stop(0);
        nrk_wait(tmp_time);

    }

}
Beispiel #3
0
void Task1()
{
nrk_time_t t;
uint16_t cnt;
cnt=0;
nrk_kprintf( PSTR("Nano-RK Version ") );
printf( "%d\r\n",NRK_VERSION );


setup_uart1(UART_BAUDRATE_19K2);
DDRE=0x2;
printf( "My node's address is %u\r\n",NODE_ADDR );
  
printf( "Task1 PID=%u\r\n",nrk_get_pid());
t.secs=5;
t.nano_secs=0;

// setup a software watch dog timer
nrk_sw_wdt_init(0, &t, NULL);
nrk_sw_wdt_start(0);


	putc1('~'); putc1('A'); putc1(' '); putc1('8');
	nrk_wait_until_next_period();
	putc1('~'); putc1('A'); putc1(' '); putc1('8');
  while(1) {
	// Update watchdog timer
	nrk_sw_wdt_update(0);
	nrk_led_toggle(ORANGE_LED);
	nrk_gpio_toggle(NRK_DEBUG_0);
	printf( "Task1 cnt=%u\r\n",cnt );
	nrk_wait_until_next_period();
        // Uncomment this line to cause a stack overflow
	// if(cnt>20) kill_stack(10);

	// At time 50, the OS will halt and print statistics
	// This requires the NRK_STATS_TRACKER #define in nrk_cfg.h
	// if(cnt==50)  {
	//	nrk_stats_display_all();
	//	nrk_halt();
	//	}


	cnt++;
	}
}
Beispiel #4
0
void Task1()
{
  nrk_time_t t;
  uint16_t cnt;
  uint8_t val;
  cnt=0;
  nrk_kprintf( PSTR("Nano-RK Version ") );
  printf("b");
  printf( "%d\r\n",NRK_VERSION );
  
  printf( "My node's address is %u\r\n",NODE_ADDR );
  
  printf( "Task1 PID=%u\r\n",nrk_get_pid());
  t.secs=30;
  t.nano_secs=0;
  
  // setup a software watch dog timer
 nrk_sw_wdt_init(0, &t, NULL);
 nrk_sw_wdt_start(0);
 
 nrk_gpio_direction(BUTTON, NRK_PIN_INPUT);
 
 while(1) {
   // Update watchdog timer
   nrk_sw_wdt_update(0);
   nrk_led_toggle(ORANGE_LED);
   val=nrk_gpio_get(BUTTON);
   
   // Button logic is inverter 0 means pressed, 1 not pressed
   printf( "Task1 cnt=%u button state=%u\r\n",cnt,val );
   nrk_wait_until_next_period();
   // Uncomment this line to cause a stack overflow
   // if(cnt>20) kill_stack(10);
   
   // At time 50, the OS will halt and print statistics
   // This requires the NRK_STATS_TRACKER #define in nrk_cfg.h
   if(cnt==50)  {
     nrk_stats_display_all();
     nrk_halt();
   }
   
   
   cnt++;
 }
}
Beispiel #5
0
void rx_task ()
{
    nrk_time_t t;
    uint16_t cnt;
    int8_t v;
    uint8_t len, i;
    uint8_t chan;


    cnt = 0;
    nrk_kprintf (PSTR ("Nano-RK Version "));
    printf ("%d\r\n", NRK_VERSION);


    nrk_kprintf( PSTR( "RX Task PID=" ));
    printf ("%u\r\n", nrk_get_pid ());
    t.secs = 5;
    t.nano_secs = 0;

    while (cal_done==0)
        nrk_wait_until_next_period ();


    chan = RADIO_CHANNEL;
    if (SET_MAC == 0x00) {

        v = read_eeprom_mac_address (&mac_address);
        if (v == NRK_OK) {
            v = read_eeprom_channel (&chan);
            v = read_eeprom_aes_key(aes_key);
        }
        else {
            while (1) {
                nrk_kprintf (PSTR
                             ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
                nrk_wait_until_next_period ();
            }
        }
    }
    else
        mac_address = SET_MAC;

    printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
    printf ("chan = %d\r\n", chan);
    len=0;
    for(i=0; i<16; i++ ) {
        len+=aes_key[i];
    }
    printf ("AES checksum = %d\r\n", len);



    tdma_init (TDMA_CLIENT, chan, (mac_address));

    tdma_aes_setkey(aes_key);
    tdma_aes_enable();

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    v = tdma_tx_slot_add (mac_address&0xff);

    if (v != NRK_OK)
        nrk_kprintf (PSTR ("Could not add slot!\r\n"));

    // setup a software watch dog timer
    t.secs=30;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);
    while (1) {
        // Update watchdog timer
        nrk_sw_wdt_update(0);
        v = tdma_recv (&rx_tdma_fd, &rx_buf, &len, TDMA_BLOCKING);
        if (v == NRK_OK) {
            // printf ("src: %u\r\nrssi: %d\r\n", rx_tdma_fd.src, rx_tdma_fd.rssi);
            // printf ("slot: %u\r\n", rx_tdma_fd.slot);
            // printf ("cycle len: %u\r\n", rx_tdma_fd.cycle_size);
            v=buf_to_pkt(&rx_buf, &rx_pkt);
            if(v==NRK_OK)
            {
                if(((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || ((rx_pkt.dst_mac&0xff)==0xff))
                {
                    if(rx_pkt.type==PING)
                    {
                        send_ack=1;
                        nrk_led_clr(0);
                        nrk_led_clr(1);
                        if(rx_pkt.payload[0]==PING_1)
                        {
                            nrk_led_set(0);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(0);
                        }
                        if(rx_pkt.payload[0]==PING_2)
                        {
                            nrk_led_set(1);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(1);
                        }
                        if(rx_pkt.payload[0]==PING_PERSIST)
                        {
                            nrk_led_set(0);
                        }


                    }

                    if(rx_pkt.type==APP)
                    {
                        // payload 1: Key
                        if(rx_pkt.payload[1]==2)
                        {
                            send_ack=1;
                            // payload 2: Outlet Number
                            // payload 3: On/Off
                            if(rx_pkt.payload[3]==0) {
                                power_socket_disable(rx_pkt.payload[2]);
                                plug_led_green_clr();
                                //printf( "Disable %d\r\n", rx_pkt.payload[2] );
                            }
                            if(rx_pkt.payload[3]==1) {
                                power_socket_enable(rx_pkt.payload[2]);
                                //printf( "Enable %d\r\n", rx_pkt.payload[2] );
                                plug_led_green_set();
                            }
                        }

                        // payload 1: Key
                        if(rx_pkt.payload[1]==3)
                        {
                            send_ack=1;
                            true_power_thresh=((uint32_t)rx_pkt.payload[3])<<16 | ((uint32_t)rx_pkt.payload[4])<<8 | (uint32_t)rx_pkt.payload[5];
                            set_power_thresh(true_power_thresh);
                        }




                    }

                }

            }
            /*      printf ("len: %u\r\npayload: ", len);
            for (i = 0; i < len; i++)
              printf ("%d ", rx_buf[i]);
            printf ("\r\n");

            if(rx_buf[0]==(mac_address&0xff))
            {
            if(rx_buf[2]==0) {
            	power_socket_disable(rx_buf[1]);
            	printf( "Disable %d\r\n", rx_buf[1] );
            }
            if(rx_buf[2]==1) {
            	power_socket_enable(rx_buf[1]);
            	printf( "Enable %d\r\n", rx_buf[1] );
            }
                 }
                 */

        }

        tdma_rx_pkt_release();
        //  nrk_wait_until_next_period();
    }

}
Beispiel #6
0
void tx_task ()
{
    uint8_t j, i, val, cnt;
    int8_t len;
    int8_t v,fd;
    uint8_t buf[2];
    nrk_sig_mask_t ret;
    nrk_time_t t;



    // Set Port D.0 as input
    // On the FireFly nodes we use this for motion or syntonistor input
    // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway
//  DDRD &= ~(0x1);



    printf ("tx_task PID=%d\r\n", nrk_get_pid ());

    // setup a software watch dog timer
    t.secs=10;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // set port G as input for gpio
    DDRG=0;


    while (1) {

        nrk_led_clr(RED_LED);


        tx_pkt.payload[0] = 1;  // ELEMENTS
        tx_pkt.payload[1] = 3;  // Key

        fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ);
        if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));


        val=nrk_set_status(fd,SENSOR_SELECT,MOTION);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[28]=buf[1];
        tx_pkt.payload[29]=buf[0];



        val=nrk_set_status(fd,SENSOR_SELECT,BAT);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[2]=buf[1];
        tx_pkt.payload[3]=buf[0];
        //printf( "Task bat=%d",buf);

//	tx_pkt.payload[2]=0;
//	tx_pkt.payload[3]=0;

        val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[4]=buf[1];
        tx_pkt.payload[5]=buf[0];
        //printf( " light=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,TEMP);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[6]=buf[1];
        tx_pkt.payload[7]=buf[0];
        //printf( " temp=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_X);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[8]=buf[1];
        tx_pkt.payload[9]=buf[0];
        //printf( " acc_x=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[10]=buf[1];
        tx_pkt.payload[11]=buf[0];
        //printf( " acc_y=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[12]=buf[1];
        tx_pkt.payload[13]=buf[0];
        //printf( " acc_z=%d",buf);


        val=nrk_set_status(fd,SENSOR_SELECT,HUMIDITY);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[16]=buf[3];
        tx_pkt.payload[17]=buf[2];
        tx_pkt.payload[18]=buf[1];
        tx_pkt.payload[19]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,TEMP2);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[20]=buf[3];
        tx_pkt.payload[21]=buf[2];
        tx_pkt.payload[22]=buf[1];
        tx_pkt.payload[23]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,PRESS);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[24]=buf[3];
        tx_pkt.payload[25]=buf[2];
        tx_pkt.payload[26]=buf[1];
        tx_pkt.payload[27]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[14]=buf[1];
        tx_pkt.payload[15]=buf[0];


        // GPIO data
//	gpio_pin = !!(PINE & 0x4);
        gpio_pin = PING & 0x1;
        tx_pkt.payload[30]=gpio_pin;
        //printf( " audio=%d\r\n",buf);

        nrk_close(fd);

        tx_pkt.payload_len=31;
        tx_pkt.src_mac=mac_address;
        tx_pkt.dst_mac=0;
        tx_pkt.type=APP;

        len=pkt_to_buf(&tx_pkt,&tx_buf );

        if(len>0)
        {
            v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING);
            if (v == NRK_OK) {
            }
        } else {
            nrk_kprintf(PSTR("Pkt tx error\r\n"));
            nrk_wait_until_next_period();
        }


        nrk_sw_wdt_update(0);
    }



}
Beispiel #7
0
void tx_task ()
{
  uint8_t j, i, val, cnt;
  int8_t len;
  int8_t v,fd;
  uint8_t buf[2];
  nrk_sig_mask_t ret;
  nrk_time_t t;
  int8_t* success;
  int16_t acbuf[3];
  int gyrobuf[3];
  int magbuf[3];

	/* Setup application timer with:
	 * Prescaler = 5 
	 * Compare Match = 2500
	 * Sys Clock = 1.0 MHz according to Mega128RFA1 manual pg149
	 * Prescaler 5 means divide sys clock by 1024
	 * 1000000 / 1024 = 976.5625 Hz clock
	 * 1 / 976.5625 = 1.024 ms per tick
	 * 1.024 ms * 1000 = ~1024 ms / per interrupt callback
	 */
	
	val=nrk_timer_int_configure(NRK_APP_TIMER_0, 5, 1000, &my_timer_callback );
	if(val==NRK_OK) nrk_kprintf( PSTR("Callback timer setup\r\n"));
	else nrk_kprintf( PSTR("Error setting up timer callback\r\n"));
	
	// Zero the timer...
	nrk_timer_int_reset(NRK_APP_TIMER_0);
	// Start the timer...
	nrk_timer_int_start(NRK_APP_TIMER_0);

  // Set Port D.0 as input
  // On the FireFly nodes we use this for motion or syntonistor input
  // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway
  //  DDRD &= ~(0x1);
  	
   
   //nrk_kprintf( PSTR("Booting Up ADXL345. . .\r\n") );
  /**** Start Accelerometer and check connections 
   * Start up accelerometer
   * Ensure ADXL is connected. 
   * Enable continous measurement
   * INT_SCALER =  10000 
   * Set accelerometer range to maximum og 2G
   * Set sample rate to 1.6Khz
   ****/	
   set_up_ADXL345();
	
  /**** Start Gyro and check connections 
   * Start up gyro
   * Zero calibrate gyro.
   * INT_SCALER_GYRO =  100  
   * Leave gyro still for about 2 seconds to zero-calibrate
   * sets gyro sample rate to 8Khz with a 256Hz BW(bandwidth) LP (low pass) filter
   ****/
   set_up_ITG3200();	
	
  /**** Start magnetometer and check connections 
   * set scale to +/- 1.3 gauss
   * set measurement mode to continous	
   * INT_SCALER_MAG = 1000
   * sets sample rate to highest value of 75Hz
   * sets averaging value to 8 samples per reading givein at 75 Hz
   ****/
   success = set_up_HMC5883L();
   
   if (*success) nrk_kprintf( PSTR("HMC5883L(Magnetometer) boot success. Scale +/- 1.3 Ga. . .\r\n"));
   if ( *(success+1) ) nrk_kprintf( PSTR("HMC5883L measurement mode: continuous . .\r\n") );
	
	//printf( "My node's address is %d\r\n",NODE_ADDR );

  printf ("tx_task PID=%d\r\n", nrk_get_pid ());

   // setup a software watch dog timer
   t.secs=10;
   t.nano_secs=0;
   nrk_sw_wdt_init(0, &t, NULL);
   nrk_sw_wdt_start(0);

   while (!tdma_started ())
   nrk_wait_until_next_period ();

	

  while (1) {

	//nrk_led_toggle(GREEN_LED);


	tx_pkt.payload[0] = 1;  // ELEMENTS
	tx_pkt.payload[1] = 3;  // Key

   /* 	fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ);
        if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));
//	val=nrk_set_status(fd,SENSOR_SELECT,BAT);
//	val=nrk_read(fd,&buf,2);
//	tx_pkt.payload[2]=buf[1];
//	tx_pkt.payload[3]=buf[0];
	//printf( "Task bat=%d",buf);
	tx_pkt.payload[2]=0;
	tx_pkt.payload[3]=0;
	
	//val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[4]=buf[1];
	tx_pkt.payload[5]=buf[0];
	//printf( " light=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,TEMP);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[6]=buf[1];
	tx_pkt.payload[7]=buf[0];
	//printf( " temp=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,ACC_X);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[8]=buf[1];
	tx_pkt.payload[9]=buf[0];
	//printf( " acc_x=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[10]=buf[1];
	tx_pkt.payload[11]=buf[0];
	//printf( " acc_y=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[12]=buf[1];
	tx_pkt.payload[13]=buf[0];
	//printf( " acc_z=%d",buf);
	
	/*
	val=nrk_set_status(fd,SENSOR_SELECT,HUMIDITY);
	val=nrk_read(fd,&buf,4);
	tx_pkt.payload[16]=buf[3];
	tx_pkt.payload[17]=buf[2];
	tx_pkt.payload[18]=buf[1];
	tx_pkt.payload[19]=buf[0];


	val=nrk_set_status(fd,SENSOR_SELECT,TEMP2);
	val=nrk_read(fd,&buf,4);
	tx_pkt.payload[20]=buf[3];
	tx_pkt.payload[21]=buf[2];
	tx_pkt.payload[22]=buf[1];
	tx_pkt.payload[23]=buf[0];


	val=nrk_set_status(fd,SENSOR_SELECT,PRESS);
	val=nrk_read(fd,&buf,4);
	tx_pkt.payload[24]=buf[3];
	tx_pkt.payload[25]=buf[2];
	tx_pkt.payload[26]=buf[1];
	tx_pkt.payload[27]=buf[0];

	//val=nrk_set_status(fd,SENSOR_SELECT,MOTION);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[28]=buf[1];
	tx_pkt.payload[29]=buf[0];

	//val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[14]=buf[1];
	tx_pkt.payload[15]=buf[0];


	// GPIO data
	//gpio_pin = !!(PINE & 0x4);
	//tx_pkt.payload[30]=gpio_pin;
	//printf( " audio=%d\r\n",buf);

    	nrk_close(fd);*/
	  tx_pkt.payload[30]=30;
	  tx_pkt.payload[29]=29;
	  tx_pkt.payload[28]=8;
	  tx_pkt.payload[27]=0;
	  tx_pkt.payload[26]=0;
	  tx_pkt.payload[25]=29;
	  tx_pkt.payload[24]=8;
	  tx_pkt.payload[23]=0;
	  tx_pkt.payload[22]=0;
	  tx_pkt.payload[21]=8;
	  tx_pkt.payload[20]=0;
	  tx_pkt.payload[19]=0;
	  tx_pkt.payload[18]=0;
	  tx_pkt.payload[17]=0;
	  tx_pkt.payload[16]=0;
      tx_pkt.payload[15]=60;
	  tx_pkt.payload[14]=0;
	  tx_pkt.payload[13]=0;
	  tx_pkt.payload[12]=0;
	  tx_pkt.payload[11]=0;
	  tx_pkt.payload[10]=0;
	  tx_pkt.payload[9]=0;
	  tx_pkt.payload[8]=0;
	  tx_pkt.payload[7]=155;
	  tx_pkt.payload[6]=45;
	  tx_pkt.payload[5]=8;
	  tx_pkt.payload[4]=5;
	  tx_pkt.payload[3]=3;
	  tx_pkt.payload[2]=2;

  	 tx_pkt.payload_len=31;
	 tx_pkt.src_mac=mac_address;
	 tx_pkt.dst_mac=0;
	 tx_pkt.type=APP;

    len=pkt_to_buf(&tx_pkt,&tx_buf );

 if(len>0){
    v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING);
    if (v == NRK_OK) {
 	 }
 }
 else { 
	nrk_kprintf(PSTR("Pkt tx error\r\n")); 
	nrk_wait_until_next_period(); 
 }
   

	nrk_sw_wdt_update(0);  
  }
}
Beispiel #8
0
void rx_task()
{
nrk_time_t t;
uint16_t cnt;
int8_t v;
uint8_t len,i,chan;


cnt=0;
nrk_kprintf( PSTR("Nano-RK Version ") );
printf( "%d\r\n",NRK_VERSION );

  
printf( "Gateway Task PID=%u\r\n",nrk_get_pid());
t.secs=10;
t.nano_secs=0;

// setup a software watch dog timer
nrk_sw_wdt_init(0, &t, NULL);
nrk_sw_wdt_start(0);

  chan = 16;
  if (SET_MAC == 0xffff) {

    v = read_eeprom_mac_address (&mac_address);
    if (v == NRK_OK) {
      v = read_eeprom_channel (&chan);
    }
    else {
      while (1) {
        nrk_kprintf (PSTR
                     ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
  	nrk_led_toggle(RED_LED);
        nrk_wait_until_next_period ();
      }
    }
  }
  else
    mac_address = SET_MAC;

  printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
  printf ("chan = %d\r\n", chan);


tdma_init(TDMA_HOST, chan, mac_address);

// Change these parameters anytime you want...
tdma_set_slot_len_ms(10);
tdma_set_slots_per_cycle(12);

slip_init (stdin, stdout, 0, 0);

while(!tdma_started()) nrk_wait_until_next_period();
nrk_led_set(GREEN_LED);
  while(1) {
	v=tdma_recv(&rx_tdma_fd, &slip_tx_buf, &len, TDMA_BLOCKING );	
	nrk_led_set(ORANGE_LED);
	if(v==NRK_OK)
	{
		//for(i=0; i<len; i++ ) printf( "%c", rx_buf[i]);
		// Got a packet from the network so send it over SLIP
		slip_tx ( slip_tx_buf, len );
	}
	else tdma_rx_pkt_release();
	nrk_led_clr(ORANGE_LED);
	nrk_sw_wdt_update(0); 
  	}
}
Beispiel #9
0
void tx_task ()
{
    uint8_t j, i, val, cnt;
    int8_t len;
    int8_t v,fd;
    nrk_sig_mask_t ret;
    nrk_time_t t;



    // Set Port D.0 as input
    // On the FireFly nodes we use this for motion or syntonistor input
    // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway



    printf ("tx_task PID=%d\r\n", nrk_get_pid ());

    // setup a software watch dog timer
    t.secs=10;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // set port G as input for gpio
    nrk_gpio_direction(NRK_PORTG_0,NRK_PIN_INPUT );

    send_ack=0;

    while (1) {
        nrk_led_clr(RED_LED);

        tx_pkt.payload[0] = 1;  // ELEMENTS
        tx_pkt.payload[1] = 4;  // Key
        tx_pkt.payload[2] = nrk_gpio_get(NRK_PORTG_0);  // Key
        tx_pkt.payload_len=3;
        tx_pkt.src_mac=mac_address;
        tx_pkt.dst_mac=0;
        tx_pkt.type=APP;

        if(send_ack==1)
        {
            tx_pkt.type=ACK;
            tx_pkt.payload_len=0;
            send_ack=0;
        }


        len=pkt_to_buf(&tx_pkt,&tx_buf );

        if(len>0)
        {
            v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING);
            if (v == NRK_OK) {
            }
        } else {
            nrk_kprintf(PSTR("Pkt tx error\r\n"));
            nrk_wait_until_next_period();
        }


        nrk_sw_wdt_update(0);
    }



}